Problem while running CNN

username: rsotogar
cooked: <p>Hi! I am new to PyTorch and I get this error when trying to create my first CNN:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/2/2236f9bb765115013c803cc9f211924a5bf61126.png" data-download-href="https://discuss.pytorch.org/uploads/default/2236f9bb765115013c803cc9f211924a5bf61126" title="Screenshot 2020-08-25 at 16.14.57"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/2236f9bb765115013c803cc9f211924a5bf61126_2_690x36.png" alt="Screenshot 2020-08-25 at 16.14.57" data-base62-sha1="4SG196fz5tNkeEHH0YptT3lABTM" width="690" height="36" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/2/2236f9bb765115013c803cc9f211924a5bf61126_2_690x36.png, https://discuss.pytorch.org/uploads/default/original/3X/2/2/2236f9bb765115013c803cc9f211924a5bf61126.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/2/2236f9bb765115013c803cc9f211924a5bf61126.png 2x" data-dominant-color="ECEBEB"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2020-08-25 at 16.14.57</span><span class="informations">1022×54 11.6 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>I presume my tensors are of different shapes so hence the error right? How can I make tensors of equal size while preserving the 3 channels?</p>
<p>This is the simple model I was trying to run:</p>
<p>simple_model = nn.Sequential(nn.Conv2d(in_channels = 3,out_channels = 8,<br>
kernel_size = 3, stride = 1, padding = 1),<br>
nn.MaxPool2d(2,2))</p>
<p>for image, label in train_dl:<br>
out = simple_model(image)<br>
print(out.shape)<br>
break</p>
<p>Thanks!</p>

username: BaruchG
cooked: <p>What is the size of the images in your batch?  You may need to resize your images using a transform in your dataset before they can be fed into the network.  I would guess that the error is telling you that the height dimensions of some of your images in the batch are not equal.</p>

username: rsotogar
cooked: <p>They vary, actually very few are squared:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b2cca19aeede2943a7fbaa8ebb35c062b1b5af5.png" data-download-href="https://discuss.pytorch.org/uploads/default/3b2cca19aeede2943a7fbaa8ebb35c062b1b5af5" title="Screenshot 2020-08-25 at 16.38.52"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b2cca19aeede2943a7fbaa8ebb35c062b1b5af5_2_690x268.png" alt="Screenshot 2020-08-25 at 16.38.52" data-base62-sha1="8ru8akyy5hc0rOR7RhnGvK6Fcrz" width="690" height="268" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b2cca19aeede2943a7fbaa8ebb35c062b1b5af5_2_690x268.png, https://discuss.pytorch.org/uploads/default/optimized/3X/3/b/3b2cca19aeede2943a7fbaa8ebb35c062b1b5af5_2_1035x402.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/3/b/3b2cca19aeede2943a7fbaa8ebb35c062b1b5af5.png 2x" data-dominant-color="F7F7F7"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2020-08-25 at 16.38.52</span><span class="informations">1133×441 48.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Question is which function I can use to do this?</p>
<p>Thanks!</p>

username: BaruchG
cooked: <p>They don’t need to be squared per se, but all the images in your batch need to be the same to allow for batch processing.<br>
For example this is a standard transform that would resize the images to be 224x224 and make it suitable for a imagenet trained classifier:</p>
<pre><code class="lang-auto">transforms.Compose([
        transforms.RandomResizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ])
</code></pre>
<p>this would then be passed into the <code>transforms</code> argument when you create your dataset and would resize and make those other transformations for you as well.  You can see a worked out example of how this works at <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" rel="nofollow noopener">https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html</a>.</p>

username: rsotogar
cooked: <p>Great now the tensors are reshaped, but still get an error when trying to parse the training set to the model:</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/d/e/de4cb2b75b4e4c53bf4d1ca68025ed817816a4ce.png" data-download-href="https://discuss.pytorch.org/uploads/default/de4cb2b75b4e4c53bf4d1ca68025ed817816a4ce" title="Screenshot 2020-08-25 at 17.19.38"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/d/e/de4cb2b75b4e4c53bf4d1ca68025ed817816a4ce_2_690x41.png" alt="Screenshot 2020-08-25 at 17.19.38" data-base62-sha1="vIyk35g5n9KWjXp26SgMPYbZ8nk" width="690" height="41" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/d/e/de4cb2b75b4e4c53bf4d1ca68025ed817816a4ce_2_690x41.png, https://discuss.pytorch.org/uploads/default/optimized/3X/d/e/de4cb2b75b4e4c53bf4d1ca68025ed817816a4ce_2_1035x61.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/d/e/de4cb2b75b4e4c53bf4d1ca68025ed817816a4ce.png 2x" data-dominant-color="F4F4F4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">Screenshot 2020-08-25 at 17.19.38</span><span class="informations">1053×63 9.36 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Now I don’t know what this means…</p>