如何修复导入 Torchvision 时返回 PILLOW_VERSION 错误的错误

如何修复导入 Torchvision 时返回 PILLOW_VERSION 错误的错误

我在使用 pytorch+cuda 构建 docker 时遇到问题。这是我使用的 dockerfile

#FROM nvidia/cuda:9.0-base-ubuntu16.04
FROM nvidia/cuda:9.2-base-ubuntu18.04
# Install some basic utilities
RUN apt-get update && apt-get install -y \
    curl \
    ca-certificates \
    sudo \
    git \
    bzip2 \
    libx11-6 \
    python3-pip\
 && rm -rf /var/lib/apt/lists/*

# Create a working directory
RUN mkdir /app
WORKDIR /app

# Create a non-root user and switch to it
RUN adduser --disabled-password --gecos '' --shell /bin/bash user \
 && chown -R user:user /app
RUN echo "user ALL=(ALL) NOPASSWD:ALL" > /etc/sudoers.d/90-user
USER user

# All users can use /home/user as their home directory
ENV HOME=/home/user
RUN chmod 777 /home/user

# Install Miniconda
RUN curl -so ~/miniconda.sh https://repo.continuum.io/miniconda/Miniconda3-4.7.12.1-Linux-x86_64.sh \
 && chmod +x ~/miniconda.sh \
 && ~/miniconda.sh -b -p ~/miniconda \
 && rm ~/miniconda.sh
ENV PATH=/home/user/miniconda/bin:$PATH
#ENV CONDA_AUTO_UPDATE_CONDA=false

# Create a Python 3.6 environment
RUN /home/user/miniconda/bin/conda install conda-build \
 && /home/user/miniconda/bin/conda create -y --name py36 python=3.6.5 \
 && /home/user/miniconda/bin/conda clean -ya
ENV CONDA_DEFAULT_ENV=py36
ENV CONDA_PREFIX=/home/user/miniconda/envs/$CONDA_DEFAULT_ENV
ENV PATH=$CONDA_PREFIX/bin:$PATH

# CUDA 9.0-specific steps
RUN conda install pytorch torchvision cudatoolkit=9.2 -c pytorch \
 && conda clean -ya

# Install HDF5 Python bindings
RUN conda install -y h5py=2.8.0 \
 && conda clean -ya
RUN pip3 install h5py-cache==1.0

# Install Torchnet, a high-level framework for PyTorch
RUN pip3 install torchnet==0.0.4

# Install Requests, a Python library for making HTTP requests
RUN conda install -y requests=2.19.1 \
 && conda clean -ya

# Install Graphviz
RUN conda install -c anaconda graphviz \
 && conda clean -ya

# Install OpenCV3 Python bindings
RUN sudo apt-get update && sudo apt-get install -y --no-install-recommends \
    libgtk2.0-0 \
    libcanberra-gtk-module \
 && sudo rm -rf /var/lib/apt/lists/*
RUN conda install -y -c menpo opencv3=3.1.0 \
 && conda clean -ya

# Install Numpy
RUN conda install -y -c anaconda numpy \
 && conda clean -ya

# Install matplotlib
RUN conda install -c conda-forge matplotlib \
 && conda clean -ya

# Install pandas
RUN conda install -c anaconda pandas \
 && conda clean -ya

# Install Nano
RUN sudo apt-get update && sudo apt-get install -y nano

#Copy data
RUN mkdir sfsnet
COPY /SfSNet/ /app/sfsnet/

# Set the default command to python3
#CMD ["python3"]

问题是每当我尝试导入 torchvision 时它总是返回与 PIL 相关的错误。

 PIL.__version__ 
'7.0.0'
>>> import torchvision
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/home/user/miniconda/envs/py36/lib/python3.6/site-packages/torchvision/__init__.py", line 4, in <module>
    from torchvision import datasets
  File "/home/user/miniconda/envs/py36/lib/python3.6/site-packages/torchvision/datasets/__init__.py", line 9, in <module>
    from .fakedata import FakeData
  File "/home/user/miniconda/envs/py36/lib/python3.6/site-packages/torchvision/datasets/fakedata.py", line 3, in <module>
    from .. import transforms
  File "/home/user/miniconda/envs/py36/lib/python3.6/site-packages/torchvision/transforms/__init__.py", line 1, in <module>
    from .transforms import *
  File "/home/user/miniconda/envs/py36/lib/python3.6/site-packages/torchvision/transforms/transforms.py", line 17, in <module>
    from . import functional as F
  File "/home/user/miniconda/envs/py36/lib/python3.6/site-packages/torchvision/transforms/functional.py", line 5, in <module>
    from PIL import Image, ImageOps, ImageEnhance, PILLOW_VERSION
ImportError: cannot import name 'PILLOW_VERSION'

我读过其他帖子,说这是由于旧版本的错误造成的,应该在新版本的 Torchvision 中修复,但问题仍然存在(参考)有人能给我一些解决方案吗?谢谢

答案1

在 Ubuntu 上pip安装适用于 Python v2 环境的软件包,而您使用的是 Python v3。请改用pip3

编辑:我没有看到pillow您的 Dockerfile 已安装。此外,似乎所有东西都可以用 Conda 安装,也许应该以这种方式安装,否则它可能不包含在 Conda 环境中。

答案2

Pillow 7.0 版包含对 API 的重大更改,这些更改未完全纳入您所使用的 torchvision 版本。针对此问题的修复已发布到 pipy/Conda 的 Linux 和 Mac 版本。pip install torchvision==0.5.0 是修复重大 API 问题的要求。

打开了一些相关问题。

https://github.com/pytorch/vision/issues/1774

相关内容