pytorch cifar10 dataset - cannot get first item

Viewed 440

I have selected the CIFAR 10 dataset using the torchvision library:

trainset = torchvision.datasets.CIFAR10(root='./data', train=True,
                                        download=True, transform=transforms.ToTensor())

Then I try to select the first item in the dataset, which as I understand implements the get_item method of the dataset class:

trainset[0]

and I get

File "env\lib\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

Any ideas why I would get this error?

Python 3.7.9, torch==1.9.0, torchvision==0.10.0

1 Answers

I was hitting this error too:

def get_transformations():
    return transforms.Compose([transforms.ToTensor()])

...

self.transforms = get_transformations()

...

# Load the image + augment
img = Image.open(img_path).convert("RGB")
img = self.transforms(img)

...

Original Traceback (most recent call last):
  File "c:\2021-mcm-master\src\PyTorch-RCNN\ui-prediction\env\lib\site-packages\torch\utils\data\_utils\worker.py", line 287, in _worker_loop
    data = fetcher.fetch(index)
  File "c:\2021-mcm-master\src\PyTorch-RCNN\ui-prediction\env\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in fetch
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "c:\2021-mcm-master\src\PyTorch-RCNN\ui-prediction\env\lib\site-packages\torch\utils\data\_utils\fetch.py", line 44, in <listcomp>
    data = [self.dataset[idx] for idx in possibly_batched_index]
  File "c:\2021-mcm-master\src\PyTorch-RCNN\ui-prediction\src\screenshot_dataset.py", line 112, in __getitem__
    img = self.transforms(img)
  File "c:\2021-mcm-master\src\PyTorch-RCNN\ui-prediction\env\lib\site-packages\torchvision\transforms\transforms.py", line 60, in __call__
    img = t(img)
  File "c:\2021-mcm-master\src\PyTorch-RCNN\ui-prediction\env\lib\site-packages\torchvision\transforms\transforms.py", line 97, in __call__
    return F.to_tensor(pic)
  File "c:\2021-mcm-master\src\PyTorch-RCNN\ui-prediction\env\lib\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

As @Phil suggested, downgrading Pillow from 8.3.0 to 8.2.0 solved the issue:

pip install pillow==8.2.0
Related