RuntimeError: Given groups=1, weight of size [1000, 512, 1, 1], expected input[4, 384, 1, 1] to have 512 channels, but got 384 channels instead

No_Yeah: <p>Hi I am to trying to reduce some layers of squeezenet</p>
<pre><code>model = models.squeezenet1_1(pretrained=False)
PruneModels=[]
PruneModels.append(model)
PruneAcc=[]
PruneLoss=[]
bnum=[]
name = (f'{model.__class__.__name__}')

while PruneModels:
    model = PruneModels.pop()
    accs = []
    torch.cuda.empty_cache()

    if name =='SqueezeNet': # 13 blocks
        for find in range(len(model.features)-1, 3, -1):
            del model.features[find]
            last_features = 0
            try:
                last_features = model.features[-1].expand3x3.out_channels
                last_features *= 2
                model.classifier[1].in_channels=last_features
                loss, acc = start_train(model)
</code></pre>
<p>during training I got the following error</p>
<p>RuntimeError: Given groups=1, weight of size [1000, 512, 1, 1], expected input[4, 384, 1, 1] to have 512 channels, but got 384 channels instead</p>
<p>and the below is squeezenet architecture</p>
<pre><code>(11): Fire(
  (squeeze): Conv2d(384, 64, kernel_size=(1, 1), stride=(1, 1))
  (squeeze_activation): ReLU(inplace=True)
  (expand1x1): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))
  (expand1x1_activation): ReLU(inplace=True)
  (expand3x3): Conv2d(64, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
  (expand3x3_activation): ReLU(inplace=True)
)
**(12): Fire(**
</code></pre>
<p>**      (squeeze): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))**<br>
**      (squeeze_activation): ReLU(inplace=True)**<br>
**      (expand1x1): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))**<br>
**      (expand1x1_activation): ReLU(inplace=True)**<br>
**      (expand3x3): Conv2d(64, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))**<br>
**      (expand3x3_activation): ReLU(inplace=True)**<br>
**    )**<br>
**  )**<br>
(classifier): Sequential(<br>
(0): Dropout(p=0.5, inplace=False)<br>
(1): Conv2d(512, 1000, kernel_size=(1, 1), stride=(1, 1))<br>
(2): ReLU(inplace=True)<br>
(3): AdaptiveAvgPool2d(output_size=(1, 1))<br>
)<br>
)</p>
<p>In code, I set the (classifer) Conv2D input to correspond to the last conv layer. If I remove the bolded block, then (11) expanded3x3 layer is the target and got error below</p>
<p>RuntimeError: Given groups=1, weight of size [1000, 512, 1, 1], expected input[4, 384, 1, 1] to have 512 channels, but got 384 channels instead</p>
<p>I set the Conv2d input as 512, 384,… by hardcoding but none of them work</p>
<p>I have thought some skip connection is the matter? but there was no skip connection in 11~12 block…</p>
<p>how to deal with this problem? the below is the full structure of squeezenet</p>
<p>[SqueezeNet(<br>
(features): Sequential(<br>
(0): Conv2d(3, 64, kernel_size=(3, 3), stride=(2, 2))<br>
(1): ReLU(inplace=True)<br>
(2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)<br>
(3): Fire(<br>
(squeeze): Conv2d(64, 16, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(16, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
(4): Fire(<br>
(squeeze): Conv2d(128, 16, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(16, 64, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(16, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
(5): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)<br>
(6): Fire(<br>
(squeeze): Conv2d(128, 32, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(32, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
(7): Fire(<br>
(squeeze): Conv2d(256, 32, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(32, 128, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(32, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
(8): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)<br>
(9): Fire(<br>
(squeeze): Conv2d(256, 48, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(48, 192, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(48, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
(10): Fire(<br>
(squeeze): Conv2d(384, 48, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(48, 192, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(48, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
(11): Fire(<br>
(squeeze): Conv2d(384, 64, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(64, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
(12): Fire(<br>
(squeeze): Conv2d(512, 64, kernel_size=(1, 1), stride=(1, 1))<br>
(squeeze_activation): ReLU(inplace=True)<br>
(expand1x1): Conv2d(64, 256, kernel_size=(1, 1), stride=(1, 1))<br>
(expand1x1_activation): ReLU(inplace=True)<br>
(expand3x3): Conv2d(64, 256, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))<br>
(expand3x3_activation): ReLU(inplace=True)<br>
)<br>
)<br>
(classifier): Sequential(<br>
(0): Dropout(p=0.5, inplace=False)<br>
(1): Conv2d(512, 1000, kernel_size=(1, 1), stride=(1, 1))<br>
(2): ReLU(inplace=True)<br>
(3): AdaptiveAvgPool2d(output_size=(1, 1))<br>
)<br>
)</p>

ptrblck: <aside class="quote no-group" data-username="No_Yeah" data-post="1" data-topic="184020">
<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/no_yeah/48/61273_2.png" class="avatar"> No_Yeah:</div>
<blockquote>
<p>I set the Conv2d input as 512, 384,… by hardcoding but none of them work</p>
</blockquote>
</aside>
<p>Changing the <code>in_channels</code> <em>after</em> initializing the module won’t work as seen here:</p>
<pre data-code-wrap="python"><code class="lang-python">conv = nn.Conv2d(384, 1, 3, 1, 1)
print(conv.weight.shape)
# torch.Size([1, 384, 3, 3])

conv.in_channels = 512
print(conv.weight.shape)
# torch.Size([1, 384, 3, 3])
print(conv)
# Conv2d(512, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1))
</code></pre>