Skip to content
mlmentorship

Semantic segmentation

Assign a class to every pixel: encoder-decoder architectures, losses, IoU, class imbalance, boundaries, and deployment constraints.

Published · 7 min read ·Specialist ·Intermediate
Reading depth

Summary

Semantic segmentation predicts a semantic class at each evaluated pixel, rather than a box or a separate object identity. An encoder can recognize an object while discarding its precise boundary. Ask which spatial evidence survives and which denominator reveals a small-class failure.

Output logits have shape : batch, classes, height, and width. Align predictions with the target coordinates and exclude declared ignore labels. Resize categorical targets by nearest-neighbor unless a separate soft-label policy applies.

Architectures

  • FCN: replaces dense heads with convolutional prediction.
  • U-Net: combines coarse semantics with retained higher-resolution encoder features.
  • DeepLab: uses dilation and multiscale context with a declared output stride.
  • Transformer decoders: combine global context with learned masks or pixel queries.

If two boundaries produce the same coarse representation, its decoder alone cannot identify which boundary occurred. For example, averaging and yields the same value . Interpolation restores size, not that erased distinction. A learned prior may infer plausible detail; a skip supplies additional evidence without guaranteeing exact recovery.

Learning objective

Enlarging a coarse grid cannot recover an edge location that downsampling lost

Upsampling cannot restore an edge that was discardedTwo high-resolution patterns can average to the same coarse value. Enlarging that value cannot identify which original boundary was present.Different boundaries collapse to thesame coarse cell0.50.5Enlarging 0.5 cannot reveal whichpattern was pooled.Fine-resolution evidence supplies themissing distinction.
Derivation
Downsampling loses exact boundary location, while an encoder skip supplies fine evidence to the decoder A fine eight-column input feature grid contains an object edge between columns five and six. Encoding and downsampling produce a coarse four-column semantic grid where the edge occupies an uncertain cell. Upsampling that coarse grid creates a larger mask but leaves a wide dashed uncertain boundary. A dashed skip path carries same-scale fine encoder features around the bottleneck; fusing those features with coarse semantics places a sharp solid boundary between columns five and six. SPATIAL SIZE IS NOT THE SAME AS SPATIAL EVIDENCE 1 · FINE ENCODER FEATURES high resolution · exact edge retained edge at one pixel boundary encode + downsample 2 · COARSE SEMANTICS object recognized · edge location compressed object object edge? wide cell background upsample alone 3 · LARGER GRID, SAME UNCERTAINTY boundary could lie here SKIP fine edge evidence 4 · FUSE SEMANTICS + SAME-SCALE DETAIL
the fine grid retains the edge, while the coarse grid leaves an uncertain cell. Enlarging that cell does not restore distinctions already erased. The dashed skip supplies fine evidence that can distinguish candidate boundaries; success still depends on those features and the decoder. The illustrated sharp edge is an outcome, not a reconstruction guarantee. Original synthesis informed by Long et al. (2015) and Ronneberger et al. (2015).
Check: Why does enlarging the coarse grid alone not determine the original one-pixel edge location?
Read it this way: Two high-resolution patterns can average to the same coarse value. Enlarging that value cannot identify which original boundary was present.
Which original pattern can be recovered from the coarse value alone?

Losses

Mean pixel cross-entropy averages only evaluated pixels; an empty valid set needs an explicit skip/error policy. Weighted or focal losses change contributions; Dice emphasizes overlap; boundary terms emphasize localization. None removes annotation uncertainty or automatically supplies calibrated probabilities.

Metrics

Intersection over Union for class is

For 95 background and five object pixels, predicting background everywhere gives:

ClassTPFPFNIoU
Object0050
Background95500.95

Accuracy is , while two-class macro mIoU is . The metric correctly measures pixel agreement, not rare-class success. Zero IoU denominators require a stated absent-class policy; ignored pixels are not background.

Make rare positives more important

New task: prioritize the small foreground class.

Picture and change: weight positive cross-entropy terms by , background terms by .

Check: weighted class masses become and . A constant-score model’s optimum changes from foreground probability to weighted score . Report class-specific results and validate calibration rather than interpreting the weighted score as the original probability.

Use when: missed foreground carries different task costs; the all-background object’s IoU remains zero.

Fit inference into smaller tiles

New task: full-resolution activations exceed memory.

Picture and change: a tile uses 262,144 pixels versus 921,600 for a image. Along each axis, origins and produce a 64-pixel overlap.

Check: global coordinate is local coordinate in the second tile. Align and blend overlapping logits before class selection; do not average discrete class IDs. Context, padding, and tile boundaries may still differ from full-image inference.

Use when: activation memory requires tiling, with measured overlap, latency, and boundary checks.

In an interview

Define the spatial transform, ignore mask, label set, and evaluation unit before choosing an architecture. Report per-class IoU, the declared class average, boundary quality, and relevant object/task outcomes. Memory savings require accounting for feature channels, temporary workspaces, tiling overlap, and batch size, not pixels alone.

Common confusions

Hard binary Dice is . One soft convention uses . Declare , valid pixels, per-image versus pooled reduction, and the empty-mask policy. Other denominator conventions define different objectives.

Boundary penalties can emphasize uncertain or inconsistent annotations. Confidence thresholds and abstention need validation on the deployment population; higher mask precision is not automatically higher task value.

Can semantic labels distinguish two touching objects of the same class?

Answer

Not necessarily: both objects can share the same semantic label throughout their union. Instance segmentation supplies separate identities; detection supplies boxes rather than full pixel assignments.

Related: CNN architecture, ResNet, and object detection.