Given groups=1, weight of size [256, 1, 5, 5], expected input[4, 3, 256, 256] to have 1 channels, but got 3 channels instead

username: Bhavishya_Pandit
cooked: <p>Hello, I am having a channel issue. Can anyone please help me out with the same? Thanks in advance.</p>
<pre><code class="lang-auto">#CNN architecture
class classifier(nn.Module):
    def __init__(self):
        super(classifier,self).__init__()
        self.block1=self.conv_block(cin=1,cout=256,dropout=0.1,kernel_size=5,padding=1,stride=2)
        self.block2=self.conv_block(cin=256,cout=256,dropout=0.1,kernel_size=3,padding=1,stride=1)
        self.block3=self.conv_block(cin=256,cout=128,dropout=0.1,kernel_size=3,padding=1,stride=1)
        self.lastcnn=nn.Conv2d(in_channels=128,out_channels=102,kernel_size=15,padding=0,stride=1)
        
        self.maxpool=nn.MaxPool2d(kernel_size=2,stride=2)
        
    def forward(self,x):
        x=self.block1(x)
        x=self.maxpool(x)
        
        x=self.block2(x)
        x=self.maxpool(x)
        
        x=self.block3(x)
        x=self.maxpool(x)
        
        x=self.lastcnn(x)
        x = x.flatten(start_dim=1)
        return x
        
    def conv_block(self,cin,cout,dropout,**kwargs):
        seq_block=nn.Sequential(
                    nn.Conv2d(in_channels=cin,out_channels=cout,**kwargs),
                    nn.BatchNorm2d(num_features=cout),
                    nn.ReLU(),
                    nn.Dropout2d(p=dropout)            
        )
        return seq_block
</code></pre>
<p><strong>Training Block</strong></p>
<pre><code class="lang-auto">print('Training')
epochs=10
for epoch in tqdm(range(epochs)):
        train_loss,train_acc=0,0
        model.train()
        for inputs,targets in train_loader:
            #inputs,targets=inputs.to(device),targets.to(device)
            optimizer.zero_grad()
            print(inputs.shape)
            y_pred=model(inputs)
            #print(y_pred.shape)
            loss=criterion(y_pred,targets)
            accuracy=accuracy(y_pred,targets)
            loss.backward()
            optimizer.step()
            train_loss+=loss.item()
            train_acc+=accuracy.item()
        loss_stats['train'].append(train_loss/len(train_loader))
        accuracy_stats['train'].append(accuracy_loss/len(train_loader))
        print(f'Epoch {e+0:02}: | Train Loss: {train_loss/len(train_loader):.3f} | Train Acc: {train_acc/len(train_loader):.3f}')
</code></pre>
<p><strong>Error</strong></p>
<pre><code class="lang-auto">RuntimeError                              Traceback (most recent call last)
&lt;ipython-input-125-16c3c99a7be2&gt; in &lt;module&gt;
      8             optimizer.zero_grad()
      9             print(inputs.shape)
---&gt; 10             y_pred=model(inputs)
     11             print(y_pred.shape)
     12             loss=criterion(y_pred,targets)

~\Anaconda3\lib\site-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-122-87a5f180cde4&gt; in forward(self, x)
     11 
     12     def forward(self,x):
---&gt; 13         x=self.block1(x)
     14         x=self.maxpool(x)
     15 

~\Anaconda3\lib\site-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)

~\Anaconda3\lib\site-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 

~\Anaconda3\lib\site-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)

~\Anaconda3\lib\site-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):

~\Anaconda3\lib\site-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 [256, 1, 5, 5], expected input[4, 3, 256, 256] to have 1 channels, but got 3 channels instead
</code></pre>

username: Eta_C
cooked: <p>Your input shape is <code>[4, 3, 256, 256]</code>, check it again.</p>

username: 1chimaruGin
cooked: <aside class="quote no-group" data-username="Bhavishya_Pandit" data-post="1" data-topic="109553">
<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/bhavishya_pandit/48/32728_2.png" class="avatar"> Bhavishya_Pandit:</div>
<blockquote>
<p><code>self.block1=self.conv_block(cin=1,cout=256,dropout=0.1,kernel_size=5,padding=1,stride=2)</code></p>
</blockquote>
</aside>
<p>The model expect <code>[4, 1, 256, 256]</code> but your input shape is <code>[4, 3, 256, 256]</code></p>