请稍等 ...
×

采纳答案成功!

向帮助你的同学说点啥吧!感谢那些助人为乐的人

cannot import name 'load_img' from 'keras.preprocessing.image'

#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.

正在回答 回答被采纳积分+3

1回答

提问者 Reylcn 2022-10-27 21:55:37

keras.preprocessing API is deprecated in Tensorflow 2.9.1. Please use tf.keras.utils instead, to import load_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.


2 回复 有任何疑惑可以回复我~
问题已解决,确定采纳
还有疑问,暂不采纳
意见反馈 帮助中心 APP下载
官方微信