Lensa ML

How CNNs See

Step 1 of 6: An Image Is Just Numbers

A one-pixel shift changes many of them

0000000001111100000001000011110000000100000001000111110000000000red outline = pixel that changed because of the shift
Numbers in the image
64
Numbers changed by the shift
0 / 64
Weights per neuron, dense layer
64

To a computer this "3" is 64 numbers, 1 for ink and 0 for paper. It has no idea there is a shape. Nudge it one pixel with the arrows.

Pick a digit and nudge it with the arrows. A greyscale image is a grid of numbers, here 8 × 8 = 64; a colour photo has three channels, so a 224 × 224 image holds about 150,000. A fully connected layer gives each of those numbers its own weight per neuron, so it has no built-in idea that neighbouring pixels belong together or that a pattern can move. That's the problem convolution solves.

Step 2 of 6: The Sliding Filter

Nine weights, multiplied and summed at every position

input 8×80000000001111100000001000011110000000100000001000111110000000000kernel↑ click cells to edit-1-1-1000111output 6×6pixel × weight at this position1×-1=-11×-1=-11×-1=-10×0=00×0=00×0=00×1=01×1=11×1=1sum = -1 → ReLU → 0position (1, 1)
Position8/36
Weighted sum here
-1
After ReLU
0
Weights used
9, at all 36 positions

The 3×3 kernel sits on a patch of the image. Each pixel is multiplied by the weight on top of it, and the 9 products are added: here -1 + -1 + -1 + 0 + 0 + 0 + 0 + 1 + 1 = -1. Then the kernel slides one pixel and does it again, 36 times, filling the 6×6 output, called a feature map. Big values mean "my pattern is here". Click the kernel cells to cycle −1, 0, 1 and build your own detector. In a real CNN these 9 numbers aren't typed in; they're learned by backpropagation, and the same 9 are reused at every position.

Step the kernel across the image and edit its weights. Deep-learning libraries call this convolution, but they compute cross-correlation: the kernel isn't flipped, which makes no difference once the weights are learned. Without padding a 3×3 kernel shrinks 8×8 to 6×6; real CNNs usually pad the border with zeros to keep the size, and use a stride above 1 when they want to downsample. Each output is followed by a non-linearity, usually ReLU.

Step 3 of 6: What Filters See

Four kernels, four feature maps, 36 shared weights

"7"│ vertical edge100010100110001100011000021000020000─ horizontal edge000011000000001100011000010000000000╲ ↘ line100020000202002000020002010020001100╱ ↗ line100000100002000060000600003000011000
Weights per kernel
9
4 kernels, total weights
36
Dense layer for the same 144 outputs
9,216
Strongest detector here
╱ 6

Each kernel lights up where its own pattern appears: on the "7", the ↗ line detector fires hardest (6). Four kernels give four feature maps, 144 numbers, from only 36 weights, because every kernel is reused at all 36 positions. A dense layer producing the same 144 numbers would need 9,216 weights. That sharing is the main idea of a CNN. These four are hand-set edge detectors; trained first-layer filters often end up looking much like them.

Switch digits and compare the four feature maps. A convolution layer applies many kernels at once, typically 32 to 512, each producing its own map; together they form the channels of the next layer's input. The weight count depends only on kernel size and channels, not on image size, so the same layer works on a 32 × 32 or a 1024 × 1024 image. Visualizations of trained networks (Zeiler and Fergus, 2014) show first-layer filters that look like these edge and line detectors.

Step 4 of 6: Pooling Forgives Small Shifts

Keep each block's maximum

feature map 6×6 (shift 1, 0)0000110000000000000000001233210000002×2 max-pool → 3×3001000232red outline = differs from the unshifted digit
Pixels changed
12 / 64
Feature-map cells unchanged
30 / 36
Pooled cells unchanged (this kernel)
6 / 9
All 36 pooled features unchanged
20 / 36

Shifting the "3" by (1, 0) changes 12 of 64 pixels, but 20 of the 36 pooled features stay exactly the same, because a strong response that moves within its 2×2 block is still that block's maximum. Not all of them survive: a shift that crosses a block border still changes the result. Pooling buys tolerance to small shifts, not full invariance, and it halves the map size so later layers cost less.

Pick a kernel and shift the digit. Max pooling with a 2×2 window and stride 2 keeps one value in four and has no weights to learn. It makes the network tolerant to small shifts, as the readouts show, but not truly invariant: a pattern that moves into a neighbouring block can change the output. Many modern networks replace pooling with strided convolutions, and almost all end with global average pooling, which averages each feature map to a single number before the classifier.

Step 5 of 6: Each Layer Sees More

The receptive field grows with depth

input 8×8pool 1 (2×2): 3×3

Click a cell on the right to see which input pixels it depends on.

This layer's map
3×3
One cell sees (receptive field)
4×4
Share of the image
25%

