【问题标题】:Tensorflow with R and Anaconda - error "Could not import PIL.Image. The use of `load_img` requires PIL"带有 R 和 Anaconda 的 Tensorflow - 错误“无法导入 PIL.Image。使用 `load_img` 需要 PIL”
【发布时间】:2019-12-08 18:20:50
【问题描述】:

这个问题在 Python 环境中有一些答案,但这些解决方案不适用于我的 RStudio 环境。这是我的代码:

library(keras)
library(tensorflow)
use_condaenv("tf")
train_dir = "C:/training_images/"
train_datagen <- image_data_generator(rescale = 1/255)
validation_datagen <- image_data_generator(rescale = 1/255)
train_generator <- flow_images_from_directory(
    train_dir,
    train_datagen,
    target_size = c(150, 150),
    batch_size = 20,
    class_mode = "binary"
  )
batch <- generator_next(train_generator)

代码一直工作到最后一个“批处理”行,它像这样爆炸:

Error in py_iter_next(it, completed) : 
  ImportError: Could not import PIL.Image. The use of `load_img` requires PIL.

Detailed traceback: 
  File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\iterator.py", line 104, in __next__
    return self.next(*args, **kwargs)
  File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\iterator.py", line 116, in next
    return self._get_batches_of_transformed_samples(index_array)
  File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\iterator.py", line 230, in _get_batches_of_transformed_samples
    interpolation=self.interpolation)
  File "C:\Users\mory3\ANACON~1\envs\tf\lib\site-packages\keras_preprocessing\image\utils.py", line 108, in load_img
    raise ImportError('Could not import PIL.Image. '

R 版本 3.6.1
Conda 4.7 版
Python 3.7 版

【问题讨论】:

  • 另外,由于同样的原因,尝试运行fit_generator函数失败,所以我无法运行模型!
  • 通过conda update --all 进行的完整 conda 更新对我有用!

标签: tensorflow anaconda


【解决方案1】:

我遇到了同样的问题 经过几个小时的研究,我想出了一个适合我的解决方案。 我使用此代码来解决 PIL 问题。我尝试使用 anaconda 提示符,但这段代码在 r 中对我有用...

reticulate::py_install("pillow",env=tf)

接下来我想出了这个错误...

loaded runtime CuDNN library: 7.4.2 but source was compiled with: 7.6.0.

确保您安装了正确的 cudnn 版本。对我来说,它是 CUDA 10 和 7.6.0 cudnn 和 10。错误的输出会告诉你使用哪一个。 确保您已清除安装先前版本时环境变量中的任何额外路径变量。

I'm using windows 10
gpu = GeForce GTX 1060 with Max-Q Design
R - 3.6.1
tensorflow = 1.13
python = 3.7
anaconda = Anaconda3–2019.03-Windows-x86_64.exe

【讨论】:

  • 我为相同的情况运行了这个答案的第一个命令。使用我的 anaconda 本地安装和 python 3.6 安装了 Pillow -7.1.2。但是,这对初始错误没有任何影响。我的笔记本电脑没有 GPU。
【解决方案2】:

我最终完全卸载了 Anaconda,这使得解决与 R 的 Python 连接中剩余错误的问题变得更加简单

【讨论】:

  • 我从来没有尝试过在没有 anaconda 的情况下在 r 中运行 gpu 版本的 keras 和 tensorflow,但我可以想象它的设置是为了做到这一点,哈哈,我一直有 anaconda,希望有一天能学习 python,但是还没搞定
  • 我听到你@FrosyFeet456,很容易对这么多包的版本不一致感到沮丧。我系统地更新了每个组件,但是 conda 的完整更新解决了我的系统问题。
【解决方案3】:

我在 Win7 上使用“使用 R 进行深度学习”CNN 示例时遇到了同样的问题。我是这样解决的:

  1. 我在 PATH 中添加了 Anaconda3 路径。就我而言,它是 Windows,所以路径是这样的: C:\Anaconda3\Scripts;C:\Anaconda3\Library\bin 默认情况下,$PATH 中没有 conda 的路径。

  2. 将枕头(它包含 PIL)安装到 python: 点安装枕头

  3. 配置 r-reticulate。 这个答案Could not import PIL.Image even if Pillow already installed? 帮助了我。我已经有了枕头,但是 conda 环境没有正确配置,所以枕头不可见。

如果您没有 Nvidia CUDA,也请安装它 - TensorFlow 也需要它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-12-30
    • 1970-01-01
    • 2018-05-09
    • 1970-01-01
    • 2019-05-02
    • 2020-01-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多