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

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

我一直在学习 pytorch 教程 (https://pytorch.org/tutorials/intermediate/torchvision_tutorial.html),但遇到了这个错误,我不知道如何修复。完整错误如下:

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

我相信这意味着我在某处使用带有 2 个参数的数组,这是不允许的,但我真的不知道发生了什么事 - 也许是在他们预先编写的库之一中?

如果需要,我可以分享完整的代码,但我认为它有点笨拙。有谁知道可能导致此错误的原因?

我在使用时遇到了同样的错误:

torch==1.9.0
torchvision==0.10.0

在我的 requirements.txt 文件中,我降级了 torch 库,这迫使我降级 torchvision,这为我修复了错误。我最终使用的没有引发错误的库版本是:

torch==1.8.1
torchvision==0.9.1

PyTorch 已经考虑到了这一点 issue。看来不是PyTorch的问题。

如问题中xwang233所述,我们可以通过降级pillow来修复它:

pip install pillow==8.2.0

更改您的代码:

np.array(pic ,np.float32)

至:

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

将 Pillow 从版本 8.3.0 升级到 8.3.1 也可以解决此问题。我和

有同样的问题
torch==1.9.0+cu111
torchvision==0.10.0+cu111
Pillow==8.3.0

如下所示,将 Pillow 升级到 8.3.1(没有更改 torch 和 torchvision)后,问题就消失了:

pip install --upgrade pillow

感谢 DRTorresRuiz 提供有关 Pillow 的线索。