How to Fix It? Attributeerror: Module 'Keras. Preprocessing. Image' Has No Attribute 'Load_Img'
    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

1

14 Answers

Replace:

from keras.preprocessing import image

for:

import keras.utils as image

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 too had this and fixed

changes in import

  1. "from keras.utils import load_img, img_to_array instead" of "from keras.preprocessing import image"

and change

  1. "img = image.load_img(path, target_size=(150,150))" to "load_img(path, target_size=(150,150))"

  2. "x = image.img_to_array(img)" to "x = img_to_array(img)"

2

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

Try this out

change this

from keras.preprocessing import image
test_image = image.load_img('$PATH', target_size = (64, 64))
test_image =  image.img_to_array(test_image)

To this

from keras.utils import load_img, img_to_array
test_image = load_img('$PATH', target_size = (64, 64))
test_image = img_to_array(test_image)

reff :-

Source :-

The "AttributeError: module 'keras.preprocessing.image' has no attribute 'load_img'" occurs because the keras preprocessing API has been deprecated. To solve the error, import the load_img() function from tensorflow.keras.utils.load_img.

1

Use this first

from keras.utils import load_img, img_to_array

Then use load image like this

train_image = []

# ...

# Assume `train` is each batch X output in the form of dictionary
for i in tqdm(range(train.shape[0])):
    img = load_img('path to folder' + train['Name'][i], target_size=(400, 400, 3))
    img = img_to_array(img)
    img = img/255
    train_image.append(img)

X = np.array(train_image)

there is no 'load_img'

I suppose you trying to use load_img of keras.utils.image_utils

2

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.

1

first import

import tensorflow.compat.v2 as tf

then

tf.keras.preprocessing.image.load_img

So I had a similar error

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

and I had this import statement first

from keras.preprocessing import image

then I added utils so that

from keras.preprocessing import image
import keras.utils as image

and then the error disappeared.

I got the same error.

To resolve this error, you can try importing the load_img function from the tensorflow.keras.preprocessing.image module instead of the keras.preprocessing.image module. You can modify your code to include the following import statement :

from tensorflow.keras.preprocessing import image

it worked for me

I had a similar error and added:

import keras.utils as image

and its worked

1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Chloe Bennett

Chloe Bennett

Culture, Media & Entertainment Columnist

Chloe Bennett explores the intersection of pop culture, streaming entertainment, digital trends, and contemporary lifestyle. Her weekly commentary reaches thousands of culture enthusiasts.

Share this article
Twitter Facebook Pinterest