RuntimeError: Given groups=1, weight[64, 3, 3, 3], so expected input[1, 500, 500, 3] to have 3 channels, but got 500 channels instead

username: malek
cooked: <p>can someone please help me with this error<br>
this is my CNN</p>
<pre><code class="lang-auto">class DnCNN(nn.Module):
    def __init__(self, channels, num_of_layers=17):
        super(DnCNN, self).__init__()
        kernel_size = 3
        padding = 1
        features = 64
        layers = []
        layers.append(nn.Conv2d(in_channels=channels, out_channels=features, kernel_size=kernel_size, padding=padding, bias=False))
    
        layers.append(nn.ReLU(inplace=True))
        for _ in range(num_of_layers-2):
            layers.append(nn.Conv2d(in_channels=features, out_channels=features, kernel_size=kernel_size, padding=padding, bias=False))
            layers.append(nn.BatchNorm2d(features))
            layers.append(nn.ReLU(inplace=True))
        layers.append(nn.Conv2d(in_channels=64, out_channels=3, kernel_size=kernel_size, padding=padding, bias=False))
        self.dncnn = nn.Sequential(*layers)
    def forward(self, x):
        out = self.dncnn(x)
        return out

</code></pre>
<p>this is a part of the training</p>
<pre><code class="lang-auto"> # Build model
    net = DnCNN(channels=3, num_of_layers=opt.num_of_layers)
    net.apply(weights_init_kaiming)
    criterion = nn.MSELoss(size_average=False)
</code></pre>
<p>the training code runs normally and it outputs an error only <strong>at the end of the first epoch</strong><br>
saying<br>
RuntimeError: Given groups=1, weight[64, 3, 3, 3], so expected input[1, 500, 500, 3] to have 3 channels, but got 500 channels instead<br>
as you can see i dont have any number of channels set to 500 and still it outputs this error…</p>

username: ptrblck
cooked: <p>Based on the error message it seems like you are passing an image tensor in channel-last ordering (<code>[batch_size, w, h, c]</code>), while PyTorch expects channel-first inputs as <code>[batch_size, c, h, w]</code>.<br>
Could you check, if all your inputs have this shape?</p>

username: malek
cooked: <p>Yes that was the problem and I fixed it by resizing the input, thank you very much for your reply</p>

username: ptrblck
cooked: <p>Good to hear it’s working.<br>
However, I’m a bit afraid when you say “resizing the input”, as a <code>view</code> or <code>reshape</code> won’t yield the desired output.<br>
To swap the axes, use <code>permute</code>, if you haven’t already done so.</p>

username: malek
cooked: <p>my input’s shape is <code>[500,500,3]</code>, and since, as you said pytorch expects <code>[batch_size,c,h,w]</code> I resized the image by using <code>np.resize(img,(3,500,500))</code> and then I converted it into a tensor by using <code>torch.from_numpy(img)</code> and then I applyed <code>torch.unsqueeze(img,0)</code>, will this give the desired output ? or is there a problem?</p>

username: ptrblck
cooked: <p>Yeah, that might be problematic.<br>
Have a look at this small example:</p>
<pre><code class="lang-python">x = np.zeros((500, 500, 3))
x[200:300, 200:300, :] = 1.
plt.imshow(x)

x_reshaped = np.resize(x, (3, 500, 500))
print(x_reshaped.shape)
&gt; (3, 500, 500)
print(x_reshaped.sum(1).sum(1))
&gt; [    0. 30000.     0.]

x_transposed = x.transpose(2, 0, 1)
plt.imshow(x_transposed[0])
print(x_transposed.sum(1).sum(1))
&gt; [10000. 10000. 10000.]
</code></pre>
<p>As you can see, the reshape operation will put all ones into the second channel, which is wrong.<br>
Use <code>transpose</code> in numpy or <code>permute</code> in pytorch instead.</p>

username: malek
cooked: <p>Okay thank you very much for your help</p>