Skip to content
BIASED

Semantic segmentation

Dense prediction, encoder–decoder / FCN, skip connections, upsampling, mIoU — closes the CNN module.

Dense labelling

The topic Object detection located objects with boxes. A box is a coarse answer: a road or the sky has no useful box, and a box around a cyclist contains as much street as cyclist. Segmentation labels every pixel instead. The output has the size of the input, which a classification backbone — built to shrink an image to one vector — cannot produce directly.

Three tasks share the name. Semantic segmentation assigns every pixel a class (two cars become one “car” region). Instance segmentation gives each countable “thing” its own mask. Panoptic segmentation combines both: every pixel gets a class, and things also get an instance id; amorphous “stuff” gets only a class. This topic focuses on the semantic task, the base of the other two.

A patch classifier at every pixel works and is slow: a 512×512512 \times 512 image needs 262,144262{,}144 forward passes, almost all redundant. Key idea: run the network once on the whole image; restore resolution with skips or keep it with dilated convolutions; train with a loss summed over pixels.

Fully convolutional networks

A classification network ends in fully connected layers that fix the input size. A linear layer on a C×7×7C \times 7 \times 7 map is exactly a 7×77 \times 7 convolution with no padding: the same weighted sum, now sliding. Rewritten this way, the network accepts any size and returns a map of class scores. Fully convolutional networks (FCN, Long, Shelhamer and Darrell, 2015) convert a backbone so and train end to end on per-pixel labels.

A VGG-16 backbone has total stride 32: a 512×512512 \times 512 input yields a 16×1616 \times 16 map, each cell standing for a 32×3232 \times 32 block — blocky if upsampled alone. FCN-32s upsamples that map directly; FCN-16s and FCN-8s add scores from pool4 and pool3 before upsampling. On VOC 2012 test, FCN-8s reached 62.2 mean IoU.

U-Net: encoder, decoder, skips

U-Net (Ronneberger, Fischer and Brox, 2015) gives the upsampling path as much capacity as the downsampling path and passes whole feature maps across. The encoder stacks 3×33 \times 3 convolutions and 2×22 \times 2 max pools, doubling channels as resolution halves. The decoder mirrors it: upsample (often a 2×22 \times 2 transposed convolution with stride 2 — kernel size a multiple of stride, so no checkerboard from Convolution), concatenate the encoder map of the same size, then two convolutions. A final 1×11 \times 1 maps each pixel to KK logits.

The encoder supplies context; the skips supply localization; the decoder learns to combine them. Designed for biomedical images with few labels, U-Net leaned on heavy augmentation and a border-weighted loss. The same shape appears as the noise predictor in diffusion and as the generator in image-to-image GANs.

Dilations and DeepLab

Instead of throwing resolution away and buying it back, dilated (atrous) convolutions keep more of it. Rate rr reads every rr-th input: a 3×33 \times 3 with r=2r = 2 spans 5×55 \times 5 with nine weights. DeepLab changes a pretrained backbone’s output stride: set a stride-2 layer to stride 1 and double later dilations so that in the receptive-field recursion rl=rl−1+(k−1) d jr_l = r_{l-1} + (k-1)\, d\, j the product d⋅jd\cdot j is unchanged. Classification ResNets use stride 32; DeepLabv3 trains at 16 and tests at 8 or 16. Halving output stride multiplies the cost of those stages by 4.

ASPP runs parallel 3×33 \times 3 convolutions at several rates (e.g. 6, 12, 18) plus a 1×11 \times 1 and image-level pooling, then mixes with another 1×11 \times 1. Logits are bilinearly upsampled to the image. DeepLabv3 reached 85.7% mean IoU on VOC 2012 test with ResNet-101 pretrained on ImageNet then COCO.

Losses: CE and Dice

The default loss is per-pixel cross-entropy

LCE=−1HW∑ulog⁡pu,yu.L_{\mathrm{CE}} = -\frac{1}{HW} \sum_u \log p_{u, y_u}.

Every pixel weighs the same, so a class that covers 1% of pixels contributes ~1% of the loss. Weight maps (inverse frequency, or U-Net’s border emphasis) help.

The Dice coefficient D=2∣P∩G∣/(∣P∣+∣G∣)=2 IoU/(1+IoU)D = 2\lvert P \cap G \rvert / (\lvert P \rvert + \lvert G \rvert) = 2\,\mathrm{IoU}/(1+\mathrm{IoU}) is pixel F1. Soft Dice loss

LDice=1−2∑upugu∑upu+∑uguL_{\mathrm{Dice}} = 1 - \frac{2 \sum_u p_u g_u}{\sum_u p_u + \sum_u g_u}

does not shrink when the image grows around a fixed object: missing a 4-pixel object costs ~0.980.98 in both a 100-pixel and a 10{,}000-pixel image, while CE falls from ~0.180.18 to ~0.0020.002. Dice’s gradient couples all pixels through the denominator, so CE + Dice is a common default.

mIoU and the cost of labels

With confusion counts nijn_{ij} (true ii, predicted jj),

IoUi=niiti+∑jnji−nii,mIoU=1K∑iIoUi,\mathrm{IoU}_i = \frac{n_{ii}}{t_i + \sum_j n_{ji} - n_{ii}}, \qquad \mathrm{mIoU} = \frac1K \sum_i \mathrm{IoU}_i,

i.e. TP/(TP+FP+FN)\mathrm{TP}/(\mathrm{TP}+\mathrm{FP}+\mathrm{FN}) per class, then the unweighted mean. Pixel accuracy ∑inii/∑iti\sum_i n_{ii}/\sum_i t_i is dominated by the majority class. In the note’s 6×66 \times 6 example, accuracy is 0.8890.889 and mIoU 0.7670.767; predicting background everywhere scores 0.6110.611 accuracy but only 0.2040.204 mIoU. Boundary errors hit small objects hard: a disk of radius 4 predicted one pixel too small has IoU 0.560.56 while frame accuracy barely moves.

Mask R-CNN adds a 28×2828 \times 28 mask head to Faster R-CNN with RoIAlign for fractional alignment — instance masks on top of detection.

This closes the convolutional-networks module: from one convolution layer through backbones and boxes to every pixel. Labels at that density are expensive. The next topic, Self-supervised learning, asks how to learn transferable features with pretext tasks and contrastive losses when dense labels are scarce.

Check your understanding

The same questions as in Pulse. Answers are shared between modes.

Full notes

Answers are saved on the server and tied to this browser until you sign in.