Given groups=1, weight of size [96, 192, 1, 1], expected input[1, 64, 125, 125] to have 192 channels, but got 64 channels instead

vzmb: <p>Hello, I am new to using pytorch, but I manage to get almost everything working, but now I am using Googlenet pretrained and the model works fine, the problem is that I need to see the visualization layers and verify that the model is considering the relevant aspects. , what I want is something like the following image</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/0/b/0b2692a5513ae3ac146ecc58553df82d73a5b0c3.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/0b2692a5513ae3ac146ecc58553df82d73a5b0c3" title="image"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/0/b/0b2692a5513ae3ac146ecc58553df82d73a5b0c3_2_690x176.jpeg" alt="image" data-base62-sha1="1ADTqTmKUmJbMLOz54FxT8mE5hN" width="690" height="176" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/0/b/0b2692a5513ae3ac146ecc58553df82d73a5b0c3_2_690x176.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/0/b/0b2692a5513ae3ac146ecc58553df82d73a5b0c3.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/0/b/0b2692a5513ae3ac146ecc58553df82d73a5b0c3.jpeg 2x" data-dominant-color="77B0B2"><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">image</span><span class="informations">966×247 50.5 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>
<p>The problem is that when I try to iterate over the convolutional layer it gives me this error</p>
<p>Given groups=1, weight of size [96, 192, 1, 1], expected input[1, 64, 125, 125] to have 192 channels, but got 64 channels instead</p>
<p>This is a part of the model architecture.</p>
<p><strong>[GoogLeNet(</strong><br>
(conv1): <strong>BasicConv2d(</strong><br>
(conv): Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)<br>
(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(maxpool1): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)<br>
(conv2): BasicConv2d(<br>
(conv): <strong>Conv2d</strong>(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(conv3): BasicConv2d(<br>
(conv): <strong>Conv2d</strong>(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(maxpool2): MaxPool2d(kernel_size=3, stride=2, padding=0, dilation=1, ceil_mode=True)<br>
(inception3a): Inception(<br>
(branch1): BasicConv2d(<br>
(conv): **Conv2d(**192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(branch2): Sequential(<br>
(0): BasicConv2d(<br>
(conv): <strong>Conv2d</strong>(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): BasicConv2d(<br>
(conv): Conv2d(96, 128, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(branch3): Sequential(<br>
(0): BasicConv2d(<br>
(conv): Conv2d(192, 16, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(16, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): BasicConv2d(<br>
(conv): Conv2d(16, 32, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn): BatchNorm2d(32, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(branch4): Sequential(<br>
(0): MaxPool2d(kernel_size=3, stride=1, padding=1, dilation=1, ceil_mode=True)<br>
(1): BasicConv2d(<br>
(conv): Conv2d(192, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(32, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
)<br>
(inception3b): Inception(<br>
(branch1): BasicConv2d(<br>
(conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(branch2): Sequential(<br>
(0): BasicConv2d(<br>
(conv): Conv2d(256, 128, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(128, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): BasicConv2d(<br>
(conv): Conv2d(128, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn): BatchNorm2d(192, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)<br>
(branch3): Sequential(<br>
(0): BasicConv2d(<br>
(conv): Conv2d(256, 32, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
(bn): BatchNorm2d(32, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
(1): BasicConv2d(<br>
(conv): Conv2d(32, 96, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)<br>
)<br>
)</p>
<p>and stops at the fifth convolutional Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)</p>
<p>i have a list of all the convolutionals like :</p>
<p>[Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False), Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False), Conv2d(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False), Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False), Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False), …]</p>
<p>and i iterate over them using this code:</p>
<p><strong>outputs = []</strong><br>
<strong>names = []</strong><br>
<strong>for layer in conv_layers[0:]:</strong><br>
print(layer)<br>
image = layer(image)<br>
outputs.append(image)<br>
names.append(str(layer))<br>
print(len(outputs))<br>
for i in outputs:<br>
print(i.shape)</p>
<p>Somebody could help me? I have no idea of ​​the problem. I can see that the convolutional that is giving me trouble is part of a sequential, but I don’t know if that is the problem.</p>
<h2><a name="conv2d3-64-kernel_size7-7-stride2-2-padding3-3-biasfalse-conv2d64-64-kernel_size1-1-stride1-1-biasfalse-conv2d64-192-kernel_size3-3-stride1-1-padding1-1-biasfalse-conv2d192-64-kernel_size1-1-stride1-1-biasfalse-conv2d192-96-kernel_size1-1-stride1-1-biasfalse-1" class="anchor" href="#conv2d3-64-kernel_size7-7-stride2-2-padding3-3-biasfalse-conv2d64-64-kernel_size1-1-stride1-1-biasfalse-conv2d64-192-kernel_size3-3-stride1-1-padding1-1-biasfalse-conv2d192-64-kernel_size1-1-stride1-1-biasfalse-conv2d192-96-kernel_size1-1-stride1-1-biasfalse-1"></a>Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)<br>
Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
Conv2d(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False)<br>
Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)<br>
Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)</h2>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in <br>
3 for layer in conv_layers[0:]:<br>
4     print(layer)<br>
----&gt; 5     image = layer(image)<br>
6     outputs.append(image)<br>
7     names.append(str(layer))</p>
<p>2 frames<br>
/usr/local/lib/python3.7/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)<br>
452                             _pair(0), self.dilation, self.groups)<br>
453         return F.conv2d(input, weight, bias, self.stride,<br>
 → 454                         self.padding, self.dilation, self.groups)<br>
455<br>
456     def forward(self, input: Tensor) → Tensor:</p>
<p>RuntimeError: Given groups=1, weight of size [96, 192, 1, 1], expected input[1, 64, 125, 125] to have 192 channels, but got 64 channels instead</p>

ptrblck: <p>Your code is not formatted properly (you can wrap it into three backticks ```), but it seems the error is raised in:</p>
<pre><code class="lang-python">(branch1): BasicConv2d(
(conv): **Conv2d(**192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn): BatchNorm2d(64, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)
)
(branch2): Sequential(
(0): BasicConv2d(
(conv): Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)
(bn): BatchNorm2d(96, eps=0.001, momentum=0.1, affine=True, track_running_stats=True)
)
</code></pre>
<p>as the <code>Conv2d</code> layer in <code>branch1</code> returns 64 channels while the <code>Conv2d</code> layer in <code>branch2</code> expects 192 channels.</p>

vzmb: <p>Yes I’m sorry, I didn’t know about the hree backticks ```, but yes,  the problem is with branch2, but this code works fine when i am training the model, what occurs to me is that the order is not correct, when I am iterating to obtain the effect of the convolution on the images, since otherwise when I train it it would give me an error, but it is not clear to me what the proper order should be, I will continue looking in the documentation, and thanks!</p>

ptrblck: <p>I don’t know how the actual <code>forward</code> method is defined, but the conv setup should never work assuming these layers are executed sequentially:</p>
<pre><code class="lang-python">convs = [nn.Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False),
         nn.Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False),
         nn.Conv2d(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False),
         nn.Conv2d(192, 64, kernel_size=(1, 1), stride=(1, 1), bias=False), # !!! WRONG !!!
         nn.Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)]

model = nn.Sequential(*convs)
x = torch.randn(2, 3, 224, 224)
out = model(x)
# RuntimeError: Given groups=1, weight of size [96, 192, 1, 1], expected input[2, 64, 112, 112] to have 192 channels, but got 64 channels instead


convs = [nn.Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False),
         nn.Conv2d(64, 64, kernel_size=(1, 1), stride=(1, 1), bias=False),
         nn.Conv2d(64, 192, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False),
         nn.Conv2d(192, 192, kernel_size=(1, 1), stride=(1, 1), bias=False), # !!! FIXED !!!
         nn.Conv2d(192, 96, kernel_size=(1, 1), stride=(1, 1), bias=False)]

model = nn.Sequential(*convs)
x = torch.randn(2, 3, 224, 224)
out = model(x)
print(out.shape)
# torch.Size([2, 96, 112, 112])
</code></pre>