Modules / Deep Learning / Convolutional Layer

Padding in CNN

Visualize how adding a border of zeros (padding) preserves spatial dimensions and edge information during convolution.

Padding Amount (P)

Output Size Formula

O = (W - K + 2P) / S + 1
O = (8 - 3 + 2(0)) / 1 + 1 = 6

Filter Kernel (K = 3)

Understanding Padding in CNNs

Why do we surround our images with zeros? Solving the shrinking problem and preserving edge information.

The Shrinking Problem

In a standard convolutional layer, a kernel (like a 3x3 grid) slides across an image. However, the center of a 3x3 kernel cannot be placed on the absolute edge pixel of the input image without the rest of the kernel "falling off" the edge. Therefore, the kernel must start one pixel inward.

The result? The output feature map is smaller than the input image. If you pass an 8x8 image through a 3x3 kernel, you get a 6x6 output. If you stack 10 of these layers in a deep neural network, your image shrinks rapidly to nothing! This is called Valid convolution.

The Solution: Zero-Padding

To fix this, we artificially expand the original image by adding a border of zeros around it before applying the convolution. This is called Padding.

By adding a 1-pixel border of zeros (Padding = 1), our 8x8 image becomes 10x10. Now, when the 3x3 kernel slides over it, the output feature map is exactly 8x8. Because the input and output dimensions are the same, this specific padding amount is commonly referred to as "Same" Padding.

Guided Experiments with This Interactive

  1. Observe "Valid" Shrinkage (P=0):

    Make sure Padding is set to P = 0. Look at the Output Feature Map on the right. Notice it is physically smaller (6x6) than the base 8x8 input. Click Slide Window and watch how the kernel box is restricted; it can never reach the very corners of your drawing.

  2. Switch to "Same" Padding (P=1):

    Select P = 1. Watch the Input canvas dynamically expand. A dark border containing "0"s appears around your shape. The Output canvas now perfectly matches your base 8x8 dimension! Click Slide Window again. Notice the kernel now beautifully sweeps over the entire base image, centering on the extreme edge pixels by borrowing space from the padded zeros.

  3. The "Information Loss" Test:

    Set padding to P = 0. Draw a bright dot in the absolute top-left corner of the input. Notice how weakly it registers in the output feature map (because the kernel only ever touches it with its bottom-right corner). Now switch to P = 1. The corner dot is now proudly represented in the output, because the kernel was able to center directly on top of it. Padding prevents information loss at the borders!

  4. Check the Math:

    Look at the formula box in the center. Play with the padding radio buttons and watch the math update in real time. For a kernel ($K$) of size 3, $P=1$ is required to maintain the dimension. If we used a 5x5 kernel, we would need $P=2$ to achieve "Same" padding.

The Universal Formula

The dimension of the output feature map ($O$) can always be calculated using:

O = [(W - K + 2P) / S] + 1
  • W = Input width/height
  • K = Kernel size
  • P = Padding amount
  • S = Stride (step size, default 1)

Key Takeaways

  • Valid Padding (P=0): No padding. The image shrinks. Only pixels where the kernel fits entirely are computed.
  • Same Padding: Padding is added so the output size matches the input size. For K=3, P=1. For K=5, P=2.
  • Padding solves the shrinking problem in deep networks.
  • Padding ensures edge pixels are given equal processing weight as center pixels.

By manipulating padding, machine learning engineers control the spatial dimensions flowing through a neural network's architecture.

Cheat sheet

Padding in CNN

In a standard convolutional layer, a kernel (like a 3x3 grid) slides across an image. However, the center of a 3x3 kernel cannot be placed on the absolute edge pixel of the input image without the rest of the kernel "falling off" the edge. Therefore, the kernel must start one pixel inward.

COMPUTER VISION · vizlearn.in/computer_vision/padding_in_cnn.html