RuntimeError: stack expects each tensor to be equal size, but got [3, 1500, 2000] at entry 0 and [3, 1728, 2304] at entry 1

username: Khawar_Islam
cooked: <p>I am using custom dataset and doing training on UNet but getting an error.<br>
Repo <a href="https://github.com/milesial/Pytorch-UNet" rel="nofollow noopener">https://github.com/milesial/Pytorch-UNet</a><br>
Error</p>
<pre><code class="lang-auto">Traceback (most recent call last):
  File "/home/khawar/Pytorch-UNet/train.py", line 186, in &lt;module&gt;
    val_percent=args.val / 100)
  File "/home/khawar/Pytorch-UNet/train.py", line 70, in train_net
    for batch in train_loader:
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 363, in __next__
    data = self._next_data()
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 989, in _next_data
    return self._process_data(data)
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 1014, in _process_data
    data.reraise()
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/_utils.py", line 395, in reraise
    raise self.exc_type(msg)
RuntimeError: Caught RuntimeError in DataLoader worker process 0.
Original Traceback (most recent call last):
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/_utils/worker.py", line 185, in _worker_loop
    data = fetcher.fetch(index)
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/_utils/fetch.py", line 47, in fetch
    return self.collate_fn(data)
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py", line 74, in default_collate
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py", line 74, in &lt;dictcomp&gt;
    return {key: default_collate([d[key] for d in batch]) for key in elem}
  File "/home/khawar/.local/lib/python3.6/site-packages/torch/utils/data/_utils/collate.py", line 55, in default_collate
    return torch.stack(batch, 0, out=out)
RuntimeError: stack expects each tensor to be equal size, but got [3, 900, 1600] at entry 0 and [3, 1500, 2000] at entry 1
</code></pre>

username: RaLo4
cooked: <p>The error is telling you that you are trying to put images of different size (size 1: [3, 900, 1600], size 2: [3, 1500, 2000]) into one batch. This is not possible tho. The images need to be of the same size.</p>

username: Khawar_Islam
cooked: <p>But my images are same in size. How to give same size image through programmatically?</p>

username: RaLo4
cooked: <p>I don’t know if you noticed, but you have asked about the exact same error message in a post 2 days ago.</p>
<pre><code class="lang-auto">RuntimeError: stack expects each tensor to be equal size, but got [3, 288, 352] at entry 0 and [3, 256, 256] at entry 1
</code></pre>
<p>And you have already gotten <a href="https://discuss.pytorch.org/t/runtimeerror-the-size-of-tensor-a-256-must-match-the-size-of-tensor-b-32-at-non-singleton-dimension-3/94546/2">an answer to it</a>.</p>
<p>This answer still applies here. The only difference is that you are now using a custom dataset.</p>
<p>If you want a specific answer for your custom dataset:<br>
In <code>utils/dataset.py</code> you resize images based on a constant scaling factor instead of a fixed size:</p>
<pre><code class="lang-auto">    def preprocess(cls, pil_img, scale):
        w, h = pil_img.size
        newW, newH = int(scale * w), int(scale * h)
        assert newW &gt; 0 and newH &gt; 0, 'Scale is too small'
        pil_img = pil_img.resize((newW, newH))
</code></pre>
<p>I am guessing that the images you are using are not all the same size. If you then preprocess them by simply scaling each of them with a constant factor the will stay differently sized.<br>
You either need images of the same size for this scaling to work or you resize every image to a given size. Which is also what was suggested to you in the above mentioned previous post you did about this topic.</p>