Error with PIL 8.3.0: __array__() takes 1 positional argument but 2 were given
See original GitHub issue🐛 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
Issue Analytics
- State:
- Created 2 years ago
- Comments:15 (9 by maintainers)
Top Results From Across the Web
TypeError: __array__() takes 1 positional argument but 2 were ...
I've tried setting dtype=None in the image.img_to_array method . import tensorflow as tf from tensorflow import keras from tensorflow.keras ...
Read more >Quickstart tutorial: __array__() takes 1 positional argument but ...
Running this gives me the error: TypeError: __array__() takes 1 positional argument but 2 were given. It's the for loop that causes the ......
Read more >Python takes 1 positional argument but 2 were given Solution
When you call a method associated with an object, there is one positional argument that is supplied by default: self.
Read more >8.3.1 - Pillow (PIL Fork) 9.3.0 documentation
This fixes a regression introduced in 8.3.0 when converting an image to a NumPy array with a dtype argument. >>> from PIL import...
Read more >TypeError: __array__() takes 1 positional argument but 2 were ...
这个问题更奇葩,图像读取时的报错。大致是下面这种情况:. np.array ...
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
We can close this now. Unless someone explicitly specifies
Pillow==8.3.0
,pip install torch torchvision
will now install a compatiblePillow
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