Lecture 7

Convolutional neural networks

Introduction to Machine Learning
KU Leuven, campuses Diepenbeek and Geel
Academic year 2026–2027

Where exercise 3 stopped

The MLP of exercise 3 read Fashion-MNIST as 784 numbers:

  • every pixel has its own weights into every hidden unit,
  • a pixel and its neighbour are as unrelated as two opposite corners,
  • a pattern learned in one place is lost a few pixels further.
A grid of pixels is flattened into one long row of 784 numbers. A red pixel and the blue pixel directly below it end up 28 places apart in the row.
Today: a layer that looks at small patches, with the same weights everywhere.

Today in three choices

Model

convolutional layers

the same filter at every position

Loss

cross-entropy

over ten classes, the softmax of lecture 3

Optimiser

Adam

unchanged since lecture 6

The plan

  1. Convolution
  2. CNN architectures
  3. CNNs in practice

Exercise 4 writes a convolution in numpy and trains a CNN with blocks 1 and 2.

Block 1

Convolution

An image as numbers

A test image of a trouser, 28 by 28 grey pixels, with a framed 8 by 8 patch at the left edge of a leg. Beside it the patch as numbers: zeros in the four left columns, where the background is, and values up to 248 on the right, where the fabric is.
A Fashion-MNIST image is a 28 × 28 grid of brightness values from 0 to 255. The edge of the trouser is where the numbers jump.

Dense layers scale badly

One dense layer of 128 units:

  • on 28 × 28 pixels: \(784 \cdot 128 + 128 = 100{,}480\) parameters,
  • on a colour photo of 224 × 224: \(150{,}528 \cdot 128 + 128 = 19{,}267{,}712\).
Parameters on a log scale. Dense layers into 128 units: 100,480 on 28 by 28 pixels and 19,267,712 on a colour photo of 224 by 224. A convolutional layer of 128 filters of 3 by 3: 1,280 on a grey image and 3,584 on a colour one.
Red: dense, growing with the pixels. Blue: a convolutional layer of today, the same for any image size.

Scrambled pixels

Left, the trouser image. Right, the same 784 pixels in a fixed random order: noise in which no trouser can be seen.
One fixed reordering of the pixels, applied to every image for training and testing. The MLP reaches 0.85 on the validation images in both cases.

What an MLP unit looks for

The 784 weights into eight hidden units of the trained MLP, each drawn as a 28 by 28 image in red for positive and blue for negative weights. The patterns are blotchy and fixed to positions in the image.
The 784 weights into eight hidden units of the MLP, drawn at the pixel each one multiplies. Red weights raise the unit, blue weights lower it.

A shift breaks the MLP

Test accuracy of an MLP against a shift of the test images to the right by 0 to 4 pixels. The accuracy falls from 0.82 without shift to 0.76 at 1 pixel, 0.51 at 2, 0.31 at 3 and 0.23 at 4 pixels. Guessing gives 0.1.
An MLP trained on centred images, tested on the same images moved right: 0.82 at no shift, 0.51 at 2 pixels, 0.31 at 3.

Convolution

A 5 by 5 input with zeros in its two left columns and ones in its three right columns, and a 3 by 3 filter with -1, 0 and 1 in every row. The window covers the top left 3 by 3 patch. The sum of the products is 3, the first cell of the 3 by 3 output.
Put a 3 × 3 filter on the top-left patch, multiply cell by cell, and add: the first output.

One pixel to the right

The same input and filter, with the window moved one pixel to the right. The sum is again 3, the second cell of the output.
The same nine weights on the next patch. It still holds the step, so the output is again 3.

The end of the row

The window on the three right columns, all ones. The sum is 0, the third cell of the first output row.
Over the flat region the output is 0. The window then moves one row down and starts again at the left.

The whole feature map

A 5 by 5 input with zeros in its two left columns and ones in its three right columns. A 3 by 3 filter with -1, 0 and 1 in every row. The 3 by 3 window in the top left corner of the input gives 3, the top left value of the 3 by 3 output. The output holds 3, 3 and 0 in every row.
A small filter slides over the image. Each output is the inner product of the filter with the patch under it: the same few weights at every position.

