Output channel during Conv2d process

rrz: <p>I understand that during convolution process that using specific kernel size we do operation input(image) * kernel.</p>
<p>then, as i saw the pytorch Conv2d, <div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/3/32060026991d88d82634bd7c182df1854032d3e0.png" data-download-href="https://discuss.pytorch.org/uploads/default/32060026991d88d82634bd7c182df1854032d3e0" title="image"><img src="https://discuss.pytorch.org/uploads/default/original/2X/3/32060026991d88d82634bd7c182df1854032d3e0.png" alt="image" data-base62-sha1="78wJf3TUdRPx1jk8cqz3trMrRv2" width="690" height="61" data-dominant-color="E8E3E4"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">image</span><span class="informations">761×68 7.71 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>i notice that we could get our desired output_channel.<br>
for example:<br>
if i have rgb image so in_channel will be 3 and i could create output channel 15. what happen during this process to get 15 output channel from 3 channel? do we randomize weight in kernel to get 15 channel?</p>

LeviViana: <p>The number of output channels corresponds to the number of convolution filters/kernels you want this layer to have. You should maybe read some tutorials on how convolutions work to get what I mean by kernel, maybe <a href="https://towardsdatascience.com/convolutional-neural-networks-part-1-2aeb17fc208c" rel="nofollow noopener">this one</a> could be helpful.</p>
<p>Here is a little snippet to get a better grasp on how it works in PyTorch:</p>
<pre><code class="lang-python">import torch
batch = torch.rand(16, 3, 100, 100) # N, C, H, W
conv = torch.nn.Conv2d(
		in_channels=3, # RGB channels
		out_channels=7, # Number of kernels
		kernel_size=5, # Size of kernels, i. e. of size 5x5
		stride=1,
		padding=2)
print(conv.weight.size()) # 7 x 5 x 5 x 3 (7 kernels of size 5x5 having 3 of depth)
print(conv(batch).size()) 
</code></pre>
<p>The feature maps of each kernel are concatenated in the second dimension to for a <code>(16, 7, 100, 100)</code> Tensor, this is how you “increase” the number of channels.</p>

rrz: <p>yes, your example is good.<br>
thank you for your response.</p>

Choung_young_jae: <p>This example code helps me a lot <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"> Thanks</p>

adarsh_pandey: <p><a class="mention" href="/u/leviviana">@LeviViana</a> what is N,C,H,W in the second line of your snippet<br>
plz help…</p>

LeviViana: <p>N -&gt; the batch size<br>
C -&gt; Nb of channels<br>
H -&gt; Height<br>
W -&gt; Width</p>
<p>So, to perform inference on a batch of 10 RGB images of size 100 x 200, you’ll have N=10, C=3, H=100, W=200.</p>
<p>Suppose that you performed a 2d-convolution with 128 kernels of size 3x3 and padding=1, you’ll have a feature map of size N=10, C=128, H=100, W=200.</p>

adarsh_pandey: <p><a class="mention" href="/u/leviviana">@LeviViana</a> Thanks for the help,<br>
Regards ,<br>
Adarsh</p>