Kumita← Open visual lesson

Slide the Kernel

The math behind the picture

Every number in the output comes from one placement of nine weights over nine pixels.

Connection 1 of 4

Pair each weight with the pixel under it

Place the kernel over a 3 × 3 patch. The weight in the top-left corner meets the top-left pixel, the centre weight meets the centre pixel, and so on. Nine weights, nine pixels, nine pairs.

pair(i, j) = K[i][j] · I[row + i][col + j]

Read it as: Each product uses one weight and the one pixel directly beneath it.

Connection 2 of 4

Multiply, then add

On the watermelon, nine products from the melon's right outline are added into one response, 19, written to the matching output cell.
From guided stage 4 · Static lesson frame

On the watermelon, nine products from the melon's right outline are added into one response, 19, written to the matching output cell.

On the melon's right outline the patch is [0 8 8; 0 8 8; 5 0 8] and the kernel is [−1 0 1; −1 0 1; −1 0 1]. The products are [0 0 8; 0 0 8; −5 0 8]. Adding the nine products gives 19.

response = Σᵢ Σⱼ K[i][j] · I[row + i][col + j]

Read it as: Add the nine products; the sum is the response at this position.

Products added0 + 0 + 8 + 0 + 0 + 8 − 5 + 0 + 8
Response19

Connection 3 of 4

Slide, reuse the weights, fill the map

The scan fills the output one cell per position, each in the same row and column as the patch that produced it.
From guided stage 6 · Static lesson frame

The scan fills the output one cell per position, each in the same row and column as the patch that produced it.

Move one pixel right and the same weights meet new pixels, so the response changes. Scanning left to right and then down fills the output one cell per position.

output[r][c] = response of the window whose top-left pixel is (r·s, c·s)

Read it as: The output cell keeps the row and column of the window that produced it.

Connection 4 of 4

Count the positions

Without padding, the window must stay inside the picture. Height and width are counted separately. The 17 × 18 picture and a 3 × 3 kernel give 15 × 16 positions at stride 1 and 8 × 8 at stride 2.

output size = floor((input − kernel) / stride) + 1

Read it as: This applies to valid filtering without padding or dilation.

Stride 115 × 16 = 240
Stride 28 × 8 = 64

Connect it back to the picture

A feature map is the same small set of weights applied at every position. Signed responses show how well the pixels match the pattern the weights describe.

Full notation and technical reference

The expressions below collect the operations discussed above. You can return here when you want the compact notation.

response(row, col) = Σᵢ Σⱼ K[i][j] · I[row·s + i][col·s + j]
output size = floor((input − kernel) / stride) + 1, for height and for width
  • I is the image, K the 3 × 3 kernel and s the stride; i and j each run from 0 to 2.
  • Positions are visited left to right, then down. No padding and no dilation are used.
  • The unflipped operation is cross-correlation; convolution flips K by half a turn first.

Numerical reference

On the watermelon picture, the patch at the melon's right outline is [0 8 8; 0 8 8; 5 0 8]. With K = [−1 0 1; −1 0 1; −1 0 1] the products are [0 0 8; 0 0 8; −5 0 8] and their sum is 19. With a stride of 1, the 17 × 18 picture gives a 15 × 16 output; with a stride of 2 it gives 8 × 8.

Where this explanation stops

The kernel here is written by hand, and a large response means only that the pixels match the pattern the weights describe, not that an object is present. In CNNs the kernel values are learned. The size formula does not cover padding or dilation.

Follow the same idea in the Python or JavaScript notebook →