Your turn

A 4 by 4 input with rows 1 2 0 1, 0 1 3 1, 2 0 1 0 and 1 1 0 2, a 2 by 2 filter with rows 1 -1 and 1 -1, and an empty 3 by 3 output.

Compute the 3 × 3 output.

−201
1−33
20−1

Convolution in numpy

Pseudocode

k ← width of the filter K
n ← width of X
m ← n − k + 1
for i = 0 … m − 1:
  for j = 0 … m − 1:
    P ← k × k patch at (i, j)
    Y[i, j] ← Σ P ⊙ K
return Y, m × m

numpy

def conv2d(X, K):
    k = K.shape[0]
    m = X.shape[0] - k + 1
    Y = np.zeros((m, m))
    for i in range(m):
        for j in range(m):
            P = X[i:i + k, j:j + k]
            Y[i, j] = np.sum(P * K)
    return Y

An edge detector

Left, the trouser image. Middle, the 3 by 3 filter with -1 in its left column, 0 in its middle column and +1 in its right column. Right, the 26 by 26 feature map: red along the left edges of both legs, where the image brightens from left to right, blue along the right edges, and white on the flat background and fabric.
A filter set by hand responds to vertical edges: positive where the image brightens to the right, negative where it darkens. The 28 × 28 image gives a 26 × 26 map.

Four filters, four questions

A trouser and a sneaker, each under four filters set by hand. Vertical edges: red and blue bands along the legs of the trouser. Horizontal edges: bands along the sole and top of the sneaker. Blur: a softened copy. Sharpen: a copy with crisper edges.
Vertical edges, horizontal edges, a blur of nine equal weights and a sharpening filter. Each filter asks one question at every position.

Weight sharing

Left, a dense layer from 6 inputs to 4 outputs with 24 grey connections, one weight each. Right, a convolutional layer from 6 inputs to 4 outputs, each output reading its 3 neighbouring inputs with the same three weights a, b and c, drawn in the same three colours at every position.
A dense layer gives every connection its own weight. A convolutional layer reuses the few weights of its filter at every position.

The map moves with the image

Top, the trouser and its vertical edge map. Bottom, the trouser moved 3 pixels to the right and its edge map, which is the same map moved 3 pixels to the right. A dashed line marks the same edge in both maps.
Move the image 3 pixels right and its feature map moves 3 pixels right, otherwise unchanged. The dashed lines mark the same edge.

Without padding the map shrinks

The trouser image four times. A blue square marks the region that keeps an output after 1, 2, 3 and 10 convolutions of 3 by 3 without padding: 26 by 26, 24 by 24, 22 by 22 and 8 by 8. The lost border is shaded red.
Each 3 × 3 convolution without padding loses a ring of one pixel: 26 × 26 after one layer, 8 × 8 after ten.

Padding

A 5 by 5 input with a ring of zeros around it. The 3 by 3 window sits on the top left corner, partly on the zeros, and gives the top left cell of a 5 by 5 output.
A ring of zeros lets the window sit on the border pixels, so that the output keeps the size of the input.

Stride

Stride 2: the 3 by 3 window at the left of a 5 by 5 input and, dashed, two pixels further right. The output is 2 by 2.
Output width for input \(n\), filter \(k\), padding \(p\), stride \(s\): \(\left\lfloor \frac{n + 2p - k}{s} \right\rfloor + 1\). With \(k = 3, p = 1, s = 1\) the size stays \(28 \to 28\), and a stride of 2 halves it.

A colour image is three grids

A colour photo of a pagoda and its red, green and blue channels, each a grid of brightness values from 0 to 255.
A colour photo stores red, green and blue brightness at every pixel: three grids, called channels. Photo by danielbuechele, CC BY 2.0, a sample image of scikit-learn.

One filter reads every channel

Three stacked input maps, red, green and blue. One filter made of three 3 by 3 slices reads the same window on all three maps and gives one number of a single output map.
A filter holds one 3 × 3 slice per input channel. It adds the 27 products and a bias into one number per position.

