Ch 3 βΈ Stride, padding and dilation βΈ Module 4/4
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.
Now put the three settings together. Padding grows the input to pixels. The kernel spans of them, so its starting position can range from to . Stepping through that range at a time gives the output size:
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.
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.
1A 28Γ28 input, 3Γ3 kernel, padding 1, stride 2. How many pixels wide is the output?
2A 32Γ32 input, 3Γ3 kernel, no padding, dilation 2, stride 1. How many pixels wide is the output?
3A 64Γ64 input, 3Γ3 kernel, padding 1, stride 2. How many pixels wide is the output?
4Why does the formula round down (the β β)?