Find a course

Module 5: Architectures That Changed AILesson 10 of 1116 min

Convolutional Neural Networks

See how convolution filters scan images with shared weights, compute feature map sizes and parameter counts, and learn how pooling builds hierarchies.

Step 1 of 12Learn

Why dense layers struggle with images

A modest color photo of 1,000 × 1,000 pixels has 3 million input values (three color channels per pixel). Connect that to a dense layer of just 1,000 neurons and you need 3 billion weights in the first layer alone. That's far too many to train well from a typical dataset, and it's wasteful.

It also ignores what we know about images. A dense layer treats pixel 1 and pixel 2 as unrelated inputs, with no idea that they're neighbors. And a cat in the top-left corner and the same cat in the bottom-right would activate completely different weights, so the network would have to learn "cat" separately for every position.

Convolutional neural networks (CNNs) build two facts about images into the architecture:

  • Local patterns matter. Edges, corners and textures are made of nearby pixels, so each neuron should look at a small patch, not the whole image.
  • A pattern means the same thing anywhere. A vertical edge is a vertical edge wherever it appears, so the same detector should be reused at every position. This is weight sharing.

These two ideas cut the parameter count by orders of magnitude and make CNNs far better at vision. They dominated image recognition from 2012 onward and are still widely used.

Step 2 of 12Practice · Work it out

A 200 × 200 color image (3 channels) is fed into a dense layer with 500 neurons. How many weights does that layer need (ignore biases)?

Step 3 of 12Learn

The convolution operation

A filter (or kernel) is a small grid of weights, typically 3 × 3. It slides across the image, and at each position it multiplies its 9 weights by the 9 pixel values underneath, adds them up (plus a bias), and writes one number into an output grid called a feature map.

Here's a classic hand-designed filter that detects vertical edges:

10−1
10−1
10−1

Over a flat region, the left and right columns cancel and the output is near 0. Where bright pixels on the left meet dark pixels on the right, the output is large. The feature map lights up exactly where vertical edges are.

In a CNN, nobody designs filters by hand. Each filter's weights are learned by backpropagation, and a layer learns many filters at once, say 64, producing 64 feature maps stacked as channels. The next convolutional layer looks at small patches across all 64 channels, combining simple features into more complex ones. A ReLU is applied after each convolution, just as in dense layers.

Convolution: a 3 by 3 filter slides over a 6 by 6 input image; each position produces one value in a 4 by 4 feature mapinput 6×63×3 filterfeature map 4×4multiply and sum

Step 4 of 12Practice · Work it out

A 3 × 3 image patch has rows (1, 2, 0), (0, 1, 3) and (2, 1, 1). The vertical-edge filter has rows (1, 0, −1), (1, 0, −1), (1, 0, −1). What value does the filter output at this position (no bias)?

Step 5 of 12Learn

Stride, padding and output size

Two settings control how a filter moves:

  • Stride (s): how many pixels the filter jumps each time. Stride 1 visits every position; stride 2 skips every other one and roughly halves the output size.
  • Padding (p): extra pixels, usually zeros, added around the border so the filter can be centered on edge pixels. With a 3 × 3 filter, a padding of 1 keeps the output the same size as the input at stride 1, often called "same" padding.

For an n × n input and a k × k filter, the output size is

output = (n − k + 2p) / s + 1 (rounded down)

Example: a 32 × 32 image with a 5 × 5 filter, no padding, stride 1 gives (32 − 5 + 0)/1 + 1 = 28, a 28 × 28 feature map.

The parameter count is where weight sharing pays off. A filter covers k × k positions across all input channels, plus one bias. A layer with 64 filters of size 3 × 3 on an RGB image has 64 × (3 × 3 × 3 + 1) = 1,792 parameters, whatever the image size. Compare that with the billions a dense layer would need.

Step 6 of 12Try the calculator

Convolution layer calculator

Output size uses (n − k + 2p) / s + 1, rounded down. Parameters are filters × (k × k × input channels + 1).

Output width and height
32
Output channels
64
Parameters in this layer
1,792

Step 7 of 12Practice · Work it out

A 64 × 64 input goes through a convolution with a 3 × 3 filter, padding 1 and stride 2. What is the width of the output feature map?

Step 8 of 12Learn

Pooling and the feature hierarchy

Between convolutional layers, CNNs often shrink the feature maps with pooling. The most common kind, max pooling with a 2 × 2 window and stride 2, keeps only the largest value in each 2 × 2 block. A 28 × 28 feature map becomes 14 × 14. Pooling has no weights; it reduces computation and makes the network a little less sensitive to the exact position of a feature. Some modern designs replace it with stride-2 convolutions.

Stack several convolution, ReLU and pooling stages and a hierarchy emerges. Early layers detect edges and color blobs; middle layers detect textures and simple shapes; deep layers respond to parts such as eyes, wheels or leaves. At the end, the final feature maps are flattened into a vector and passed through one or more dense layers to a softmax output.

A few landmark designs show how the field moved: LeNet-5 (1998) read digits on checks; AlexNet (2012) won ImageNet using GPUs, ReLU and dropout; VGG (2014) used stacks of small 3 × 3 filters; and ResNet (2015) added residual connections that made networks with over 100 layers trainable. Image transformers now compete with CNNs on many tasks, but convolution remains a standard tool, especially on phones and other small devices.

Step 9 of 12Practice · Diagram

The feature hierarchy in a trained CNN

Each stage combines the features of the stage before into something larger and more abstract.

Which stage do the filters of the very first convolutional layer typically detect?

Tap the shape that answers the question, then press Check.

The feature hierarchy in a trained CNNProcess diagram: Pixels → Edges and color blobs → Textures and corners → Object parts → Whole objects.PixelsEdges and colorblobsTextures andcornersObject partsWhole objects
Pixels
Edges and color blobs

First-layer filters respond to simple oriented edges and color contrasts.

Textures and corners

Combinations of edges: stripes, grids, fur-like patterns.

Object parts

Eyes, wheels, leaves, letters.

Whole objects

The last layers respond to cats, cars or digits as a whole.

Step 10 of 12Note

Step 11 of 12Practice · Put in order

Put the stages of a typical small CNN classifier in order, from input to prediction.

Drag the items, or use the arrow buttons, to put them in order.

  1. Flatten the feature maps into a vector
  2. Convolution followed by ReLU
  3. A second convolution followed by ReLU and pooling
  4. Input image
  5. Max pooling
  6. Dense layer with softmax output

Step 12 of 12Practice · Choose one

What is the main reason a convolutional layer has far fewer parameters than a dense layer on the same image?