CadetX
CX Learn | Complete Neural Networks Guide
TensorFlow · Keras
CadetX logo CadetX CX Learn
NN-101 · Module 8 Intermediate about 45 minutes 11 Lessons Prereq: Modules 1 to 7

Convolutional Neural Networks (CNNs)

Convolutional Neural Networks (CNNs) are how computers learned to see. They power face unlock, photo search, medical scan analysis and self-driving research. In this module you'll learn how a CNN scans an image for patterns, try it yourself, and build one in Keras.

  • Level: Intermediate
  • Time: about 45 minutes
  • Needs: Modules 1 to 7

By the end of this module you will be able to

  • Explain how a computer stores an image as numbers
  • Explain why normal (dense) networks struggle with images
  • Work out a convolution by hand, and describe what filters detect
  • Explain stride, padding and pooling
  • Describe the layers of a typical CNN and what each one learns
  • Name famous CNNs and what made each one important
  • Build, train and compare a CNN in Keras

8.1How a computer sees an image

To you, a photo is a face or a dog. To a computer, it's just a grid of numbers. Each tiny square, or pixel, gets a number for how bright it is.

A handwritten 3 shown two ways. On the left, as a picture of grey squares. On the right, as the 8 by 8 grid of numbers from 0 to 16 that the computer actually sees.What you seeWhat the computer sees007151310008136154000211313000002151110000011212100000110800084514900071313900=64 numbers: 0 is black, 16 is white. A colour photo has 3 grids: red, green and blue.
Figure 1. A handwritten 3 from the digits dataset. Every image a CNN sees is a grid of numbers like this.
Image typeHow it's storedExample size
GreyscaleOne grid of brightness values (1 channel)8 × 8 × 1 = 64 numbers
ColourThree grids: red, green and blue (3 channels)224 × 224 × 3 = 150,528 numbers
Phone photoThree grids at high resolution4,000 × 3,000 × 3 = 36 million numbers

8.2Why normal networks struggle with images

You could feed an image into the dense networks from earlier modules by flattening it: laying every pixel out in one long row. It works for tiny images, but causes three big problems:

ProblemWhat goes wrong
Far too many weightsA 224 × 224 colour image has 150,528 inputs. One dense layer of 1,000 neurons would need over 150 million weights, just for the first layer. That's slow to train and easy to overfit
It ignores which pixels are neighboursFlattening throws away the 2D layout. The network no longer knows that one pixel sits right above another, even though nearby pixels together form edges and shapes
It depends on positionA dense network learns "a cat in the top-left corner" separately from "a cat in the bottom-right". Move the cat and it has to learn it all again

How you actually look at a picture

When you look for your friend in a crowd photo, you don't take in every pixel at once. You scan across the picture looking for small, familiar features: a hairstyle, a face shape, a colour of jacket. And you'd recognise them wherever they're standing. A CNN works in exactly this way.

8.3Convolution: scanning with a filter

The key idea of a CNN is the filter (also called a kernel): a tiny grid of weights, usually 3 × 3. The filter slides across the image, one position at a time. At each position it:

  1. multiplies each of its 9 weights by the pixel underneath,
  2. adds up the 9 results,
  3. writes that one number into a new grid, called a feature map.

This sliding multiply-and-add is called convolution, which is where CNNs get their name.

Convolution. A 3 by 3 filter slides over a 6 by 6 image whose left half is dark and right half is bright. At each position it multiplies the numbers underneath by the filter and adds them up. The result is a 4 by 4 feature map with high values exactly where the dark-to-bright edge is.Image (6×6)Filter (3×3)Feature map (4×4)000999000999000999000999000999000999-101-101-101Spots a dark-to-brightedge running up and down027270027270027270027270Highlighted: (−1×0 + 0×0 + 1×9) on each of the 3 rows = 9 + 9 + 9 = 27
Figure 2. A "vertical edge" filter sliding over an image that is dark on the left and bright on the right. Where there's no edge, the result is 0. Where the filter sits over the dark-to-bright edge, the result is a big 27. The feature map is a map of where the edges are.

Different filters detect different things. One filter finds vertical edges, another horizontal edges, another spots or corners. And here's the crucial part: in a CNN, nobody designs the filters. The weights in each filter are learned during training, exactly like any other weight. The network discovers for itself which patterns are useful.

Why this solves the three problems

