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

JakeAndFinn: <p>I am playing around with making several different data loaders using different transformations and methods and got this error</p>
<pre><code class="lang-auto">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
</code></pre>
<p>When I am trying to use the data loaders.<br>
So the basic code for one of them looks like this (which is giving the error but all of them do too)</p>
<pre><code class="lang-auto">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)

</code></pre>
<p>Here is the whole code I am working on.<br>
<a href="https://www.kaggle.com/matthewmillar/pytorchdataloaderexamples/edit" class="onebox" target="_blank" rel="nofollow noopener">https://www.kaggle.com/matthewmillar/pytorchdataloaderexamples/edit</a></p>
<p>and here is the whole stack trace</p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-30-91a907a2cbb6&gt; in &lt;module&gt;
      1 # Simple visuzlization for dataloaders to check what they are producing.
      2 basic_dataiter = iter(aug_train_loader)
----&gt; 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):
--&gt; 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
--&gt; 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]
---&gt; 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)
---&gt; 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 &lt;listcomp&gt;(.0)
     77     elif isinstance(elem, container_abcs.Sequence):
     78         transposed = zip(*batch)
---&gt; 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)
---&gt; 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
</code></pre>
<p>Thanks for any help with this.</p>

ptrblck: <p>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.<br>
You could add <code>transforms.Resize(size)</code> to the transformation to create tensors with the same spatial size.</p>

JakeAndFinn: <p>Yes that fixed it.<br>
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,<br>
Thank you I appreciate it.</p>