Calculate the output feature map shape of a convolutional layer given kernel and stride.
Every convolutional or pooling layer transforms spatial dimensions according to: output_size = floor((input_size − kernel_size + 2 × padding) / stride) + 1. This formula applies independently to height and width, so square inputs/kernels give square outputs, while rectangular inputs require running the calculation separately for each dimension. Padding compensates for the kernel 'eating into' the input at the borders (with 'same' padding chosen so output size matches input size at stride 1), while stride greater than 1 downsamples the spatial resolution, commonly used to reduce feature map size progressively through a network.
output_size = floor((input_size - kernel_size + 2 * padding) / stride) + 1
For stride 1, choosing padding = (kernel_size − 1) / 2 (when kernel_size is odd) keeps the output size equal to the input size, which is the common 'same' padding convention in CNN architectures.
The result is floored — any 'leftover' pixels at the edge that don't form a complete kernel window are simply dropped, which is standard behavior for convolution operations in frameworks like PyTorch and TensorFlow.
Apply the same formula separately using the height and its corresponding kernel/padding/stride values, then again for the width, since the two spatial dimensions are computed independently.
Yes, pooling layers use the identical output-size formula as convolutions, just without learnable weights — a MaxPool with kernel=2, stride=2, padding=0 uses this same calculation to halve spatial dimensions.