MobileNets for a custom image size

梦想与她 提交于 2020-01-15 17:56:11

问题


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).


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/55571664/mobilenets-for-a-custom-image-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!