Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Bug] TypeError: __array__() takes 1 positional argument but 2 were given #61125

Closed
sainatarajan opened this issue Jul 1, 2021 · 10 comments
Closed
Labels
module: third_party module: vision triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Comments

@sainatarajan
Copy link

sainatarajan commented Jul 1, 2021

馃悰 Bug

Unable to perform torchvision transform on an image.

To Reproduce

Steps to reproduce the behavior:

import torch
import torchvision.transforms as T

from PIL import Image

transforms = T.Compose([
    T.Resize(size=224),
    T.ToTensor(),
    T.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])

img = Image.open(path_to_image).convert('RGB')
data = transforms(img)

Expected behavior

The code should have successfully run and the image should've been transformed.

Environment

PyTorch version: 1.9.0+cu111
Is debug build: False
CUDA used to build PyTorch: 11.1
ROCM used to build PyTorch: N/A

OS: Ubuntu 20.04.2 LTS (x86_64)
GCC version: (Ubuntu 9.3.0-17ubuntu1~20.04) 9.3.0
Clang version: Could not collect
CMake version: Could not collect
Libc version: glibc-2.31

Python version: 3.8.10 (default, Jun  4 2021, 15:09:15)  [GCC 7.5.0] (64-bit runtime)
Python platform: Linux-5.8.0-59-generic-x86_64-with-glibc2.17
Is CUDA available: True
CUDA runtime version: Could not collect
GPU models and configuration: GPU 0: GeForce RTX 3060
Nvidia driver version: 460.84
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A

Versions of relevant libraries:
[pip3] numpy==1.20.2
[pip3] torch==1.9.0+cu111
[pip3] torch-geometric==1.7.2
[pip3] torch-scatter==2.0.7
[pip3] torch-sparse==0.6.10
[pip3] torchaudio==0.9.0
[pip3] torchvision==0.10.0+cu111
[conda] numpy                     1.20.2                   pypi_0    pypi
[conda] torch                     1.9.0+cu111              pypi_0    pypi
[conda] torch-geometric           1.7.2                    pypi_0    pypi
[conda] torch-scatter             2.0.7                    pypi_0    pypi
[conda] torch-sparse              0.6.10                   pypi_0    pypi
[conda] torchaudio                0.9.0                    pypi_0    pypi
[conda] torchvision               0.10.0+cu111             pypi_0    pypi

Additional context

Stack trace:

Traceback (most recent call last):
  File "test.py", line 14, in <module>
    data = transforms(img)
  File "/home/snatarajan/anaconda3/envs/dl3d/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 60, in __call__
    img = t(img)
  File "/home/snatarajan/anaconda3/envs/dl3d/lib/python3.8/site-packages/torchvision/transforms/transforms.py", line 97, in __call__
    return F.to_tensor(pic)
  File "/home/snatarajan/anaconda3/envs/dl3d/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

cc @fmassa @vfdev-5 @pmeier

@sainatarajan sainatarajan changed the title TypeError: __array__() takes 1 positional argument but 2 were given [Bug] TypeError: __array__() takes 1 positional argument but 2 were given Jul 1, 2021
@artemisart
Copy link
Contributor

We have the same issue on numpy 1.20 and 1.21, had to downgrade to torch 1.8.1 torchvision 0.9.1

@xwang233
Copy link
Collaborator

xwang233 commented Jul 1, 2021

Interesting, I also started seeing this error since last night.

@xwang233
Copy link
Collaborator

xwang233 commented Jul 2, 2021

I did some debug. I think the problem comes from pypi Pillow 8.2.0 -> 8.3.0 update from June 30, 2021. Downgrading with

pip install pillow==8.2.0

may resolve the issue. We are using numpy 1.20.2 and latest pytorch build from master.

cc @ptrblck

@ptrblck
Copy link
Collaborator

ptrblck commented Jul 2, 2021

@t-vi has a pending fix for this issue: python-pillow/Pillow#5572

@plaban1981
Copy link

Requirement already satisfied: pillow==8.2.0 in /usr/local/lib/python3.7/dist-packages (8.2.0), Have already the same version , but still encounter the same error

@t-vi
Copy link
Collaborator

t-vi commented Jul 5, 2021

@plaban1981 Pillow 8.2 doesn't define __array__, so it is very improbable that you have the error with that version. I would advise print(PIL.Image) and compare the path there with your installation.
One thing to look out for is having multiple versions of pillow on the search path, a typical thing to shoot yourself in the foot with is 1) pip install pillow 2) sudo pip install pillow==8.2.0. This will give you pillow 8.2 for root and all users except the one where you did 1) and the most recent version for that user.

@plaban1981
Copy link

Hi @t-vi

sudo pip install pillow==8.2.0 worked
Thanks,

dasmy added a commit to emdgroup/tnmf that referenced this issue Jul 6, 2021
The latest version 8.3.0 seems to contain a bug, see https://stackoverflow.com/a/68240030 and pytorch/pytorch#61125 (comment)

@AdrianSosic , @dasmy : We should definitely cherry-pick this change to main. Otherwise the CI Workflows (and manual package use inside the repository) will not work properly
dasmy added a commit to emdgroup/tnmf that referenced this issue Jul 12, 2021
The latest version 8.3.0 seems to contain a bug, see https://stackoverflow.com/a/68240030 and pytorch/pytorch#61125 (comment)

@AdrianSosic , @dasmy : We should definitely cherry-pick this change to main. Otherwise the CI Workflows (and manual package use inside the repository) will not work properly
@heitorschueroff heitorschueroff added module: third_party triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module labels Jul 12, 2021
czlee added a commit to czlee/jadeite that referenced this issue Jul 29, 2021
There's currently a bug that interferes with the .to_tensor() function
in torch.

see: pytorch/pytorch#61125 (comment)
and: python-pillow/Pillow#5572
@deutschmn
Copy link

Seems to be fixed with pillow release 8.3.1 that merged PR #5572 馃帀

@artemisart
Copy link
Contributor

Yes this can be closed

@vfdev-5
Copy link
Collaborator

vfdev-5 commented Sep 29, 2021

Let's close it. Feel free to reopen if needed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: third_party module: vision triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module
Projects
None yet
Development

No branches or pull requests

9 participants