93

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.

Mateen Ulhaq
  • 24,552
  • 19
  • 101
  • 135
Monk247uk
  • 1,170
  • 1
  • 8
  • 15
  • 2
    I’m voting to close this question because it is not about programming as defined in the [help] but about ML theory and/or methodology - please see the intro and NOTE in the `machine-learning` [tag info](https://stackoverflow.com/tags/machine-learning/info). – desertnaut Sep 30 '21 at 09:40

5 Answers5

170

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.

The BrownBatman
  • 2,593
  • 1
  • 13
  • 29
  • 3
    Further reading: https://en.wikipedia.org/wiki/Convolutional_neural_network#Convolutional_layer – Happypig375 Dec 02 '18 at 12:32
  • 2
    what if the calculated size wasn't an integer number? how should the number be rounded? – asalimih Jun 24 '20 at 21:01
  • 3
    @asalimih i just ran a small test and it seems to round down in my case. Feel free to create a model with an input shape of 224 and replicate! – The BrownBatman Jul 12 '20 at 23:52
  • Doesn't the number of input channels have an effect? – r4bb1t Oct 13 '20 at 17:57
  • 1
    @PyWalker2797 afaik it doesnt as the way the operations are done on the input plane is for each channel, no matter the number of input channels. – The BrownBatman Oct 13 '20 at 22:34
  • 6
    The square brackets "[ ]" should in fact be the [floor function](https://en.wikipedia.org/wiki/Floor_and_ceiling_functions) – Yanfeng Liu Dec 16 '20 at 21:44
  • @asalimih - [In this wiki link](https://en.wikipedia.org/wiki/Convolutional_neural_network#Spatial_arrangement) it says: *"If this number is not an integer, then the strides are incorrect and the neurons cannot be tiled to fit across the input volume in a symmetric way."* – Alaa M. Jun 25 '21 at 07:03
  • Thank you. Is there a proof for this formula please? – Avv Feb 25 '22 at 06:42
  • Shoudnt it be: Math.roundUp(W−K+2P+1/S) ? – Trevor Blythe Jun 12 '22 at 16:54
17

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 
Abinav R
  • 365
  • 2
  • 16
Ramzan Shahid
  • 167
  • 1
  • 4
  • 1
    For other readers, you can do a [WolframAlpha computation of this formula](https://www.wolframalpha.com/input/?i=floor%5B%28W%2B2*p+-+1*%28k-1%29+-+1%29%2FS%2B1%5D+for+W%3D256%2C+p%3D1%2C+k%3D%7B2%2C3%2C4%7D%2C+S%3D2) to quickly check the effect of some of these parameters. – Erik Jul 12 '21 at 10:44
3

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

Sam Oz
  • 106
  • 6
0

Formula : n[i]=(n[i-1]−f[i]+2p[i])/s[i]+1

where,

n[i-1]=128

f[i]=5

p[i]=0

s[i]=1

so,

n[i]=(128-5+0)/1+1 =124

so the size of the output layer is: 124x124x40 Where '40' is the number of filters

Rahul Verma
  • 2,988
  • 2
  • 11
  • 26
0

(124*124*3)*40 = 1845120 width = 124 height = 124 depth = 3 no. of filters = 40 stride = 1 padding = 0