vision: Error with PIL 8.3.0: __array__() takes 1 positional argument but 2 were given

🐛 Bug

The torchvision.transformer function ToTensor raise a TypeError when receiving a image loaded with PIL. The error origins from a call to np.array and contains the message: __array__() takes 1 positional argument but 2 were given

To Reproduce

Steps to reproduce the behavior:

import io
import requests
import torchvision.transforms as T
from PIL import Image

resp = requests.get('https://picsum.photos/200')
img = Image.open(io.BytesIO(resp.content))

preprocess1 = T.Compose([
   T.ToTensor(),
])

x = preprocess1(img)
print(x.shape)

Pip Freeze Log:

certifi==2021.5.30
chardet==4.0.0
idna==2.10
numpy==1.21.0
Pillow==8.3.0
requests==2.25.1
torch==1.9.0
torchvision==0.10.0
typing-extensions==3.10.0.0
urllib3==1.26.6

Full Traceback:

Traceback (most recent call last):
  File "/Users/sebastianlettner/Desktop/tmp/test.py", line 13, in <module>
    x = preprocess1(img)
  File "/Users/sebastianlettner/Desktop/tmp/.venv/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 60, in __call__
    img = t(img)
  File "/Users/sebastianlettner/Desktop/tmp/.venv/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 97, in __call__
    return F.to_tensor(pic)
  File "/Users/sebastianlettner/Desktop/tmp/.venv/lib/python3.8/site-packages/torchvision/transforms/functional.py", line 129, in to_tensor
    np.array(pic, mode_to_nptype.get(pic.mode, np.uint8), copy=True)
TypeError: __array__() takes 1 positional argument but 2 were given

Expected behavior

I expect that the conversion works and I reveive a torch.Tensor with shape (200, 200, 3)

Environment

Collecting environment information...
PyTorch version: 1.9.0
Is debug build: False
CUDA used to build PyTorch: None
ROCM used to build PyTorch: N/A

OS: macOS 10.15.7 (x86_64)
GCC version: Could not collect
Clang version: 12.0.0 (clang-1200.0.32.29)
CMake version: Could not collect
Libc version: N/A

Python version: 3.8.10 (default, May  4 2021, 03:05:50)  [Clang 12.0.0 (clang-1200.0.32.29)] (64-bit runtime)
Python platform: macOS-10.15.7-x86_64-i386-64bit
Is CUDA available: False
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.21.0
[pip3] torch==1.9.0
[pip3] torchvision==0.10.0
[conda] Could not collect
  • PyTorch / torchvision Version (e.g., 1.0 / 0.4.0): 1.9.0 / 0.10.0
  • OS (e.g., Linux): macOS (10.15.7 (19H15))
  • How you installed PyTorch / torchvision (conda, pip, source): pip
  • Build command you used (if compiling from source): n/A
  • Python version: 3.8.10
  • CUDA/cuDNN version: n/A
  • GPU models and configuration: n/A
  • Any other relevant information:

Additional context

n/A

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Comments: 15 (9 by maintainers)

Most upvoted comments

Pillow 8.3.1 should now be released with a fix for this.

We can close this now. Unless someone explicitly specifies Pillow==8.3.0, pip install torch torchvision will now install a compatible Pillow version.

Re-opening temporarily as other users might bump into this issue, this will hopefully help avoid duplicated entries like https://github.com/pytorch/vision/issues/4152

Is this a common practice? I’ve never seen it.

I’ve seen it a few times, for example here

but if we pushed the logic further, we might as well exclude all PIL versions that have known bugs, a.k.a only accept the very latest PIL?

If we know that there are Pillow versions that will break our code, than yes, we should explicitly exclude them. Most of the time Pillow bugs will only degrade our quality.