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 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
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
- "from keras.utils import load_img, img_to_array instead" of "from keras.preprocessing import image"
and change
"img = image.load_img(path, target_size=(150,150))" to "load_img(path, target_size=(150,150))"
"x = image.img_to_array(img)" to "x = img_to_array(img)"
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.
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
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.
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