NameError:未定义名称"图像"

Ste*_*eve 0 python classification python-3.x keras jupyter-notebook

当我尝试运行预训练的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
Run Code Online (Sandbox Code Playgroud)

我在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
Run Code Online (Sandbox Code Playgroud)

我已经看到了相同名称的错误在这里,但是这似乎是一个单独的问题(因为我附上我映像路径).其他帖子提出了PIL的问题.但是如果我测试的是PIL是用一个简单的脚本操作(比如下面的),我就不会得到PIL错误.

from PIL import Image
im = Image.open('MobileNet-inference-images/American_Cam.jpg')
im.show()
Run Code Online (Sandbox Code Playgroud)

Dru*_*lan 7

image你导入,ImageDataGenerator但你还需要其他属性,更好地改变它

from keras.preprocessing.image import ImageDataGenerator
Run Code Online (Sandbox Code Playgroud)

from keras.preprocessing import image
# and use 
# image.ImageDataGenerator()
# image.load_img()
Run Code Online (Sandbox Code Playgroud)