【发布时间】:2019-01-17 10:25:53
【问题描述】:
尝试使用 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 团队提出问题吗? 我错过了什么吗?
【问题讨论】: