Simple Algorithm
Deep Learning · Activation · 17 min

Convolutional Networks & Vision

Locality and weight sharing: the networks that learned to see.

A photo is just a grid of numbers, and a cat can sit anywhere in it. Convolutional networks build that fact into their wiring: one small detector, slid across the whole image, reused everywhere. That one idea took computers from reading digits on bank checks to recognizing a thousand kinds of objects.

What you’ll learn

  • Explain why a fully connected layer on raw pixels needs far too many weights and has no notion that a pattern means the same thing anywhere in the frame.
  • Compute what a small filter does to an image, and recognize edge detectors among learned filters.
  • Describe how stacking convolutions, strides and pooling grows the receptive field and builds a hierarchy from edges to objects.
  • Trace the milestones from LeNet and AlexNet to ResNet and Vision Transformers, and name the vision tasks beyond classification.

Pictures are grids of numbers

A digital image is a grid of pixels. A grayscale pixel is one number, its brightness; a color pixel is three, its red, green and blue intensities. So a color photo is a tensor of shape height × width × 3, and the three slices are its color channels. At 224×224224\times224 pixels, the size most classic image networks take as input, that is 224×224×3=150,528224\times224\times3 = 150{,}528 numbers.

The simplest network you could build connects every one of those numbers to every unit of the first layer, as a plain multilayer perceptron does. How many weights would that take?

Predict before you look

A fully connected first layer maps a 224×224224\times224 color image to 1,000 hidden units, with one weight for every (pixel value, unit) pair. How many weights is that, in millions?

Show the answer

150.5 million

150,528×1,000≈150.5150{,}528\times1{,}000\approx150.5 million weights, for the first layer alone. For comparison, the whole of AlexNet, the network that won the 2012 ImageNet challenge, has about 60 million parameters.

Size is not the only problem. Picture a cat in the top-left corner, then the same cat in the bottom-right. The two images light up completely different input weights, so a fully connected layer has to learn "cat" separately at every position, from examples at every position.

Worse, it ignores which pixels are neighbors. Scramble the pixels of every image with the same fixed shuffle, and a fully connected network trains exactly as well as before: to it, the image was never a grid.

Sliding a filter

A filter (also called a kernel) is a small grid of weights, 3×33\times3 here. Lay it on the image, multiply each weight by the pixel beneath it and add up the nine products: that is one output number. Slide the filter one pixel to the right and repeat, row after row. The grid of outputs is a feature map, and it is large wherever the image contains the filter's pattern.

y[i,j]  =  b+∑u=−11∑v=−11w[u,v]  x[i+u,  j+v]y[i,j] \;=\; b + \sum_{u=-1}^{1}\sum_{v=-1}^{1} w[u,v]\;x[i+u,\;j+v]
One output of a 3×33\times3 filter: nine weights ww and a bias bb, the same at every position (i,j)(i,j). Mathematicians' convolution flips the filter first; deep learning libraries skip the flip and still call the operation convolution (strictly, it is a cross-correlation). Since the weights are learned, nothing is lost.
Check your understanding

At one position, a filter's output is large and positive. What does that tell you about the image there?

  1. The image is bright everywhere under the filter
  2. The filter's weights are larger at that position than elsewhere
  3. The patch under the filter resembles the filter's own pattern of weights
  4. That position is near the center of the image
Show the answer

C. The patch under the filter resembles the filter's own pattern of weights

The output is a weighted sum: it is largest when bright pixels sit under large positive weights and dark pixels under negative ones, that is, when the patch matches the filter's pattern. Each filter is a little template, and the feature map shows where the template fits.

Many filters, learned rather than designed

One filter finds one pattern, so a convolutional layer applies many, say 64, each producing its own feature map. Stacked together, the 64 maps form a new "image" with 64 channels, which the next layer reads. On a color image each filter is 3×3×33\times3\times3, spanning all three input channels, so 64 filters need 64×2764\times27 weights plus 64 biases: 1,792 parameters, whatever the size of the image. Compare that with the 150 million above.

Nobody types those weights in. They start random, and backpropagation adjusts them to reduce the classification error, exactly as in any other network. After each convolution comes a nonlinearity, usually the ReLU max⁡(0,z)\max(0, z), so that stacked layers can compute more than one big linear filter.

Think first For two decades before 2012, computer vision used carefully hand-designed features. Why let the network learn its filters instead?

Hand design works for the first layer (edges are easy to write down) but runs out of ideas fast: nobody knows how to write down the twentieth layer of a good dog detector. Learning tunes every layer jointly for the task at hand, and once large labeled datasets and fast GPUs existed, learned features beat the best hand-crafted pipelines by a wide margin.

Depth widens the view

A 3×33\times3 filter looks at a 3×33\times3 patch. Put a second 3×33\times3 layer on top: each of its outputs combines a 3×33\times3 block of first-layer outputs, each of which looked at 3×33\times3 pixels, so it depends on a 5×55\times5 patch of the image. The region of the input that one unit depends on is its receptive field, and every 3×33\times3 layer adds two pixels to its width.

This growing view is what lets small filters recognize large objects. Visualizations of trained networks (Zeiler & Fergus, 2014) show a hierarchy:

  • early layers, with small receptive fields, respond to edges and color blobs;
  • middle layers combine edges into corners, grids and textures;
  • deep layers, whose fields cover much of the image, respond to parts such as dog faces and bird legs, and to whole objects.

Nobody programmed this progression. It is what training finds when each layer can only build on the features of the layer below.

Check your understanding

A network stacks four 3×33\times3 convolutions, all with stride 1. How wide is the receptive field of one unit in the fourth layer?

  1. 12 pixels
  2. 81 pixels
  3. 9 pixels
  4. 3 pixels
Show the answer

C. 9 pixels

Start at 1 pixel and add 3−1=23-1=2 per layer: 1+4×2=91+4\times2=9. The field grows linearly with depth when there is no stride or pooling.

Pooling: forgiving small shifts

Convolution has a neat property: shift the image one pixel to the right and the feature map shifts one pixel to the right, otherwise unchanged. The layer is translation equivariant: the same object anywhere gives the same response, just in a different place.

For classification you eventually want the opposite, an answer that does not depend on position. Pooling moves toward it. Max pooling splits a feature map into 2×22\times2 blocks and keeps the largest value in each, which halves the width and height: it keeps "this feature is present around here" and forgets exactly where. At the very end, global average pooling averages each map over all positions, so the output records whether each feature is present anywhere in the image, an answer that is approximately translation invariant.

From digits to ImageNet

A complete convolutional network for image classification repeats one pattern: convolution, ReLU, and every few layers a stride or pooling step. The maps get smaller while the channels multiply (64, 128, 256, 512 …), trading where for what. A global average pool and one fully connected layer then turn the final maps into one score per class, and a softmax turns the scores into probabilities. Everything is trained end to end on labeled images.

NetworkWhat it showed
LeNet-5 (LeCun et al., 1998)Seven layers and about 60,000 parameters reading handwritten digits; LeNet-based systems read millions of bank checks a day by the late 1990s.
AlexNet (2012)Eight learned layers, 60 million parameters, ReLU, dropout and two GPUs. Won the 2012 ImageNet challenge with 15.3% top-5 error, against 26.2% for the runner-up.
VGG (2014)16–19 layers built almost entirely from 3×33\times3 convolutions: depth with small filters.
GoogLeNet (2014)22 layers of parallel branches (inception modules), with about 12 times fewer parameters than AlexNet; won the 2014 challenge with 6.67%.
ResNet (He et al., 2016)Residual connections made 152 layers trainable; won the 2015 challenge with 3.57%.
Top-5 error: the fraction of test images whose correct label is not among the network's five best guesses.

Beyond one label per image

Classification outputs one label per image. Most real tasks want more:

  • Object detection finds every object and draws a bounding box with a label around each.
  • Semantic segmentation labels every pixel ("road", "car", "sky"); instance segmentation also separates one car from the next.
  • Video understanding adds time, recognizing actions across frames.

All of them start from the same kind of convolutional backbone and add task-specific heads on top.

Vision Transformers (Dosovitskiy et al., 2021) took a different route: cut the image into 16×1616\times16 patches, treat each patch like a word, and let attention connect every patch to every other from the very first layer. With less structure built in, they needed more training data, or heavier augmentation and regularization, to beat CNNs. Modernized ConvNets such as ConvNeXt (2022) then caught up again, and as of the mid-2020s both families, and hybrids of the two, are in wide use.

Vision also meets language. CLIP (Radford et al., 2021) trains an image encoder and a text encoder together so that each photo lands close to its own caption among hundreds of millions of image–text pairs from the web. The result classifies images into categories it was never trained on, just from their names written as text (zero-shot classification), and image encoders of this kind feed the multimodal models that describe images and answer questions about them.

Key takeaways

  • An image is a tensor of height × width × channels. A fully connected layer on raw pixels needs an enormous number of weights and must relearn each pattern at every position.
  • A convolutional layer slides small learned filters across the image: locality and weight sharing give few parameters and the same detector everywhere, and its feature maps shift when the input shifts.
  • Stacking layers, with strides and pooling, grows the receptive field; trained networks build a hierarchy from edges to textures, parts and objects.
  • Pooling and global averaging make the output tolerant of shifts, and data augmentation teaches the invariances the task allows, but not those that change the label.
  • LeNet, AlexNet, VGG, GoogLeNet and ResNet carried CNNs from digits to ImageNet; the same backbones serve detection, segmentation and video, and Vision Transformers offer an alternative with less built-in structure.

Check yourself

In the interactive lesson these are scored with your confidence, and what you miss comes back for review.

Question 1

Why is a fully connected layer a poor first layer for photographs?

  1. It cannot represent edges at all
  2. It only works on grayscale images
  3. It needs a separate weight for every pixel–unit pair and must learn each pattern separately at every position
  4. Its output cannot be differentiated, so it cannot be trained by backpropagation
Show the answer

C. It needs a separate weight for every pixel–unit pair and must learn each pattern separately at every position

For a 224×224224\times224 color image, 1,000 units already need about 150 million weights, and nothing ties the weights for a pattern in one place to the weights for the same pattern elsewhere. A convolutional layer fixes both by connecting locally and sharing weights.

Question 2

A 3×33\times3 filter has rows (−1,0,1)(-1, 0, 1), (−2,0,2)(-2, 0, 2), (−1,0,1)(-1, 0, 1). Where is its output largest?

  1. On a uniformly bright patch
  2. Where a dark region on the left meets a bright region on the right
  3. Where a dark region on top meets a bright region below
  4. On a single bright pixel in the middle of the patch
Show the answer

B. Where a dark region on the left meets a bright region on the right

The left column has negative weights and the right column positive ones, so the sum measures how much brighter the right side is than the left. A step from 0 on the left to 1 on the right gives 1+2+1=41+2+1=4; a flat patch gives 00. This is the Sobel filter for vertical edges.

Question 3

You apply 2×22\times2 max pooling with stride 2 to a stack of 64 feature maps, each 32×3232\times32. What comes out?

  1. 64 maps of 16×1616\times16
  2. 16 maps of 16×1616\times16
  3. 64 maps of 31×3131\times31
  4. 64 maps of 32×3232\times32, each with its largest values kept
Show the answer

A. 64 maps of 16×1616\times16

Pooling acts on each channel separately, keeping the maximum of every non-overlapping 2×22\times2 block: width and height halve, and the 64 channels stay 64.

Question 4

How can a CNN built only from 3×33\times3 filters recognize an object that fills most of the image?

  1. Each layer combines outputs of the layer below, so the receptive field grows with depth, and faster after strides and pooling
  2. Every deeper layer automatically uses a larger filter
  3. The first layer already looks at every pixel through its 9 weights
  4. It cannot: CNNs only recognize small, local patterns
Show the answer

A. Each layer combines outputs of the layer below, so the receptive field grows with depth, and faster after strides and pooling

Units in deep layers depend on large input regions even though every filter is small. Early layers find edges, later layers combine them into parts and whole objects.

Question 5

Convolutional networks were reading handwritten digits in the 1990s. What changed around 2012 to make them dominate computer vision?

  1. Convolution was invented in 2012
  2. Large labeled datasets such as ImageNet, fast GPUs, and training tricks such as ReLU and dropout made much larger networks trainable
  3. Networks became shallower and therefore easier to train
  4. CNNs stopped needing labeled data
Show the answer

B. Large labeled datasets such as ImageNet, fast GPUs, and training tricks such as ReLU and dropout made much larger networks trainable

AlexNet took a decades-old architecture to a new scale, 1.2 million labeled images on two GPUs, and added ReLUs for faster training and the then-new dropout, with augmentation, against overfitting. Its 15.3% top-5 error against 26.2% started the deep learning era in vision.

Go deeper

Further reading

  • Y. LeCun, L. Bottou, Y. Bengio & P. Haffner, "Gradient-based learning applied to document recognition," Proceedings of the IEEE 86(11) (1998).
  • A. Krizhevsky, I. Sutskever & G. E. Hinton, "ImageNet classification with deep convolutional neural networks," NeurIPS (2012).
  • S. J. D. Prince, Understanding Deep Learning, ch. 10 "Convolutional networks" (MIT Press, 2023), free at udlbook.github.io.
  • I. Goodfellow, Y. Bengio & A. Courville, Deep Learning, ch. 9 "Convolutional Networks" (MIT Press, 2016), free at deeplearningbook.org.