Runtime Error: Channel

username: Rahul_pillai
cooked: <p>this is my code.</p>
<pre><code class="lang-auto">class ResNet9(nn.Module):
    def __init__(self, in_channels, out_channels):
        super().__init__()

        #1st Block
        self.conv1 = conv_block(in_channels, 64)
        self.conv2 = conv_block(64, 128, True) 
        #Residual layer
        self.res1 = nn.Sequential(conv_block(128,128), conv_block(128,128))
        
        #2nd Block
        self.conv3 = conv_block(128, 256, True)
        self.conv4 = conv_block(256, 512, True)
        #Residual layer
        self.res2 = nn.Sequential(conv_block(512,512), conv_block(512,512))
        
        #Linear Network
        self.linear = nn.Sequential(
            nn.MaxPool2d(16), 
            nn.Flatten(), 
            nn.Linear(512, 128),
            nn.ReLU(),
            nn.Linear(128, out_channels),
            nn.LogSoftmax()
            )
        
    def forward(self,x):
        #Block-1
        out = self.conv1(x)
        out = self.conv2(x)
        res1 = self.res1(out) + out

        #Block-1
        out = self.conv3(x)
        out = self.conv4(x)
        res2 = self.res2(out) + out

        #Linear network
        out = self.Linear(res2)
        return out

model = ResNet9(1,2).to(device)
</code></pre>
<p>im trying to give in a batch of img data with size [100,1,128,128], but it gives this error.</p>
<pre><code class="lang-auto">---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-20-1aacca183d36&gt; in &lt;module&gt;()
----&gt; 1 model(x.to(device))

6 frames
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

&lt;ipython-input-12-6ed96ba919e0&gt; in forward(self, x)
     28         #Block-1
     29         out = self.conv1(x)
---&gt; 30         out = self.conv2(x)
     31         res1 = self.res1(out) + out
     32 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/container.py in forward(self, input)
     98     def forward(self, input):
     99         for module in self:
--&gt; 100             input = module(input)
    101         return input
    102 

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/module.py in __call__(self, *input, **kwargs)
    548             result = self._slow_forward(*input, **kwargs)
    549         else:
--&gt; 550             result = self.forward(*input, **kwargs)
    551         for hook in self._forward_hooks.values():
    552             hook_result = hook(self, input, result)

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in forward(self, input)
    351 
    352     def forward(self, input):
--&gt; 353         return self._conv_forward(input, self.weight)
    354 
    355 class Conv3d(_ConvNd):

/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)
    348                             _pair(0), self.dilation, self.groups)
    349         return F.conv2d(input, weight, self.bias, self.stride,
--&gt; 350                         self.padding, self.dilation, self.groups)
    351 
    352     def forward(self, input):

RuntimeError: Given groups=1, weight of size [128, 64, 3, 3], expected input[100, 1, 128, 128] to have 64 channels, but got 1 channels instead
</code></pre>

username: albanD
cooked: <p>Hi,</p>
<p><code>out = self.conv2(x)</code> -&gt; <code>out = self.conv2(out)</code> should fix it no? And same for conv4.</p>

username: Rahul_pillai
cooked: <p>Oh yeah, silly mistake <img src="https://discuss.pytorch.org/images/emoji/apple/sweat_smile.png?v=9" title=":sweat_smile:" class="emoji" alt=":sweat_smile:">. It works perfectly fine now.<br>
Thank you <a class="mention" href="/u/alband">@albanD</a>.</p>