Building A set of data loaders but error is thrown when reading in data set

I am playing around with making several different data loaders using different transformations and methods and got this error

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 280 and 499 in dimension 2 at /opt/conda/conda-bld/pytorch_1579022034529/work/aten/src/TH/generic/THTensor.cpp:612

When I am trying to use the data loaders.
So the basic code for one of them looks like this (which is giving the error but all of them do too)

basic = transforms.Compose([transforms.ToTensor()])
basic_train_dataset = datasets.ImageFolder(TRAIN_DIR, transform=basic)
data, lable = next(iter(basic_train_dataset))
print(lable)  #This works fine

BATCH_SIZE = 20
NUM_WORKERS = 0
VALIDATION_SIZE = 0.15

basic_train_loader = DataLoader(basic_train_dataset, batch_size=BATCH_SIZE, shuffle=False, num_workers=NUM_WORKERS)

basic_dataiter = iter(basic_train_loader)
images, labels = basic_dataiter.next() # The error is thrown here
print(labels)

Here is the whole code I am working on.
https://www.kaggle.com/matthewmillar/pytorchdataloaderexamples/edit

and here is the whole stack trace

RuntimeError                              Traceback (most recent call last)
<ipython-input-30-91a907a2cbb6> in <module>
      1 # Simple visuzlization for dataloaders to check what they are producing.
      2 basic_dataiter = iter(aug_train_loader)
----> 3 images, labels = basic_dataiter.next()
      4 print(labels)

/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
    343 
    344     def __next__(self):
--> 345         data = self._next_data()
    346         self._num_yielded += 1
    347         if self._dataset_kind == _DatasetKind.Iterable and \

/opt/conda/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _next_data(self)
    383     def _next_data(self):
    384         index = self._next_index()  # may raise StopIteration
--> 385         data = self._dataset_fetcher.fetch(index)  # may raise StopIteration
    386         if self._pin_memory:
    387             data = _utils.pin_memory.pin_memory(data)

/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py in fetch(self, possibly_batched_index)
     45         else:
     46             data = self.dataset[possibly_batched_index]
---> 47         return self.collate_fn(data)

/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
     77     elif isinstance(elem, container_abcs.Sequence):
     78         transposed = zip(*batch)
---> 79         return [default_collate(samples) for samples in transposed]
     80 
     81     raise TypeError(default_collate_err_msg_format.format(elem_type))

/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py in <listcomp>(.0)
     77     elif isinstance(elem, container_abcs.Sequence):
     78         transposed = zip(*batch)
---> 79         return [default_collate(samples) for samples in transposed]
     80 
     81     raise TypeError(default_collate_err_msg_format.format(elem_type))

/opt/conda/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py in default_collate(batch)
     53             storage = elem.storage()._new_shared(numel)
     54             out = elem.new(storage)
---> 55         return torch.stack(batch, 0, out=out)
     56     elif elem_type.__module__ == 'numpy' and elem_type.__name__ != 'str_' \
     57             and elem_type.__name__ != 'string_':

RuntimeError: invalid argument 0: Sizes of tensors must match except in dimension 0. Got 280 and 499 in dimension 2 at /opt/conda/conda-bld/pytorch_1579022034529/work/aten/src/TH/generic/THTensor.cpp:612

Thanks for any help with this.

Based on the error message it seems the spatial size is different for the loaded tensors, which will raise this error while creating a batch.
You could add transforms.Resize(size) to the transformation to create tensors with the same spatial size.

1 Like

Yes that fixed it.
I thought that all the images were the same size but some are rotated so it was a 800x600 and 600x800 in the folder which messed things up. I though it would automatically adjust for it but apparently not,
Thank you I appreciate it.