keras.preprocessing 的导入错误

ImportError with keras.preprocessing

我正在关注 image classification tutorial at Tensorflow。在 运行 以下代码 -

import PIL
import tensorflow as tf
from tensorflow import keras

sunflower_url = "https://storage.googleapis.com/download.tensorflow.org/example_images/592px-Red_sunflower.jpg"
sunflower_path = tf.keras.utils.get_file('Red_sunflower', origin=sunflower_url)
img = keras.preprocessing.image.load_img(sunflower_path, target_size=(180, 180))

我在最后一行收到以下错误。

ImportError: Could not import PIL.Image. The use of `load_img` requires PIL.

如何解决上述问题?

请注意,我在我的 conda 工作环境中安装了 pillow(python=3.8,Tensorflow=2.3)。

错误表明您的计算机上没有安装 pillow。 如果你使用的是 conda,那么你必须做

conda install pillow

如果你不使用 conda,那我就试试

pip install pillow

编辑 1:如果您已经在 conda 环境中安装了 PIL,请尝试

conda uninstall PIL
conda install Pillow

编辑 2:如果您安装的旧版本 Pillow 无法与环境中安装的 TensorFlow/Keras 版本一起使用,则重新安装 Pillow 可能帮助。