Part X — Computer Vision: CNNs, ResNets, Object Detection & Segmentation · Chapter 10

Train a CNN and inspect its learned feature filters

Hook

Chapter 3's kernel, dense weights, and biases were all hand-picked to make one particular pipeline work on one particular image. Real networks don't get that luxury — they start from random numbers and have to earn every weight through training. This capstone does exactly that, live, in the browser.

Intuition
-0.1-0.2-0.30.4-0.20.10.5-0.60.1
Learned filter 0
-1.40.00.6-0.9-0.7-0.1-0.7-0.00.5
Learned filter 1
-1.00.01.0-1.00.01.0-1.00.01.0
Chapter 1's hand-designed kernel
true: Vertical edge
predicted: Vertical edge (57%)
true: Vertical edge
predicted: Vertical edge (55%)
true: Horizontal edge
predicted: Vertical edge (55%)
true: Horizontal edge
predicted: Horizontal edge (59%)
epoch 0 — loss = 0.6208

Four tiny 6×6 images, two classes: a vertical edge (the boundary runs top-to-bottom) and a horizontal edge (the boundary runs left-to-right). Two 3×3 filters, initialized to small random numbers, feed a small dense layer through ReLU (short for Rectified Linear Unit) and max pooling — the exact shape from Chapter 3, but every weight starts unlearned. Click train and watch the loss fall as the filters reorganize themselves into something that actually tells the two classes apart.

Formalize

The architecture is unchanged from Chapter 3 — image → convolution → ReLU → pool → flatten → dense → softmax — but now there are two filters instead of one, and a real loss to minimize:

L=logptrue class\mathcal{L} = -\log p_{\text{true class}}
  • L\mathcal{L} — the loss being minimized during training.
  • ptrue classp_{\text{true class}} — the softmax probability the network assigns to the correct class.
  1. This is cross-entropy loss

    It's the cross-entropy loss from Part II, computed over the softmax of the two output logits.

  2. Backprop threads through every stage

    Backpropagation runs through every stage, in reverse order:

    1. The softmax gradient
    2. Back through the dense weights
    3. Back through max-pooling, which routes each gradient only to the position that actually won each window
    4. Back through ReLU, zero wherever the pre-activation wasn't positive
    5. Finally back into the two 3×3 kernels themselves

    The same chain rule as always, just longer.

Play
true: Vertical edge
predicted: Vertical edge (57%)
true: Vertical edge
predicted: Vertical edge (55%)
true: Horizontal edge
predicted: Vertical edge (55%)
true: Horizontal edge
predicted: Horizontal edge (59%)
-0.1-0.2-0.30.4-0.20.10.5-0.60.1
Learned filter 0
-1.40.00.6-0.9-0.7-0.1-0.7-0.00.5
Learned filter 1
-1.00.01.0-1.00.01.0-1.00.01.0
Chapter 1's hand-designed kernel
epoch 0 — loss = 0.6208

Reset, then train one step at a time. Early on, the two random filters produce feature maps that don't separate the classes — predictions hover near 50%. After a few steps, watch one filter's weights drift toward something with a clear positive-to-negative split across a row or column — a shape it invented on its own, not copied from anywhere, because it's the shape that actually helps tell a vertical edge from a horizontal one.

Worked example
  1. Chapter 1's hand-designed kernel

    The vertical-edge kernel (101101101)\begin{pmatrix} -1 & 0 & 1 \\ -1 & 0 & 1 \\ -1 & 0 & 1 \end{pmatrix}: every row sums to zero, and the sign flips from one side of the kernel to the other.

  2. What a trained filter converges to

    A filter that learns to solve this task tends toward the same kind of structure — not the identical numbers, but the same shape of solution: something whose row-sums or column-sums cancel out for a constant patch, and don't cancel when a patch straddles an edge. Nobody told the network that; gradient descent found it because it's what reduces the loss.

Checkpoint

Train the network until its loss on the digit set drops below 0.01.

true: Vertical edge
predicted: Vertical edge (57%)
true: Vertical edge
predicted: Vertical edge (55%)
true: Horizontal edge
predicted: Vertical edge (55%)
true: Horizontal edge
predicted: Horizontal edge (59%)
-0.1-0.2-0.30.4-0.20.10.5-0.60.1
Learned filter 0
-1.40.00.6-0.9-0.7-0.1-0.7-0.00.5
Learned filter 1
-1.00.01.0-1.00.01.0-1.00.01.0
Chapter 1's hand-designed kernel
epoch 0 — loss = 0.6208
Click train to try it
Summary
L=logptrue class\mathcal{L} = -\log p_{\text{true class}}

Every weight in this network — both kernels, both biases, the dense layer — was found by gradient descent, not hand-picked. That's the entire difference between Chapter 3 and this capstone: same architecture, same forward pass, but the numbers came from data instead of from a human. Part IV continues from images to sequences next: the same idea of a small, structured architecture, learned end-to-end, applied to data where order is the whole point.