I would like to apply in Keras MobileNetV2 on images of size 39 x 39 to classify 3 classes. My images represent heat maps (e.g. what keys have been pressed on the keyboard). I think MobileNet was designed to work on images of size 224 x 224. I will not use transfer learning but train the model from scratch.
To make MobileNet work on my images, I would like to replace the first three stride 2 convolutions with stride 1. I have the following code:
from tensorflow.keras.applications import MobileNetV2
base_model = MobileNetV2(weights=None, include_top=False,
input_shape=[39,39,3])
x = base_model.output
x = GlobalAveragePooling2D()(x)
x = Dropout(0.5)(x)
output_tensor = Dense(3, activation='softmax')(x)
cnn_model = Model(inputs=base_model.input, outputs=output_tensor)
opt = Adam(lr=learning_rate)
cnn_model.compile(loss='categorical_crossentropy',
optimizer=opt, metrics=['accuracy', tf.keras.metrics.AUC()])
How can I replace the first three stride 2 convolutions with stride 1 without building MobileNet myself?
Here is one workaround for your need but I think probably it's possible to have a more general approach. However, in the
MobileNetV2, there is only oneconvlayer withstrides 2. If you follow the source code, hereAnd the rest of the blocks are defined as follows
So, here I will deal with the first
convwithstride=(2, 2). The idea is simple, we will add a new layer in the right place of the built-in model and then remove the desired layer.The above
_make_divisiblefunction simply derived from the source code. Anyway, now we impute this layer to theMobileNetV2right before the firstconvlayer, as follows:Now, if we observe
Layer name
Conv1_andConv1are the new layer (withstrides = 1) and old layer (withstrides = 2) respectively. And as we need, now we remove layerConv1withstrides = 2as follows:Now, you have
cnn_modelmodel withstrides = 1on its firstconvlayer. However, in case you're wondering about this approach and possible issue, please see my other answer related to this one. Remove first N layers from a Keras Model?