【发布时间】:2018-11-27 02:27:15
【问题描述】:
当我尝试运行预训练的 MobileNet 分类时,我在帖子标题中遇到错误。我用来运行脚本的图像位于我的“MobileNet-inference-images/American_Cam.jpg”目录中。
任何帮助或建议将不胜感激。
这是我的脚本、我的环境、错误回溯以及到目前为止所调查的内容。
import numpy as np
import keras
from keras import backend as K
from keras.layers.core import Dense
from keras.optimizers import Adam
from keras.metrics import categorical_crossentropy
from keras.preprocessing.image import ImageDataGenerator
from keras.models import Model
from keras.applications import imagenet_utils
from sklearn.metrics import confusion_matrix
import itertools
import matplotlib.pyplot as plt
%matplotlib inline
mobile =keras.applications.mobilenet.MobileNet()
def prepare_image(file):
img_path = 'MobileNet-inference-images/'
img = image.load_img(img_path + file, target_size=(224, 224))
img_array = image.img_to_array(img)
img_array_expanded_dims = np.expand_dims(img_array, axis=0)
return
keras.applications.mobilenet.preprocess_imput(img_array_expanded_dims)
preprocessed_image = prepare_image('MobileNet-inference-images/American_Cam.jpg')
predictions = mobile.predict(preprocessed_image)
results = imagenet_utils.decode_predictions(predictions)
results
我在 Juypter 笔记本的 Anaconda“自定义”环境(64 位)中运行 python 3.6.1。
回溯是
---------------------------------------------------------------------------
NameError Traceback (most recent call last)
<ipython-input-40-90b9684f2691> in <module>()
----> 1 preprocessed_image = prepare_image('MobileNet-inference-images/American_Cam.jpg')
2 predictions = mobile.predict(preprocessed_image)
3 results = imagenet_utils.decode_predictions(predictions)
4 results
<ipython-input-32-c204346d1e63> in prepare_image(file)
1 def prepare_image(file):
2 img_path = 'MobileNet-inference-images/'
----> 3 img = image.load_img(img_path + file, target_size=(224, 224))
4 img_array = image.img_to_array(img)
5 img_array_expanded_dims = np.expand_dims(img_array, axis=0)
NameError: name 'image' is not defined
我看到了同名错误here,但这似乎是一个单独的问题(因为我附上了我的图像路径)。 Other posts 提出了 PIL 的问题。但是,如果我测试 PIL 是否使用简单的脚本(如下所示)运行,我不会收到 PIL 错误。
from PIL import Image
im = Image.open('MobileNet-inference-images/American_Cam.jpg')
im.show()
【问题讨论】:
-
你可能想导入
load_img,因为你已经导入了ImageDataGenerator并直接使用它。
标签: python python-3.x keras jupyter-notebook classification