Convolution and Feature Maps Playground

see which visual patterns survive each kernel choice

Loading interactive simulation...

Without padding, a deep network shrinks the image away 🖖

An unpadded 3×3 kernel at stride 1 costs you one pixel on every edge, so this tool's 28×28 input comes out 26×26. That sounds harmless until you stack layers: ten leave 8×8, and after fourteen there is nothing left. This is the whole reason "same" padding exists — adding a one-pixel border restores the output to the input size and lets a network be as deep as you like. Stride compounds it in the other direction, dividing rather than subtracting: blur at stride 2 turns 28×28 into 14×14 in one layer. The two settings on this tool are therefore not cosmetic: they decide how many layers your architecture can afford before it runs out of image.

A tiny window that slides 🖖

A convolution slides a small grid of numbers, the kernel, across the image. At each stop it multiplies the overlapping values and adds them into one output pixel. The result is a feature map that brightens wherever the kernel's pattern appears. Try the edge-detect kernel here: flat regions go dark while boundaries light up, because the kernel responds to change, not to brightness itself.

It isn't really convolution 🖖

The operation nearly every CNN calls "convolution" is actually cross-correlation. True mathematical convolution first flips the kernel top-to-bottom and left-to-right before sliding it; deep-learning libraries skip that flip. Since the network learns the weights anyway, a flipped kernel would simply be learned in reverse and give an identical result, which is why the misnomer stuck. With a symmetric kernel like blur, the two are indistinguishable.

Common wrong intuition

A deeper feature map is not always "better". High stride or harsh kernels can remove information your later layers needed.

Problem solved in full

  1. Output map size for a 28ร—28 input map and 3ร—3 sobel-x kernel 6 steps

    The layer: a 28ร—28 input map, a 3ร—3 sobel-x kernel, stride 1, padding same. Work out the size of the output map and what one pass costs in multiply-adds โ€” then find how many such layers have to be stacked before a single output pixel depends on every pixel of the image.

    1. Padding fixes the size, so settle it first. same lays a one-pixel border of zeros around the map, which is exactly enough for a 3ร—3 window to sit centred on a corner pixel instead of hanging off the edge. The border a kernel needs is half of it, not counting the centre.

    2. Slide the window along one row. Its left edge starts at 0 and advances by the stride until its right edge reaches the end of the padded row, so counting output pixels is a fencepost count: the number of advances, plus 1 for the starting position. At stride 1 the border gives back exactly the 2 columns the kernel takes away, which is what same is named for.

    3. Each of the 784 output pixels is one weighted sum of 9 input values. Add the sobel-x weights and they cancel to 0, so any window whose 9 inputs are all equal returns exactly 0 โ€” however bright that region is. The map goes dark wherever the image is flat and responds only where the left column of the window differs from the right.

    4. The arithmetic bill follows: 9 multiply-accumulates per output pixel, one output pixel per window position. The panel prints that count.

    5. Depth is where it compounds. After one layer an output pixel sees 3 input pixels across. Add a second and each of those 3 was itself a window of 3 โ€” but neighbouring windows overlap by 2, so the span reaches 5, not 9. Every further 3ร—3 stride-1 layer widens it by exactly 2.

    6. Set the span to cover all 28 columns and solve for the depth. 13 layers reach 27 and fall one column short; the 14th is the first whose output pixels can be influenced by the whole image.

    Answer

    28ร—28 out, 7056 multiply-adds per layer, and 14 layers before one pixel sees the whole image. Now count what that reach costs. The 14 stacked layers spend 14 ร— 9 = 126 multiply-accumulates per output pixel. A single layer reaching just as far needs a 29ร—29 kernel, at 841 per output pixel โ€” 6.7 times more arithmetic for exactly the same field of view. Depth buys reach far more cheaply than width does, which is why vision networks are long stacks of 3ร—3 kernels rather than short stacks of large ones.

References (1)
  • Insight block 3 โ€” the operation called convolution is cross-correlation: I. Goodfellow, Y. Bengio and A. Courville, Deep Learning, ch. 9. MIT Press, 2016. ISBN 978-0-262-03561-3 โ€” "many machine learning libraries implement cross-correlation but call it convolution".

Example problems

  • digit sobel-x - Sobel-x sums to zero, so anything flat comes out black and only vertical change survives. Same padding at stride 1 keeps the output 28x28.
  • shape edges - The edge kernel sums to zero too - 8 in the middle against eight -1s - so flat regions vanish and only boundaries are left. Output stays 28x28.
  • blur + stride 2 - The only preset that shrinks by division: stride 2 turns 28x28 into 14x14, a quarter of the pixels. Blur sums to 1, so brightness survives even where detail does not.
  • sharpen valid - The only preset that shrinks by subtraction: valid padding turns 28x28 into 26x26, one pixel off each edge. Sharpen sums to 1, so flat regions keep their level and edges are pushed apart.