Size mismatch, m1: [128 x 512], m2: [25088 x 4096]

111186: <p>i want to use the ptr-trainVGG19 , to initialization my parameter , and then to train my model. but got this problem. help me<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/1/a/1acd18a626fe9beae262e30c412d7b507756f647.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/1acd18a626fe9beae262e30c412d7b507756f647" title="1"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1acd18a626fe9beae262e30c412d7b507756f647_2_690x175.jpeg" alt="1" data-base62-sha1="3P5PwlGVQ6qaCo5GSH5sDqYQh5J" width="690" height="175" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1acd18a626fe9beae262e30c412d7b507756f647_2_690x175.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1acd18a626fe9beae262e30c412d7b507756f647_2_1035x262.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/1/a/1acd18a626fe9beae262e30c412d7b507756f647_2_1380x350.jpeg 2x" data-dominant-color="343330"><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">1</span><span class="informations">3073×781 277 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/b/7/b76c7022a74b573f3b0a8a168a2880e8739f0577.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/b76c7022a74b573f3b0a8a168a2880e8739f0577" title="2"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/b/7/b76c7022a74b573f3b0a8a168a2880e8739f0577_2_690x281.jpeg" alt="2" data-base62-sha1="qaDHG5rkk7SPq1yaYidvqflx5kz" width="690" height="281" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/b/7/b76c7022a74b573f3b0a8a168a2880e8739f0577_2_690x281.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/b/7/b76c7022a74b573f3b0a8a168a2880e8739f0577.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/b/7/b76c7022a74b573f3b0a8a168a2880e8739f0577.jpeg 2x" data-dominant-color="33342D"><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">2</span><span class="informations">861×351 34.4 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> <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/9/1/91b7f79bdf255d8a34b001b2afe32c6738fdb048.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/91b7f79bdf255d8a34b001b2afe32c6738fdb048" title="3"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/91b7f79bdf255d8a34b001b2afe32c6738fdb048_2_690x58.jpeg" alt="3" data-base62-sha1="kN5nDdFTGJzcbuJZ00NPzXcQrbO" width="690" height="58" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/91b7f79bdf255d8a34b001b2afe32c6738fdb048_2_690x58.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/91b7f79bdf255d8a34b001b2afe32c6738fdb048_2_1035x87.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/optimized/3X/9/1/91b7f79bdf255d8a34b001b2afe32c6738fdb048_2_1380x116.jpeg 2x" data-dominant-color="393B33"><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">3</span><span class="informations">1473×125 23 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>

crowsonkb: <p>You should try something along the lines of:</p>
<pre><code class="lang-python">num_ftrs = net.classifier[-1].in_features
net.classifier[-1] = nn.Linear(num_ftrs, 7)
</code></pre>
<p>This will make the model output raw scores for seven classes instead of the default 1000.</p>

111186: <p>thanks ,but it doesn’t work. i guess it could be caused by my img’size</p>

ptrblck: <p>How large is each image you pass to the model?<br>
<code>torchvision.models.vgg</code> uses an <a href="https://github.com/pytorch/vision/blob/d2c763e14efe57e4bf3ebf916ec243ce8ce3315c/torchvision/models/vgg.py#L44">adaptive pooling layer</a> which should take variable sized inputs. However, if you are using an older version, your model might be missing this layer.</p>

111186: <p>my picture is 44 x 44 and 3channels</p>

ptrblck: <p>In that case <a class="mention" href="/u/crowsonkb">@crowsonkb</a>’s suggestion should work fine:</p>
<pre><code class="lang-python">model = models.vgg19()
num_ftrs = model.classifier[-1].in_features
model.classifier[-1] = nn.Linear(num_ftrs, 7)

x = torch.randn(1, 3, 44, 44)
output = model(x)
print(output.shape)
&gt; torch.Size([1, 7])
</code></pre>