ProblemHow convolution fixes it
Too many weightsA filter has only 9 weights (per channel), reused at every position. This is called weight sharing. 64 filters on a colour image need just 1,792 weights, not 150 million
Ignores neighboursEach filter looks at a small patch of neighbouring pixels, keeping the 2D layout
Depends on positionThe same filter scans everywhere, so an edge is found wherever it appears

8.4Try convolution yourself

Choose a filter and watch it scan a simple picture of a house. Drag the slider (or press Play) to move the filter, and see the calculation at each position.

Try it: convolution explorer

Input image (12 × 12)
Filter (3 × 3)
Feature map (10 × 10)

Notice that the edge filters light up only along the edges of the house: the walls for "vertical", the roof base and floor for "horizontal". Positive values (amber) mark one direction of change, negative values (blue) the opposite. A real CNN layer runs dozens of filters at once, producing a stack of feature maps.

8.5Stride and padding

You may have noticed that the 12 × 12 image produced a smaller 10 × 10 feature map. That's because a 3 × 3 filter can't be centred on the edge pixels. Two settings control the size of the output:

SettingWhat it doesEffect on sizeIn Keras
PaddingAdds a border of zeros around the image, so the filter can reach the edge pixels"valid" (no padding): output shrinks. "same": output stays the same sizepadding="same"
StrideHow many pixels the filter moves each stepStride 1: full size. Stride 2: half the width and heightstrides=2
Output size = (input size − filter size + 2 × padding) ÷ stride + 1

For the lab: (12 − 3 + 0) ÷ 1 + 1 = 10. With padding="same", the output would stay at 12 × 12.

8.6Pooling: shrinking the feature maps

After a convolution, CNNs usually shrink the feature maps with a pooling layer. The most common kind, max pooling, splits the map into small blocks (usually 2 × 2) and keeps only the biggest number from each.

Max pooling. A 4 by 4 feature map is split into four 2 by 2 blocks, and only the largest number in each block is kept, giving a 2 by 2 result: 6, 2, 8 and 4.13215612720418336284Keep the biggest ineach 2×2 blockFeature map (4×4)After pooling (2×2)
Figure 3. 2 × 2 max pooling halves the width and height, keeping only the strongest signal in each block. Pooling has no weights to learn.
Why pool?Explanation
Less workA quarter of the numbers to process in the next layers
Keeps what matters"Was there a strong edge somewhere in this area?" is kept; the exact pixel isn't
Tolerates small shiftsIf a feature moves by a pixel, the biggest value in its block is often still the same
Wider viewAfter pooling, each filter in the next layer covers a larger area of the original image

8.7Putting it together: a full CNN

A typical CNN has two parts. The first part finds patterns with repeated blocks of convolution, ReLU and pooling. The second part makes the decision with the dense layers you already know.

A typical CNN. An input image passes through a convolution layer with ReLU that makes several feature maps, then a pooling layer that shrinks them. This repeats with more feature maps. The maps are flattened into a list of numbers and passed to dense layers, ending in a softmax output that gives a probability for each class.Image012…9InputConvolution+ ReLUPoolingConvolution+ ReLUPoolingFlattenDenseSoftmaxFeature extraction: finds patternsClassification: decides
Figure 4. A typical CNN for classifying digits. The feature maps get smaller (thanks to pooling) but more numerous (more filters) as you go deeper. Then they're flattened into one long list and passed to dense layers, ending with softmax (Module 3).

What each layer learns

Because each layer builds on the one before, CNNs learn a hierarchy of features, from simple to complex:

What each layer of a CNN learns to detect. Early layers find edges and colours, middle layers find shapes and textures, deeper layers find parts such as eyes and wheels, and the final layers recognise whole objects such as faces and cars.Early layersEdges and coloursMiddle layersShapes and texturesDeeper layersParts of objectsFinal layersWhole objects
Figure 5. When researchers look inside trained CNNs, early filters respond to edges and colours; middle layers to shapes and textures; deeper layers to parts like eyes and wheels; and the final layers to whole objects. Nobody programs this; it emerges from training.

Colour images and many filters

For a colour image, each filter is actually 3 × 3 × 3: one 3 × 3 slice for each of the red, green and blue channels, all added together. And each layer uses many filters. A layer with 32 filters produces 32 feature maps, which become 32 channels for the next layer to scan.

8.8Famous CNNs

