Padding in CNN
Without any special treatment, applying a convolutional filter to an image reduces the spatial dimensions of the output because the filter cannot be center...
Why Padding Exists
Without any special treatment, applying a convolutional filter to an image reduces the spatial dimensions of the output because the filter cannot be centered on pixels at the edge of the image. With a 3-by-3 filter and no padding, a 28-by-28 input produces a 26-by-26 output. Stack ten such layers and the output shrinks by 20 pixels in each dimension, which can eliminate useful edge information and limits how deep the network can be before the feature maps become too small.
Padding solves this by artificially extending the input around its border before applying the filter, allowing the filter to be centered on every original pixel. The most common choice is zero padding, where the border is filled with zeros, but other padding strategies such as reflection padding or replication padding copy values from the image border instead.
Exam tip: The term same padding refers to the amount of zero padding needed to make the output the same spatial size as the input when using stride one. For a kernel of size k, the required padding on each side is the floor of k divided by two. Be able to derive this from first principles rather than memorizing the result.
Valid, Same, and Full Padding Modes
The three standard padding modes correspond to different output sizes. Valid padding means no padding is applied, and the output is strictly smaller than the input. Same padding adds just enough zeros to keep the output the same size as the input when stride is one. Full padding adds enough zeros so that the filter visits every position where it overlaps at least one input pixel, producing an output larger than the input.
In most deep learning frameworks, valid and same are the two commonly used modes. Full padding is more common in signal processing contexts. When building a network that needs to maintain spatial dimensions through many layers, applying same padding consistently throughout the encoder makes reasoning about feature map sizes straightforward, because each layer's output has the same height and width as its input.
Architectures designed for generative tasks often mix valid padding in downsampling layers with reflection padding in upsampling layers, because reflection padding avoids the artificial edge effects introduced by zero padding in tasks where output quality at image borders matters.
Padding and Border Effects in Practice
Zero padding introduces an implicit signal to the network: the zeros at the border are not part of the actual data. Research has shown that CNNs can sometimes use this information to infer absolute position within the image, which is occasionally helpful for tasks like scene understanding but undesirable when translation invariance is important. Reflection padding reduces this effect by extending the input with plausible pixel values.
For tasks involving high-resolution outputs where edge quality matters, such as super-resolution or style transfer, the choice of padding strategy can produce visible artifacts. A network trained with zero padding may produce slightly blurred or ringing artifacts near image boundaries, while reflection padding tends to produce cleaner transitions.
When explaining padding choices in an interview, connecting the technical mechanism to the downstream quality implication demonstrates applied knowledge. A concise answer would note the trade-off between implementation simplicity for zero padding and output quality for reflection or replication padding.