Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the Output size in Convolution layer [closed]

How do I calculate the output size in a convolution layer?

For example, I have a 2D convolution layer that takes a 3x128x128 input and has 40 filters of size 5x5.

like image 219
Monk247uk Avatar asked Dec 02 '18 12:12

Monk247uk


People also ask

What is the output of a convolution layer?

The output volume of the convolutional layer is obtained by stacking the activation maps of all filters along the depth dimension. Since the width and height of each filter is designed to be smaller than the input, each neuron in the activation map is only connected to a small local region of the input volume.

What is the size of image after convolution?

For a gray scale (n x n) image and (f x f) filter/kernel, the dimensions of the image resulting from a convolution operation is (n – f + 1) x (n – f + 1). For example, for an (8 x 8) image and (3 x 3) filter, the output resulting after convolution operation would be of size (6 x 6).

What will be the dimensions of the output feature map after Convolving a 32x32 image with a 5x5 filter?

If we have an input image of size 32x32 with 3 channels, and if we choose to use a 5x5 filter, the dimensions of the filter would be 5x5x3 (implicitly).


3 Answers

you can use this formula [(W−K+2P)/S]+1.

  • W is the input volume - in your case 128
  • K is the Kernel size - in your case 5
  • P is the padding - in your case 0 i believe
  • S is the stride - which you have not provided.

So, we input into the formula:

Output_Shape = (128-5+0)/1+1

Output_Shape = (124,124,40)

NOTE: Stride defaults to 1 if not provided and the 40 in (124, 124, 40) is the number of filters provided by the user.

like image 85
The BrownBatman Avatar answered Oct 14 '22 04:10

The BrownBatman


You can find it in two ways: simple method: input_size - (filter_size - 1)

W - (K-1)
Here W = Input size
            K = Filter size
            S = Stride
            P = Padding

But the second method is the standard to find the output size.

Second method: (((W - K + 2P)/S) + 1)
        Here W = Input size
        K = Filter size
        S = Stride
        P = Padding 
like image 39
Ramzan Shahid Avatar answered Oct 14 '22 04:10

Ramzan Shahid


Let me start simple; since you have square matrices for both input and filter let me get one dimension. Then you can apply the same for other dimension(s). Imagine your are building fences between trees, if there are N trees, you have to build N-1 fences. Now apply that analogy to convolution layers.

Your output size will be: input size - filter size + 1

Because your filter can only have n-1 steps as fences I mentioned.

Let's calculate your output with that idea. 128 - 5 + 1 = 124 Same for other dimension too. So now you have a 124 x 124 image.

That is for one filter.

If you apply this 40 times you will have another dimension: 124 x 124 x 40

Here is a great guide if you want to know more about advanced convolution arithmetic: https://arxiv.org/pdf/1603.07285.pdf

like image 1
Sam Oz Avatar answered Oct 14 '22 06:10

Sam Oz