Many filters, many maps

An input of 3 channels, four filters of 3 by 3 by 3 in four colours, and an output of 4 maps, one per filter. 3 times 3 times 3 times 4 plus 4 is 112 weights.
Each filter gives one output map, so 4 filters give 4 output channels, which the next layer reads as its input channels.

Counting the weights

A filter spans all \(C_{\text{in}}\) input channels, and each of the \(C_{\text{out}}\) filters gives one output channel:

\[ k^2 \, C_{\text{in}} \, C_{\text{out}} + C_{\text{out}} \text{ parameters} \]

Independent of the size of the image: every position shares the same filters.

Today's CNN: \(3^2 \cdot 1 \cdot 8 + 8 = 80\) and \(3^2 \cdot 8 \cdot 16 + 16 = 1{,}168\) parameters in its two convolutional layers.

nn.Conv2d

layer = nn.Conv2d(1, 8, 5,
                  padding=2)
X = torch.zeros(64, 1, 28, 28)
Y = layer(X)
Y.shape  # 64 × 8 × 28 × 28

Images enter as N × C × H × W: batch, channels, height, width.

The arguments are \(C_{\text{in}}\), \(C_{\text{out}}\) and \(k\), then stride and padding.

A batch of 64 grey images, each 1 channel of 28 by 28, labelled N, C, H and W, passes nn.Conv2d(1, 8, 5, padding=2) and becomes 64 stacks of 8 maps of 28 by 28.

Checkpoint

This layer of 8 filters of 5 × 5, with padding 2 and stride 1, reads a 28 × 28 greyscale image. What is the shape of its output, and how many parameters does it hold?

Block 2

CNN architectures

Max pooling by hand

A 4 by 4 feature map with rows 1 3 0 2, 5 2 1 0, 0 1 4 6 and 2 0 3 1, cut into four 2 by 2 windows. Their maxima, 5, 2, 2 and 6, form the 2 by 2 pooled map.
Each 2 × 2 window keeps its largest value. A 4 × 4 map becomes 2 × 2, with no weights to learn.

Pooling

Left, the edge feature map after ReLU, 26 by 26, red along the left edges of the legs. Right, its 2 by 2 max pooling, 13 by 13: the same two red bands at half the resolution.
2 × 2 max pooling keeps the strongest response in each window. It halves width and height and has no parameters.

Pooling absorbs part of a shift

Top row, before pooling: the edge map of the trouser after ReLU, the map of the trouser moved 1 pixel, and their difference, with a correlation of 0.45 between the two maps. Bottom row, the same after 2 by 2 max pooling, with a correlation of 0.66.
The trouser and the trouser moved 1 pixel. Before pooling no active cell keeps its value, after pooling a quarter do, and the correlation of the two maps rises from 0.45 to 0.66.

Receptive fields

One row of the image with three layers of units. A unit of layer 1 is connected to three input pixels. A unit of layer 2 is connected to three units of layer 1, and so it sees five input pixels.
Each layer of 3 × 3 filters widens the patch of the image a unit sees by 2 pixels.

Receptive fields on the image

The trouser with four nested squares: 3 by 3 for a unit of the first convolution, 4 by 4 after the first pooling, 8 by 8 for the second convolution and 10 by 10 after the second pooling.
The patch one unit of the lecture CNN reads: 3, 4, 8 and 10 pixels wide after each stage. After a pooling, a 3 × 3 convolution widens it by 4 pixels rather than 2.

Conv, ReLU, pool

The CNN of the lecture from left to right: the input 1 by 28 by 28; a convolution of 8 filters of 3 by 3 with ReLU giving 8 by 28 by 28, 80 parameters; max pooling to 8 by 14 by 14; a convolution of 16 filters with ReLU giving 16 by 14 by 14, 1,168 parameters; max pooling to 16 by 7 by 7; flattening and a linear layer giving 10 scores, 7,850 parameters. 9,098 parameters in all.
The pattern repeats: convolution, ReLU, pooling. The maps get smaller and more numerous, and a linear layer turns the last ones into 10 class scores.

One image through the network

