The Pixels That Can Reach It

ConvolutionHard+30 XP+30

The Pixels That Can Reach It

A unit deep in a network only ever saw a small window of the layer below it. But each unit inside that window saw its own window of the layer below it, and so on down. Follow the chain all the way to the image and the region of pixels able to influence that one unit is far larger than any single filter.

A network is given as a list of layers, ordered from the input towards the output. Each layer is a pair [kernel, stride] — a square window of side kernel, moved stride positions at a time. Pooling layers are written the same way, since they sweep the same way: a 2×2 max pool with stride 2 is [2, 2]. There is no padding anywhere, and the input is large enough that the region never runs off its edge.

Task: write receptive_field(layers), returning a list whose iii-th entry is the side length — measured in input pixels — of the region that can affect a single unit in the output of layer iii.

  • Every window is square, so one number describes the region completely.
  • The region has the same size for every unit in a given layer; only its position differs.
  • Every entry is a whole number of pixels, but hand it back as a float: receptive_field([[3, 1]]) gives [3.0], not [3].

While every stride is 1, each layer widens the region by kernel - 1 pixels. One 3×3 layer reaches 3 pixels, a second reaches 5, a third 7.

A stride breaks that rule, and this is the part worth working through. Neighbouring units of a layer are built from windows one stride apart in whatever they read, so a stride-2 layer leaves its own units standing twice as far apart in the image as the units it read were. That spacing is the running product of every stride passed so far — after two stride-2 layers it is 4 input pixels, not 2. And a window of kernel units laid on units ddd pixels apart reaches across (kernel−1)×d(\texttt{kernel} - 1) \times d(kernel−1)×d input pixels, not kernel - 1.

Worked through, receptive_field([[3, 2], [3, 1]]):

  • Layer 0 — a 3×3 window on input pixels, which are 1 apart: the region is 1+2×1=31 + 2 \times 1 = 31+2×1=3. This layer's stride of 2 then leaves its own units 2 pixels apart.
  • Layer 1 — a 3×3 window on units 2 apart: the region is 3+2×2=73 + 2 \times 2 = 73+2×2=7. So the answer is [3.0, 7.0].

This is the number that decides what a layer is even capable of recognising: a unit whose region covers twelve pixels cannot be reporting on an object forty pixels wide, no matter how it was trained.

layers
1
field
3×3

Each 3×3 layer widens the field by two. Depth is how a network comes to see the whole picture.

Examples

layers = [[3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1], [3, 1]]
returns [3.0, 5.0, 7.0, 9.0, 11.0, 13.0, 15.0, 17.0]
layers = [[3, 1], [2, 2], [3, 1], [2, 2]]
returns [3.0, 4.0, 8.0, 10.0]

Hints

Solve it to unlock the solution

Once your code passes every test, a reference solution shows up here so you can compare it with yours.

solution.py
CtrlEnterto run