Feature map
- Machine Learning course
- Advanced
- Lesson 44/78
- 5 min
The Conv2d output is a learned feature map.
The feature map
Each filter's output is a feature map: it shows where in the image, and how strongly, the pattern the filter looks for is found. As the layers progress the maps shrink but the channel count grows:
# input (N, 3, 32, 32)
# block 1 -> (N, 32, 16, 16) edges, colours
# block 2 -> (N, 64, 8, 8) textures, parts
# block 3 -> (N, 128, 4, 4) object partsEach neuron in a deep layer looks at a larger region of the original image; this is called the receptive field.
Block structure
A CNN block usually consists of convolution, normalisation, activation and pooling. Producing blocks with a function shortens the code:
def blok(giris, cikis):
return nn.Sequential(
nn.Conv2d(giris, cikis, 3, padding=1),
nn.BatchNorm2d(cikis),
nn.ReLU(),
nn.MaxPool2d(2))
govde = nn.Sequential(blok(3, 32), blok(32, 64), blok(64, 128))
print(govde(torch.zeros(1, 3, 32, 32)).shape) # torch.Size([1, 128, 4, 4])BatchNorm2d normalises each channel's activations, which speeds up training and makes it stable.
Practice Body shapes
Build a three-block body with the given blok function (3→16→32→64 channels). For a (2, 3, 64, 64) input, print the shape after every block on separate lines.
Starter code
import torch
from torch import nn
def blok(giris, son):
return nn.Sequential(nn.Conv2d(giris, son, 3, padding=1), nn.BatchNorm2d(son), nn.ReLU(), nn.MaxPool2d(2))
x = torch.zeros(2, 3, 64, 64)
# buraya yazSample solution
import torch
from torch import nn
def blok(giris, son):
return nn.Sequential(nn.Conv2d(giris, son, 3, padding=1), nn.BatchNorm2d(son), nn.ReLU(), nn.MaxPool2d(2))
x = torch.zeros(2, 3, 64, 64)
govde = nn.Sequential(blok(3, 16), blok(16, 32), blok(32, 64))
for b in govde:
x = b(x)
print(x.shape)Expected output
torch.Size([2, 16, 32, 32]) torch.Size([2, 32, 16, 16]) torch.Size([2, 64, 8, 8])
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 How many pixels wide is the receptive field of two consecutive 3×3 convolutions (stride 1)?
- 3
- 9
- 5 Correct answer
- 6
Explanation: Each 3×3 layer grows the receptive field by 2 pixels: 3 + 2 = 5.
2 Add per-channel normalisation.
nn.____(32)
nn.BatchNorm2d(32)
Explanation: BatchNorm2d takes the channel count as its parameter.
3 Match the layers.
| Term | Meaning |
|---|---|
Conv2d |
Extracts patterns |
BatchNorm2d |
Normalises |
ReLU |
Non-linearity |
MaxPool2d |
Shrinks |