python - keras 2.2.4 中的 imagenet_utils.preprocess_input 方法是否有错误,或者只是我一个人?

标签 python keras

尝试使用 keras 提供的 imagenet_utils.preprocess_input(x) 方法并抛出错误:

File "C:\Dev\workspace\venvs\venv36\lib\site-packages\keras_applications\imagenet_utils.py", line 186, in preprocess_input
data_format = backend.image_data_format()
AttributeError: 'NoneType' object has no attribute 'image_data_format'

我找到了这篇文章( Keras : Create MobileNet_V2 model "AttributeError" ),两者之间是否有可能存在链接?

我使用 Python 3.6、Keras 2.2.4 和 Tensorflow 后端 1.12 运行此脚本

我正在尝试使用已经预训练的模型,特别是 VGG16 模型,并复制了在几个博客上找到的代码示例。 例如这里:https://blog.keras.io/building-a-simple-keras-deep-learning-rest-api.html

代码从 imagenet_utils.preprocess_input 方法引发异常,因为此底层代码:

backend, _, _, _ = get_submodules_from_kwargs(kwargs)

返回 backend = None 因此代码无法继续...

这就是为什么我在调用方法之前打印后端以查看它是否为 None 。似乎某个地方可以将其替换为 None ?

import keras
import numpy as np
from keras.applications import VGG16
from keras_applications import imagenet_utils
from keras_preprocessing.image import load_img, img_to_array


if __name__ == '__main__':
    model = VGG16(weights="imagenet")
    print("backend: {}".format(keras.backend.image_data_format()))

    img = load_img('./images/whatever.jpg', target_size=(224, 224))
    x = img_to_array(img)
    x = np.expand_dims(x, axis=0)

    print("backend: {}".format(keras.backend.image_data_format()))

    # Seems there is this bug to solve in 2.2.4
    x = imagenet_utils.preprocess_input(x)  # Will throw an error 'AttributeError: 'NoneType' object has no attribute 'image_data_format''
    predictions = model.predict(x)
    top_preds = imagenet_utils.decode_predictions(predictions)

    print(top_preds)

我应该向 Keras 团队提出问题吗? 我错过了什么吗?

最佳答案

如果有人遇到同样的问题,我终于找到了“错误”在哪里。 看完this github issue与我的非常相似,我更改了导入:

  • from keras_applications import VGG16 现在变为 from keras.applications import VGG16

  • from keras_applications import imagenet_utils 现在变为 from keras.applications import imagenet_utils

关于python - keras 2.2.4 中的 imagenet_utils.preprocess_input 方法是否有错误,或者只是我一个人?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54233814/

相关文章:

python - 损失曲线分析: raw data VS normalized data (Machine learning/Keras)

python - 继续使用 keras 中保存的模型训练 CNN

python-3.x - 无法保存自定义子类化模型

tensorflow - Keras 中的 .fit() 方法触发损失函数多少次

python - 如何使用 numpy 进行按列减法?

Python 支持有限形式的多重继承。以什么方式限制?

python - 如何更改此 python 行以使其读取其最新时间戳的图像而不说明特定的图像名称?

python - 线性回归的标准差/误差

python - 将文本文件中的 block 读入二维数组

machine-learning - 我可以在训练后改变 RNN 的状态吗?