Images and filters
To a computer, an image is a grid of numbers, one per pixel (picture dot). Our tiny 6×6 image uses 1 for bright and 0 for dark. It shows a short vertical stroke, two pixels wide.
A convolutional network (CNN) looks at small patches, not the whole image at once. It uses a filter, also called a kernel: a 3×3 grid of weights. Ours, +1 left, 0 middle, −1 right, looks for bright-left, dark-right edges.
Convolution
Lay the filter on the top-left 3×3 patch. Multiply each pixel by the weight on it, then add the nine products. Every row gives 0 + 0 − 1, so the total is −3. That one number starts the feature map.
Slide the filter one pixel right and repeat, row by row. It fits 6 − 3 + 1 = 4 ways across and 4 down: 16 positions, 16 numbers. Over the stroke's right edge it gives 3. These numbers form a 4×4 feature map.
The same nine weights were used at all 16 positions. That is weight sharing: this layer has just 9 weights plus 1 bias (here 0), whatever the image size. Linking all 36 pixels to all 16 outputs would need 576 weights.
ReLU and pooling
Next, an activation function called ReLU turns every negative number into 0 and keeps the rest: max(0, v). The −3s marked the stroke's other edge, dark-to-bright, which this filter is not looking for. Now only its edge remains.
Max pooling splits the map into 2×2 blocks and keeps the biggest number in each. The 4×4 map becomes 2×2: 0, 3, 0, 2. It is four times smaller and still says: the edge is on the right, strongest at the top.
Deeper networks
A real layer has many filters, each making its own feature map: one finds vertical edges, another horizontal ones, another colour blobs. AlexNet's first layer has 96. Deeper layers filter those maps, finding corners, then textures, then parts of objects.
At the end, the small maps are flattened into one list of numbers and fed to an ordinary dense layer. It gives a score to each class, here "vertical" or "horizontal" stroke, and the highest score is the answer.
We chose our filter by hand. In a real CNN the filter weights start random. Training compares the answer with the label, and backpropagation with gradient descent nudges every weight to lower the loss. Edge-finding filters appear on their own.
What happens
;; step 1/10 · image
x (6x6), 1 = bright, 0 = dark
0 0 1 1 0 0
0 0 1 1 0 0
0 0 1 1 0 0
0 0 1 1 0 0
0 0 0 0 0 0
0 0 0 0 0 0
a real photo: 3 numbers per pixel (red, green, blue)
Strictly, CNN libraries compute cross-correlation: the filter is not flipped before it slides. They still call it convolution, and since the weights are learned, the difference does not matter. The final dense layer is shown, not computed: its weights would be learned too. The Python program is real; run it and it prints the feature map and the pooled 2×2 map.