A conv-1 cell sees only a 3×3 patch, enough for an edge. After 2×2 pooling each cell summarizes a 4×4 patch, and a 3×3 conv on top of that combines 3×3 pooled cells, which together cover all 8×8 = 64 input pixels. So the second layer can respond to arrangements of edges, such as "a horizontal stroke above a curve", which is how a CNN builds up from edges to parts to whole digits. Deep CNNs repeat this dozens of times on larger images; a cell late in ResNet-50 sees hundreds of pixels across.

Pick a layer and click a cell. The receptive field is the patch of input pixels that can affect one output value. Each 3×3 convolution adds 2 pixels to it, and each 2×2 pooling doubles the step between neighbouring cells, so the field grows quickly with depth. In practice, a cell's effective receptive field is smaller than its theoretical one, because pixels near its centre influence it much more than pixels at the edge.

Step 6 of 6: From Maps to a Prediction

A real classifier, tested on shifted digits

flatten: 4 kernels × 3×3 pooled = 36 numbersCNN: features → softmax00.0610.1630.0870.70raw pixels → softmax00.2510.6130.0670.08
CNN says
"7" 0.70
Pixel model says
"1" 0.61
Accuracy on all 1-px shifts: CNN vs pixels
78% vs 39%

Both classifiers are trained here, by gradient descent, on the four digits centred only. The CNN flattens its 36 pooled features and feeds them to a softmax layer; the other model feeds the 64 raw pixels straight in. Shifted by (0, 1), the "7" is called "7" by the CNN and "1" by the pixel model. Over all 36 one-pixel shifts the CNN gets 78% right and the pixel model 39%: weight sharing plus pooling generalize to positions it never saw, though not perfectly. Switch on shifted training copies to compare.

Pick a digit, shift it, and switch the training data. Both classifiers here are softmax regressions trained in your browser; the CNN version only sees the 36 pooled features. Two ideas make CNNs robust to position: weight sharing, so a pattern is detected anywhere, and pooling, so small moves don't change the summary. Data augmentation, training on shifted, flipped, or cropped copies, adds robustness to any model and is used when training almost every image classifier.

What is a convolutional neural network (CNN)?

A convolutional neural network (CNN) is a neural network that processes grid-shaped data, such as images, by sliding small learned filters across the input. Each filter computes a dot product at every position and produces a feature map that marks where its pattern appears. Stacking convolution, ReLU, and pooling layers lets the network build from edges up to whole objects before a dense layer makes the final prediction.

CNNs are the standard model for image classification, object detection, segmentation, and medical imaging, and they also run on audio spectrograms and time series. Their strength comes from two constraints: each filter looks at a small local patch, and the same filter weights are reused at every position. That cuts the number of weights by orders of magnitude compared with a dense network and makes a pattern detectable wherever it appears in the image.

Key ideas

  • A convolution slides a small kernel over the input and computes a dot product at each position.
  • Weight sharing means one kernel detects its pattern anywhere in the image.
  • Kernel values are learned by gradient descent, not designed by hand.
  • ReLU keeps positive responses and zeros out the rest.
  • Pooling shrinks feature maps and adds tolerance to small shifts.
  • Deeper layers combine simpler features into parts and objects, then a dense layer classifies.

Common questions

What is the difference between a CNN and a regular neural network?

A regular (fully connected) layer connects every input to every output, so it ignores where pixels sit relative to each other. A convolutional layer connects each output only to a small local patch and reuses the same weights across positions. This gives far fewer parameters and builds in the assumption that nearby pixels are related.

What is a 1x1 convolution used for?

A 1x1 convolution mixes channels at each pixel without looking at neighbors. It is used to shrink or expand the number of channels cheaply, for example in the bottleneck blocks of ResNet and in Inception modules. It works like a small dense layer applied at every position.

Are CNNs still used now that vision transformers exist?

Yes. Vision transformers match or beat CNNs when trained on very large datasets, but CNNs are still common on smaller datasets, on phones and embedded devices, and as backbones for detection and segmentation. Many current models mix the two, using convolutions in early layers and attention later.

Can CNNs work on data other than images?

Yes. A 1D convolution slides along a sequence, so it works on audio waveforms, sensor readings, and text. A 3D convolution adds a time or depth axis for video and medical scans. Any data where nearby values are related is a good fit.

Related explainers

References

  1. LeCun, Bottou, Bengio & Haffner (1998), "Gradient-Based Learning Applied to Document Recognition", Proc. IEEE
  2. Krizhevsky, Sutskever & Hinton (2017), "ImageNet Classification with Deep Convolutional Neural Networks", Communications of the ACM
  3. Zeiler & Fergus (2013), "Visualizing and Understanding Convolutional Networks"
  4. Stanford CS231n, "Convolutional Neural Networks"

Last updated