Discussions>PyTorch default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists>

PyTorch default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists

Why I'm getting this error when I try to load my dataset? I just use the native example of PyTorch to load Cifar10 and Cifar100 datasets, but it returns this.

default_collate: batch must contain tensors, numpy arrays, 
numbers, dicts or lists; found <class 'PIL.Image.Image'>

Any idea how to fix it?

2 votesLP235.00
1 Answers
JO268.00
2

It seems like you have not converted it into a PyTorch Tensor. The dataset just loads images with the PIL library and does not convert them into necessary types like list or Tensor. Just add a transformation into Datasets.

from torchvision import transforms

transform = transforms.Compose([
    transforms.ToTensor()
])

cifar100 = torchvision.datasets.CIFAR100('path/cifar100/', transform=transform)
Reply
Couldn't find what you were looking for?and we will find an expert to answer.
How helpful was this page?