9 Kitty Vision
Convolutional Neural Networks, Filters, and Pooling
🐾 Evergreen Chapter: Core mathematics and theory do not expire. Last verified: 2026-02-24
Prerequisites: Chapter 8 Technical Keywords: Convolutional Neural Networks (CNNs), Images as Tensors, Convolution Operation, Filters (Kernels), Feature Maps, Stride, Padding, Pooling (Max/Average), Translation Invariance, ResNets, Skip Connections
In Chapter 8, we built Pixel a mathematical brain (a Multi-Layer Perceptron, or MLP). It was a stadium of kittens, where every kitten in one row was connected to every single kitten in the next row. This is called a Fully Connected Network.
But what happens when we try to feed this brain an image?
Let’s say the human holds up a tiny 100 \times 100 pixel color photo of a bird.
- Width: 100
- Height: 100
- Color Channels (Red, Green, Blue): 3
- Total inputs: 100 \times 100 \times 3 = \textbf{30,000} numbers.
If the first hidden layer of our brain has just 1,000 neurons, and it is fully connected, the weight matrix (W) connecting the input to the first layer has 30,000 \times 1,000 = \textbf{30 million weights}.
For a tiny 100 \times 100 image! If we use a real 4K RGB photo (3840 \times 2160 \times 3 \approx 24.9 million inputs) connected to just 1,000 neurons, that single layer already demands ~24.9 billion weights. The math explodes. The memory crashes.
Furthermore, if the bird is in the top-left corner of the photo today, and the bottom-right corner tomorrow, a Fully Connected Network has to learn what a bird looks like all over again for every possible location. It has no concept of space.
To teach Pixel how to see, we cannot use a standard neural network. We need a network that understands that a bird is a bird, no matter where it is. We need a Convolutional Neural Network (CNN).
9.1 Images as Tensors: The Grid of Numbers
To Pixel’s mathematical brain, an image is not a picture; it is a stack of three grids of numbers.
A single grayscale image is a 2D matrix (from Chapter 1) where each number represents the brightness of a pixel (0 is black, 255 is white). A color image is three of these matrices stacked on top of each other: one for Red, one for Green, one for Blue.
A multi-dimensional matrix is called a Tensor.
Our 100 \times 100 color image is a Tensor with the shape (3, 100, 100).
Tensor Shape Convention
This book uses PyTorch ordering (C, H, W) throughout. If you are using TensorFlow, mentally map to (H, W, C).
9.2 The Filter: Whiskers Feeling for Edges
Instead of looking at all 30,000 pixels at once, Pixel does what a real kitten does: he uses his whiskers to feel a tiny, specific area.
In a CNN, this “whisker” is called a Filter (or Kernel).
A Filter is just a tiny matrix of weights—usually 3 \times 3 per channel. Unlike the massive 30-million-weight matrix in our old network, a grayscale 3 \times 3 filter only has 9 weights (and a 3-channel RGB 3 \times 3 filter has 3 \times 3 \times 3 = 27 weights).
What do these 9 numbers do? They look for specific patterns. Imagine a filter designed to find vertical edges (like the side of a door or the leg of a chair). Its weights might look like this:
\begin{bmatrix} 1 & 0 & -1 \\ 1 & 0 & -1 \\ 1 & 0 & -1 \end{bmatrix}
This filter mathematically asks: “Is the left side of this tiny 3 \times 3 patch bright (1) and the right side dark (-1)?” If yes, it outputs a high number (we found an edge!). If the patch is all the same color, the 1s and -1s cancel out, and it outputs 0 (no edge here).
Analogy
Pixel closes his eyes and sweeps his paw across the floor. He isn’t trying to understand the whole room at once. He is just feeling for a sharp drop-off (an edge) or a soft fuzzy patch (a texture).

