Thursday 5 August 2021

Adaptation module design for stacking two CNNs

I'm trying to stack two different CNNs using an adaptation module to bridge them, but I'm having a hard time determining the adaption module's layer hyperparameters correctly.

To be more precise, I would like to train the adaptation module to bridge two convolutional layers:

  1. Layer A with output shape: (29,29,256)
  2. Layer B with input shape: (8,8,384)

So, after Layer A, I sequentially add the adaptation module, for which I choose:

  • Conv2D layer with 384 filters with kernel size: (3,3) / Output shape: (29,29,384)
  • MaxPool2D with pool size: (2,2), strides: (4,4) and padding: "same" / Output shape: (8,8,384)

Finally, I try to add layer B to the model, but I get the following error from tensorflow:

InvalidArgumentError: Dimensions must be equal, but are 384 and 288 for ' = FusedBatchNormV3[T=DT_FLOAT, U=DT_FLOAT, data_format="NHWC", epsilon=0.001, exponential_avg_factor=1, is_training=false](Placeholder, batch_normalization_159/scale, batch_normalization_159/ReadVariableOp, batch_normalization_159/FusedBatchNormV3/ReadVariableOp, batch_normalization_159/FusedBatchNormV3/ReadVariableOp_1)' with input shapes: [?,8,8,384], [288], [288], [288], [288].

There's a minimal reproducible example of it:

from keras.applications.inception_resnet_v2 import InceptionResNetV2
from keras.applications.mobilenet import MobileNet
from keras.layers import Conv2D, MaxPool2D
from keras.models import Sequential

mobile_model = MobileNet(weights='imagenet')
server_model = InceptionResNetV2(weights='imagenet')

hybrid = Sequential()

for i, layer in enumerate(mobile_model.layers):
  if i <= 36:
    layer.trainable = False
    hybrid.add(layer)

hybrid.add(Conv2D(384, kernel_size=(3,3), padding='same'))
hybrid.add(MaxPool2D(pool_size=(2,2), strides=(4,4), padding='same'))

for i, layer in enumerate(server_model.layers):
  if i >= 610:
    layer.trainable = False
    hybrid.add(layer)


from Adaptation module design for stacking two CNNs

No comments:

Post a Comment