RECEPTIVE FIELD

Ch 3 β–Έ Stride, padding and dilation β–Έ Module 4/4

  1. 1Stride
  2. 2Padding
  3. 3Dilation
  4. 4Putting it together

Putting it together.

πŸ”’ This module is locked

It builds on Dilation. Pass that module’s knowledge check to unlock it, or skip ahead if you already know the material.

Go to Dilation

Now put the three settings together. Padding grows the input to n+2pn + 2p pixels. The kernel spans keff=d(kβˆ’1)+1k_\text{eff} = d(k-1) + 1 of them, so its starting position can range from 00 to n+2pβˆ’keffn + 2p - k_\text{eff}. Stepping through that range ss at a time gives the output size:

o=⌊n+2pβˆ’d(kβˆ’1)βˆ’1sβŒ‹+1o = \left\lfloor \frac{n + 2p - d(k-1) - 1}{s} \right\rfloor + 1

The ⌊ βŒ‹ rounds down. It’s the leftover pixels from the stride module: if the last step would leave the window hanging off the edge, that step isn’t taken.

This is exactly the formula in the PyTorch documentation for nn.Conv2d. The figure below has every setting at once, plus a listing of the dot product behind the highlighted output. Change the settings and check the listing’s out_size line against the formula.

Try the other kernels too. sobel_x and outline have negative weights, so their outputs can go below zero, and the figure switches to blue for negative and red for positive. You can also type your own weights into the kernel.

FIG 3.5A β–Έ INPUT x34Γ—34
The input x, 32Γ—32, padded to 34Γ—34. The highlighted squares are the nine pixels the kernel reads.
FIG 3.5B β–Έ OUTPUT y32Γ—32
DARKBRIGHT
The output map y, 32Γ—32, written one dot product at a time. Every weight is positive, so the output is shaded like the input: brighter means a larger value.
0/0 OUTPUTS Β· SWEEP 0.0 S
Image
Stride
Padding
Dilation
Kernel
15 STEPS/S
PYTHON β–Έ conv.py
>>> out_size(n=32, k=3, p=1, s=1, d=1)
32 # ⌊(32 + 2Β·1 βˆ’ 1Β·2 βˆ’ 1) / 1βŒ‹ + 1
>>> patch =
0.000.000.000.000.000.000.000.000.00
*
>>> (patch * w).sum()
–

In code, the three settings are just arguments:

import torch.nn as nn

# 32Γ—32 in β†’ 16Γ—16 out
nn.Conv2d(1, 1, kernel_size=3, stride=2, padding=1)

# 32Γ—32 in β†’ 32Γ—32 out ("same" only works at stride 1)
nn.Conv2d(1, 1, kernel_size=3, padding="same")

# 32Γ—32 in β†’ 32Γ—32 out, each output sees a 5Γ—5 area
nn.Conv2d(1, 1, kernel_size=3, padding=2, dilation=2)

Knowledge check β–Έ 4 questions

Before you move on.

Answer every question correctly to unlock the next module. A wrong answer gets a hint, and you can try again as many times as you like.

  1. 1A 28Γ—28 input, 3Γ—3 kernel, padding 1, stride 2. How many pixels wide is the output?

  2. 2A 32Γ—32 input, 3Γ—3 kernel, no padding, dilation 2, stride 1. How many pixels wide is the output?

  3. 3A 64Γ—64 input, 3Γ—3 kernel, padding 1, stride 2. How many pixels wide is the output?

  4. 4Why does the formula round down (the ⌊ βŒ‹)?

0 of 4 answered correctly.

πŸ”’ Chapter 5: Pooling