Simple Classifier size mismatch

username: Zila_Tajudin
cooked: <p>Size mismatched.<br>
I am a beginner and I try to use simple classifier I found online and use my own custom image.<br>
I have 2 class of RGB image with size of 224x224.<br>
When I run below code I get an mismatched size.<br>
May I know how the number came from?<br>
RuntimeError: size mismatch, m1: [2 x 119072], m2: [1568 x 2] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290<br>
I know 10 came from batch size but how about 100352?<br>
Below is my code and my error<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/3/5365388036a70d4deb401f5434e5bd0eb672f287.png" data-download-href="https://discuss.pytorch.org/uploads/default/5365388036a70d4deb401f5434e5bd0eb672f287" title="27ogos"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/3/5365388036a70d4deb401f5434e5bd0eb672f287_2_669x500.png" alt="27ogos" data-base62-sha1="bTKuEvS5rBEmQMwyjRrf2bdF9xZ" width="669" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/3/5365388036a70d4deb401f5434e5bd0eb672f287_2_669x500.png, https://discuss.pytorch.org/uploads/default/optimized/3X/5/3/5365388036a70d4deb401f5434e5bd0eb672f287_2_1003x750.png 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/3/5365388036a70d4deb401f5434e5bd0eb672f287.png 2x" data-dominant-color="F1F1F1"><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">27ogos</span><span class="informations">1158×865 104 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>

username: RaLo4
cooked: <p>Hi,<br>
can you next time please provide your code in a code block and not post a screenshot of it.<br>
Makes it easier to debug <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>
<p>Your problem is most likely with the input dimension of your fully connected layer <code>self.fc = nn.Linear(7*7*32, num_classes)</code><br>
It seams like this network was made to run with RGB images of size 28x28. But since you provide it with images of size 224x224 the feature maps that get get passed into the fc layer are not of size 7x7x32 but 56x56x32 (if my calculations are correct).<br>
So to make it work again change:</p>
<pre><code class="lang-auto">self.fc = nn.Linear(7*7*32, num_classes)
</code></pre>
<p>to</p>
<pre><code class="lang-auto">self.fc = nn.Linear(56*56*32, num_classes)
</code></pre>

username: Zila_Tajudin
cooked: <aside class="quote no-group" data-username="RaLo4" data-post="2" data-topic="94253">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/r/65b543/48.png" class="avatar"> RaLo4:</div>
<blockquote>
<p><code>7*32, num_classes)</code><br>
It seams like this network was made to run with RGB images of size 28x28. But since you provide it with images of size 224x224 the feature maps that get get passed into the fc layer are not of size 7x7x32 but 56x56x32 (if my calculations are correct).<br>
So to make it work again change:</p>
</blockquote>
</aside>
<p>Hi <a class="mention" href="/u/ralo4">@RaLo4</a>,</p>
<p>Thanks for your help.<br>
Noted on the code part.<br>
May I know how did (7<em>7</em>32) derived from?<br>
or from my case (56<em>56</em>32)?<br>
How did from conv2(16,32,5) become (56<em>56</em>32).<br>
From my understanding the output of conv2 became input of fc.<br>
Or am I wrong?</p>

username: RaLo4
cooked: <aside class="quote no-group" data-username="Zila_Tajudin" data-post="3" data-topic="94253">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/z/b5ac83/48.png" class="avatar"> Zila_Tajudin:</div>
<blockquote>
<p>From my understanding the output of conv2 became input of fc.</p>
</blockquote>
</aside>
<p>yes you are correct. well, there is still the batch norm, relu and maxpool in between but more or less, yes.<br>
it help to calculate and trace the shape of your image tensor throughout your network.<br>
At the point your image tensor leaves <code>self.layer2</code> your image tensor is of shape: <strong>batch_size x 32 x 56 x 56</strong>. In this shape you could just pass it through another conv layer. But if you want to pass it through an fully connected layer, you need to flatten this tensor first.<br>
Your</p>
<pre><code class="lang-auto">out = out.reshape(out.size(0), -1)
</code></pre>
<p>does this for you.<br>
So if you flatten your tensor it becomes <strong>batch_size x 32 * 56 * 56</strong> or <strong>batch_size x 100352</strong></p>
<p>I explained this recently using some example code. If you want to check this out <a href="https://discuss.pytorch.org/t/transition-from-conv2d-to-linear-layer-equations/93850/3">here is a link</a>.</p>
<p>If you are still not quite getting it or have any more questions, please feel free to ask more!</p>