我想使用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:可选的形状元组,仅当include_top为False时才指定(否则,输入形状必须为(224,224,3)(具有“ channels_last”数据格式)或(3,224,224)(具有“ channels_first”数据格式)。它应该恰好具有3个输入通道,宽度和高度应不小于32。例如,(200,200,3)是一个有效值。
但是,我收到以下错误消息:
ValueError:如果正在加载imagenet权重,则输入必须具有静态正方形形状((128、128),(160、160),(192、192)或(224、224)之一)。提供的输入形状=(200,200,3)
如果我指定include_top=False
,为什么为什么要求输入形状与训练的形状匹配?
Keras:2.2.4,TensorFlow:1.13.1
答案 0 :(得分: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.
答案 1 :(得分:1)
此异常是错误的,最近(2019年3月29日)从Keras中删除(请参阅GitHub上的issue和pull request)。截至2019年4月8日,此提交尚未发布,因此您必须从telnet [ip] 15106
Connection refused
安装。