RuntimeError: Given groups=1, weight of size [32, 16, 3, 3], expected input[32, 32, 56, 56] to have 16 channels, but got 32 channels instead

username: Janhawi
cooked: <p>batch_size = 32<br>
class Net(nn.Module):</p>
<pre><code>def __init__(self):

    super(Net,self).__init__()

    self.conv1 = nn.Conv2d(3,16,3,padding=1)

    self.conv2 = nn.Conv2d(16,32,3,padding=1)

    #self.conv2_bn = nn.BatchNorm2d(32)

    self.conv3 = nn.Conv2d(32,64,3,padding=1)

    #self.conv3_bn = nn.BatchNorm2d(64)

    self.pool = nn.MaxPool2d(2,2)

    self.fc1 = nn.Linear(64*28*28,500)

    self.fc2 = nn.Linear(500,2)

    self.dropout = nn.Dropout(0.3)



def forward(self,x):

    #x = torch.randn(16, 3, 3, 3)

    x = self.pool(F.relu(self.conv1(x)))

    x = self.pool(F.relu(self.conv2(x)))

    x = self.pool(F.relu(self.conv2(x)))

    #flattern the image

    print(x.shape)

    x = x.view(-1,64*28*28)

    x = self.dropout(x)

    x = F.relu(self.fc1(x))

    x = self.dropout(x)

    x = F.relu(self.fc2(x))

    return x
</code></pre>
<p>model = Net()</p>
<p>print(model)</p>
<p>if train_on_gpu:</p>
<pre><code>model.cuda()
</code></pre>
<p>RuntimeError                              Traceback (most recent call last)<br>
 in ()<br>
9             data,target =data.cuda(), target.cuda()<br>
10         optimizer.zero_grad()<br>
—&gt; 11         output = model(data)<br>
12         loss = criterion(output, target)<br>
13         loss.backward()</p>
<p>4 frames<br>
/usr/local/lib/python3.6/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight)<br>
414                             _pair(0), self.dilation, self.groups)<br>
415         return F.conv2d(input, weight, self.bias, self.stride,<br>
–&gt; 416                         self.padding, self.dilation, self.groups)<br>
417<br>
418     def forward(self, input: Tensor) -&gt; Tensor:</p>
<p>RuntimeError: Given groups=1, weight of size [32, 16, 3, 3], expected input[32, 32, 56, 56] to have 16 channels, but got 32 channels instead</p>

username: Usama_Hasan
cooked: <p>Hy <a class="mention" href="/u/janhawi">@Janhawi</a>, have you inserted the batch size batch size before in the input before passing it to the model<br>
input should be like (batch_size,chaneels,width,height)<br>
can you confirm this first.</p>

username: RaLo4
cooked: <p>Here is your mistake. You copied the line but forgot to change the the second <code>conv2</code> to <code>conv3</code></p>
<aside class="quote no-group" data-username="Janhawi" data-post="1" data-topic="94153">
<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/janhawi/48/13373_2.png" class="avatar"> Janhawi:</div>
<blockquote>
<pre><code class="lang-auto">    x = self.pool(F.relu(self.conv2(x)))

    x = self.pool(F.relu(self.conv2(x)))
</code></pre>
</blockquote>
</aside>
<pre><code class="lang-auto">
def forward(self,x):

    #x = torch.randn(16, 3, 3, 3)

    x = self.pool(F.relu(self.conv1(x)))

    x = self.pool(F.relu(self.conv2(x)))

    x = self.pool(F.relu(self.conv2(x)))        # &lt;--- HERE

    #flattern the image

    print(x.shape)

    x = x.view(-1,64*28*28)

    x = self.dropout(x)

    x = F.relu(self.fc1(x))

    x = self.dropout(x)

    x = F.relu(self.fc2(x))

    return x
</code></pre>

username: Janhawi
cooked: <p>Yaa got it …Thank you so much</p>