NetworkYearWhy it matters
LeNet-51998Yann LeCun's early CNN, used by banks to read handwritten digits on cheques. It had the same conv → pool → dense pattern still used today
AlexNet2012Won the ImageNet image competition by a huge margin, using ReLU, dropout and GPUs. Widely seen as the start of the deep learning boom
VGG2014From the University of Oxford. Showed that stacking many small 3 × 3 filters works brilliantly
ResNet2015From Microsoft Research. Added "skip connections" that let the signal jump over layers, making networks with 100+ layers trainable
MobileNet, EfficientNet2017 onwardsDesigned to be small and fast enough to run on phones

You rarely train from scratch

Training a big CNN needs millions of images and a lot of computing power. In practice, people usually take a CNN that has already been trained on a huge dataset (like ImageNet) and adapt it to their own task. This is called transfer learning, and it's the subject of Module 11.

8.9Building a CNN in Keras

We'll build a small CNN for the handwritten digits, and compare it with a normal dense network trained in exactly the same way. Then we'll test both on the same digits shifted one pixel to the right, to see which one really understands shapes.

digits_cnn.py
import os
os.environ["TF_CPP_MIN_LOG_LEVEL"] = "3"          # hide TensorFlow start-up messages
import numpy as np
from sklearn.datasets import load_digits
from sklearn.model_selection import train_test_split
import keras
from keras import layers

keras.utils.set_random_seed(42)

# 1. Images: 1,797 handwritten digits, each 8x8 pixels with 1 channel (greyscale)
X, y = load_digits(return_X_y=True)
X = (X / 16.0).reshape(-1, 8, 8, 1)               # scale to 0-1, keep the 2D shape
X_train, X_test, y_train, y_test = train_test_split(
    X, y, test_size=0.25, random_state=42, stratify=y)

# 2. A small CNN
cnn = keras.Sequential([
    keras.Input(shape=(8, 8, 1)),
    layers.Conv2D(16, kernel_size=3, padding="same", activation="relu"),
    layers.MaxPooling2D(pool_size=2),             # 8x8 -> 4x4
    layers.Conv2D(32, kernel_size=3, padding="same", activation="relu"),
    layers.MaxPooling2D(pool_size=2),             # 4x4 -> 2x2
    layers.Flatten(),
    layers.Dense(64, activation="relu"),
    layers.Dense(10, activation="softmax"),       # one output per digit
])
cnn.summary(line_length=70)                       # look inside the CNN

# 3. A normal (dense) network for comparison
dense = keras.Sequential([
    keras.Input(shape=(8, 8, 1)),
    layers.Flatten(),                             # 8x8 grid -> 64 numbers in a row
    layers.Dense(128, activation="relu"),
    layers.Dense(64, activation="relu"),
    layers.Dense(10, activation="softmax"),
])

# 4. Train both the same way
for name, model in [("CNN", cnn), ("Dense network", dense)]:
    model.compile(optimizer="adam", loss="sparse_categorical_crossentropy",
                  metrics=["accuracy"])
    model.fit(X_train, y_train, epochs=30, batch_size=32, verbose=0,
              validation_split=0.1)
    _, acc = model.evaluate(X_test, y_test, verbose=0)
    # Test again with every image shifted one pixel to the right
    shifted = np.roll(X_test, shift=1, axis=2)
    shifted[:, :, 0, :] = 0
    _, acc_shift = model.evaluate(shifted, y_test, verbose=0)
    print(f"{name:14} weights={model.count_params():6,}  "
          f"accuracy={acc:.3f}  shifted images={acc_shift:.3f}")
Output
Model: "sequential"
┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━┓
┃ Layer (type)                 ┃ Output Shape          ┃     Param # ┃
┡━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━┩
│ conv2d (Conv2D)              │ (None, 8, 8, 16)      │         160 │
├──────────────────────────────┼───────────────────────┼─────────────┤
│ max_pooling2d (MaxPooling2D) │ (None, 4, 4, 16)      │           0 │
├──────────────────────────────┼───────────────────────┼─────────────┤
│ conv2d_1 (Conv2D)            │ (None, 4, 4, 32)      │       4,640 │
├──────────────────────────────┼───────────────────────┼─────────────┤
│ max_pooling2d_1              │ (None, 2, 2, 32)      │           0 │
│ (MaxPooling2D)               │                       │             │
├──────────────────────────────┼───────────────────────┼─────────────┤
│ flatten (Flatten)            │ (None, 128)           │           0 │
├──────────────────────────────┼───────────────────────┼─────────────┤
│ dense (Dense)                │ (None, 64)            │       8,256 │
├──────────────────────────────┼───────────────────────┼─────────────┤
│ dense_1 (Dense)              │ (None, 10)            │         650 │
└──────────────────────────────┴───────────────────────┴─────────────┘
 Total params: 13,706 (53.54 KB)
 Trainable params: 13,706 (53.54 KB)
 Non-trainable params: 0 (0.00 B)
