MLG 025 Convolutional Neural Networks
A convolutional neural network slides small learned filters across an image, each making a feature map that is high where its pattern appears, and stacks these layers so later ones find larger patterns. Also covers window size, stride, same and valid padding, max pooling, and the CNN line from LeNet-5 to the ImageNet winners AlexNet, GoogLeNet and ResNet. Links Notes and resources at ocdevel.com/mlg/25 Join the free MLG group: listen to my other AI shows, ask me questions, and talk with other listeners Why not use an MLP for images? A multilayer perceptron (MLP) flattens an image into a list of pixels and connects every pixel to every neuron of its first dense layer, each connection with its own weight. Nothing in that design tells it that neighboring pixels belong together, and a pattern it learns in one corner has to be learned again for every other position. A convolutional neural network (CNN, or ConvNet) builds both in: each neuron looks only at a small window of neighboring pixels, and the same filter is reused at every position, so a striped shirt is found wherever Waldo stands. That's why CNNs took over from MLPs for vision. An 8-bit grayscale image stores each pixel as 0 (black) to 255 (white); scaled to 0–1, 1 is white. A color image has three channels, red, green and blue, so the input is a width × height × 3 volume. What is a filter? A filter, or kernel, is a small grid of learned weights, say 5×5, that spans every channel of its input: a 5×5 filter on an RGB image holds 5×5×3 weights plus a bias. At each position it multiplies the window's values by its weights, sums the products, adds the bias, and usually passes the result through a ReLU. Weights can be negative, so a horizontal-stripe detector has rows of positive weights alternating with rows of negative ones. It responds strongly where light and dark stripes line up with it and weakly elsewhere. Training learns the weights by backpropagation. You choose how many filters a layer has and their size. Filters in early layers tend to learn edges and color blobs, later ones textures and parts of objects. Filter, feature map and convolutional layer Filter: the small grid of learned weights. Feature map: the output of sliding one filter across the whole input, one value per position, high wherever the filter's pattern appears. The filter is fixed after training; a feature map is computed fresh for every image. Convolutional layer: a set of filters and the stack of feature maps they produce, a width × height × (number of filters) volume. The next layer's filters span that whole depth, so they combine the previous layer's patterns into larger ones. A classifier CNN stacks convolutional layers, shrinking width and height while depth grows, then ends in dense layers with a softmax or sigmoid output. Many modern CNNs replace the big dense layers with global average pooling, which averages each feature map to one number, followed by a single dense layer. Window, stride and padding The window is the filter's size; the stride is how far the filter moves between positions. For input width W, filter size F, padding P and stride S: output width = (W − F + 2P) / S + 1 Stride 1 moves one pixel at a time, so neighboring windows overlap heavily. Stride equal to the filter size, 5 for a 5×5 filter, tiles the input with no overlap and shrinks it by that factor, at the risk of splitting an object across two windows. Stride 2 is the usual middle ground for downsampling. Padding adds zeros around the border, on every side. "Same" padding adds enough that at stride 1 the output keeps the input's width and height (P = (F − 1)/2 for an odd F). "Valid" padding adds none, so the filter only visits positions where it fits entirely, and at stride 1 the output shrinks by F − 1. What does pooling do? Max pooling keeps the largest value in each window of a feature map, typically 2×2 with stride 2, which keeps one value in four and halves width and height. It has no learned weights and works on each feature map separately, so depth is unchanged. It cuts the compute and memory later layers need and makes the network more tolerant of small shifts in where a feature appears. Average pooling takes the mean instead. Many modern CNNs downsample with strided convolutions in place of pooling. Pretrained architectures and transfer learning The number of layers, filter sizes, strides and pooling positions are hyperparameters that are hard to tune, so most projects start from a published architecture. An early landmark is LeNet-5 (1998), Yann LeCun and colleagues' network for handwritten digits. The ImageNet Large Scale Visual Recognition Challenge (ILSVRC), which ran yearly from 2010 to 2017, came later, and its winners and runners-up became the standard CNNs: AlexNet won in 2012. GoogLeNet, also called Inception v1, won in 2014, with VGG second. ResNet (He et al., 2015) adds skip connections; an ensemble of ResNets, the deepest 152 layers, won in 2015. There is no yearly winner to pick now. The usual route is transfer learning: take a backbone pretrained on ImageNet or a larger dataset, such as ResNet, EfficientNet, ConvNeXt or a Vision Transformer, and fine-tune it on your own images. torchvision, timm and the Hugging Face Hub provide the pretrained weights. CNNs, Vision Transformers and GPUs A Vision Transformer (ViT, 2020) cuts an image into patches and runs a transformer over them (MLG 033). Pretrained on enough data, ViTs match or beat CNNs on many vision benchmarks. CNNs stay common, especially with small datasets and on phones and other devices. CNNs run far faster on a GPU than on a CPU, but they're no longer the most compute-hungry networks; large transformers are. The GTX 1080 Ti (11 GB) mentioned in the episode is several generations old. GPU memory caps the model and batch size you can train, and large data center GPUs such as NVIDIA's H100 carry 80 GB or more. Where to learn CNNs Stanford's CS231n: lectures on YouTube, and course notes on convolutional networks that work through the output-size arithmetic above. Aurélien Géron's Hands-On Machine Learning, its chapter on deep computer vision with CNNs (Chapter 12 in the PyTorch edition). fast.ai's Practical Deep Learning for Coders, Lesson 8, "Convolutions (CNNs)". Next: MLG 026, Project Bitcoin Trader, a community project applying deep reinforcement learning to crypto trading.
Embed this episode
No transcript yet.