导入错误:无法导入Python Imaging Library(PIL),这是在TensorFlow上加载图像文件所需的库。

55

我正在Udacity上学习深度学习课程。在第一个作业中,当我尝试运行下面问题1所示的脚本时,我遇到了这个错误。因此,我尝试卸载了PIL和pillow,然后单独安装它们,但我没有成功。

我需要帮助,我正在使用带Python笔记本的tensorflow docker镜像。

# These are all the modules we'll be using later. Make sure you can import them
# before proceeding further.
from __future__ import print_function
import matplotlib.pyplot as plt
import numpy as np
import os
import sys
import scipy
import tarfile
from IPython.display import display, Image
from scipy import ndimage
from sklearn.linear_model import LogisticRegression
from six.moves.urllib.request import urlretrieve
from six.moves import cPickle as pickle
# Config the matplotlib backend as plotting inline in IPython
%matplotlib inline 

url = 'http://commondatastorage.googleapis.com/books1000/'
last_percent_reported = None

def download_progress_hook(count, blockSize, totalSize):
    percent = int(count * blockSize * 100 / totalSize)

   if last_percent_reported != percent:
     if percent % 5 == 0:
  sys.stdout.write("%s%%" % percent)
  sys.stdout.flush()
else:
  sys.stdout.write(".")
  sys.stdout.flush()

last_percent_reported = percent

在这里可以查看代码。我在第一个问题后的代码块中出现了错误。

错误图片

我尝试了stack overflow上的解决方法1解决方法2,但都没有成功。

操作系统:

使用docker,在容器中安装了tensorflow和IPython笔记本电脑。

从python -c "import tensorflow; print(tensorflow.version)"得到输出结果为0.11.0。


你安装了PIL吗?如果没有,请按照错误信息中的链接进行安装。 - lhk
1
@lhk 是的,我尝试过了,但是还是出现了相同的错误。 - user2633120
5个回答

71

pip install pillow

然后将 from IPython.display import display, Image 替换为 from IPython.display import display from PIL import Image


8
安装完pip后别忘了重启环境,例如:source deactivate; source activate..等。 - ChaosPredictor

15

我遇到了同样的问题。但是我在使用tensorflow时采用了不同的设置。操作系统:Ubuntu 14.04 LTS。使用Anaconda进行安装。按照Pillow安装指南中的警告进行解决。但对于tensorflow的docker安装可能无效。

以下是我所做的步骤。首先进入tensorflow环境,

source activate tensorflow

然后卸载PIL并安装Pillow

conda uninstall PIL
conda install Pillow

然后在提供的代码中替换

from IPython.display import display, Image

from IPython.display import display
from PIL import Image

就是这样。重新运行代码,它将不会出现PIL错误。


4
注意:您可能需要重新启动 Spyder 才能使更改生效。(感谢 SanjeevKumar 指出这一点!) - Zain Rizvi
谢谢。我正在Udemy学习人工智能,在第255堂课中遇到了这种情况。我使用的是Ubuntu 16.04。再次感谢。 - LUISAO

2
我通过卸载Jupyter并重新正确安装,解决了此问题。问题与笔记本内核相关。我的终端和笔记本没有相同的内核。为了检查它,我在我的虚拟环境中执行了以下操作:
jupyter-kernelspec list
然后进入您的内核目录列表并打开JSON文件(类似于/Library/Jupyter/kernels/virtualenv/kernel.json),并检查Python链接是否与which python的输出相同。
如果不相同,请为您的虚拟环境创建另一个内核。

这个答案让我有了很大的帮助。为了为我的虚拟环境创建另一个内核,我不得不参考这份文档:http://ipython.readthedocs.io/en/stable/install/kernel_install.html,其中的“不同环境下的内核”部分。 - Tyler Byers

1
对于使用Anaconda的Windows用户,你们的问题可能有一个简单的解决方案。如果你使用pip或其变种(如pip3)安装了'tensorflow',那么你需要再次安装tensorflow,但这次使用命令conda install tensorflow

这个安装的是CPU还是GPU版本? - Gabriel Fair

-2

在Anaconda中安装PIL,然后执行以下操作:

from PIL import Image
model.fit_generator(
        train_generator,
        steps_per_epoch=2000 // batch_size,
        epochs=50,
        validation_data=validation_generator,
        validation_steps=800 // batch_size)
model.save_weights('first_try.h5') 

output will display like: Epoch 1/50
 34/125 [=======>......................] - ETA: 7:23 - loss: 0.7237 - acc: 0.5478 ... comntinue 

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接