The trouser image, then four of the maps at each stage of the trained CNN: 28 by 28 after the first convolution, 14 by 14 after pooling and after the second convolution, 7 by 7 after the second pooling. On the right, the softmax probabilities of the ten classes: 1.00 for Trouser and close to 0 for the rest.
The trouser in the trained CNN: four of the maps at each stage, then the softmax over the ten classes, 1.00 on Trouser.

Where the parameters are

Parameters per layer of the lecture CNN: 80 in the first convolution, 1,168 in the second, 7,850 in the linear layer. Below, 101,770 for the whole MLP.
7,850 of the CNN's 9,098 parameters sit in its last linear layer. The MLP of the shift figures holds 101,770.

One weight, many uses

A row of 6 inputs and 4 outputs of a convolution with the filter a, b, c. The four connections that use the weight a are drawn thick and red. The gradient of a is the sum of its four uses.
Backpropagation adds the gradient of every position where a weight was used. The loss, cross-entropy, and the optimiser, Adam, stay those of lecture 6.

A CNN in PyTorch

Layers

image, 1 × 28 × 28
conv 3×3, 8 filters → 8×28×28
ReLU
max pool → 8×14×14
conv 3×3, 16 filters → 16×14×14
ReLU
max pool → 16×7×7
flatten → 784
linear → 10 scores

PyTorch

model = nn.Sequential(
    nn.Conv2d(1, 8, 3, padding=1),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Conv2d(8, 16, 3, padding=1),
    nn.ReLU(),
    nn.MaxPool2d(2),
    nn.Flatten(),
    nn.Linear(16 * 7 * 7, 10),
)

Training it

loss_fn = nn.CrossEntropyLoss()
opt = torch.optim.Adam(
    model.parameters(), lr=2e-3)
history = train(model, loader,
    loss_fn, opt, epochs=8,
    val_loader=val_loader)

train from mlcourse.training runs the loop of lecture 6 and returns the history per epoch.

8,000 training and 2,000 validation images of the subset of exercise 3, split with the lecture's own seed.

CNN against MLP

Accuracy against epoch for 8 epochs. The CNN's training accuracy rises from 0.67 to 0.89. Its validation accuracy rises from 0.79 to 0.87. The MLP's validation accuracy stays between 0.81 and 0.86 and ends at 0.85.
After 8 epochs the CNN reaches 0.87 on the validation images, the MLP 0.85, with 9,098 parameters against about 102,000.

What the CNN gets wrong

The confusion matrix of the CNN on the 2,000 test images, true class against predicted class. The largest errors: 45 T-shirts, 42 pullovers and 22 coats predicted as shirts. Beside it, six misclassified images with their true and predicted classes, mostly tops.
0.86 on the 2,000 test images. Most errors are tops: 45 T-shirts and 42 pullovers taken for shirts, and shirts are right 71 times in 100.

Shifts, again

Test accuracy against a shift of 0 to 4 pixels for the MLP and the CNN. The CNN starts at 0.86 and falls to 0.83 at 1 pixel, 0.69 at 2, 0.40 at 3 and 0.26 at 4. The MLP falls from 0.82 to 0.51 at 2 pixels.
At 2 pixels the CNN keeps 0.69, the MLP 0.51. At 3 pixels both collapse: 0.40 and 0.31.

Feature maps

The trouser image and the outputs of the trained CNN. The eight maps of the first layer, 28 by 28, respond to the edges, the background or the fabric of the trouser. The sixteen maps of the second layer, 14 by 14, respond to combinations of these, such as the gap between the legs; one of them is empty.
The first layer responds to edges and regions, the second to combinations of them, at half the resolution.

Feature maps of three classes

A trouser, a sneaker and a bag, each with the eight maps of the first layer of the trained CNN. The same map behaves alike on all three: maps 2 and 4 light up the background, the others the edges or the fabric.
The eight first-layer maps on a trouser, a sneaker and a bag. Each map asks the same question of every image: maps 2 and 4 light up the background, the others edges or fabric.

Learned filters

