cannot import name 'load_img' from 'keras.preprocessing.image'
来源:8-4 实战(一)
Reylcn
2022-10-27
#load single image
from keras.preprocessing.image import load_img, img_to_array
pic_dog = '/content/drive/MyDrive/imooc/dataset/dog.jpg’
pic_dog = load_img(pic_dog,target_size=(50,50))
pic_dog = img_to_array(pic_dog)
pic_dog = pic_dog/255
pic_dog = pic_dog.reshape(1,50,50,3)
result = model.predict_classes(pic_dog)
print(result)
报错
ImportError Traceback (most recent call last)
in
1 #load single image
----> 2 from keras.preprocessing.image import load_img, img_to_array
3 pic_dog = '/content/drive/MyDrive/imooc/dataset/dog.jpg’
4 pic_dog = load_img(pic_dog,target_size=(50,50))
5 pic_dog = img_to_array(pic_dog)
ImportError: cannot import name ‘load_img’ from ‘keras.preprocessing.image’ (/usr/local/lib/python3.7/dist-packages/keras/preprocessing/image.py)
NOTE: If your import is failing due to a missing package, you can
manually install dependencies using either !pip or !apt.
To view examples of installing some common dependencies, click the
"Open Examples" button below.
1回答
-
Reylcn
提问者
2022-10-27
keras.preprocessing
API is deprecated in Tensorflow 2.9.1. Please usetf.keras.utils
instead, to importload_img
as follows:from tensorflow.keras.utils import load_img
To load dataset from directories please use
tensorflow.keras.utils.image_dataset_from_directory
. For more details, please refer to this link.20
相似问题