Alex
Alex

Reputation: 172

TypeError: __array__() takes 1 positional argument but 2 were given

I've been doing the pytorch tutorial (https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html) and have been getting this error that I don't know how to fix. The full error is below:

Traceback (most recent call last):
  File "main.py", line 146, in <module>
    main()
  File "main.py", line 138, in main
    train_one_epoch(model, optimizer, data_loader, device, epoch, print_freq=10)
  File "/engine.py", line 26, in train_one_epoch
    for images, targets in metric_logger.log_every(data_loader, print_freq, header):
  File "/utils.py", line 180, in log_every
    for obj in iterable:
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 521, in __next__
    data = self._next_data()
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 1203, in _next_data
    return self._process_data(data)
  File "/usr/local/lib/python3.6/dist-packages/torch/utils/data/dataloader.py", line 1229, in _process_data
    data.reraise()
  File "/usr/local/lib/python3.6/dist-packages/torch/_utils.py", line 425, in reraise
    raise self.exc_type(msg)
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

I believe it means somewhere I'm using an array with 2 arguments which isn't allowed, but I don't really know where abouts that is happening - perhaps in one of their pre written libraries?

I can share the code in full if desired, but thought its a bit unwieldy. Does anyone know what might be causing this error?

Upvotes: 2

Views: 6415

Answers (4)

coolgpu
coolgpu

Reputation: 21

This issue could be fixed as well by upgrading Pillow from version 8.3.0 to 8.3.1. I had the same issue 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

Thanks to DRTorresRuiz for providing the clue about Pillow.

Upvotes: 2

bozhao
bozhao

Reputation: 1

change your code:

np.array(pic ,np.float32)

to:

np.array(pic).astype('float32')

Upvotes: 0

Daniel R. Torres Ruiz
Daniel R. Torres Ruiz

Reputation: 130

PyTorch has already considered this issue. It does not seem to be a PyTorch problem.

As xwang233 mentioned in the issue, we can fix it by downgrading pillow:

pip install pillow==8.2.0

Upvotes: 7

bpmason1
bpmason1

Reputation: 1940

I had the same error when using:

torch==1.9.0
torchvision==0.10.0

In my requirements.txt file I downgraded the torch library, which forced me to downgrade torchvision, and that fixed the error for me. The library versions I ended up using that did not raise the error were:

torch==1.8.1
torchvision==0.9.1

Upvotes: 1

Related Questions