A Gentle Introduction to Convolutions
Convolutions are the workhorse behind most modern image models. At their core, they are a simple idea: slide a small window over an image and, at each stop, combine the pixels underneath it into a single number.
The Core Idea
A convolution uses a small grid of numbers called a kernel (or filter). You place the kernel over a patch of the image, multiply each kernel value by the pixel beneath it, and add everything up. That sum becomes one pixel in the output.
Move the kernel one step to the right and repeat. Do this across the whole image and you get a new image called a feature map.
Formally, for an input image and a kernel , the value at position of the output feature map is a sum of element-wise products:
Here's that exact operation written out as a tiny 2D convolution in NumPy:
import numpy as np
def convolve2d(image, kernel):
kh, kw = kernel.shape
h, w = image.shape[0] - kh + 1, image.shape[1] - kw + 1
out = np.zeros((h, w))
for i in range(h):
for j in range(w):
patch = image[i:i + kh, j:j + kw]
out[i, j] = np.sum(patch * kernel) # multiply, then add
return out
# A simple vertical-edge detector (Sobel kernel)
edge = np.array([[-1, 0, 1],
[-2, 0, 2],
[-1, 0, 1]])
Why It Works
The magic is in the kernel's values. Different kernels detect different things:
- A kernel tuned to horizontal changes finds horizontal edges.
- A kernel tuned to vertical changes finds vertical edges.
- Blurry, averaging kernels smooth the image.
In a neural network, we don't hand-pick these kernels. The model learns them from data, discovering whatever patterns help it solve the task.
A smoothing (blur) kernel follows a Gaussian profile, - the weights peak at the center and taper off toward the edges:
Stacking Layers
One convolution finds simple patterns like edges. Feed its output into another convolution and it combines those edges into corners and textures. Stack a few more and the network builds up to eyes, wheels, or whole objects.
This hierarchy - simple parts combining into complex ones - is why convolutional neural networks (CNNs) are so good at understanding images.
Two Handy Knobs
- Stride: how far the kernel jumps each step. A larger stride shrinks the output and skips detail.
- Padding: adding a border of zeros around the image so the kernel can reach the edges and keep the output the same size.
Takeaway
A convolution is just a sliding weighted sum. Repeat it, learn the weights, and stack the layers - that simple recipe is the foundation of modern computer vision.