Explaination of Conv2d

ragas: <p>Hi,</p>
<p>I was trying to follow <a href="https://github.com/vinhkhuc/PyTorch-Mini-Tutorials/blob/master/5_convolutional_net.py" rel="nofollow noopener">this tutorial</a>. But I’m not fully understanding the following section of the code. The code used mnist data, of size 28 x 28 x 1 &amp; 10 classes.</p>
<pre><code>self.conv = torch.nn.Sequential()
        self.conv.add_module("conv_1", torch.nn.Conv2d(1, 10, kernel_size=5))
        self.conv.add_module("maxpool_1", torch.nn.MaxPool2d(kernel_size=2))
        self.conv.add_module("relu_1", torch.nn.ReLU())
        self.conv.add_module("conv_2", torch.nn.Conv2d(10, 20, kernel_size=5))
        self.conv.add_module("dropout_2", torch.nn.Dropout())
        self.conv.add_module("maxpool_2", torch.nn.MaxPool2d(kernel_size=2))
        self.conv.add_module("relu_2", torch.nn.ReLU())

        self.fc = torch.nn.Sequential()
        self.fc.add_module("fc1", torch.nn.Linear(320, 50))
        self.fc.add_module("relu_3", torch.nn.ReLU())
        self.fc.add_module("dropout_3", torch.nn.Dropout())
        self.fc.add_module("fc2", torch.nn.Linear(50, output_dim))
</code></pre>
<p>In the code, we are having 2 convolution layer followed by 1 fully connected layer. After each convolution layer we have 1 max pooling and 1 ‘relu’.</p>
<p>But my questions are the following:</p>
<ol>
<li>Why we are having 10 in first convolution layer? Generally output of first conv layer is given by <code>(W−F+2P)/S+1</code>.</li>
<li>In second conv layer we have output as 20? Again why we are using it.</li>
<li>In the fully connected layer, we have 320 &amp; 50 as input and output respectively. From where we are getting these values.</li>
</ol>
<p>I’m completely new to pytorch. So having difficulty in understanding simple things. The documentations are always not very clear.</p>
<p>If someone can answer these questions, then it will be very helpful.</p>
<p>Thank you!</p>

ImgPrcSng: <aside class="quote no-group" data-username="ragas" data-post="1" data-topic="8082">
<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/r/7c8e57/48.png" class="avatar"> ragas:</div>
<blockquote>
<p>Why we are having 10 in first convolution layer? Generally output of first conv layer is given by (W−F+2P)/S+1.</p>
</blockquote>
</aside>
<p>You seem to be confusing Spatial output vs Channel Output. The number 10 and 20 represent channels. The spatial output size is calculated from the formula you mentioned (if you have dilation factor, that is also considered in pytorch)<br>
Sequenctially this is what is happening</p>
<ul>
<li>(28x28x1) → conv1 → (28-5+1) → (24x24x10)</li>
<li>(24x24x10) -&gt;max1 → (24/2) → (12x12x10)</li>
<li>(12x12x10) -&gt;conv2 → (12-5+1) → (8x8x20)</li>
<li>(8x8x20) → max2 → (8/2) → (4 x 4 x 20)</li>
<li>(4x4x20) = 320 → Linear → 50</li>
<li>50 → Linear → output_dim</li>
</ul>

ragas: <p><a class="mention" href="/u/imgprcsng">@ImgPrcSng</a>: Thanks a lot for answering my question. I almost lost my hope that I get the answer to this simple question. But can you suggest me some literature on this difference between  “Spatial output vs Channel Output”. Also, how to implement spatial method in pytorch. I’m coming from tensorflow world. So I can relate things with that world much more comfortably.</p>
<p>Thank you!</p>

ImgPrcSng: <p>Pytorch Convolutions are no different from Tensorflow Convolutions. It is just a notation difference</p>
<p>Consider an image Ci x H x W, Where Ci is referred as <strong>input channels</strong> in the image, H and W are <strong>input spatial dimensions</strong>(width and height).</p>
<p>Lets say you want to convolve this image with a kernel of size <strong>K x K</strong> (lets keep stride =1  and padding = 0) to produce Co (<strong>output channels</strong>) Feature maps.</p>
<p>Thus the size of Convolution Kernel will be Co x Ci x K x K.<br>
The operation produces <em>Co x Ho x Wo</em>, where <em>Ho = ( H - K + 1), Wo = ( W - K + 1)</em><br>
Co refers the number of feature maps (output channels) and Ho,Wo refer to the output spatial dimension, calculated using the same formula.</p>
<p>The choices we have are <code>Co, K, stride, padding, dilation</code> while defining a Conv Layer. The remaining information is either calculated(<em>Wo, Ho</em>) or fixed in the input (<em>Ci, H, W</em>)</p>

ragas: <p>Thank you Prasanna! I requested few literature, but gave me the complete answer.</p>
<p>Thanks a lot!</p>

Katarina_Vuckovic: <p>what is the input channel?</p>

ptrblck: <p><code>in_channels</code> define the number of input channels for this particular convolution.<br>
By default each kernel will use all input channels of the incoming activation and perform the convolution in the spatial dimensions.<br>
<a href="https://cs231n.github.io/convolutional-networks/#conv">CS231n - CNN</a> gives a good explanation of the general work flow on convolutions.</p>
<p>Let me know, if something is unclear or if I misunderstood your question.</p>

venuv: <aside class="quote no-group" data-username="ragas" data-post="1" data-topic="8082">
<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/r/7c8e57/48.png" class="avatar"> ragas:</div>
<blockquote>
<p>this tutorial</p>
</blockquote>
</aside>
<p>in nn.Sequential blocks like these (where the Conv layers are represented in terms of channel input/output) – how is the spatial input to such a block specified. Or is that implicit. TIA</p>

ptrblck: <p>Conv and pooling layers work on variable spatial input shapes as long as it’s larger than the kernel size.<br>
Conv layers only need the definition of the input channels (defined by the activation input), output channels (number of filters), as well as the kernel size.<br>
Let me know, if I misunderstood your question.</p>

venuv: <p>thanks, the explanation helps me understand why layer definitions don’t specify input shapes (thus enabling them to be applied to variable input shapes). are the shapes are just derived from the data that is passed, rather than being passed as an explicit parameter?</p>

ptrblck: <p>Yes, the spatial output size depends on the passed inputs size, the kernel size, padding, stride and the dilation. <a href="https://arxiv.org/abs/1603.07285">This paper</a> gives more details about the conv arithmetic and the <a href="https://pytorch.org/docs/stable/generated/torch.nn.Conv2d.html#torch.nn.Conv2d">docs for <code>nn.Conv2d</code></a> give you the applied formula.</p>