HOW TO FIX IT? AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'

Viewed 11990
    import numpy as np
    from keras.preprocessing import image
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    import matplotlib.pyplot as plt
    import matplotlib.image as mpimg
    
    
    %matplotlib inline
    
    
    
    path = './test/paper2.png'
    
    img = image.load_img(path, target_size=(150,150))
    imgplot = plt.imshow(img)
    x = image.img_to_array(img)
    img_test = np.expand_dims(x, axis=0)
    
    classes = model.predict(img_test, batch_size=10)
    
    print(classes)
    paper, rock, scissors = classes[0]
    
    if paper==1.:
        print('paper')
    elif rock==1.:
        print('rock')
    else:
        print('scissors')

output :


AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'

when I try to run. What does the error mean and how can I fix it? help guys :) I'm trying to learn I don't know anymore which one is wrong

6 Answers

I'm facing the same problem today. You can try using tensorflow 2.8.0 to fix it or try tf.keras.utils.load_img instead of image.load_img.

I also face the same error. I used from tensorflow.keras.utils import load_img, img_to_array and it work for me.

use keras.utils.load_img

import keras
import tensorflow as tf

image = keras.utils.load_img('path_to_image', target_size=(img_size, img_size))

I just added 'tensorflow.' infront of the keras like 'tensorflow.keras.' and it worked.

I too faced the same problem. I used from tensorflow.keras.utils import load_img, img_to_array. Problem got solved.

Related