Segmentation & Detection¶
Classification answers "what is in this image?" Segmentation answers "which pixels belong to what?" and detection answers "where are the objects and what are they?" Both need spatial output that plain classifiers throw away.
Rapid Recall
Segmentation labels every pixel (semantic = by class, instance = per object, panoptic = both) with an encoder-decoder plus skip connections (U-Net): the encoder captures what, the decoder recovers where, and skips carry sharp detail across. Loss is cross-entropy + Dice (Dice fixes class imbalance); the metric is mIoU. Detection outputs boxes for an unknown number of objects: two-stage (Faster R-CNN, accurate) proposes then classifies, one-stage (YOLO) regresses boxes over a grid in one pass using anchors, with NMS to remove duplicates; scored by mAP@[0.5:0.95].
§1 Image segmentation¶
Classification answers "what is in this image?" (one label). Segmentation answers "which pixels belong to what?", a label for every pixel. The output is the same H×W as the input, each pixel holding a class instead of a color.
| Flavor | What it does |
|---|---|
| Semantic | Label every pixel by class. All cats are "cat"; two overlapping cats become one blob. No notion of individual objects. |
| Instance | Separate each object. Cat #1 and #2 get distinct masks. Counts "things," ignores "stuff" (sky, road). |
| Panoptic | The union: every pixel gets a class and countable objects get distinct instance IDs. Stuff + things. |
Architecture, encoder-decoder¶
A classifier shrinks spatially and discards location to produce one label. Segmentation needs full-resolution output, so it uses an encoder-decoder (fully convolutional network):
- Encoder (downsampling), a normal CNN. Shrinks spatially, grows channels. Captures what is present, loses precise where.
- Decoder (upsampling), progressively upsamples back to full resolution via transposed convolutions or interpolation. Recovers where.
- Skip connections (U-Net's key trick), copy high-resolution encoder feature maps across to the matching decoder level. Encoder-deep layers know what; encoder-early layers know precise where (sharp edges). Skips give the decoder both. Without them, boundaries are blurry.
A normal conv maps a region to one value (downsample). A transposed conv reverses it, mapping one value to a region, learnably. Output size is \(O = (W - 1)S - 2P + K\). People increasingly prefer bilinear upsample + a 3×3 conv instead, because transposed convs cause checkerboard artifacts (uneven kernel overlap when K isn't divisible by S).
Getting data¶
Labels are per-pixel masks, an image where each pixel's value is its class ID. Expensive: tracing object boundaries can take 10 to 30 min/image. Formats: single-channel mask (pixel value = class index); instance segmentation also stores per-instance polygons / RLE (COCO). Datasets: COCO, Cityscapes, Pascal VOC, ADE20K. Cost mitigation: SAM (Segment Anything) generates masks from a click/box; weak supervision; augmentation.
Critical augmentation rule
Any geometric transform applied to the image must be applied identically to the mask (flip image → flip mask). Photometric transforms (brightness, blur) apply to the image only.
Loss functions¶
Output: per-pixel logits \([B, C, H, W]\); target: \([B, H, W]\) of class indices. Two families, usually combined. Pixel-wise cross-entropy:
Its problem is class imbalance: if 95% of pixels are background, CE is dominated by easy background and the model predicts "background everywhere." Fixes: class-weighted CE, or focal loss (down-weights easy pixels). Dice loss directly optimizes overlap:
It measures predicted ∩ truth over their sizes, so a tiny foreground region contributes equally regardless of surrounding background, and imbalance stops mattering. The workhorse combo is \(\mathcal{L} = \mathcal{L}_{CE} + \mathcal{L}_{Dice}\), especially in medical imaging.
Metric, IoU¶
Compute per class, average to get mIoU, the headline number for semantic segmentation. Dice/F1 is also reported (medical); \(\text{Dice} = 2\cdot\text{IoU}/(1+\text{IoU})\), so they are monotonic.
| Net | Idea |
|---|---|
| FCN (2015) | First fully-convolutional; replaced dense layers with convs, learned upsampling. |
| U-Net (2015) | Encoder-decoder with skip connections; dominant in medical/small-data. |
| DeepLab v3+ | Atrous/dilated convolutions (enlarge receptive field without losing resolution) + ASPP (multi-scale). |
| Mask R-CNN (2017) | Instance-segmentation standard: detection + a mask head. |
§2 Object detection and YOLO¶
Detection = classification + localization, for multiple objects at once. The output is a set of bounding boxes, each \((x, y, w, h, \text{class}, \text{confidence})\). It is harder than classification because the number of objects is unknown and variable, so there is no fixed-size output.
The central dichotomy is two paradigms:
| Two-stage (R-CNN family) | One-stage (YOLO, SSD, RetinaNet) |
|---|---|
| Accuracy-first. Stage 1: a Region Proposal Network proposes ~1000s of candidate regions. Stage 2: a CNN classifies + refines each. Lineage: R-CNN → Fast R-CNN (run CNN once, RoI-pool per region) → Faster R-CNN (learnable RPN, end-to-end) → Mask R-CNN (+ mask head = instance segmentation). | Speed-first. Skip proposals. In a single forward pass, directly predict boxes + classes over a grid. Real-time; historically slightly weaker on small/crowded objects, gap now largely closed. |
How YOLO works ("You Only Look Once")¶
Frame detection as a single regression problem over a grid:
- Divide the image into an \(S \times S\) grid (for example 13×13).
- Each cell predicts \(B\) boxes \((x, y, w, h, \text{objectness})\) plus class probabilities. Objectness = "is there an object whose center falls in my cell, and how confident am I."
- One forward pass produces all boxes for the whole image at once, vs R-CNN looking thousands of times.
Image → 13×13 grid. The cell containing the object's CENTER detects it.
┌──┬──┬──┬──┐
│ │ │ │ │ Each cell outputs B boxes:
├──┼──┼──┼──┤ (x, y, w, h, conf) × B + class probs
│ │ ●│ │ │ ● = object center → that cell predicts the box
├──┼──┼──┼──┤
│ │ │ │ │
└──┴──┴──┴──┘
Predicting raw box sizes is unstable. Define a few anchor boxes, prior shapes (tall, wide, square) per cell, learned from dataset box statistics (k-means on training boxes). The network predicts offsets/scales relative to anchors, which is much easier to learn. (YOLO v8+ moved to anchor-free, predicting center+size directly, but anchors are the classic concept.)
Interview note: Non-Max Suppression (NMS)
The grid produces many overlapping boxes for one object. NMS cleans up: sort boxes by confidence; keep the highest-confidence box; remove every other box whose IoU with it exceeds a threshold (for example 0.5), the duplicates of the same object; repeat with the next-highest remaining box. Without NMS you get 5 boxes around one cat. A near-guaranteed interview question.
The YOLO loss is multi-task:
The box loss is regression on coordinates. Original YOLO used squared error on \((x, y, \sqrt{w}, \sqrt{h})\), the square root makes a 5px error matter more on a small box than a large one; modern variants use IoU-based losses (GIoU/DIoU/CIoU). The objectness loss asks does this box contain an object (BCE); \(\lambda_{coord} \approx 5\) up-weights localization and \(\lambda_{noobj} \approx 0.5\) down-weights the flood of empty boxes (the foreground/background imbalance that also motivated focal loss in RetinaNet). The classification loss is which class given an object (CE / BCE).
Data and metric¶
Labels: bounding boxes \((\text{class}, x_c, y_c, w, h)\), usually normalized to [0,1]. Cheaper than masks, pricier than classification. Datasets: COCO (80 classes), Pascal VOC, Open Images. Augmentation: box-aware (flips/crops transform the boxes too) plus Mosaic (stitch 4 images, a YOLO signature).
For mAP (mean Average Precision): a prediction is a True Positive if IoU with a ground-truth box ≥ threshold (for example 0.5) and the class matches. Sweep the confidence threshold to get a precision-recall curve; Average Precision is the area under it, per class; average AP over classes to get mAP. mAP@0.5 uses IoU 0.5; mAP@[0.5:0.95] (COCO's primary) averages over IoU 0.5 to 0.95 in 0.05 steps, rewarding tight boxes.