Week 8

Written by:

This week’s reading was Ujjwal Karn’s An Intuitive Explanation of Convolutional Neural Networks, and the coding half was building on ml5’s “Training Image Classifiers” examples. They turned out to be two halves of the same idea, so I’ll write about them together.

What finally clicked from the reading

I’ve used image classifiers before without really knowing what was happening inside them. Karn’s post is the first explanation that made the black box feel less black. The part that stuck with me is that a convolution is just a small filter sliding across the image, and each filter is a tiny feature detector — one responds to edges, another to a curve, another to a patch of texture. Sliding it across produces a “feature map” that basically answers where in this image does this feature show up. Then ReLU throws away the negatives, pooling shrinks everything down so the network cares more that a feature appeared than exactly where, and you stack these blocks until the early layers are catching edges and the deep layers are catching whole objects.

The sentence I keep coming back to is that nobody hand-designs those filters. They’re learned. The network starts with random filters and backpropagation slowly tunes them until they’re useful. That reframed the whole thing for me — a CNN isn’t a clever algorithm someone wrote, it’s a structure that discovers what to look for from the examples you give it.

What I built

The four ml5 examples are all variations on training a classifier on your own images instead of using a pre-trained one. I took that as a starting point and built it into a small “teachable machine” — a webcam tool where you define classes on the fly, collect samples, train, and watch it predict live.

The core loop is dead simple and maps directly onto the reading. I shrink each webcam frame down to 24×18 pixels and flatten the RGB values into a flat array of numbers:

js

function getFeatures() {
small.image(video, 0, 0, CW, CH);
small.loadPixels();
const f = [];
for (let i = 0; i < small.pixels.length; i += 4) {
f.push(small.pixels[i]/255, small.pixels[i+1]/255, small.pixels[i+2]/255);
}
return f;
}

That’s the whole input — raw pixels, normalized to 0–1. I feed them to ml5.neuralNetwork, add a label, and let train() learn the weights. It’s a scaled-down version of exactly what Karn describes: give the network examples, let it figure out the mapping.

On top of the basic example I added a few things to make it feel less like a demo and more like a tool:

  • Dynamic classes — you start with two and press a key to add more, up to six.
  • Burst collection — holding the spacebar records a sample every frame, so you can gather fifty images in a couple of seconds by just moving around a bit. This mattered more than I expected: more varied samples made the predictions noticeably steadier.
  • A live loss curve — during training I plot the loss dropping in real time. Watching that line fall is the most concrete version of “the network is learning” I’ve seen. It made the abstract idea of backprop visible.
  • Prediction smoothing and locking — raw frame-by-frame predictions flicker a lot, so I low-pass the confidences and only “lock” a label once it’s held above 85% for a moment. This is a small UX decision but it’s the difference between something that feels broken and something that feels confident.

https://editor.p5js.org/jz6294/sketches/21593Dfkrhttps://editor.p5js.org/jz6294/sketches/21593Dfkr

Leave a Reply

Latest Articles

Previous:
Next: