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 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
0

Python3入门人工智能 掌握机器学习+深度学习

人工智能基础全方位讲解,构建完整人工智能知识体系,带你入门AI

1946 学习 · 594 问题

查看课程