Start studying
Module 3 · Computer Vision Module contents
Module 3  ·  Card 8 / 22beginner

CNNs

A convolutional neural network stacks convolution, activation and pooling layers.

Early layers capture local detail and later layers capture global structure.

The spatial map shrinks while the number of channels grows.

Dense layers at the end turn the final features into a prediction.

CNNs were the architecture that made modern computer vision work.

Learn in depth ↓
Module 03 · Computer Vision · 22 concepts
In depth  /  15 lines

CNNs, in more detail

A convolutional neural network is convolution, activation and downsampling, stacked.

The pattern repeats: convolve to find features, apply ReLU, then shrink the map.

Through that stack the spatial size falls while the channel count rises.

An image might go from 224 by 224 by 3 to 7 by 7 by 512.

That is the network trading where something is for what it is.

Early layers hold precise positions of simple things such as edges.

Deep layers hold vague positions of complicated things such as faces.

A classifier head at the end turns the final features into class scores.

Global average pooling followed by one dense layer is the modern way to do that.

The architecture builds in two assumptions about images.

The first is that a useful pattern is useful anywhere, which gives weight sharing.

The second is that nearby pixels are related, which gives small local kernels.

Those assumptions are why a CNN needs far less data than a dense network on the same task.

They are also why vision transformers, which assume less, need much more data to compete.

For most practical vision work with limited data a CNN remains the sensible starting point.

Want the full technical article? Wikipedia ↗
Deep dive

What is underneath CNNs

The two assumptions that make it work

A convolutional network is a dense network with two constraints imposed on the weights, and both constraints are statements about images.

locality:O(i,j) depends only on I within K of (i,j)\text{locality} : O(i,j) \text{ depends only on } I \text{ within } K \text{ of } (i,j)
weight sharing:the same K is applied at every position\text{weight sharing} : \text{the same } K \text{ is applied at every position}

Locality says a pixel is explained by its neighbours. Sharing says a pattern is worth finding wherever it appears.

dense:2242⋅3⋅1000=1.5×108 weights in one layer\text{dense} : 224^2 \cdot 3 \cdot 1000 = 1.5 \times 10^{8} \text{ weights in one layer}
conv:32⋅3⋅64=1,728 weights in one layer\text{conv} : 3^2 \cdot 3 \cdot 64 = 1{,}728 \text{ weights in one layer}

Those constraints are what make the parameter count independent of image size, and they are the entire reason images are tractable.

The stack, and what it does to the shapes

(H,W,C)→(H2,W2,2C)(H, W, C) \to \left( \frac{H}{2}, \frac{W}{2}, 2C \right)

The usual pattern is a block of convolutions, then a downsample, repeated. Spatial size halves and channel count doubles at each stage.

HWC→H2⋅W2⋅2C=HWC2H W C \to \frac{H}{2} \cdot \frac{W}{2} \cdot 2C = \frac{HWC}{2}

That halves the activation volume per stage while keeping the arithmetic per layer roughly level, which is the trade the design is built around.

Early layers are cheap in parameters and expensive in arithmetic, because the maps are large. Late layers are the reverse.

One forward pass, in order

  1. Convolve, producing one map per filter.
  2. Normalise across the batch, then apply ReLU.
  3. Repeat for the other convolutions in the block.
  4. Downsample by pooling or by a stride-2 convolution.
  5. Repeat the block at the next scale, with more channels.
  6. Global average pool to one vector, then a single dense layer to the classes.

Counting the cost of a layer

params=K2CinCout,FLOPs=2HoutWoutK2CinCout\text{params} = K^2 C_{\mathrm{in}} C_{\mathrm{out}}, \qquad \mathrm{FLOPs} = 2 H_{\mathrm{out}} W_{\mathrm{out}} K^2 C_{\mathrm{in}} C_{\mathrm{out}}

A 3 by 3 layer from 256 to 256 channels on a 28 by 28 map is 590,000 parameters and about 925 million operations per image.

memory≈B⋅H⋅W⋅C⋅bytes\text{memory} \approx B \cdot H \cdot W \cdot C \cdot \text{bytes}

Training memory is dominated by activations rather than weights, because every map is held for the backward pass and the batch multiplies all of them.

That is why batch size is the first thing to cut when a convolutional model does not fit.

What the constraints cost

Weight sharing gives equivariance rather than invariance: shifting the input shifts the feature map, and it takes pooling or augmentation to stop caring where.

Nothing in a convolution is invariant to rotation or scale, so those have to come from data augmentation. A network trained on upright faces does not recognise upside down ones.

A vision transformer drops both constraints and learns the relationships instead, which needs far more data to reach the same place and then keeps going past it.

Test yourself

One question on this card. Every option explains itself once you pick, right or wrong.

Question 1
What happens to the data as it moves deeper through a CNN?
Computer Vision

22 concepts in this module, then a graded test with every answer explained.

Study this module →
↑↓ move↵ openesc close