How to calculate the output size after convolving and pooling to the input image
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
When designing convolutional neural networks, output shape calculation is not optional. If one layer shape is wrong, the next layer fails or wastes parameters unexpectedly. The key is to apply the convolution and pooling formulas consistently, layer by layer, with explicit handling of padding, stride, and dilation.
Core Formula for Convolution Output
For one spatial dimension, output size is:
out = floor((in + 2 * pad - dilation * (kernel - 1) - 1) / stride) + 1
For two-dimensional images, apply formula independently to height and width.
inis input height or width.kernelis filter size on that axis.padis zero padding.strideis movement step.dilationis kernel spacing.
If dilation is not used, set dilation = 1.
Convolution Example
Input image size 32 x 32, kernel 3 x 3, stride 1, padding 1, dilation 1.
Height calculation:
out_h = floor((32 + 2*1 - 1*(3-1) - 1)/1) + 1 = 32
Width is identical, so output is 32 x 32.
If this layer has 64 filters, full tensor shape becomes 64 x 32 x 32 in channels-first notation.
Pooling Output Formula
Pooling uses the same structural formula, typically without dilation:
out = floor((in + 2 * pad - kernel) / stride) + 1
For max pooling with kernel 2, stride 2, no padding on 32 x 32 input:
out = floor((32 - 2)/2) + 1 = 16
So pooled output is 16 x 16 per channel.
Stacking Layers Sequentially
Output of one layer becomes input of the next. Example pipeline:
- Input:
3 x 128 x 128 - Conv, kernel
3, stride1, padding1, filters32 - MaxPool, kernel
2, stride2 - Conv, kernel
5, stride1, padding2, filters64 - MaxPool, kernel
2, stride2
Step-by-step:
- After conv1:
32 x 128 x 128 - After pool1:
32 x 64 x 64 - After conv2:
64 x 64 x 64 - After pool2:
64 x 32 x 32
This final spatial size is used to compute flattened input to dense layers.
Helper Function in Python
A small utility avoids repeated manual arithmetic mistakes.
You can call these for both height and width independently.
Framework Validation in PyTorch
Manual formulas should match framework output.
Use this check when building custom architectures with multiple branches.
Common Shape Traps
Even and odd dimensions
When stride does not divide input cleanly, floor operation truncates and may reduce size more than expected.
Padding conventions
Different frameworks may support same and valid padding abstractions. Always confirm exact numeric padding used under the hood.
Dilation surprises
Dilation increases effective receptive field and changes output size. Ignoring dilation in formula is a common bug.
Transposed convolution confusion
Transposed convolution uses different shape formula. Do not reuse standard convolution equation for decoder layers.
Common Pitfalls
A common pitfall is memorizing simplified formulas that omit dilation or the minus one term, then getting off-by-one outputs. Another issue is calculating only spatial size and forgetting channel count changes after convolution filters. Teams also frequently assume pooling keeps shape if stride equals one in all cases, while kernel and padding still matter. Mixing channels-first and channels-last conventions causes silent mistakes in flattened layer sizing. Finally, architecture edits made without recalculating downstream dense layer input dimensions often break model compilation late in the pipeline.
Summary
- Use the full convolution formula including stride, padding, and dilation.
- Apply formulas independently to height and width.
- Pooling shape calculation follows similar floor-based logic.
- Recompute shapes sequentially for every stacked layer.
- Validate manual calculations against framework output early.

