Shape Error while Fine Tuning MobileNet On A Custom Data Set

Viewed 80

I was following deeplizard to fine-tuning MobileNet. What I tried to do is to grab the output from the 5th to the last layer of the model and store it in this variable x. The output of the 5th to the last layer of the model has a shape of global_average_pooling2d_3 (None, 1, 1, 1024). Then add an output dense layer with 10 units. However, when fitting the model, I got the following error. Could anyone please kindly offer me some guidance. Thanks a lot. My code looks like the following

mobile = tf.keras.applications.mobilenet.MobileNet()
mobile.summary()
x = mobile.layers[-5].output
output =layers.Dense(units=10, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=output)

for layer in model.layers[:-23]:
    layer.trainable = False

model.compile(optimizer=Adam(lr=0.0001), 
              loss='categorical_crossentropy', 
              metrics=['accuracy'])

model.fit(x=train_batches,
            steps_per_epoch=len(train_batches),
            validation_data=valid_batches,
            validation_steps=len(valid_batches),
            epochs=30,
            verbose=2
)

ValueError: Shapes (None, None) and (None, 1, 1, 10) are incompatible
1 Answers

When you call the base model as follows, it will initiate with the default argument. Among them, include_top is set as True.

mobile = tf.keras.applications.mobilenet.MobileNet()

And, that brings (source) the GlobalAvg with keepdims=True.

  if include_top:
    x = layers.GlobalAveragePooling2D(keepdims=True)(x)

Now, based on your error, I assumed your true label shape and here you can do simply as follows

mobile = keras.applications.mobilenet.MobileNet()
x = mobile.layers[-5].output # shape=(None, 1, 1, 1024)
x = layers.Flatten()(x) # < --- Here shape=(None, 1024)

output =layers.Dense(units=10, activation='softmax')(x)
model = Model(inputs=mobile.input, outputs=output)
Related