Convolution Output Size
In one sentence: This formula counts how many spots a small sliding window can land on across an image, so you know how wide a convolution layer’s output will be.
Read it out loud: “O equals the floor of W minus K plus 2 P, all divided by S, then plus 1. The floor means round down to a whole number, dropping any decimal part. O is the output size. W is the width of the input, K is the width of the sliding window, called the kernel, P is the padding added on each side, and S is the stride, how far the window jumps each time. The number 1 counts the very first position.”
Before you start
The ideas and signs this page uses, explained from zero. For powers of ten, Greek letters and other notation in general, see Math basics.
- Pixels and image width
A pixel is one tiny dot of colored light in a picture. An image’s width is counted in pixels, so an image 32 pixels wide has 32 dots in each row. The formula works on one side of the image at a time, so the width is the count along one side.
- Convolution layer and kernel
A neural network is a computer model built from stages called layers. A convolution layer is a layer that works on images. It slides a small grid of numbers, called a kernel, across the image. At each stop, it multiplies the kernel’s numbers by the pixels under it and adds the results. The kernel’s width is written . A kernel covers 3 pixels in each direction.
- Padding
Padding adds extra pixels around the edge of the image before the window slides, usually filled with zeros. Padding of adds one pixel on the left and one on the right, so the image becomes pixels wider in total. The extra edge lets the window reach the border pixels.
- Stride
The stride, written , is how many pixels the window jumps between stops. A stride of 1 moves it one pixel at a time, and a stride of 2 skips every other spot. A bigger stride means fewer stops, so the output becomes smaller.
- The floor, which rounds down,
The floor of a number, written , is the largest whole number that is not bigger than . For a positive number, you drop the decimal part. So , and .
- Feature map
A feature map is the grid of numbers that a convolution layer produces from its input. The output size is the number of values along one side of that grid. The next layer of the network receives the feature map, which is why the sizes must be tracked from layer to layer.
What each symbol means
| Symbol | Meaning |
|---|---|
| The output size, the number of values the layer produces along one side of its output, such as the width. It is a whole number of positions, with no unit. | |
| The input size along the same side, which is the width of the image in pixels. A 32-pixel-wide image has . | |
| The kernel size, the width of the sliding window in pixels. A 3 by 3 kernel has . | |
| The padding, the number of extra pixels added to each side of the input, counted in pixels. adds one pixel on the left and one on the right. | |
| The stride, how many pixels the window jumps between stops, counted in pixels. moves the window one pixel at a time, and moves it two pixels at a time. | |
| The floor of : the largest whole number that is not greater than . It rounds down, so becomes , and any partial jump is dropped. | |
| The number 1, added at the end to count the very first stop, the window’s starting position. It has no unit. |
The formula piece by piece
The input width, the room the layer has to work with, measured in pixels. Every calculation starts from this count.
The room left after the window is placed. Subtracting takes away the window’s own width, so is how far the window can move past its starting position, counted in pixels.
Add the padding. Each side adds pixels, so the total extra room is , and the room the window can use becomes pixels.
Divide by the stride to count the jumps. A jump of pixels uses up pixels of room, so the number of possible jumps is the room divided by . With and , that gives jumps.
Round down. A partial jump is not allowed, because the window would stick out past the edge of the padded image. So the floor keeps only the whole jumps.
The output size equals the number of whole jumps the window can make, plus one for the starting position. That is the whole formula in one sentence.
The same formula with the terms in another order. Textbooks often write it this way, and it gives exactly the same number as the form above.
What the formula is doing, and why
The input is the width of one side of the image, the window width , the padding on each side and the stride . The output is one number, : how many stops the window makes along that side. Each stop becomes one value in the output. The formula counts the stops without sliding the window one stop at a time.
Why ? The window must fit completely inside the padded image. The padded image is pixels wide, and the window covers of them. The room left for the window to move is the padded width minus the window width, which is . That is why padding appears twice: it adds room on both sides.
Why divide, round down and add 1? The stride decides how much room each jump uses, so dividing by counts the jumps. A jump that cannot be finished would stick out past the edge, so the floor drops it. Finally we add 1, because the window also has a first stop at the very start, before any jump is made.
Why do padding and stride matter? Padding keeps the edge pixels in play. With a stride of 1 and an odd kernel width, padding of on each side keeps the output the same size as the input. A stride of 2 makes the window jump two pixels at a time, so the output is about half as wide and half as tall.
When it applies. The formula is for an ordinary convolution with one window size, applied to height and width separately. It does not cover dilated windows, which have gaps between their numbers, or transposed convolutions, which make the image bigger instead of smaller. Those need their own size rules.
An everyday picture
Think of a cardboard frame of fixed width that slides along a shelf, taking a snapshot at each stop. The frame may only stop where it fits entirely on the shelf. Padding is like adding a little extra shelf at each end. The stride is how far you slide between snapshots, and the number of snapshots is the output size.
Worked example: Output width of a 3 by 3 convolution on a 32-pixel image
A small picture, 32 pixels wide, goes into a convolution layer with a kernel, so pixels wide. The layer uses padding of pixel on each side and a stride of . We want the width of the output, in pixels.
- Write down the inputs. The input width is pixels, the kernel width is pixels, the padding is pixel on each side, and the stride is pixels per jump. Every number is a count of pixels, so the answer will also be a count of pixels, with no unit conversion needed.
- Find the room for the window. Add the padding on both sides and subtract the window width: . The padded picture is pixels wide, the window takes 3 of them, and pixels are left for it to move across.
- Divide by the stride to count the jumps. A jump of 2 pixels uses 2 pixels of that room, so the count is jumps. The 0.5 is a partial jump, one that the window cannot finish inside the picture.
- Round down. The floor drops the partial jump: . A window that cannot fit completely is not used, so only 15 whole jumps count.
- Add 1 for the starting position. The window also stands at the very start, before any jump. So . Counting the stops directly agrees: the window starts at pixels 0, 2, 4 and so on up to 30, which is 16 stops.
- Compare with a stride of 1. With the same padding and a stride of 1, the room is still 31, but each jump uses only 1 pixel, so . The output is the same width as the input, which is the usual way padding of 1 with a 3 by 3 kernel keeps the size.
Answer: The output is 16 pixels wide. The stride of 2 roughly halves the width, from 32 down to 16. With a stride of 1 and the same padding, the output would be 32 pixels wide, the same as the input.
The numbers in this example are recomputed by script every time the site is built.
Common mistakes
- Forgetting the floor when the division is not exact. is not a whole number, so round it down to 15 before adding 1.
- Adding when the padding is on one side only. The formula assumes the same padding on both sides, so if only one side is padded, use the padding that is really there.
- Forgetting the . The first stop counts too, so 5 pixels with no padding, a kernel of 3 and a stride of 1 give 3 stops, not 2.
- Using this formula for dilated or transposed convolutions. A dilated window has gaps between its numbers, and a transposed convolution makes the image bigger, so each one needs its own size rule.
Where you will meet it
- Designing image classifiers layer by layer, to track how the height and width shrink through the network.
- Choosing padding so a layer keeps its input size, which is what “same” padding means in many deep learning libraries.
- Pooling layers, which use the same kind of arithmetic with their own window size and stride.
- Checking the shape of a network’s output before training, so that the next layer receives the size it expects.
Questions people ask
What is a kernel?
The small grid of weights that slides over the input. A kernel covers 3 pixels in each direction at every stop.
Does the formula work for height and width separately?
Yes. Apply it to the height and to the width on their own. If the input, kernel, padding and stride are the same in both directions, the output is square.
Why does a stride of 2 roughly halve the size?
Dividing by 2 halves the number of jumps the window can make, so the output is about half as wide and half as tall.
What if the result is not a whole number?
The floor rounds it down. The last partial window does not fit, so the formula does not count it.
Related formulas
- Artificial Neuron (Weighted Sum): An artificial neuron multiplies each input by a weight, adds the results together with one extra number called the bias, and then uses a simple rule to decide how strong its output should be.
- Sigmoid Function: The sigmoid takes any number, however big or small, and squeezes it into a value between 0 and 1 that can be read as a chance, like 0.8 meaning 80 percent.
- Gradient Descent: Gradient descent nudges a model’s settings a small step in the direction that makes the error smaller, then repeats that step again and again until the error stops falling.