CNN            weights=13,706  accuracy=0.976  shifted images=0.673
Dense network  weights=17,226  accuracy=0.973  shifted images=0.469

What the output tells us

  • The model summary shows the shapes shrinking and deepening, just like Figure 4: 8 × 8 × 16 feature maps, pooled to 4 × 4, then 4 × 4 × 32, pooled to 2 × 2, then flattened to 128 numbers.
  • The first convolution layer has only 160 weights: 16 filters × 9 weights, plus 16 biases. That's weight sharing at work.
  • On normal test images, both networks score about the same (97.6% vs 97.3%). With tiny 8 × 8 images, a dense network can still cope.
  • On shifted images, the difference is clear. The dense network drops to 46.9%, because it learned "pixel number 27 should be bright". The CNN keeps 67.3%, because its filters look for shapes wherever they are. On real photos, where objects can be anywhere, this advantage is huge.
  • The CNN does it with fewer weights (13,706 vs 17,226).

Make it better yourself

Neither network was trained on shifted images, so both find them harder. Try adding data augmentation (Module 7): randomly shifting, rotating and zooming the training images so the network learns to expect variety. Keras makes this easy with layers like layers.RandomTranslation and layers.RandomRotation.

Running this code

You'll need TensorFlow installed (pip install tensorflow), or you can run it for free in Google Colab, which has it ready to go. This small example trains in under a minute on a normal laptop.

8.10CNNs in the real world

TaskWhat the CNN doesExample
Image classificationSays what's in an imagePhoto apps grouping pictures of dogs, beaches or food
Object detectionFinds and draws boxes around several objectsCars spotting pedestrians and road signs
SegmentationLabels every single pixelOutlining a tumour on a medical scan
Face recognitionChecks whether two faces are the same personFace unlock on phones
Quality controlSpots defects on a production lineFinding cracks or scratches on manufactured parts
Beyond imagesScans any grid-like dataAudio spectrograms, satellite maps, even game boards
StrengthsWeaknesses
Brilliant with images and other grid-shaped dataNeed lots of labelled images to train from scratch
Far fewer weights than dense networks, thanks to weight sharingComputationally heavy: big models need GPUs
Find patterns wherever they appear in the imageHard to explain exactly why a decision was made
Learn their own features, with no hand-designed filtersCan be fooled by tiny, carefully designed changes to an image

Since around 2020, a newer design called the Vision Transformer (based on Module 10) has matched or beaten CNNs on some large image tasks. But CNNs remain widely used, especially where speed and smaller datasets matter.

SummaryKey takeaways

  • Computers see images as grids of numbers: 1 channel for greyscale, 3 for colour.
  • Dense networks struggle with images: too many weights, no sense of neighbours, and position-dependent learning.
  • Convolution slides a small filter over the image, multiplying and adding, to make a feature map.
  • Filters are learned, and shared across every position, so CNNs need far fewer weights.
  • Padding controls whether the output shrinks; stride controls how far the filter moves.
  • Max pooling shrinks feature maps by keeping the strongest value in each block.
  • A CNN stacks conv → ReLU → pool blocks, then flattens into dense layers with softmax.
  • Layers learn a hierarchy: edges, shapes, parts, then whole objects.

Check your understanding

Your neural networks roadmap

  1. 01
    Introduction to Neural NetworksWhat neural networks are and how they work
  2. 02
    The Perceptron and the Artificial NeuronBuild a single neuron from scratch
  3. 03
    Activation FunctionsSigmoid, tanh, ReLU and softmax
  4. 04
    Loss Functions and Gradient DescentHow networks measure and reduce error
  5. 05
    BackpropagationHow every weight learns its share of the blame
  6. 06
    Building Networks with KerasYour first deep learning library
  7. 07
    Training Better NetworksOverfitting, dropout, batch normalisation, early stopping
  8. 08
    Convolutional Neural NetworksYou are here
  9. 09
    Recurrent Neural Networks and LSTMsWorking with sequences
  10. 10
    Transformers and AttentionThe architecture behind ChatGPT and Claude
  11. 11
    Transfer Learning and Pretrained ModelsStanding on the shoulders of giants
  12. 12
    Capstone ProjectBuild and present a deep learning project