Syllabus Lesson 113 of 246 · Module 10: Networks that see (CNNs)
Module 10: Networks that see (CNNs)

Padding, stride and pooling

Why it matters Every CNN design decision changes the size of the feature maps. If you can work out the sizes in your head, you can read and design any CNN.

  • Padding p: add p rows and columns of zeros around the image so the filter can sit on the edges. With p = (k - 1) / 2 the output keeps the input's size ("same" padding).
  • Stride s: move the filter s pixels at a time. Stride 2 roughly halves the size.
  • Pooling: shrink a map by keeping the largest (max pool) or the average of each 2 by 2 block.

The one formula

out = (n + 2p - k) // s + 1. Check: n = 8, k = 3, p = 1, s = 1 gives 8. With s = 2 it gives 4.

Words and symbols in this lesson
filter, feature map
the small learned grid of weights; and the image of its responses
padding (p), stride (s)
zeros added around the edge; how many pixels the filter moves each step
out = (n + 2p - k) // s + 1
the output side for input side n and filter side k
pooling
shrinking a map by keeping the max or average of each block
Your turn

Write out_size(n, k, p, s), and maxpool2(img) for a 2D image with even sides. Then predict, in predictions, the output side for the five cases listed in the starter.

Spotted a problem in this lesson? Report it · Ask about this lesson

This exercise opens soon

Finish the lesson before it and this one unlocks. The whole course is free, no account needed, and you can read this lesson right now.