AI, machine learning and deep learning · Intermediate

CNN feature map pipeline

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

A convolutional neural network turns an image into a stack of feature maps by sliding small learned filters across it, applying a nonlinearity, and pooling to shrink the result. Repeating this builds from edges to textures to object parts, and the final features feed a classifier.

Intuition

Imagine scanning a medical image, such as a chest X-ray, with a tiny magnifying window. At each position the window asks one question, for example 'is there an edge here running left to right?' The answers across all positions form a feature map. Many filters ask many questions, giving many maps. A ReLU keeps only positive evidence, and pooling summarizes neighbouring answers so small shifts do not matter. Deeper layers look at maps instead of pixels, so they combine simple patterns into complex ones. Weights are shared across positions, so the network needs far fewer parameters than a fully connected one.

Key equations

Yk(i,j)=∑c∑u=0F−1∑v=0F−1Wk,c(u,v)Xc(i+u,j+v)+bk
(1)

Convolution (strictly cross-correlation): filter k slides over all input channels c to produce one output map.

A=max⁡(0,Y)
(2)

ReLU nonlinearity applied element by element.

O=⌊N−F+2PS⌋+1
(3)

Output size from input size N, filter size F, padding P and stride S.

P(i,j)=max(u,v)∈window⁡A(is+u,js+v)
(4)

Max pooling keeps the strongest response in each window.

Derivation

  1. Start with input X of size N by N by C, for example a grayscale image with C=1.
  2. Choose K filters, each of size F by F by C. Each filter has F*F*C weights plus one bias.
  3. Place filter k at position (i,j), multiply elementwise with the patch under it, sum, and add the bias to get one number.
  4. Slide by stride S across the image; this fills a map of size O by O, with O given by the size formula.
  5. Stack the K maps to get an O by O by K volume.
  6. Apply ReLU to each value so negative responses become zero.
  7. Apply pooling with window 2 and stride 2, which halves height and width but keeps K channels.
  8. Repeat conv, ReLU, pool; then flatten or globally average and pass to a dense layer with softmax.

Worked example

Problem

A 32x32x3 image passes through a conv layer with 8 filters of 5x5, stride 1, padding 0, then ReLU, then 2x2 max pooling with stride 2. Give the output shape and the number of conv parameters.

  1. Conv output size: (32-5+0)/1+1=28, so the volume is 28x28x8.
  2. ReLU does not change shape.
  3. Pooling: 28/2=14, so the volume is 14x14x8.
  4. Parameters per filter: 5*5*3=75 weights plus 1 bias = 76.
  5. Total for 8 filters: 8*76=608.

Answer Output shape 14x14x8; the conv layer has 608 parameters.

Common mistakes

  • Forgetting that each filter spans all input channels, so its depth equals the input depth.
  • Thinking pooling has learnable parameters; it has none.
  • Miscounting output size by forgetting the +1 or the padding term.
  • Believing the number of output maps equals the number of input channels; it equals the number of filters.
  • Skipping the nonlinearity, which would make stacked layers collapse into a single linear operation.

Animation

Animation coming soon

The animated version of this lesson is being prepared for the web. In the Signals AI app, lessons are drawn step by step.

Code

scene.py(76 lines)
import sys
import numpy as np
from manim import *
from blocks import *


def conv2d(img, k):
    """Valid convolution (cross-correlation) recording each output cell."""
    n, m = len(img), len(k)
    o = n - m + 1
    out = [[0] * o for _ in range(o)]
    trace = []
    for i in range(o):
        for j in range(o):
            s = sum(img[i + a][j + b] * k[a][b] for a in range(m) for b in range(m))
            out[i][j] = s
            trace.append((i, j, s))
    return out, trace


def relu(g):
    """Apply ReLU elementwise."""
    return [[max(0, v) for v in r] for r in g]


def pool2(g):
    """2x2 max pooling."""
    return [[max(g[2 * i][2 * j], g[2 * i][2 * j + 1], g[2 * i + 1][2 * j], g[2 * i + 1][2 * j + 1])
             for j in range(len(g[0]) // 2)] for i in range(len(g) // 2)]


IMG = [[0, 0, 1, 1, 1],
       [0, 0, 1, 1, 1],
       [0, 0, 1, 1, 1],
       [0, 0, 1, 1, 1],
       [0, 0, 1, 1, 1]]
KER = [[-1, 0, 1], [-1, 0, 1], [-1, 0, 1]]
FM, TRACE = conv2d(IMG, KER)
RL = relu(FM)
# pad to 4x4 would change facts; pool a 2x2 crop of the 3x3 map
PL = pool2([r[:2] for r in RL[:2]])


class Main(Scene):
    def construct(self):
        TitleCard(self, "CNN feature map pipeline")
        # three grids: image, feature map, pooled
        g_img = GridView(IMG, cell_size=0.5).move_to([-4.5, 0.3, 0])
        g_fm = GridView([[None] * 3 for _ in range(3)], cell_size=0.5).move_to([0, 0.3, 0])
        g_pl = GridView([[None]], cell_size=0.5).move_to([4.2, 0.3, 0])
        names = [Text(t, font_size=24, color=C_ACCENT) for t in ("Image", "Conv map", "ReLU+Pool")]
        for n, g in zip(names, (g_img, g_fm, g_pl)):
            n.next_to(g, UP, buff=0.4)
        self.play(FadeIn(g_img), FadeIn(g_fm), FadeIn(g_pl), *[FadeIn(n) for n in names])
        StepCaption(self, "Slide a vertical-edge kernel over the image")
        # animate recorded conv trace
        for (i, j, s) in TRACE:
            win = [(i + a, j + b) for a in range(3) for b in range(3)]
            self.play(g_img.mark(win), g_fm.set_value((i, j), s), run_time=0.3)
            self.play(g_img.mark(win, color=BLACK), run_time=0.1)
        StepCaption(self, "ReLU keeps positives; max pool keeps the strongest")
        self.play(g_fm.mark([(i, j) for i in range(3) for j in range(3) if FM[i][j] > 0], C_GOOD))
        self.play(g_fm.mark([(0, 0), (0, 1), (1, 0), (1, 1)], C_ACCENT))
        self.play(g_pl.set_value((0, 0), PL[0][0]), g_pl.mark([(0, 0)], C_GOOD))
        self.wait(0.5)
        StepCaption(self, "Convolution finds edges; pooling keeps the strongest response")
        self.wait(1)


if __name__ == "__main__" and "--selftest" in sys.argv:
    assert len(TRACE) == 9
    assert FM[0][0] == 3 and FM[0][1] == 3 and FM[0][2] == 0
    assert FM == [[3, 3, 0]] * 3
    assert all(v >= 0 for r in RL for v in r)
    assert PL == [[3]]
    print("ok")

The Manim scene code that draws this lesson's animation in the Signals AI app (Python, Manim Community).

ccBYNC© 2026 SignalsLab. Lesson text shared as is under CC BY-NC 4.0.