Extraction of layer-> getting error

ArshadIram: <p>Hello Everyone,</p>
<p>Here is the model:</p>
<p>‘’’<br>
class Net(nn.Module):<br>
def <strong>init</strong>(self):<br>
super().<strong>init</strong>()<br>
self.conv1 = nn.Conv2d(in_channels=3, out_channels=6, kernel_size= 5)<br>
self.pool = nn.MaxPool2d(kernel_size=2,stride= 2)<br>
self.conv2 = nn.Conv2d(in_channels=6, out_channels=16, kernel_size= 5)<br>
self.fc1 = nn.Linear(in_features=400, out_features=120)<br>
self.fc2 = nn.Linear(in_features=120, out_features=84)<br>
self.fc3 = nn.Linear(in_features=84, out_features=10)</p>
<pre><code>def forward(self, x):
    con_1_output = self.pool(F.relu(self.conv1(x)))
    con_2_output = self.pool(F.relu(self.conv2(con_1_output))) #([4, 16, 5, 5])
    flatten_values = torch.flatten(con_2_output, 1) # flatten all dimensions except batch [batch_size, in_features] [4, 400]
    fc1_output = F.relu(self.fc1(flatten_values)) # output x.shape = [4, 120]
    fc2_output = F.relu(self.fc2(fc1_output))
    fc3_output = self.fc3(fc2_output)
    return fc3_output
</code></pre>
<p>‘’’<br>
I am creating a new model to extract the feature vector of the FC1</p>
<p>new_model = nn.Sequential(*list(save_net.children())[:-2])<br>
Output is:</p>
<p>‘’’<br>
Sequential(<br>
(0): Conv2d(3, 6, kernel_size=(5, 5), stride=(1, 1))<br>
(1): MaxPool2d(kernel_size=2, stride=2, padding=0, dilation=1, ceil_mode=False)<br>
(2): Conv2d(6, 16, kernel_size=(5, 5), stride=(1, 1))<br>
(3): Linear(in_features=400, out_features=120, bias=True)<br>
)</p>
<p>‘’’<br>
Problem: When I am passing the test image in the new model I am getting an error:<br>
‘’’<br>
RuntimeError: mat1 and mat2 shapes cannot be multiplied (640x10 and 400x120)<br>
‘’’</p>

fpozzi: <p>The problem is the tensor dimension inside the forward pass (look at the comments)</p>
<pre><code class="lang-python">  def forward(self, x):
    # x shape: [1, 3, 224, 224]
    x = self.pool(F.relu(self.conv1(x))) # output shape: [1, 6, 110, 110]
    x = self.pool(F.relu(self.conv2(x))) # output shape: [1, 16, 53, 53]
    x = torch.flatten(x, 1) # output shape: [1, 44944]
    x = F.relu(self.fc1(x))  
    ...
</code></pre>
<p>You get the error even if you make inference with a random tensor.</p>
<pre><code class="lang-python">model = Net()
t = torch.randn(1, 3, 224, 224)

model(t)
</code></pre>
<pre><code class="lang-python">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-71-22d29d5af2a4&gt; in &lt;module&gt;()
----&gt; 1 net(t)

3 frames
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/linear.py in forward(self, input)
    101 
    102     def forward(self, input: Tensor) -&gt; Tensor:
--&gt; 103         return F.linear(input, self.weight, self.bias)
    104 
    105     def extra_repr(self) -&gt; str:

RuntimeError: mat1 and mat2 shapes cannot be multiplied (1x44944 and 400x120)
</code></pre>
<p>As you can see, the expected input size is <code>(batch_size, 400)</code>, but you’re feeding the layer with a tensor of shape <code>(batch_size, 44944)</code>.</p>
<p>You should definitely consider to use a <a href="https://pytorch.org/docs/stable/generated/torch.nn.AdaptiveAvgPool2d.html" rel="noopener nofollow ugc">AdaptiveAvgPool2d</a> layer after the flatten operation.</p>

ArshadIram: <aside class="quote no-group" data-username="fpozzi" data-post="2" data-topic="154409">
<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/fpozzi/48/50568_2.png" class="avatar"> fpozzi:</div>
<blockquote>
<pre><code class="lang-auto">model = Net()
t = torch.randn(1, 3, 224, 224)

model(t)
</code></pre>
</blockquote>
</aside>
<p>My input size is t = torch.randn(1, 3, 32, 32) not torch.randn(1, 3, 224, 224)</p>

fpozzi: <aside class="quote no-group" data-username="ArshadIram" data-post="1" data-topic="154409">
<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/arshadiram/48/32886_2.png" class="avatar"> ArshadIram:</div>
<blockquote>
<p>new_model = nn.Sequential(*list(save_net.children())[:-2])</p>
</blockquote>
</aside>
<p>The <a href="https://pytorch.org/docs/stable/generated/torch.nn.Module.html#torch.nn.Module.children" rel="noopener nofollow ugc">children</a> method returns only <code>nn.Module</code>, so you skipping the functional flatten in the forward pass, which is needed to prepare the output for the linear layer.</p>
<p>A fast refactoring could be the following:</p>
<pre data-code-wrap="python"><code class="lang-python">import torch
import torch.nn as nn
import torch.nn.functional as F

class Net(nn.Module):
  def __init__(self):
    super().__init__()
    self.layers = nn.Sequential(
        nn.Conv2d(in_channels=3, out_channels=6, kernel_size= 5),
        nn.MaxPool2d(kernel_size=2,stride= 2),
        nn.Conv2d(in_channels=6, out_channels=16, kernel_size= 5),
        nn.Flatten(start_dim=1),
        nn.Linear(16 * 10 * 10, 120),
        nn.Linear(120, 84),
        nn.Linear(84, 10)
    )

  def forward(self, x):
    return self.layers(x)
</code></pre>
<p>Note that I had to modify the in_features of the first linear layer to fit the right dimension.</p>
<pre data-code-wrap="python"><code class="lang-python">model = Net()
x = torch.randn(1, 3, 32, 32)

print(model.layers[:-2](x).shape)

&gt;&gt;&gt; torch.Size([1, 120])
</code></pre>

ArshadIram: <aside class="quote no-group" data-username="fpozzi" data-post="2" data-topic="154409">
<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/fpozzi/48/50568_2.png" class="avatar"> fpozzi:</div>
<blockquote>
<p>batch_size</p>
</blockquote>
</aside>
<p>I have used forward hooks to extract the features and for fc1 I am getting this output (1, 4, 84). I don’t think this is right output value of fc1 layer?</p>