AI, machine learning and deep learning · Intermediate

2D convolution and feature maps

Written by SignalsLab

Early edition: these lessons are being reviewed.

Content

Summary

A 2D convolution slides a small grid of weights, called a kernel, across an image and computes a weighted sum at each position. The resulting grid of numbers is a feature map that shows where a particular pattern, such as an edge, appears. Stacking many kernels yields many feature maps, which a convolutional network learns from data.

Intuition

Imagine a small stamp with a pattern of positive and negative numbers. You press it on every position of the image and ask how well the local patch matches the stamp. A large output means a strong match; a value near zero means no match. Because the same stamp is used everywhere, the network detects a pattern wherever it occurs (weight sharing) and uses far fewer parameters than a fully connected layer. Each kernel produces one feature map, so a layer with 16 kernels produces 16 maps, each highlighting a different learned pattern. Deep layers combine earlier maps to detect more complex shapes.

Key equations

yi,j=∑m=0k−1∑n=0k−1wm,nxi+m,j+n+b
(1)

Output at position (i,j): weighted sum of the k-by-k input patch with kernel w plus bias b (cross-correlation, as used in deep learning libraries).

Hout=⌊H+2p−ks⌋+1
(2)

Output height given input height H, padding p, kernel size k and stride s; width is analogous.

params=Cout(Cink2+1)
(3)

Number of learnable parameters in a convolutional layer.

Derivation

  1. Start with an input image x of size H by W and a kernel w of size k by k.
  2. Place the kernel's top-left corner at position (i,j) and multiply each kernel weight by the pixel beneath it.
  3. Sum all k squared products and add bias b to get one output value y(i,j).
  4. Move the kernel by stride s and repeat across rows and columns to fill the feature map.
  5. With no padding and stride 1, the kernel fits in H-k+1 vertical positions, so output height is H-k+1.
  6. Adding p pixels of padding on each side makes the effective height H+2p, and stride s divides the number of steps, giving the floor formula above.
  7. For multiple input channels, the kernel has depth C_in and the sum also runs over channels; using C_out kernels gives C_out feature maps.

Worked example

Problem

Input x is 4x4: rows [1,2,0,1],[3,1,2,0],[0,1,3,1],[2,0,1,2]. Kernel w is 2x2: [1,0],[0,-1]... use rows [1,0] and [0,-1]. Bias 0, stride 1, no padding. Compute the feature map.

  1. Output size is (4-2)/1+1 = 3, so a 3x3 map.
  2. The kernel computes x[i,j] - x[i+1,j+1] for each position.
  3. Row 0: (1-1)=0; (2-2)=0; (0-0)=0.
  4. Row 1: (3-1)=2; (1-3)=-2; (2-1)=1.
  5. Row 2: (0-0)=0; (1-1)=0; (3-2)=1.

Answer Feature map = [[0,0,0],[2,-2,1],[0,0,1]]

Common mistakes

  • Calling it convolution without noting that deep learning libraries actually compute cross-correlation (no kernel flip).
  • Forgetting that the kernel spans all input channels, so a 3x3 kernel on RGB has 27 weights plus a bias.
  • Miscounting output size by ignoring padding or stride.
  • Thinking each kernel has different weights at each position; the weights are shared across the image.
  • Confusing the number of kernels with the number of input channels; output maps equal the number of kernels.

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, ker):
    """Valid 2D cross-correlation; records each output cell as a snapshot."""
    n, k = len(img), len(ker)
    m = n - k + 1
    out = [[0] * m for _ in range(m)]
    trace = []
    for i in range(m):
        for j in range(m):
            s = 0
            for a in range(k):
                for b in range(k):
                    s += img[i + a][j + b] * ker[a][b]
            out[i][j] = s
            trace.append((i, j, s))
    return out, trace


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]]
OUT, TRACE = conv2d(IMG, KER)


class Main(Scene):
    def construct(self):
        self.camera.background_color = "#14161c"
        TitleCard(self, "2D Convolution", "A kernel slides over an image")

        # input grid, kernel grid, output grid
        g_in = GridView(IMG, cell_size=0.6, max_width=4, max_height=3.2)
        g_in.move_to(LEFT * 4.2 + DOWN * 0.1)
        g_k = GridView(KER, cell_size=0.6, max_width=2, max_height=2)
        g_k.move_to(ORIGIN + UP * 0.2)
        blank = [[" "] * 3 for _ in range(3)]
        g_out = GridView(blank, cell_size=0.6, max_width=2.6, max_height=2.6)
        g_out.move_to(RIGHT * 4.2 + DOWN * 0.1)
        names = VGroup(
            Text("Image", font_size=24).next_to(g_in, UP, buff=0.3),
            Text("Kernel", font_size=24).next_to(g_k, UP, buff=0.3),
            Text("Feature map", font_size=24).next_to(g_out, UP, buff=0.3),
        )
        self.play(FadeIn(g_in), FadeIn(g_k), FadeIn(g_out), FadeIn(names))
        StepCaption(self, "Multiply the window by the kernel and sum")

        # animate recorded states only
        prev = []
        for (i, j, s) in TRACE:
            win = [(i + a, j + b) for a in range(3) for b in range(3)]
            self.play(g_in.mark(win), g_out.mark([(i, j)]),
                      g_out.set_value((i, j), s), run_time=0.7)
            self.play(*[g_in.cells[c].animate.set_fill(BLACK, opacity=1)
                        .set_stroke(C_DIM) for c in win], run_time=0.2)
        StepCaption(self, "Large outputs mark a vertical edge in the image")
        self.wait(1.5)


if __name__ == "__main__" and "--selftest" in sys.argv:
    a = np.array(IMG)
    k = np.array(KER)
    ref = np.array([[(a[i:i+3, j:j+3] * k).sum() for j in range(3)] for i in range(3)])
    assert np.array_equal(ref, np.array(OUT))
    assert len(TRACE) == 9
    assert OUT[0] == [3, 3, 0] or OUT[0][0] == 3
    assert OUT[0][2] == 0
    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.