具有R和Anaconda的Tensorflow-错误“无法导入PIL.Image。使用`load_img`需要PIL”

时间:2019-12-08 18:20:50

标签: tensorflow anaconda

在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版

3 个答案:

答案 0 :(得分:1)

我有同样的问题 经过几个小时的研究,我想出了一个对我有用的解决方案。 我使用此代码解决了PIL问题。我尝试使用anaconda提示符,但是这段代码对我来说很有效……

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

答案 1 :(得分:0)

我最终完全卸载了Anaconda,这使得故障排除与R 的Python连接中的其余错误变得更加简单。

答案 2 :(得分:0)

我在 Win7 上的“Deep Learning with R”CNN 示例中遇到了同样的问题。我是这样解决的:

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

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

  3. 配置的 r 网状。 这个答案 Could not import PIL.Image even if Pillow already installed? 帮助了我。我已经有了枕头,但 conda 环境配置不正确,所以看不到枕头。

如果您没有 Nvidia CUDA,也请安装它 - 您也需要它来用于 tensorflow。