【问题标题】:ImportError: Could not import the Python Imaging Library (PIL) required to load image files on tensorflowImportError:无法导入在 tensorflow 上加载图像文件所需的 Python Imaging Library (PIL)
【发布时间】:2017-04-28 16:56:48
【问题描述】:

我正在开设一个关于 udacity 的深度学习课程。对于第一个任务,当我尝试运行问题 1 下方的脚本时,我收到了这个错误。所以我尝试卸载 PIL 和枕头,然后单独安装它们,但我没有成功。 我需要帮助的家伙。我正在使用带有 python notebook 的 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

https://github.com/tensorflow/tensorflow/blob/master/tensorflow/examples/udacity/1_notmnist.ipynb

您可以在此处查看代码。问题 1 后代码块中出现错误 Error Image

我尝试了这两个链接或解决方案中描述的所有内容:

Solution 1 on stackoverflow

Solution 2 on stackoverflow

操作系统:

使用 docker 和 tensorflow 安装在带有 IPython notebook 的容器中。

python -c "import tensorflow; print(tensorflow.version)" 的输出。

0.11.0

【问题讨论】:

  • 你安装了 PIL 吗?如果没有,请点击错误消息中的链接
  • @lhk 是的,我做到了,它给了我同样的错误。

标签: python python-2.7 tensorflow python-imaging-library pillow


【解决方案1】:

在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 

【讨论】:

    【解决方案2】:

    对于使用 Anaconda 的 Windows 用户,您的问题可能有一个简单的解决方案。如果您使用 pip 或 pip 变体(即 pip3)安装了“tensorflow”,则必须再次安装 tensorflow,但这次使用命令 conda install tensorflow

    【讨论】:

    • 这是安装 CPU 还是 GPU 变体?
    【解决方案3】:

    pip install pillow

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

    【讨论】:

    • 不要忘记在 pip install 之后重新启动你的环境..,比如:source deactivate;源激活..等
    【解决方案4】:

    我遇到了同样的问题。但是我对张量流使用了不同的设置。操作系统:Ubuntu 14.04 LTS。使用 Anaconda 安装。 我按照Pillow installation 中的警告解决了这个问题。不过,它可能对 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 错误。

    【讨论】:

    • 注意:您可能需要重新启动 spyder 才能使更改生效。 (感谢 SanjeevKumar 指出这一点!)
    • 谢谢。我在 Udemy 学习人工智能,在 255 班我遇到了这种情况。我使用 Ubuntu 16.04。再次感谢您。
    【解决方案5】:

    我通过卸载 Jupyter 并重新正确安装它解决了这个问题。问题与笔记本内核有关。我的终端和笔记本没有相同的内核。 为了检查它,我在我的 virtualenv 中做了:

    jupyter-kernelspec list

    然后转到您的内核目录列表并打开 json 文件(类似于/Library/Jupyter/kernels/virtualenv/kernel.json

    并检查 Python 链接是否与输出中的相同 which python.

    如果没有,请为您的 virtualenv 创建另一个内核。

    【讨论】:

    猜你喜欢
    • 2016-06-02
    • 2019-08-13
    • 2016-09-27
    • 1970-01-01
    • 1970-01-01
    • 2011-01-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多