I am having issue with viewing data in dataloader on VOCDetection dataset
import os
import torch
from torch import nn
from torch.utils.data import DataLoader
from torchvision import datasets, transforms
training_data = datasets.VOCDetection(
root="data",
year = '2012',
image_set='train',
download = False,
transform = transforms.Compose([
transforms.Resize((448, 448)),
transforms.ToTensor()
])
)
val_data = datasets.VOCDetection(
root="data",
year = '2012',
image_set='trainval',
download = False,
transform = transforms.Compose([
transforms.Resize((448, 448)),
transforms.ToTensor()
])
)
batch_size = 64
train_dataloader = DataLoader(training_data, batch_size = batch_size, shuffle=True)
val_dataloader = DataLoader(val_data, batch_size = batch_size, shuffle=True)
for i, (x, y) in enumerate(val_dataloader):
print(f'shape of input data: {x.shape}')
print(f'shape of output data: {y.shape}')
break
here is the output of the error:
RuntimeError Traceback (most recent call last)
Cell In[30], line 1
----> 1 for i, (x, y) in enumerate(val_dataloader):
2 print(f'shape of input data: {x.shape}')
3 print(f'shape of output data: {y.shape}')
File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\utils\data\dataloader.py:631, in _BaseDataLoaderIter.__next__(self)
628 if self._sampler_iter is None:
629 # TODO(https://github.com/pytorch/pytorch/issues/76750)
630 self._reset() # type: ignore[call-arg]
--> 631 data = self._next_data()
632 self._num_yielded += 1
633 if self._dataset_kind == _DatasetKind.Iterable and \
634 self._IterableDataset_len_called is not None and \
635 self._num_yielded > self._IterableDataset_len_called:
File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\utils\data\dataloader.py:675, in _SingleProcessDataLoaderIter._next_data(self)
673 def _next_data(self):
674 index = self._next_index() # may raise StopIteration
--> 675 data = self._dataset_fetcher.fetch(index) # may raise StopIteration
676 if self._pin_memory:
677 data = _utils.pin_memory.pin_memory(data, self._pin_memory_device)
File ~\AppData\Local\Programs\Python\Python312\Lib\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 ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\utils\data\_utils\collate.py:277, in default_collate(batch)
216 def default_collate(batch):
217 r"""
218 Take in a batch of data and put the elements within the batch into a tensor with an additional outer dimension - batch size.
219
(...)
275 >>> default_collate(batch) # Handle `CustomType` automatically
276 """
--> 277 return collate(batch, collate_fn_map=default_collate_fn_map)
File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\utils\data\_utils\collate.py:144, in collate(batch, collate_fn_map)
141 transposed = list(zip(*batch)) # It may be accessed twice, so we use a list.
143 if isinstance(elem, tuple):
--> 144 return [collate(samples, collate_fn_map=collate_fn_map) for samples in transposed] # Backwards compatibility.
145 else:
146 try:
File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\utils\data\_utils\collate.py:129, in collate(batch, collate_fn_map)
127 if isinstance(elem, collections.abc.Mapping):
128 try:
--> 129 return elem_type({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
130 except TypeError:
131 # The mapping type may not support `__init__(iterable)`.
132 return {key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem}
File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\utils\data\_utils\collate.py:129, in collate(batch, collate_fn_map)
127 if isinstance(elem, collections.abc.Mapping):
128 try:
--> 129 return elem_type({key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem})
130 except TypeError:
131 # The mapping type may not support `__init__(iterable)`.
132 return {key: collate([d[key] for d in batch], collate_fn_map=collate_fn_map) for key in elem}
File ~\AppData\Local\Programs\Python\Python312\Lib\site-packages\torch\utils\data\_utils\collate.py:140, in collate(batch, collate_fn_map)
138 elem_size = len(next(it))
139 if not all(len(elem) == elem_size for elem in it):
--> 140 raise RuntimeError('each element in list of batch should be of equal size')
141 transposed = list(zip(*batch)) # It may be accessed twice, so we use a list.
143 if isinstance(elem, tuple):
RuntimeError: each element in list of batch should be of equal size