Transforming FER2013 testset to tensor gives nontype

Asser_Moustafa: <p>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.</p>
<p>here is the code for declaring the train and test sets:</p>
<pre><code class="lang-auto">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")
</code></pre>
<p>and this is how I am trying to get the accuracy:</p>
<pre><code class="lang-auto">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} %')
</code></pre>
<p>the error I get :</p>
<pre><code class="lang-auto">TypeError                                 Traceback (most recent call last)
Cell In[43], line 6
      4 with torch.no_grad():
      5     print("hi")
----&gt; 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]
--&gt; 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 &gt; 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
--&gt; 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]
---&gt; 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             &gt;&gt;&gt; default_collate(batch)  # Handle `CustomType` automatically
    263     """
--&gt; 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):
--&gt; 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 &lt;listcomp&gt;(.0)
    139 transposed = list(zip(*batch))  # It may be accessed twice, so we use a list.
    141 if isinstance(elem, tuple):
--&gt; 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]
--&gt; 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 &lt;class 'PIL.Image.Image'&gt;

</code></pre>

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

Asser_Moustafa: <aside class="quote no-group" data-username="Asser_Moustafa" data-post="1" data-topic="177172">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/asser_moustafa/48/59325_2.png" class="avatar"> Asser_Moustafa:</div>
<blockquote>
<p><code>transform=transform</code></p>
</blockquote>
</aside>
<p>Hey, Thank you for your answer</p>
<p>also,  sorry, i copied one thing wrong</p>
<p>the error in the original post is given when there is NO TRANSFORM on the train set</p>
<pre><code class="lang-auto">testset = torchvision.datasets.FER2013(root='./biai-proj/data', split="test")
</code></pre>
<p>when there is a transform to tensor, the error is as follows:</p>
<pre><code class="lang-auto">TypeError: default_collate: batch must contain tensors, numpy arrays, numbers, dicts or lists; found &lt;class 'NoneType'&gt;
</code></pre>
<p>I iterated through the testset,<br>
in both cases (with and without transforming to tensor) the type is</p>
<pre><code class="lang-auto">&lt;class 'tuple'&gt;
</code></pre>

ptrblck: <p>My suggestion was to iterate the <code>Dataset</code> not the <code>DataLoader</code> as the latter raises the error to check what the raw sample outputs contain before they would be fed to the <code>collate_fn</code>.</p>

Asser_Moustafa: <p>I iterated on the trainset &amp; testset, and in both cases (with and without transforming) got</p>
<pre><code class="lang-auto">&lt;class 'tuple'&gt;
</code></pre>

japina: <p>Did you manage to solve that? I have the same problem.</p>