18.1 Convolutional Neural Networks (CNNs) I: The Convolution Operation

Convolutional Neural Networks (CNNs) have revolutionized the field of computer vision and are the backbone of modern image recognition systems. Their power comes from a specialized operation called a convolution, which allows the network to learn hierarchical patterns in data.

What is a Convolution?

In the context of a CNN, a convolution is the process of sliding a small filter, also known as a kernel, over an input image. At each position, the kernel is used to perform a weighted sum of the pixel values it covers. The result of this operation is a single number, which forms one pixel in the output image, called a feature map.

The kernel itself is a small matrix of weights. These weights are learned during the training process. Different kernels can learn to detect different features, such as edges, corners, textures, or more complex shapes.

The key insight is parameter sharing: the same kernel is used across the entire image. This makes the network efficient and allows it to detect a feature regardless of its position in the image (an property known as translation invariance).

Interactive Convolution Visualization

In the visualization below, a 3x3 kernel is sliding over a 5x5 input image (or "matrix"). You can hover over a pixel in the output feature map to see which part of the input image and which kernel weights were used to compute it. You can also edit the values in the input matrix and the kernel to see how the output changes.

Input Image

Kernel

=

Feature Map

{{cell}}

Key Parameters of a Convolution

  • Kernel Size: The dimensions of the kernel (e.g., 3x3, 5x5). Smaller kernels capture finer, local features, while larger kernels capture broader features.
  • Stride: The number of pixels the kernel moves at each step. A stride of 1 moves one pixel at a time. A stride of 2 moves two pixels, resulting in a smaller output feature map.
  • Padding: It's common to add a border of zeros around the input image. This allows the kernel to be centered on the border pixels of the image and can control the spatial size of the output. "Valid" padding means no padding is used, while "Same" padding adds enough padding so that the output feature map has the same dimensions as the input.

Why It Works

By stacking convolutional layers, a CNN builds a hierarchy of features. The first layer might learn to detect simple edges. The next layer might combine these edges to detect simple shapes like corners or circles. Subsequent layers can then combine these shapes to detect more complex objects like eyes, noses, or eventually, entire faces. This hierarchical learning ability is what makes CNNs so powerful for image-related tasks.