MAI5063 Advances in Deep Learning · Lab 3b · lighter version of Lab 3

From pixels to a convolutional net

In Lab 1 the MLP flattened every image. A CNN keeps the 2D structure. Explore convolution, pooling, Dropout, BatchNorm and augmentation here, then build and compare CNNs on Fashion-MNIST in Colab. This is the same set of tasks as Lab 3 on smaller grayscale images, so the data downloads and the models train much faster.

Platform Colab + TensorFlow/Keras Runtime GPU optional Data Fashion-MNIST, 28×28 grayscale, 10 classes

By the end you can

  • Build a CNN in Keras and say what each layer type does.
  • Predict how convolution, pooling, activation and regularisation change the curves.
  • Compare model variants and justify a choice with validation evidence.
  • Use Dropout, BatchNorm and augmentation to reduce overfitting.
  • Read training and validation curves as diagnostic evidence.

Before the lab

Task 4 · Conv2D and MaxPooling2D

A kernel slides over the image and scores one pattern

Each output cell is a weighted sum of a small window of the input, and every position uses the same kernel weights. Pick an image and a kernel, then click an output cell to see its window and arithmetic.

O(i, j) = Σₘ Σₙ I(i+m, j+n) · K(m, n) output size = floor((I + 2P − K) / S) + 1
Image
Kernel
Padding P
Stride S
After conv
Input (8×8)
Kernel (3×3, editable)
Feature map

Keras computes this sum without flipping the kernel, exactly as the formula is written. Positive output is shown in red, negative in grey. The vertical-edge kernel lights up where dark meets light going left to right.

Tasks 4 and 9 · Count before you train

Shapes and parameters

Work out the shape after each layer and where the parameters sit. Compare your numbers with model.summary() in Colab.

Output size calculator

Weight sharing

A 3×3 Conv2D with same padding versus a Dense layer connecting the same inputs to the same outputs. The defaults are Conv2 of the baseline, which works on 14×14 maps.

Baseline CNN, layer by layer

Tasks 6 and 7 · Regularisation

Dropout and BatchNorm change how training behaves

Neither layer adds a feature detector. Dropout makes the network harder to memorise with. BatchNorm keeps each layer's inputs in a steady range so training is faster and smoother.

Dropout

h̃ᵢ = hᵢ · mᵢ, mᵢ ~ Bernoulli(1 − p)

Keras uses inverted dropout. Surviving units are scaled by 1/(1 − p) during training, so on average the layer's output matches inference and nothing changes when you evaluate.

Batch Normalisation

u′ = γ · (u − μ) / √(σ² + ε) + β

Whatever the earlier layers do to the batch, the output is re-centred and re-scaled. γ and β are learned, so the network can undo the normalisation where it helps. Each BatchNorm layer has 4 values per channel: γ and β are trained, the running mean and variance are not.

Task 8 · Data augmentation

Augmentation makes new training images, never new test images

The network sees a slightly different version of each image on every epoch, so it cannot rely on one exact pose. In Keras the augmentation layers run only during training and pass images through unchanged at inference. This sneaker is drawn on the page as a stand-in for a Fashion-MNIST image. It points right, so you can see when a flip happens.

Original
Eight random versions

RandomRotation(0.05) turns by up to ±5% of a full circle, which is ±18°. RandomZoom(0.1) zooms in or out by up to 10%. Empty corners are filled by reflecting the image.

Validation and test images stay untouched so the score measures real, unaltered images and can be compared between runs.
Tasks 5 to 10 · Predict, then run

Try the experiments before Colab

Choose a setup and watch the training and validation curves. The curves are a simulation tuned to typical Fashion-MNIST behaviour for this architecture. Your Colab numbers will differ, but the ordering and the shapes should look familiar. The baseline is drawn dashed for comparison.

Setup

Presets
Dropout p
BatchNorm
Augmentation
Filters
LR scheduler
Plot

Training vs validation

trainingvalidationbaseline validation
Hands-on · Google Colab

The Colab tasks

Copy each cell into Colab and run them in order. Setup, data loading and preprocessing are one card because they run together. Task numbers match Lab 3. Differences between variants are smaller on Fashion-MNIST, so repeat a run before you decide one setup is better.

Reflection

Write it in your own words

Answers are saved in this browser only. Open a hint after you have tried.