SkillByAIOpen interactive version →

Lesson 14 / 25

The Convolution Operation

A small filter slides over the image.

Detecting local patterns

A convolution slides a small grid of weights (a kernel or filter, for example 3x3) across the image and computes a weighted sum at each position, producing a feature map that is high where the pattern matches. Early filters learn edges and textures; deeper layers combine them into parts and objects. Settings: stride (step size), padding (keep borders) and number of output channels (how many filters). In a CNN the filter values are learned, not designed.

Local patterns, shared weights

CNNs scan small filters across images, reusing the same weights everywhere.

Figure 5.1 — Convolution, architecture and efficiency.

A vertical-edge filter by hand and in PyTorch, run

I ran this on CPU with Python 3, PyTorch 2.14.1, numpy 2.5.3 and scikit-learn 1.9.1, with fixed seeds and one thread. An image dark on the left and bright on the right is convolved with a vertical-edge kernel: the output is 3 along the edge and 0 elsewhere, and matches torch.nn.functional.conv2d exactly. (Like most deep learning libraries, PyTorch computes cross-correlation, without flipping the kernel.)

import numpy as np, torch
img = np.zeros((6, 6)); img[:, 3:] = 1.0          # dark left half, bright right half
kernel = np.array([[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]], dtype=float)   # vertical edge detector
out = np.array([[np.sum(img[i:i + 3, j:j + 3] * kernel) for j in range(4)] for i in range(4)])
print("manual convolution output:\n", out)
t = torch.nn.functional.conv2d(torch.tensor(img, dtype=torch.float32)[None, None],
                               torch.tensor(kernel, dtype=torch.float32)[None, None])
print("matches torch conv2d:", np.allclose(out, t[0, 0].numpy()))

Output:

manual convolution output:
 [[0. 3. 3. 0.]
 [0. 3. 3. 0.]
 [0. 3. 3. 0.]
 [0. 3. 3. 0.]]
matches torch conv2d: True

Visualise first-layer filters

Plotting learned first-layer filters often shows edge and colour detectors, a quick sanity check that training worked.

Quick check: What does a convolutional filter produce?

  • A new dataset
  • A single class label
  • A loss value
  • A feature map showing where its pattern appears
Answer

A feature map showing where its pattern appears — Filters respond to local patterns.