Transforming FER2013 testset to tensor gives nontype

hi, I am training a CNN on FER2013 dataset. The training went smoothly, however when I try to measure the accuracy on the testset i get an error.

here is the code for declaring the train and test sets:

transform = transforms.ToTensor()

batch_size = 4 #how many units in each insert in the pipeline

#  declaring the training set as the train.csv file from the stated directory
trainset = torchvision.datasets.FER2013(root='./biai-proj/data', split="train",
                                         transform=transform) 

# setting up the loader (aka iterator) for the train set 
trainloader = torch.utils.data.DataLoader(trainset, batch_size=batch_size,
                                          shuffle=True, num_workers=0)

# repeating for the test set
testset = torchvision.datasets.FER2013(root='./biai-proj/data', split="test",
                                        transform=transform)
testloader = torch.utils.data.DataLoader(testset, batch_size=batch_size,
                                         shuffle=False, num_workers=0)

classes = ("Angry", "Disgust", "Fear", "Happy", "Sad", "Surprise", "Neutral")

and this is how I am trying to get the accuracy:

correct = 0
total = 0
# since we're not training, we don't need to calculate the gradients for our outputs
with torch.no_grad():
    for data in testloader:
        images, labels = data
        # calculate outputs by running images through the network
        outputs = net(images)
        # the class with the highest energy is what we choose as prediction
        _, predicted = torch.max(outputs.data, 1)
        total += labels.size(0)
        correct += (predicted == labels).sum().item()

print(f'Accuracy of the network on the 10000 test images: {100 * correct // total} %')

the error I get :

TypeError                                 Traceback (most recent call last)
Cell In[43], line 6
      4 with torch.no_grad():
      5     print("hi")
----> 6     for data in testloader:
      7         images, labels = data
      8         print(type(images))

File ~/dev/biai/env/lib/python3.8/site-packages/torch/utils/data/dataloader.py:634, in _BaseDataLoaderIter.__next__(self)
    631 if self._sampler_iter is None:
    632     # TODO(https://github.com/pytorch/pytorch/issues/76750)
    633     self._reset()  # type: ignore[call-arg]
--> 634 data = self._next_data()
    635 self._num_yielded += 1
    636 if self._dataset_kind == _DatasetKind.Iterable and \
    637         self._IterableDataset_len_called is not None and \
    638         self._num_yielded > self._IterableDataset_len_called:

File ~/dev/biai/env/lib/python3.8/site-packages/torch/utils/data/dataloader.py:678, in _SingleProcessDataLoaderIter._next_data(self)
    676 def _next_data(self):
    677     index = self._next_index()  # may raise StopIteration
--> 678     data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    679     if self._pin_memory:
    680         data = _utils.pin_memory.pin_memory(data, self._pin_memory_device)

File ~/dev/biai/env/lib/python3.8/site-packages/torch/utils/data/_utils/fetch.py:54, in _MapDatasetFetcher.fetch(self, possibly_batched_index)
     52 else:
     53     data = self.dataset[possibly_batched_index]
---> 54 return self.collate_fn(data)

File ~/dev/biai/env/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py:264, in default_collate(batch)
    203 def default_collate(batch):
    204     r"""
    205         Function that takes in a batch of data and puts the elements within the batch
    206         into a tensor with an additional outer dimension - batch size. The exact output type can be
   (...)
    262             >>> default_collate(batch)  # Handle `CustomType` automatically
    263     """
--> 264     return collate(batch, collate_fn_map=default_collate_fn_map)

File ~/dev/biai/env/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py:142, in collate(batch, collate_fn_map)
    139 transposed = list(zip(*batch))  # It may be accessed twice, so we use a list.
    141 if isinstance(elem, tuple):
--> 142     return [collate(samples, collate_fn_map=collate_fn_map) for samples in transposed]  # Backwards compatibility.
    143 else:
    144     try:

File ~/dev/biai/env/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py:142, in <listcomp>(.0)
    139 transposed = list(zip(*batch))  # It may be accessed twice, so we use a list.
    141 if isinstance(elem, tuple):
--> 142     return [collate(samples, collate_fn_map=collate_fn_map) for samples in transposed]  # Backwards compatibility.
    143 else:
    144     try:

File ~/dev/biai/env/lib/python3.8/site-packages/torch/utils/data/_utils/collate.py:150, in collate(batch, collate_fn_map)
    146         except TypeError:
    147             # The sequence type may not support `__init__(iterable)` (e.g., `range`).
    148             return [collate(samples, collate_fn_map=collate_fn_map) for samples in transposed]
--> 150 raise TypeError(default_collate_err_msg_format.format(elem_type))

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

Could you iterate the trainset and check the types of all outputs?
Based on the error it seems at least one output is a PIL.Image which is invalid. I see you have specified transform=transforms.ToTensor() so unsure what’s causing the issue.

Hey, Thank you for your answer

also, sorry, i copied one thing wrong

the error in the original post is given when there is NO TRANSFORM on the train set

testset = torchvision.datasets.FER2013(root='./biai-proj/data', split="test")

when there is a transform to tensor, the error is as follows:

TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found <class 'NoneType'>

I iterated through the testset,
in both cases (with and without transforming to tensor) the type is

<class 'tuple'>

My suggestion was to iterate the Dataset not the DataLoader as the latter raises the error to check what the raw sample outputs contain before they would be fed to the collate_fn.

I iterated on the trainset & testset, and in both cases (with and without transforming) got

<class 'tuple'>

Did you manage to solve that? I have the same problem.