Per-pixel and multi-scale: U-Net and feature pyramids
Networks that answer for every pixel, or for objects of every size. U-Net's encoder–decoder with skip connections, and the feature pyramid's top-down path inside modern detectors.
What runs in CodeSplash
- DNN012U-Net Needs a data type CodeSplash doesn't load yet
- DNN013Feature Pyramid Network (FPN) Partly runs in CodeSplash (Experimental)
- Object detection (Faster R-CNN, COCO) Experimental
- Instance segmentation (Mask R-CNN, COCO) Experimental
- Semantic segmentation (DeepLabV3, 20 classes) Experimental
You will be able to
- Explain why per-pixel tasks need a decoder and skip connections
- Trace shapes through one U-Net down-and-up step
- Describe a feature pyramid's top-down path and lateral connections
- Say where CodeSplash runs a feature pyramid, and that it has no U-Net
Read first
Answers for every pixel
ReadA classifier pools an image down to one answer. Segmentation needs an answer for every pixel, at full resolution. The encoder's pooling throws away exactly the "where" that a per-pixel answer needs.
U-NetU-NetAn encoder–decoder CNN for per-pixel labels: the decoder upsamples and concatenates the encoder's maps of the same size. adds a decoder that upsamples step by step back to the input size. At each step it concatenates the encoder's map of the same size, through a skip connection. The deep path says what is there; the skip says precisely where.
One step down and up
ReadThe program pools a 4 × 4 map to 2 × 2, upsamples it back by repetition, and stacks it with the original as a second channel: 4 × 4 × 2. At pixel (1, 1) the skip channel keeps the exact value, 0.3125, while the coarse channel holds the blurred average of its 2 × 2 block, 0.1563. A learned convolution then combines the two.
import numpy as np
def pool2(x):
return np.array([[x[i:i + 2, j:j + 2].mean() for j in range(0, x.shape[1], 2)]
for i in range(0, x.shape[0], 2)])
skip = np.arange(16.).reshape(4, 4) / 16 # encoder map, full size
low = pool2(skip) # 2 × 2, coarse
up = np.repeat(np.repeat(low, 2, axis=0), 2, axis=1) # back to 4 × 4
fused = np.stack([skip, up], axis=-1) # skip connection: concatenate
print(fused.shape, fused[1, 1])pool2 <- function(x) {
out <- matrix(0, nrow(x) %/% 2, ncol(x) %/% 2)
for (i in seq_len(nrow(out))) for (j in seq_len(ncol(out)))
out[i, j] <- mean(x[(2 * i - 1):(2 * i), (2 * j - 1):(2 * j)])
out
}
skip <- matrix(0:15, 4, 4, byrow = TRUE) / 16
up <- kronecker(pool2(skip), matrix(1, 2, 2)) # upsample by repetition
fused <- array(c(skip, up), c(4, 4, 2))
print(dim(fused)); print(fused[2, 2, ])Objects of every size
ReadSmall objects show up in fine, early maps that know little about what they see; large ones in coarse, late maps that know a lot. A feature pyramid networkFeature pyramid networkAdds a top-down path that sends coarse, meaningful features back to finer maps, so objects of every size are found. gets both: a top-down path upsamples each coarse map and adds it to a 1 × 1 projection of the finer encoder map (a lateral connection), then smooths with a 3 × 3 convolution. Every level ends up with deep features, and the detector predicts boxes at each level.
The reference program builds two levels from a 4 × 4 map: P3 is 4 × 4 and P4 2 × 2, with a lateral weight of 0.8.
Concatenate or add?
PredictPredict first
How do U-Net and a feature pyramid each merge the upsampled coarse map with the finer one?
Reveal
U-Net concatenates the encoder's map as extra channels; FPN adds the upsampled map to a 1 × 1 projection of the finer map, so every level keeps the same channel count.
What runs here
Read| Entry | In CodeSplash |
|---|---|
| U-Net (DNN012) | Needs a data type CodeSplash doesn't load yet: per-pixel mask labels. Semantic segmentation uses a pretrained DeepLabV3, which keeps resolution with dilated convolutions instead |
| Feature Pyramid Network (DNN013) | Partly runs in CodeSplash: inside the pretrained ResNet-50 FPN models of Object detection and Instance segmentation; you run them, you don't train them |
Check: U-Net and FPN
CheckpointCheck yourself
Why does U-Net copy encoder maps to the decoder?
Show the answer
Answer: a
The deep path knows what is in the image but has lost detail. The skip connections bring back the where.
Check yourself
A decoder upsamples a 16 × 16 × 128 map to 32 × 32 × 128 and concatenates the encoder's 32 × 32 × 64 map. What enters the next convolution?
Show the answer
Answer: a
Concatenation stacks channels: 128 + 64 = 192. Heights and widths must match, which is why the decoder upsamples first.
Check yourself
Can you train a U-Net in CodeSplash today?
Show the answer
Answer: b
Semantic segmentation uses a pretrained DeepLabV3, not a U-Net, and doesn't train on your masks.
Check yourself
What does a feature pyramid's top-down path give the fine, early maps?
Show the answer
Answer: a
Fine maps see small objects but know little; the top-down path adds what the deep maps learned, so small objects are recognized too.
Check yourself
Where does a feature pyramid run in CodeSplash?
Show the answer
Answer: a
Both use torchvision's COCO weights, converted once to safetensors. You run them; you don't train their pyramids.
Recap
Recap- Per-pixel tasks need a decoder; U-Net's skip connections concatenate full-resolution encoder maps back in.
- A feature pyramid adds upsampled deep features to lateral projections of finer maps, then predicts at every level.
- In CodeSplash, FPNs run inside the pretrained detectors; U-Net needs mask labels and is explained here.
Concepts in this lesson