Compute the receptive field of a stack of conv layers

Why it matters Designers of segmentation and detection networks check receptive fields to make sure big objects fit.

The receptive field tells you how much of the input one output pixel can see. It decides whether a network can recognise large objects.

Write receptive_field(layers). layers is a list of (k, s, d): kernel size, stride and dilation. Start with r = 1 and jump j = 1 (the input pixel spacing). For each layer, the effective kernel is k_eff = d * (k - 1) + 1, then r = r + (k_eff - 1) * j and j = j * s. Return the list of (r, j) after each layer.

Signaturedef receptive_field(layers)

Example

receptive_field([(3, 1, 1), (3, 2, 1), (3, 1, 2)]) is [(3, 1), (5, 2), (13, 2)].

Understand the idea

One pixel in a deep feature map depends on a patch of the original image. Each 3 x 3 layer lets it see one more pixel on each side; strides make later steps count for more input pixels; dilation spreads taps apart. It is like a rumour: each layer of people passing it on spreads it to more of the original crowd.

Tiny example: 3 x 3 stride 1: r = 1 + 2 = 3. Then 3 x 3 stride 2: r = 3 + 2 * 1 = 5, jump becomes 2. Then 3 x 3 dilation 2 (k_eff 5): r = 5 + 4 * 2 = 13.

Rules: k_eff = d (k - 1) + 1; r += (k_eff - 1) * j; j *= s.

WaveNet and DeepLab stack dilations 1, 2, 4, 8 to grow the receptive field exponentially without pooling.

solution.pyPython idle
Ctrl/Cmd + Enter runs sample tests

Run checks the first 2 tests. Submit checks all 3.

  • Sample 1: examplereceptive_field([(3, 1, 1), (3, 2, 1), (3, 1, 2)])
  • Sample 2: vgg-likereceptive_field([(3, 1, 1), (3, 1, 1), (2, 2, 1), (3, 1, 1), (3, 1, 1), (2, 2, 1)])
  • Test 3: dilated stackreceptive_field([(3, 1, 1), (3, 1, 2), (3, 1, 4), (3, 1, 8)])