Reputation: 55
I want to use the MobileNet model pre-trained on ImageNet for feature extraction. I am loading the model as follows:
from keras.applications.mobilenet import MobileNet
feature_model = MobileNet(include_top=False, weights='imagenet', input_shape=(200, 200, 3))
The Keras manual clearly says that this input shape is valid:
input_shape: optional shape tuple, only to be specified if include_top is False (otherwise the input shape has to be (224, 224, 3) (with 'channels_last' data format) or (3, 224, 224) (with 'channels_first' data format). It should have exactly 3 inputs channels, and width and height should be no smaller than 32. E.g. (200, 200, 3) would be one valid value.
However, I get the following error message:
ValueError: If imagenet weights are being loaded, input must have a static square shape (one of (128, 128), (160, 160), (192, 192), or (224, 224)). Input shape provided = (200, 200, 3)
Why does it require the input shape to match the one it was trained on if I specify include_top=False
?
Keras: 2.2.4, TensorFlow: 1.13.1
Update: As @Soroush pointed out, this exception was removed recently. However, the issue was not fully resolved as described here.
Update2: The problem was resolved by these two pull requests (1, 2).
Upvotes: 1
Views: 18143
Reputation: 1253
To use custom image size in MobileNet, download weights form this link: https://github.com/fchollet/deep-learning-models/releases/tag/v0.6
But make sure which weights you need because it contains different weights files according to the research paper of MobileNet, as each model is dependent on the parameters alpha
and depth_multiplier
. There are four different values for alpha
: 0.25, 0.50, 0.75, 1.0. Also, depth_multiplier
is 1 according to this implementation of mobilenet. I would recommend that you download mobilenet_1_0_224_tf.h5
. It has the highest ImageNet accuracy among all according to research paper Table 7.
After that,
from keras.applications.mobilenet import MobileNet
feature_model = MobileNet(include_top=False, weights=None, input_shape=(200, 200, 3), alpha=1.0, depth_multiplier=1)
feature_model.load_weights('mobilenet_1_0_224_tf.h5') # give the path for downloaded weights
And you're good to go.
Upvotes: 4
Reputation: 1105
This exception is wrong and was recently (Mar 29, 2019) removed from Keras (see the issue and pull request on GitHub). As of Apr 8, 2019, this commit is not released yet, so you have to install from master
.
Upvotes: 1