NameError: name 'image' is not defined

51,148

from image you import only ImageDataGenerator but you also need other attributes, better change it

from keras.preprocessing.image import ImageDataGenerator

to

from keras.preprocessing import image
# and use 
# image.ImageDataGenerator()
# image.load_img()
Share:
51,148

Related videos on Youtube

Steve
Author by

Steve

Updated on June 18, 2020

Comments

  • Steve
    Steve almost 3 years

    I am getting the error in the post title when I attempt to run a pretrained MobileNet classification. The image I am using to run the script is located in my 'MobileNet-inference-images/American_Cam.jpg directory.

    Any help or advice would be appreciated.

    Here is my script, my environment, the error traceback, and what have investigated so far.

    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
    

    I am running python 3.6.1 in an Anaconda "custom" environment (64 bit) in a Juypter notebook.

    The traceback is

    ---------------------------------------------------------------------------
    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
    

    I have seen an error of the same name here, but that appears to be a separate issue(as I am enclosing my image path). Other posts have suggested an issue with PIL. But if I test is PIL is operating with a simple script(such as below), I don't get a PIL error.

    from PIL import Image
    im = Image.open('MobileNet-inference-images/American_Cam.jpg')
    im.show()
    
    • Klaus D.
      Klaus D. almost 5 years
      You might want to import load_img as you have imported ImageDataGenerator and use it directly.

Related