23
    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

Muhammad Syahdewa
  • 231
  • 1
  • 2
  • 4
  • Try `from tensorflow.keras.preprocessing import image` instead as mentioned here @Almaz Fazulzyanov – Elias Jun 23 '22 at 13:40

13 Answers13

22

Replace:

from keras.preprocessing import image

for:

import keras.utils as image
F.Souza
  • 221
  • 2
  • 4
17

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.

RiveN
  • 2,595
  • 11
  • 13
  • 26
rvp
  • 171
  • 4
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)"

THINKER J
  • 25
  • 3
  • Could you share the keras version you're working with for reference? Thanks! – Ben Saunders Nov 28 '22 at 23:24
  • 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](/help/how-to-answer). – Community Nov 28 '22 at 23:24
2

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

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

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.

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)
Wakeme UpNow
  • 523
  • 1
  • 4
  • 22
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

falm
  • 51
  • 1
  • 6
  • It looks to be in the documentation: https://keras.io/api/preprocessing/image/? Any idea why the documentation is wrong? – DavidW May 25 '22 at 20:32
  • 1
    documentations says `import tf.keras.preprocessing.image` instead `keras.preprocessing.image` – falm May 25 '22 at 20:59
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))
Udesh
  • 2,415
  • 2
  • 22
  • 32
0

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

  • 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](/help/how-to-answer). – Community Jul 19 '22 at 09:13
0

first import

import tensorflow.compat.v2 as tf

then

tf.keras.preprocessing.image.load_img
Jack Deeth
  • 3,062
  • 3
  • 24
  • 39
Pirate
  • 1
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.

Kliment
  • 39
  • 2
0

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