To solve TypeError: __array__() takes 1 positional argument but 2 were given error follow below methods.
ERROR LOG
TypeError: Caught TypeError in DataLoader worker process 0.
Original Traceback (most recent call last):
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/worker.py", line 287, in _worker_loop
data = fetcher.fetch(index)
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in fetch
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/_utils/fetch.py", line 44, in <listcomp>
data = [self.dataset[idx] for idx in possibly_batched_index]
File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataset.py", line 311, in __getitem__
return self.dataset[self.indices[idx]]
File "main.py", line 64, in __getitem__
img, target = self.transforms(img, target)
File "/transforms.py", line 26, in __call__
image, target = t(image, target)
File "/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py", line 1051, in _call_impl
return forward_call(*input, **kwargs)
File "/transforms.py", line 50, in forward
image = F.to_tensor(image)
File "/usr/local/lib/python3.6/dist-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
Contents
Why TypeError: __array__() takes 1 positional argument but 2 were given occurs ?
The issue is with the Pillow 8.3.0 version. This problem could also be resolved by upgrading Pillow from version 8.3.0 to 8.3.1. I experienced the same problem with
torch==1.9.0+cu111
torchvision==0.10.0+cu111
Pillow==8.3.0
After Pillow was upgraded to 8.3.1 (with no change to torch and torchvision) as below, the issue is gone:
pip install --upgrade pillow
How to solve TypeError: __array__() takes 1 positional argument but 2 were given ?
I think the problem comes from pypi Pillow 8.2.0 -> 8.3.0 update . Try Downgrading the Pillow version with the help of below code :
pip install pillow==8.2.0
Alternative to Solve TypeError: __array__() takes 1 positional argument but 2 were given
You may also get the same issue when you are using torch==1.9.0
and torchvision==0.10.0
. Then I downgraded the torch library in my requirements.txt file, which forced me to downgrade torchvision, which fixed the error for me. I ended up using the following library versions, which did not cause the error:
torch==1.8.1
torchvision==0.9.1
Hope the above solution works.
Also read :
Command โpython.execInTerminal-iconโ not found in Visual Studio Code
ValueError: check_hostname requires server_hostname