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

14

26
    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

Rodrick answered 25/5, 2022 at 19:43 Comment(1)
Try from tensorflow.keras.preprocessing import image instead as mentioned here @Almaz FazulzyanovReconnaissance
B
25

Replace:

from keras.preprocessing import image

for:

import keras.utils as image
Bloke answered 5/11, 2022 at 22:43 Comment(0)
C
18

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.

Charissecharita answered 26/5, 2022 at 11:10 Comment(0)
S
3

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

Saritasarkaria answered 25/11, 2022 at 13:29 Comment(2)
Could you share the keras version you're working with for reference? Thanks!Saloma
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Weaponless
E
2

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

Effectual answered 19/7, 2022 at 8:44 Comment(0)
M
2

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 :- https://keras.io/api/data_loading/image/

Source :- https://github.com/keras-team/keras/blob/v2.10.0/keras/utils/image_utils.py#L364

Monorail answered 14/10, 2022 at 12:56 Comment(0)
A
2

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.

Array answered 23/12, 2022 at 12:16 Comment(1)
This is a duplicate answer.Blastosphere
A
2

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)
Antho answered 16/2, 2023 at 16:55 Comment(0)
H
1

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

Hydrostat answered 24/2, 2023 at 14:19 Comment(0)
Z
0

there is no 'load_img' https://github.com/keras-team/keras/blob/master/keras/preprocessing/image.py

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

Zohar answered 25/5, 2022 at 20:1 Comment(2)
It looks to be in the documentation: keras.io/api/preprocessing/image? Any idea why the documentation is wrong?Sibeal
documentations says import tf.keras.preprocessing.image instead keras.preprocessing.imageZohar
T
0

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))
Trillbee answered 28/5, 2022 at 5:29 Comment(0)
M
0

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

Mothball answered 17/7, 2022 at 20:44 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Weaponless
T
0

first import

import tensorflow.compat.v2 as tf

then

tf.keras.preprocessing.image.load_img
Turino answered 7/10, 2022 at 6:3 Comment(0)
S
0

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.

Spoilfive answered 21/2, 2023 at 14:2 Comment(0)
E
0

I had a similar error and added:

import keras.utils as image

and its worked

Eddy answered 14/10, 2023 at 20:18 Comment(1)
This is already covered in existing answers. Please don't repeat answers or add "thank you" as an answer.Deviled

© 2022 - 2024 — McMap. All rights reserved.