Left, the edge filter set by hand in block 1, blue on the left and red on the right. Right, the eight 3 by 3 filters of the first layer of the trained CNN, in red for positive and blue for negative weights. Several have a positive and a negative side.
Left, the edge filter set by hand in block 1. Right, the eight filters the first layer learned. Several hold a positive and a negative side like it.

From LeNet to ResNet

A timeline. LeNet-5, 1998: digits of 32 by 32, 7 layers, about 60,000 parameters, 0.95% error on MNIST. AlexNet, 2012: photos of 224 by 224, 8 layers on two GPUs, 60 million parameters, 15.3% top-5 error. VGG, 2014: only 3 by 3 filters, 16 or 19 layers, 138 million parameters, 7.3% top-5 error. ResNet, 2015: residual blocks, up to 152 layers, fewer operations than VGG, 3.57% top-5 error.
Larger images, more layers, and a top-5 error on ImageNet that fell from 15.3% to 3.57% in three years.

LeNet

LeNet-5 as boxes: input 1 by 32 by 32, convolution of 6 filters of 5 by 5 to 6 by 28 by 28, average pooling to 6 by 14 by 14, convolution of 16 filters to 16 by 10 by 10, average pooling to 16 by 5 by 5, dense layers of 120 and 84 units, and 10 scores. About 60,000 parameters.
LeCun and colleagues, 1998: conv, pool, conv, pool, then three dense layers, about 60,000 parameters. It read the amounts on bank cheques in the United States.

VGG

VGG-16 as bars: five stages of 3 by 3 convolutions with 64, 128, 256, 512 and 512 filters, each followed by max pooling, from 224 by 224 down to 7 by 7, then dense layers of 4096, 4096 and 1000 units. 16 weight layers, about 138 million parameters, 124 million of them in the dense layers.
Simonyan and Zisserman, 2014: only 3 × 3 filters, in stacks, 16 weight layers, about 138 million parameters. Deeper was better, up to a point: a plain network of 56 layers trained worse than one of 20 (He and colleagues, 2015).

Residual block

A residual block: the input x passes a 3 by 3 convolution and a ReLU, which compute F(x). An identity path carries x around them. Both are added: h = x + F(x).
\[ \mathbf h = \vx + F(\vx) \]

The layers learn a correction to their input. The gradient reaches earlier layers through the identity path.

ResNet, 2015: residual blocks stacked to 152 layers.

Plain and residual

Training loss against epoch for two networks of a stem and 16 further convolutional layers on 2,000 images. The plain network stays at 2.30, the loss of guessing among ten classes, for all six epochs. The residual network starts at 2.95 and falls to 0.76.
A 3 × 3 stem and sixteen 3 × 3 layers of 8 channels, on 2,000 images. The plain network never leaves \(\ln 10 = 2.30\), the loss of guessing. The residual one falls to 0.76.

Plain and residual at every depth

Training loss after 6 epochs against the number of layers, 2, 4, 8 and 16. Plain: 0.72, 0.70, 0.90 and 2.30, the loss of guessing. Residual: 0.64, 0.64, 0.61 and 0.76.
The same networks at 2 to 16 layers, after six epochs. The plain one gets worse from 8 layers on and learns nothing at 16, the residual one stays between 0.61 and 0.76.

Where the gradient goes

The size of the weight gradient of each of the 17 convolutional layers at the start of training, on a log scale. Plain: from 0.014 at the last layer down to 1.6e-9 at the first. Residual: between 17 and 37 at every layer.
The gradient of each layer at the start of training. In the plain network it shrinks from 0.014 at the last layer to \(1.6 \cdot 10^{-9}\) at the first, while in the residual one it lies between 17 and 37 at every layer.

Checkpoint

Two stacked 3 × 3 convolutions or one 5 × 5 convolution, each with \(C\) channels in and out. How large a patch does each see, and which holds fewer weights?

Two 3 × 3 or one 5 × 5

Left, two stacked 3 by 3 layers: a unit of layer 2 reads 3 units of layer 1, which read 5 input pixels, with 18C squared weights and two ReLUs. Right, one 5 by 5 layer: a unit reads 5 pixels, with 25C squared weights and one ReLU.
Both see a 5 × 5 patch. The stack holds 18C² weights against 25C² and adds a second ReLU, which is why VGG uses only 3 × 3 filters.

