Problem using conv2d - wrong tensor input shape

costa98: <p>I need to forward a tensor [1, 3, 128, 128] representing a 128x128 rgb image into a</p>
<pre><code class="lang-auto">nn.Conv2d(128,128,1)
</code></pre>
<p>but I get the error</p>
<pre><code class="lang-auto">RuntimeError: Given groups=1, weight of size [128, 128, 1, 1], expected input[1, 3, 128, 128] to have 128 channels, but got 3 channels instead
</code></pre>
<p>PS: the image tensor is shaped as a 4d because the previous step is a nn.UpsamplingBilinear2d(size=None, scale_factor=2) which needs a 4d tensor as input</p>

Nikronic: <p>Hi,</p>
<aside class="quote no-group" data-username="costa98" data-post="1" data-topic="87615">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/letter_avatar_proxy/v4/letter/c/90ced4/48.png" class="avatar"> costa98:</div>
<blockquote>
<p><code>nn.Conv2d(128,128,1)</code></p>
</blockquote>
</aside>
<p>There is no way to use such a convolution as input channels does not match. Or do you mean the resulting output should be in size <code>[1, 128, 128]</code>?</p>

costa98: <p>Yes, the output should be an image, thats why i set the parameters in_channel and out_channel as 128, but I can’t understand why the expected input is basically inverted.<br>
Edit: using (3,128,1) fixed the problem but should i change also out_channels to 3 if i need a 128x128 rgb image or is it correct to leave it as 128?<br>
I’m not sure that I have correctly understood how conv2d works</p>

Nikronic: <p>You misunderstood channels with sizes. In Conv2d, you define input/output channel and kernel size and some arbitrary args like padding, not output size. Output size will be determined using kernel_size, stride, padding, etc.<br>
<a href="https://pytorch.org/docs/master/generated/torch.nn.Conv2d.html" class="onebox" target="_blank" rel="noopener nofollow ugc">https://pytorch.org/docs/master/generated/torch.nn.Conv2d.html</a></p>
<p>An image in PyTorch has thre dimensions [channel, height, width]. So for a RGB image, [3, height, width]. If you want to get a 3 channel image as the result, you need to use a convolution that takes images with same channel size of your input which is 3, and 3 channels as the output, <code>nn.Conv2d(3, 3, kernel_size)</code> where <code>kernel_size</code> is the arbitrary size for filters.</p>
<p>I have explained a little about convs in this thread, I hope it helps. Also, there are alot of great demonstrations in youtube, etc. The only thing you need to be careful about is that PyTorch is channel first which means [channel, height, width] meanwhile numpy is [height, width, channel].</p><aside class="quote quote-modified" data-post="1" data-topic="86954">
  <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/wesley_neill/48/25709_2.png" class="avatar">
    <a href="https://discuss.pytorch.org/t/why-add-an-extra-dimension-to-convolution-layer-weights/86954">Why add an extra dimension to convolution layer weights?</a> <a class="badge-category__wrapper " href="/c/vision/5"><span data-category-id="5" data-drop-close="true" class="badge-category " title="Topics related to either pytorch/vision or vision research related topics"><span class="badge-category__name">vision</span></span></a>
  </div>
  <blockquote>
    In the following sample class from Udacity’s PyTorch class, an additional dimension must be added to the incoming kernel weights, and there is no explanation as to why in the course. I’ve highlighted this fact by the multi-line comment in __init__: 

class Net(nn.Module):
    """
    Network containing a 4 filter convolutional layer and 2x2 maxpool layer. 
    """
    def __init__(self, weights):
        """
        weights: the kernel values as a tensor (n_kernels, 1, k_height, k_width).
      …
  </blockquote>
</aside>