python - 用于自定义图像尺寸的 MobileNets

标签 python tensorflow keras deep-learning

我想使用在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 .

更新2:问题已通过这两个拉取请求解决( 12 )。

最佳答案

要在 MobileNet 中使用自定义图像大小,请从此链接下载权重:https://github.com/fchollet/deep-learning-models/releases/tag/v0.6

但是请确保您需要哪些权重,因为根据 MobileNet 的研究论文它包含不同的权重文件,因为每个模型都依赖于参数 alphadepth_multiplieralpha 有四种不同的值: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/

相关文章:

python - 如何根据其他列值动态填充列值?

python - Django 序列化器 : why does self. fields.pop ('field_name' )工作吗?

python - Tensorflow CPU(在 python 交互式 shell 中正常,但运行脚本时出现 coredump)

python - 提高 Tensorflow 图像分类的速度

python - 在 TF/Numpy 中进行这种串联的优雅方法是什么?

python - 当batch_size与数据量不匹配时,Keras自定义生成器

python - 获取二维 numpy 数组中大于阈值的元素的索引

python - 我可以在没有 nvidia 显卡的情况下安装 Tensorflow gpu 吗?

tensorflow - Keras `ImageDataGenerator` 图像和掩模以不同方式增强

python - PyAudio 内存错误