Break

Ten minutes

Block 3

CNNs in practice

Data augmentation

The trouser image and seven copies in two rows of four, each moved by up to three pixels in a random direction, with black filled in at the edges.
Each batch shows the network randomly moved copies of its images. The label stays the same, so the network must learn that a shift does not matter.

Other transformations

A sneaker and a bag, each flipped left to right, rotated by 20 degrees, cropped and enlarged, and made brighter: all still a sneaker and a bag. Beside them, a 6 and the same 6 turned over, which reads as a 9.
Flips, small rotations, crops and brighter copies keep the label of a sneaker or a bag. A 6 turned over is a 9, so each task decides which transformations keep its labels.

Augmentation buys invariance

Test accuracy against shift for the MLP, the CNN and the CNN trained on shifted images. The last starts at 0.82, stays at 0.81 at 2 pixels, 0.78 at 3 and 0.70 at 4, while the CNN falls to 0.40 at 3 pixels.
The CNN trained on images shifted by up to 3 pixels: 0.78 at a shift of 3, against 0.40. It costs 3 points on centred images, 0.82 against 0.86.

Transfer learning

Two rows. First, the lecture CNN with two convolutional and two pooling layers and a linear layer of 10 outputs is trained on the seven classes of clothes and bags. Second, its weights are copied into a network for sandals, sneakers and ankle boots, whose convolutional layers are frozen and whose linear layer is replaced by a new one with three outputs, the only part trained.
Filters learned on one task are reused for another: keep the convolutional layers, replace the head.

What the frozen layers see

A sandal, a sneaker and an ankle boot, each with the eight maps of the first layer of the CNN trained without footwear. The maps trace the outlines, the straps and the soles of the shoes.
The first layer, trained on the seven classes without footwear, on three shoes it never saw. Its maps still trace their outlines, straps and soles.

Freeze and replace

model = lecture_cnn()
model.load_state_dict(pretrained)
for p in model.parameters():
    p.requires_grad = False
model[-1] = nn.Linear(784, 3)

Frozen weights get no gradient. The new layer does, since it is created after the loop.

The optimiser receives only the head: model[-1].parameters().

Few labels

Test accuracy on sandals, sneakers and ankle boots against the number of labelled images per class, from 1 to 50, averaged over 8 draws. The pretrained layers with a new head lie above training from scratch at every count: 0.65 against 0.61 at 1 image, 0.81 against 0.75 at 5, 0.86 against 0.82 at 10, 0.92 against 0.89 at 50.
Three footwear classes, averaged over 8 draws of the labelled images. Reusing the layers gains 2 to 6 points up to 20 images per class: 0.81 against 0.75 at 5.

Inside a bearing

A rolling bearing seen from the front: outer ring, inner ring and eight balls, with damage on the outer race. The shaft turns at 25 Hz. Beside it, a signal with a short decaying impulse every 11 ms, one each time a ball passes the damage.
Damage on the outer race: every ball that rolls over it gives a short impulse, about every 11 ms at a shaft speed of 25 Hz.

Vibration signals

Two vibration signals of 85 milliseconds, both with an RMS of 1.50 mm/s. The healthy one shows a slow shaft oscillation with noise. The faulty one shows the same oscillation with a sharp impulse about every 11 milliseconds.
The bearings of lectures 1 to 6, now as raw signals. Both have an RMS of 1.50 mm/s. The faulty one carries an impulse each time a ball passes the damage.

The RMS cannot tell them apart

Histograms of the vibration RMS of the 400 training signals, healthy and faulty, between about 0.6 and 4.6 mm/s. The two histograms overlap almost completely.
The RMS of the 400 training signals by class. The histograms overlap, and a logistic regression on the RMS reaches 0.475 on the test signals: a guess.

1-D convolution by hand

