我想使用在ImageNet上预训练的MobileNet模型进行特征提取。我按如下方式加载模型:
from keras.applications.mobilenet import MobileNet
feature_model = MobileNet(include_top=False, weights='imagenet', input_shape=(200, 200, 3))
Keras 手册 clearly says该输入形状有效:
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.
但是,我收到以下错误消息:
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)
如果我指定include_top=False
,为什么它要求输入形状与训练时的形状相匹配?
Keras:2.2.4,TensorFlow:1.13.1
更新:正如 @Soroush 指出的那样,此异常已被删除 recently 。但是,该问题并未按照描述完全解决 here .
最佳答案
要在 MobileNet 中使用自定义图像大小,请从此链接下载权重:https://github.com/fchollet/deep-learning-models/releases/tag/v0.6
但是请确保您需要哪些权重,因为根据 MobileNet 的研究论文它包含不同的权重文件,因为每个模型都依赖于参数 alpha
和 depth_multiplier
。 alpha
有四种不同的值:0.25、0.50、0.75、1.0。另外,根据 mobilenet 的实现,depth_multiplier
为 1。 。我建议您下载mobilenet_1_0_224_tf.h5
。根据research paper Table 7,它具有最高的 ImageNet 准确度。 .
之后,
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
你就可以开始了。
关于python - 用于自定义图像尺寸的 MobileNets,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55571664/