9.3 The Convolution: Sliding the Whiskers
How does the 3 \times 3 filter look at a 100 \times 100 image?
It Convolves.
Pixel places the filter in the top-left corner of the image. He multiplies the 9 filter weights by the 9 image pixels underneath it, and adds them all up. He records that single number.
Then, he slides the filter over by one pixel (this slide distance is called the Stride). He calculates the next number. He slides it again, scanning the entire image from left to right, top to bottom.
The output of this sliding process is a brand new grid of numbers called a Feature Map.
If the original image was a photo of a room, and the filter was looking for vertical edges, the resulting Feature Map will look like a ghostly black-and-white sketch where only the doorframes and chair legs are highlighted.
Catastrophic Failure: Shrinking the Image
If you slide a 3 \times 3 filter over a 100 \times 100 image, the filter can’t hang off the edges. This means the resulting Feature Map will only be 98 \times 98. Every time you convolve, the image shrinks! If you have a deep network, your image will eventually vanish into nothing.
The Fix: We use Padding. We artificially add a border of 0s (black pixels) around the outside of the original image so the filter can slide all the way to the true edge. The Feature Map stays exactly 100 \times 100.
Rosetta Stone
| Kitten World | Math/Concept | Python (PyTorch) |
|---|---|---|
| “The tiny whisker grid” | Kernel / Filter (W) | nn.Conv2d(in_channels, out_channels, kernel_size=3) |
| “Sliding the whiskers” | Convolution (X * W) | F.conv2d(x, weight, stride=1, padding=1) |
| “The resulting ghostly sketch” | Feature Map | The output tensor out |
9.4 Pooling: Squishing the Unimportant Details
After sliding 64 different filters across the image (finding vertical edges, horizontal edges, red blobs, and fuzzy textures), Pixel has 64 different Feature Maps.
The data is getting huge again. But does Pixel really need to know the exact, pixel-perfect coordinate of every single edge to know he is looking at a bird? No. He just needs to know: “There is an edge roughly in the top-left quadrant.”
To compress the data and make the network focus on the big picture, we use Pooling.
The most common type is Max Pooling. Pixel looks at a tiny 2 \times 2 window of the Feature Map. Out of those 4 numbers, he simply throws away the 3 smallest ones and keeps the maximum value (the strongest signal). He slides the window over and repeats.
This instantly shrinks the entire image by 50% (width and height), discarding useless background noise while preserving the most important features.
9.5 The Deep CNN Architecture: From Edges to Cats
A full Convolutional Neural Network is just a sequence of these operations stacked together, followed by the normal “brain” (MLP) we built in Chapter 8 to make the final guess.
Here is the life cycle of an image passing through a CNN:
- Input: A 100 \times 100 color photo of a cat.
- Conv Layer 1: 64 tiny filters slide over the image. They learn to find basic geometry: Edges and Colors.
- ReLU & Pooling: We apply our non-linear spark (Chapter 8) and shrink the Feature Maps to 50 \times 50.
- Conv Layer 2: Filters slide over the Feature Maps, not the original image! Because they are looking at combinations of edges, they learn to find Textures and Shapes (like a pointy ear or a round eye).
- ReLU & Pooling: Shrink the maps to 25 \times 25.
- Conv Layer 3: These filters look at combinations of shapes. They learn to find Complex Objects (like a whole face or a tail).
- Flatten: We take the final, highly abstract grids of numbers and flatten them into a single, long 1D vector of numbers.
- Fully Connected Output: We feed this vector into the Chapter 8 stadium of kittens. They look at the abstract features (“I see an ear! I see a tail!”) and output the final probability:
0.98 Cat.
9.6 Residual Networks (ResNets): The Shortcut
In 2015, researchers realized that on CIFAR-10 a 56-layer plain CNN actually performed worse—even on the training set—than a 20-layer CNN (He et al., 2016). Even after normalization mitigated vanishing gradients, deeper plain networks suffered from a degradation (optimization) problem that made them hard to optimize.
They solved this with Residual Connections (Skip Connections). Instead of forcing the signal to go rigidly from Layer 1 \rightarrow Layer 2 \rightarrow Layer 3, they drew a mathematical wire that let the signal from Layer 1 skip Layer 2 and go directly into Layer 3! f(x) = \text{Layer}_2(\text{Layer}_1(x)) + x
Analogy
Passing a note directly to the back row of kittens, skipping the middle rows entirely so the exact original message doesn’t get corrupted playing telephone.
This single trick allowed CNNs to scale to hundreds of layers, giving birth to ResNet, which remains a widely used vision architecture (alongside modern ViT variants).
9.7 Translation Invariance: A Bird is a Bird
Why are CNNs so incredibly powerful?
Because the weights (the 9 numbers in the filter) are shared across the entire image.
In the Chapter 8 network, if a bird was in the top-left corner, only the neurons connected to the top-left pixels learned what a bird was. If the bird moved to the bottom-right, the network was blind again.
In a CNN, the exact same filter slides over the top-left and the bottom-right. If the filter has learned to detect a beak, it will find that beak no matter where it appears on the screen. This sliding filter provides Translation Equivariance (if the beak moves, the detection also moves).
Combined with Spatial Pooling (which aggregates nearby signals), the network improves translation tolerance (ordinary pooling softens sensitivity to small shifts rather than guaranteeing strict translation invariance). It is the reason CNNs conquered the world of computer vision.
Micro-notebook (optional): micro-notebooks/phase03_ch09_convolution_sliding_dot.ipynb
Time: ~15 minutes. Goal: visualize convolution as a sliding dot product and inspect the resulting feature map.
9.8 Removing the Training Wheels
The Real Engineering
We used kittens sweeping their whiskers to explain CNNs. In PyTorch, a Convolution is a sliding cross-correlation operation between a learnable weight tensor and the input feature map. Shared kernel weights across spatial dimensions provide translation equivariance (shifting an input shifts its feature activations), while Max Pooling aggressively downsamples spatial dimensions to improve translation tolerance (ordinary pooling does not guarantee strict invariance).
9.9 Paws and Reflect: Chapter Quiz
Work through these conceptually before checking the answers.
You feed a 32 \times 32 image into a Conv Layer with a 3 \times 3 filter, Stride 1, and no Padding. What are the dimensions of the output Feature Map?
Why do we use Pooling layers in a CNN? (Give two reasons).
In the first layer of a CNN, the filters learn simple things like edges. What do the filters in the 50th layer of a deep CNN learn to find?
True or False: In a Convolutional layer, every pixel in the input image is connected to every neuron in the output Feature Map.
Reveal answers
- 30 \times 30. Because the filter needs 3 pixels of space, it can’t center itself on the very edges of the 32 \times 32 image. It loses 1 pixel of border on all four sides.
- First, to drastically reduce the size of the data/computation. Second, to create spatial abstraction (we care that an eye exists, not its exact millimeter-perfect coordinate).
- Highly complex, abstract, semantic concepts. They combine the shapes from earlier layers to detect things like “faces,” “car tires,” or “text.”
- False! That is a Fully Connected (Linear) layer. In a Conv layer, a neuron in the output map is only connected to a tiny local patch (like 3 \times 3) of the input image. This is called Local Connectivity.
9.10 Chapter 9 Summary
You’ve learned how to give your AI eyes:
- Images as Tensors: Pictures are just 3D grids of numbers (Channels, Height, Width).
- Filters/Kernels: Tiny matrices of weights that learn to detect specific patterns (like edges).
- Convolution: Sliding the filter across the image to create a Feature Map.
- Padding & Stride: Controls how the filter slides and whether the image shrinks.
- Pooling (Max Pooling): Squishing the Feature Maps to throw away noise and reduce computation.
- Translation Equivariance & Tolerance: Shared convolutional filters give translation equivariance across the image, while pooling helps improve tolerance to small spatial shifts.
Pixel can see. He can recognize a toy mouse anywhere in the room.
But wait. A CNN can see a mouse, but does it understand what the concept of a mouse means? If we show it a picture of a mouse and a picture of a cat, it knows they are different pixel patterns, but it doesn’t know they are both “animals.”
Before giving Pixel a flow of time and memory, we must first teach him the deep, internal meaning of things. We must translate concepts, images, and words into a physical map of scent trails.
It is time to dive into the mathematical geography of thought. 🐈🗺️
AI Safety Thread: Chapter 9
Adversarial Examples: A CNN’s vision is mathematically powerful, but completely alien to human vision. Because a CNN relies on heavily optimized textures and edges, it is vulnerable to invisible attacks.
Researchers can take a photo of a Stop Sign, use Backpropagation to calculate input perturbations, and change pixel values by tiny amounts (often difficult for humans to notice). The self-driving car’s CNN can misclassify the Stop Sign as a “Speed Limit 65” sign with high confidence under crafted perturbations. This is an important vulnerability class in how neural networks process geometry. We will dive deep into Adversarial Attacks in Phase V.