Size Mismatch in conv2d

Alymostafa: <p>im facing a runtime error when trying to implement cnn  <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/4/c4065ea799f7fbefd745e8a2cc7c8ec2e1035f29.png" data-download-href="https://discuss.pytorch.org/uploads/default/c4065ea799f7fbefd745e8a2cc7c8ec2e1035f29" title="Screenshot from 2020-04-07 01-58-07"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4065ea799f7fbefd745e8a2cc7c8ec2e1035f29_2_690x388.png" alt="Screenshot from 2020-04-07 01-58-07" data-base62-sha1="rY7elcRWgO8ktaXuDzLpflpgKnL" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4065ea799f7fbefd745e8a2cc7c8ec2e1035f29_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4065ea799f7fbefd745e8a2cc7c8ec2e1035f29_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/4/c4065ea799f7fbefd745e8a2cc7c8ec2e1035f29_2_1380x776.png 2x" data-dominant-color="373737"><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 from 2020-04-07 01-58-07</span><span class="informations">1600×900 136 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><br>
this is the link of model :<br>
<a href="https://github.com/Alymostafa/ml/blob/master/cnn_using_pytorch.ipynb" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/Alymostafa/ml/blob/master/cnn_using_pytorch.ipynb</a></p>

ptrblck: <p>The error is raised by a linear layer.<br>
Add a print statement before feeding the activation to the first linear layer and check its shape.</p>

Alymostafa: <p>like that , sorry i am beginner in cnn:</p>
<p>def forward(self, x):<br>
x.float()<br>
out = self.layer1(x)<br>
out = self.layer2(out)<br>
out = out.reshape(out.size(0), -1)<br>
out = self.drop_out(out)<br>
print(out.shape)<br>
out = self.fc1(out)<br>
out = self.fc2(out)<br>
return out</p>

ptrblck: <p>Could you post the printed shape and compare it the the <code>in_features</code> in <code>self.fc1</code>?</p>

Alymostafa: <p>like that :<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/9/c9e4966a8928067cefff8ec1817906fcce8106e5.png" data-download-href="https://discuss.pytorch.org/uploads/default/c9e4966a8928067cefff8ec1817906fcce8106e5" title="Screenshot from 2020-04-07 04-50-35"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c9e4966a8928067cefff8ec1817906fcce8106e5_2_690x388.png" alt="Screenshot from 2020-04-07 04-50-35" data-base62-sha1="sO1IwAzoudbCfAtzI1WByVDumpv" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c9e4966a8928067cefff8ec1817906fcce8106e5_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c9e4966a8928067cefff8ec1817906fcce8106e5_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/c/9/c9e4966a8928067cefff8ec1817906fcce8106e5_2_1380x776.png 2x" data-dominant-color="292929"><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 from 2020-04-07 04-50-35</span><span class="informations">1600×900 122 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>

Alymostafa: <p>the print<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/5/05ff7e687c1cd31be54422fb090d8797d90efe50.png" data-download-href="https://discuss.pytorch.org/uploads/default/05ff7e687c1cd31be54422fb090d8797d90efe50" title="Screenshot from 2020-04-07 04-50-42"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/05ff7e687c1cd31be54422fb090d8797d90efe50_2_690x388.png" alt="Screenshot from 2020-04-07 04-50-42" data-base62-sha1="R3MniFpFUwkF4OopWtkwWzhvig" width="690" height="388" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/05ff7e687c1cd31be54422fb090d8797d90efe50_2_690x388.png, https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/05ff7e687c1cd31be54422fb090d8797d90efe50_2_1035x582.png 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/0/5/05ff7e687c1cd31be54422fb090d8797d90efe50_2_1380x776.png 2x" data-dominant-color="353535"><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 from 2020-04-07 04-50-42</span><span class="informations">1600×900 152 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>

ptrblck: <p>Based on the print statements, you should set <code>in_features=7168</code>.</p>
<p>However, I’m not sure if your model is really doing what you intend to do.<br>
<code>nn.Conv2d</code> expects an input tensor in the shape <code>[batch_size, channels, height, width]</code>, while you are apparently passing the tensor as <code>[batch_size, height, width, channels]</code> and set the number of input channels to <code>224</code>, which seems to be the height.</p>
<p>You could permute the input tensor via: <code>x = x.permute(0, 3, 1, 2)</code> and pass it in the expected shape.</p>
<p>PS: It’s also better to post by wrapping it in three backticks ```, as it makes debugging easier and enables the forum search. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

Alymostafa: <aside class="quote no-group" data-username="ptrblck" data-post="7" data-topic="75627">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/ptrblck/48/1823_2.png" class="avatar"> ptrblck:</div>
<blockquote>
<p>I’m not sure if your model is really doing what you intend to do.</p>
</blockquote>
</aside>
<p>i want to take i photo with 224*224 pixel and the output is binary [1 or 0 ]<br>
i face a problems in choosing the kernel size in cov2d and in maxpool2d<br>
any advice ?</p>

ptrblck: <p>Permute the input, so that the channel dimension is in dim1 and set the <code>in_channels</code> to the number of input channels, not the height.</p>

Alymostafa: <p>sorry but i dont understand , can you take  a look  in the github repo and tell me about values of conv2d and maxpool2d , sorry for that</p>

charan_Vjy: <p>The git repo is not functional. It is throwing an error. As <a class="mention" href="/u/ptrblck">@ptrblck</a> mentioned the model expects an input of shape <code>[batch_size, channels, height, width]</code> as compared to <code>[batch_size, height, width, channels]</code>, which is what you have given. To address this issue you would need to change the shapes of the input which can be done using torch’s <code>permute</code> method as shown below.</p>
<pre><code class="lang-auto">import torch
batch_size = 16
img_h,img_w = 64,64
num_channels = 3
img_1 = torch.randn(batch_size , img_h , img_w , num_channels) ### Of the form
img_1 = img_1.permute(0,3,1,2) ### Of the form [batch_size, channels, height, width]
</code></pre>
<p>In your case, you would need to permute <code>x</code> as soon as it comes into the <code>forward</code> method of the model.<br>
Additionally, in your Conv2d declaration, the number of input channels is not the height. For instance, in the above example, the number of input channels is 3 and not 64. Therefore the Conv2d declaration would take an <code>in_shape</code> of 3.</p>

Alymostafa: <pre><code class="lang-auto">    def __init__(self, num_classes=2):
        super(ConvNet, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(3, 224, kernel_size=5, stride=1, padding=2),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=2, stride=2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(32, 64, kernel_size=5, stride=1, padding=2),
            #nn.BatchNorm2d(64),
            nn.ReLU(),
            nn.MaxPool2d(kernel_size=1, stride=0))
        self.drop_out = nn.Dropout()

        
        self.fc1 = nn.Linear(7168, 45)
        self.fc2 = nn.Linear(45,2)
        
    def forward(self, x):
        x.float()
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.reshape(out.size(0), -1)
        out = self.drop_out(out)
        out = self.fc1(out)
        out = self.fc2(out)
        return out
</code></pre>
<p>where can i change the values that you say<br>
my image size is 224*224<br>
thanks in advance</p>

charan_Vjy: <pre><code class="lang-auto">def forward(self, x):
       x.float()
       x = x.permute(0,3,1,2)
       ....
</code></pre>
<p>Change nn.Conv2d in layer1 to <code>nn.Conv2d(3,32,kernel_size = 5, stride = 1, padding = 2)</code></p>

Alymostafa: <p>gives me this error :</p>
<pre><code class="lang-auto">&lt;ipython-input-33-87fb847a7382&gt; in &lt;module&gt;()
      2 val_losses = []
      3 for epoch in range(num_epochs):
----&gt; 4     train(epoch)

5 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in linear(input, weight, bias)
   1368     if input.dim() == 2 and bias is not None:
   1369         # fused op is marginally faster
-&gt; 1370         ret = torch.addmm(bias, input, weight.t())
   1371     else:
   1372         output = input.matmul(weight.t())

RuntimeError: size mismatch, m1: [45 x 802816], m2: [7168 x 45] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290
</code></pre>

charan_Vjy: <p>The error is expected, given that your Linear layer takes in an input of 7168 and you are getting an input of 802816. Declare the Linear layer as <code>self.fc1 = nn.Linear(802816, 45)</code>. However, this solution is not recommended as  it places huge computational constraint in terms of the number of parameters. It is recommended to downsample the spatial dimension further before you use a fully connected layer.</p>

Alymostafa: <aside class="quote no-group" data-username="charan_Vjy" data-post="15" data-topic="75627">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/charan_vjy/48/13238_2.png" class="avatar"> charan_Vjy:</div>
<blockquote>
<p>It is recommended to downsample the spatial dimension further before you use a fully connected layer.</p>
</blockquote>
</aside>
<p>how can i do that and not use huge computational</p>

Alymostafa: <p>it gives me another error :</p>
<pre><code class="lang-auto">ValueError                                Traceback (most recent call last)
&lt;ipython-input-39-87fb847a7382&gt; in &lt;module&gt;()
      2 val_losses = []
      3 for epoch in range(num_epochs):
----&gt; 4     train(epoch)

4 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/functional.py in nll_loss(input, target, weight, size_average, ignore_index, reduce, reduction)
   1834     if input.size(0) != target.size(0):
   1835         raise ValueError('Expected input batch_size ({}) to match target batch_size ({}).'
-&gt; 1836                          .format(input.size(0), target.size(0)))
   1837     if dim == 2:
   1838         ret = torch._C._nn.nll_loss(input, target, weight, _Reduction.get_enum(reduction), ignore_index)

ValueError: Expected input batch_size (45) to match target batch_size (90).
</code></pre>

charan_Vjy: <p>There are 90 targets. There are only 45 inputs. This is an error in the way you are assigning targets.</p>

charan_Vjy: <p>I would suggest using Maxpool layers to downsample the input</p>