A signal of 8 samples, 0 0 1 3 1 0 0 0, a filter of 3 samples, 1 3 1, and the output of 6 values, 1 6 11 6 1 0. The window at samples 2 to 4 gives 1 times 1 plus 3 times 3 plus 1 times 1, which is 11.
The filter slides along the signal one sample at a time, with the same inner product as in two dimensions. The output peaks, at 11, where the signal looks like the filter.

1-D convolution

Top, the faulty signal. Right, a filter of 16 samples shaped like one decaying impulse. Bottom, the output of sliding the filter along the signal: flat, with a sharp peak at each impulse.
A filter of 16 samples, shaped like one impulse, slid along the signal. The output peaks wherever the signal matches it.

A 1-D CNN

Layers

signal, 1 × 1024
conv 16, 4 filters, stride 2
  → 4 × 505
ReLU, max pool 4 → 4 × 126
conv 8, 8 filters → 8 × 119
ReLU
max over time → 8
linear → 2 scores

PyTorch

model = nn.Sequential(
    nn.Conv1d(1, 4, 16, stride=2),
    nn.ReLU(),
    nn.MaxPool1d(4),
    nn.Conv1d(4, 8, 8),
    nn.ReLU(),
    nn.AdaptiveMaxPool1d(1),
    nn.Flatten(),
    nn.Linear(8, 2),
)

Max over time

Top, a healthy and a faulty signal. Bottom, map 3 of the second layer of the trained 1-D CNN on each: drawn against time: low everywhere on the healthy signal, with a maximum of 1.46, and a peak at most impulses of the faulty signal, with a maximum of 10.88.
Map 3 of the second layer, the one whose weight in the last layer votes most for a fault. It peaks at most impulses: its maximum is 10.88 on the faulty signal and 1.46 on the healthy one.

A 1-D CNN on bearings

Left, test accuracy of the 1-D CNN on 400 signals against the epoch: above 0.99 from epoch 4, ending at 0.9975. A logistic regression on the RMS reaches 0.475. Right, the four learned filters of 16 samples, drawn as waveforms.
On 400 test signals the 1-D CNN classifies 399 correctly, while a classifier on the RMS guesses: 0.475. Right, its four learned filters.

Max over position, on clothes

Test accuracy against shift. The lecture CNN falls from 0.86 to 0.40 at 3 pixels. The CNN trained on shifted images keeps 0.78 at 3 pixels. The CNN whose last layer reads the maximum of each map over all positions starts at 0.72 and falls slowly to 0.54 at 3 pixels.
The lecture CNN with the maximum of each map over all positions, as in the 1-D CNN: 0.54 at a shift of 3 pixels against 0.40, but 0.72 on centred images against 0.86.

Which one when

A chart of three questions. Do the test data vary in a way the training data do not? Then data augmentation, at the cost of slower training and a choice of transformations that keep the label. Few labels, and a network pretrained on similar images? Then transfer learning, at the cost of the pretrained architecture. A signal whose pattern can occur at any time? Then 1-D convolution, which needs more data than features made by hand.

Checkpoint

You have 200 labelled photos of cracked and intact turbine blades. Which part of a pretrained CNN do you retrain, and why?

The turbine blades

A pretrained CNN as a row of boxes: conv blocks 1 to 3 frozen, conv block 4 perhaps fine-tuned with a small learning rate, and a new head for cracked and intact, trained.
Freeze the early layers, train a new head, and perhaps fine-tune the last block with a small learning rate. Augment with flips and rotations if a turned blade is still a blade.

Takeaways

  1. A convolutional layer applies small filters at every position, with shared weights.
  2. Pooling and depth widen what a unit sees.
  3. Residual connections let deep networks train.
  4. Augmentation and transfer learning stretch a small dataset.
  5. The same layer works in one dimension, on signals.

Lecture 7 in three choices

Model

convolutional layers

the same filter at every position

Loss

cross-entropy

over ten classes

Optimiser

Adam

in PyTorch, through train

Further reading

All three books are free to read online.

Next

Exercise 4

In week 8: a convolution in numpy, a CNN on Fashion-MNIST with train, then a recurrent network on electricity load.

It uses lectures 7 and 8.

Lecture 8

Recurrent neural networks: structure for time.