3
$\begingroup$

In my CNN architecture for binary classification, I have 2 convolutional layers, 2 maxpooling layers, 2 batchnormalization operations, 1 RELu and 1 fullyconnected layer.

Case1: When the number of channels, $d=1$:

In the first layer an input of size $[28*28*d]$, $d=1$ channel is convolved with $M_1=20 $ number of filters applied over all the input channels of size {$f_h \times f_w \times d$} = $[3\times 3\times 1]$ having the step size (stride) as 1 that creates a feature map of size ${(h-f_h+1) \times (w - f_w +1)\times d \times M_1} = (28-3 +1)\times(28-3+1)\times 10 = [26\times 26\times 20]$.

The second convolutional layer contains twice the number of filters = 40 of same size $[3\times 3 \times 1]$. So, the number of parameters becomes $[23 * 23 * 1 * 40]$ as the output from the second convolutional layer. So total number of parameters = $[26\times 26\times 20]+ [23 * 23 * 40]$

Case 2: When $d=2$ and all other sizes are same. The filter size become $[3 \times 3 \times 2]$. The output of the first convolutional layer will contain: $(28-3 +1)\times(28-3+1)\times 2 \times 20 = [25\times 25\times 40]$. For the second convolutional layer, the output will contain $[23 \times 23 \times 2 \times 40]$ parameters.

Question1) Is my calculation for each case above correct?

Question2) I have read that the purpose of maxpooling is to reduce the dimensionality of the feature map. In my case each maxpooling is of size 3 and stride 2. What does it mean by reducing the dimensionality and then what will be the size of the output from each layer upon considering that there is a maxpooling operation after each convolutional layer?

$\endgroup$
  • $\begingroup$ In the title you mention output size but then in the text in some cases you talk about the number of parameters instead. Can you clarify whether your question is about output size or the number of parameters? $\endgroup$ – Sammy Jan 12 at 10:26
  • $\begingroup$ I want to know thoroughly the details and I guess output size and the number of parameters may be linked. Therefore, I would like to know both the information. Thank you. $\endgroup$ – Sm1 Jan 13 at 0:19
  • $\begingroup$ Do your conv. layers apply padding? $\endgroup$ – Sammy Jan 13 at 15:30
1
$\begingroup$

Question 1) Is my calculation for each case above correct?

No, it is not correct. The formula to calculate the spatial dimensions (height and width) of a (square shaped) convolutional layer is

$$O = \frac{I - K + 2P}S + 1$$ with $I$ being the spatial input size, $K$ being the kernel size, $P$ the padding and $S$ the stride. For your two cases that is (assuming padding of $1$ since otherwise numbers won't fit later with pooling):

Case 1:

  • Layer 1: The spatial dimensions are $O_1 = \frac{28 - 3 + 2\cdot1}1 + 1 = 28$. So the total layer has size $height \cdot width \cdot depth = 28 \cdot 28 \cdot 20$.
  • Layer 2: The second layer has $O_2 = \frac{28 - 3 + 2\cdot1}1 + 1 = 28$ with a total layer size of $height \cdot width \cdot depth = 28 \cdot 28 \cdot 40$.

With regards to parameters please note that does not refer to the size of a layer. Parameters are the variables which a neural net learns, i.e. weights and biases. There are $1 \cdot 20 \cdot 3 \cdot 3$ weights and $20$ biases for the first layer. And $20\cdot 40 \cdot 3 \cdot 3$ weights and $40$ biases for the second layer.

Case 2:

The sizes of the convolutional layer do not depend on the number of input channels. However, the number of parameters for the first layer increases to $2 \cdot 20 \cdot 3 \cdot 3$ while the number of biases remains unchanged. The second convolutional layer is not affected at all.


Question2) I have read that the purpose of maxpooling is to reduce the dimensionality of the feature map. In my case each maxpooling is of size 3 and stride 2. What does it mean by reducing the dimensionality and then what will be the size of the output from each layer upon considering that there is a maxpooling operation after each convolutional layer?

Typically convolutional layers do not change the spatial dimensions of the input. Instead pooling layers are used for that. Almost always pooling layers use a stride of 2 and have size 2x2 (i.e. the pooling does not overlap). So your example is quite uncommon since you use size 3x3.

You can apply the same formula as above (assuming padding again - see footnote (1) for an explanation)

$O_{pool\text{ }1} = \frac{28 - 3 + 2\cdot0.5}2 + 1 = 14$

Since now the input to the second conv. layer has different spatial dimensions you would get:

$O_{CNN\text{ }2} = \frac{14 - 3 + 2\cdot1}1 + 1 = 14$

And finally:

$O_{pool\text{ }2} = \frac{14 - 3 + 2\cdot0.5}2 + 1 = 7$


Footnotes:

(1) The padding of $0.5$ is the average padding required in each spatial dimension. As an example, have a look at how the second maxpooling kernel scans an input (I have chosen the second one since the input is smaller and, therefore, easier to visualize). The input has width and height of $14$:

Maxpool layer 2

As you can see from the drawing the actual padding is only required on one side for height and width respectively. Therefore, the actually performed padding is $$padding_{width} = \frac{padding_{left} + padding_{right}}{2}=\frac{0 + 1}{2}=0.5$$

And the same numbers apply to the height. Accordingly, the actual padding is $(0.5, 0.5)$.

Moreover, the drawing shows 7x7 dark blue squares (kernel centers). Which visualizes the calculations leading to an output of $pool_2$ with spatial dimensions of 7x7 ($O_{pool\text{ }2}=7$).

| improve this answer | |
$\endgroup$
  • $\begingroup$ Thank you for correcting me and for your answer. You asked if I have set the padding to some variable or not, I don't know what the value is since I set it to same which means in Matlab that the size is calculated at training so that the output has the same size as the input when the stride equals 1. So is it possible to know what the padding is? Another question I had from your answer is during calculation of the maxpooling how did you get 0.5 (O_pool1) ? $\endgroup$ – Sm1 Jan 13 at 17:11
  • $\begingroup$ The stride size in the convolutional2D step is default [1 1] and the stride = 2 in the maxpooling step. $\endgroup$ – Sm1 Jan 13 at 17:34
  • $\begingroup$ @Sm1 With regards to the conv. layers it is consistent with my calculations then. Since the spatial dimensions are not supposed to change (matlab will add the required padding to keep the spatial dimensions - just as I did in my calculations). With regards to your question on the padding of the pooling layers I have added an explanation as a footnote. $\endgroup$ – Sammy Jan 13 at 18:03
  • $\begingroup$ Thanks for the update. I have understood a lot except how to know what will the fully connected layer size or its input and output size be for this example? $\endgroup$ – Sm1 Jan 13 at 18:28
  • 1
    $\begingroup$ @Sm1 For the linear layers at the end the conv. layer outputs need to be flattened. After going through the two conv. and the two pooling layers you will have an output sized $7 \cdot 7 \cdot 40$ (depth $40$ from the second conv. layer; height and width $7$ from the second pooling layer). So you need to feed your final layers a one-dimensional vector of size $7 \cdot 7 \cdot 40 = 1960$ (depending on your implementation it might also be considered a 1960x1 matrix). The important conceptual point is that the multidimensional outout from previous layer is flattened. $\endgroup$ – Sammy Jan 13 at 18:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.