类型错误:__array__() 需要 1 个位置参数,但给出了 2 个

Ale*_*lex 1 python machine-learning computer-vision pytorch torchvision

我一直在做 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
Run Code Online (Sandbox Code Playgroud)

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

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

小智 7

PyTorch 已经考虑过这个问题。这似乎不是 PyTorch 的问题。

正如问题中提到的xwang233,我们可以通过降级枕头来解决它:

pip install pillow==8.2.0
Run Code Online (Sandbox Code Playgroud)