Runtime Error: Given groups=1, weight of size 32 32 3 3, expected input[32, 3, 178, 178] to have 32 channels, but got 3 channels instead

username: pedro
cooked: <p>I’m new to Pytorch and trying to implement the BKNet network from the article “Facial smile detection using convolutional neural networks”.</p>
<p>I wrote the following Neural network code:</p>
<pre><code class="lang-auto">model = nn.Sequential(nn.Conv2d(32, 32, kernel_size=(3,3), stride=1),
                      nn.Conv2d(32, 32, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Conv2d(64, 64, kernel_size=(3,3), stride=1),
                      nn.Conv2d(64, 64, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Conv2d(128, 128, kernel_size=(3,3), stride=1),
                      nn.Conv2d(128, 128, kernel_size=(3,3), stride=1),
                      nn.Conv2d(128, 128, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Conv2d(256, 256, kernel_size=(3,3), stride=1),
                      nn.Conv2d(256, 256, kernel_size=(3,3), stride=1),
                      nn.Conv2d(256, 256, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Linear(256,256),
                      nn.Linear(256,256),
                      nn.ReLU(),
                      nn.Linear(256,2),
                      nn.LogSoftmax(dim=1))
</code></pre>
<p>And trained it using the training code <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" rel="nofollow noopener">here</a> and an image database with many images of size 178x218, which are transformed in the following way:</p>
<pre><code class="lang-auto">data_transforms = {
    'train': transforms.Compose([
        transforms.Resize(178),
        transforms.CenterCrop(178),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
    ]),
    'val': transforms.Compose([
        transforms.Resize(178),
        transforms.CenterCrop(178),
        transforms.ToTensor(),
        transforms.Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5])
    ]),
}
</code></pre>
<p>But I’m getting the following error:</p>
<p>RuntimeError: Given groups=1, weight of size 32 32 3 3, expected input[32, 3, 178, 178] to have 32 channels, but got 3 channels instead</p>
<p>What have I done wrong?</p>

username: pedro
cooked: <p>Think I fixed it by changing the input of the first convolutional layer of each block to have the same number as the output of the last convolutional layer of the previous block.</p>

username: pedro
cooked: <p>Now getting this error:</p>
<p>RuntimeError: size mismatch, m1: [163840 x 5], m2: [256 x 256] at /pytorch/aten/src/TH/generic/THTensorMath.cpp:197</p>
<p>Could someone provide some help?</p>

username: ptrblck
cooked: <p>This error is most likely thrown, since you are not including a flatten operation after your last pooling layer. Add <code>nn.Flatten</code> right before your <code>nn.Linear</code> and run the code again.</p>

username: pedro
cooked: <p>I tried it but I’m still getting the same error <img src="https://discuss.pytorch.org/images/emoji/apple/frowning.png?v=9" title=":frowning:" class="emoji" alt=":frowning:"></p>

username: Eta_C
cooked: <ol>
<li>As <a class="mention" href="/u/ptrblck">@ptrblck</a> said, one of your error is that you should add <code>nn.Flatten</code> before <code>nn.Linear</code>.</li>
<li>Another, change first line<br>
<code>model = nn.Sequential(nn.Conv2d(32, 32, kernel_size=(3, 3), stride=1),</code><br>
to<br>
<code>model = nn.Sequential(nn.Conv2d(3, 32, kernel_size=(3, 3), stride=1),</code><br>
as your input is a tensor with 3 channels.</li>
</ol>

username: pedro
cooked: <p>I changed it to this:</p>
<pre><code class="lang-auto">model = nn.Sequential(nn.Conv2d(3, 32, kernel_size=(3,3), stride=1),
                      nn.Conv2d(32, 32, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Conv2d(32, 64, kernel_size=(3,3), stride=1),
                      nn.Conv2d(64, 64, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Conv2d(64, 128, kernel_size=(3,3), stride=1),
                      nn.Conv2d(128, 128, kernel_size=(3,3), stride=1),
                      nn.Conv2d(128, 128, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Conv2d(128, 256, kernel_size=(3,3), stride=1),
                      nn.Conv2d(256, 256, kernel_size=(3,3), stride=1),
                      nn.Conv2d(256, 256, kernel_size=(3,3), stride=1),
                      nn.MaxPool2d(kernel_size=(2,2), stride=2),
                      nn.ReLU(),
                      nn.Flatten(),
                      nn.Linear(256,256),
                      nn.Linear(256,256),
                      nn.ReLU(),
                      nn.Linear(256,2),
                      nn.LogSoftmax(dim=1))
</code></pre>
<p>But still getting this error:</p>
<p>RuntimeError: size mismatch, m1: [32 x 25600], m2: [256 x 256] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290</p>

username: Eta_C
cooked: <pre><code class="lang-python">nn.MaxPool2d(kernel_size=(2, 2), stride=2),  # output shape: [N, C, H, W]
nn.ReLU(),  # output shape: [N, C, H, W]
nn.Flatten(),  # output shape: [N, C * H * W]
nn.Linear(256 * 10 * 10, 256),  # set num_features to C * H * W
nn.Linear(256, 256),
</code></pre>

username: pedro
cooked: <p>That fixed that error, but now getting this one:</p>
<p>RuntimeError: size mismatch, m1: [32 x 6400], m2: [25600 x 256] at /pytorch/aten/src/THC/generic/THCTensorMathBlas.cu:290</p>
<p>I’m quite new to Pytorch and CNNs, so could you point me towards a resource that walks me through what the shape of each layer should be and what each component is?</p>

username: Eta_C
cooked: <p>if your input is (N, 3, 178, 178),</p>
<p>nn.Linear(256 * 10 * 10, 256)<br>
→<br>
nn.Linear(256 * 5 * 5, 256)</p>

username: pedro
cooked: <p>It works now! Thank you so much! <img src="https://discuss.pytorch.org/images/emoji/apple/relaxed.png?v=9" title=":relaxed:" class="emoji" alt=":relaxed:"></p>