Computer Vision Lab Lessons Modern detection and segmentation: FPN, focal loss, DETR and Mask2Former

Modern detection and segmentation: FPN, focal loss, DETR and Mask2Former

The ideas that shaped modern detectors and segmentation models: the feature pyramid, focal loss, anchor-free detection and set prediction.

Advanced45 minObject detectionObject detection

This content was prepared automatically from documented sources and its human scientific review is not yet complete. If you find an error, let us know.

Introduction

Since 2017 detectors have changed a lot: feature pyramids everywhere, losses that handle imbalance, then getting rid of anchors, then of NMS itself.

Learning objectives

  • Understand FPN and why it improves small objects.
  • Understand focal loss.
  • Compare anchor-based, anchor-free and set-based detection.
  • Understand the idea of mask classification in Mask2Former.

Prerequisites

Two-stage detectors and semantic segmentation networks.

Scientific explanation

FPN makes every level of the pyramid semantically strong. RetinaNet added focal loss, so one-stage detectors caught up with two-stage ones. FCOS and CenterNet got rid of anchors. DETR formulated detection as set prediction with bipartite matching, so it dispensed with NMS, and RT-DETR made it fast. Mask2Former unified the three types of segmentation through mask classification.

In-depth explanation

A timeline of the ideas

YearIdeaModelWhat it solved
2017Feature pyramidFPNObjects of different sizes without an image pyramid
2017Focal lossRetinaNetImbalance in dense detectors
2019Anchor-free (per pixel)FCOSHand-set anchor parameters
2019Objects as pointsCenterNetSimplicity and extension to other tasks
2020Set predictionDETRThe need for NMS and anchors
2022Mask classification + masked attentionMask2FormerUnifying the types of segmentation
2024Real-time DETRRT-DETRThe slowness of transformer detectors

Three ways of deciding “who is responsible for this object?”

Every detector must decide during training which prediction is matched with which ground-truth object, and this decision determines much of its behaviour:

MethodCriterionNeeds NMS?
Anchors (SSD, RetinaNet, Faster R-CNN)IoU between the anchor and the object above a thresholdYes
Locations (FCOS)The pixel is inside the box and at the right levelYes
Centres (CenterNet)The peak of the heat map at the object's centreSimple max pooling
Sets (DETR)One-to-one Hungarian matchingNo

Focal loss in numbers

Example: 100,000 easy negative anchors, each p_t=0.99, and 100 hard positives, each p_t=0.3. With cross-entropy: the negatives 10^5\times0.01\approx1005 and the positives 100\times1.20\approx120; so the easy ones dominate. With focal loss (\gamma=2, without \alpha): the negatives 1005\times10^{-4}\approx0.1 and the positives 120\times0.49\approx59; so the hard ones become dominant.

Why did segmentation and detection converge?

Mask2Former treats every “segment” (an object or a region of stuff) as a query that predicts a mask and a class, with the same bipartite matching as in DETR. So detection became a special case (a box instead of a mask), and semantic segmentation a grouping of masks by class. This unification simplified the systems and allowed ideas to be shared between tasks.

How do you choose?

  • Real time on limited hardware: lightweight detectors (SSDlite, the YOLO family, minding the licence) or RT-DETR on a GPU.
  • High accuracy and masks: Mask R-CNN as a baseline, Mask2Former for the best quality.
  • Many small objects: high-resolution FPN, and watch the APS metric.
  • Overlapping crowds: NMS-free models or Soft-NMS.

Review questions

  1. Compute the effect of focal loss if \gamma=1 in the example.
  2. Why does DETR not need NMS while FCOS does?
  3. How is detection a special case of mask classification?

Mathematical equations

FL(p_t) = −α_t(1−p_t)^γ log p_t. P_ℓ = Conv3×3(Conv1×1(C_ℓ) + Up₂(P_{ℓ+1})). σ̂ = argmin_σ Σ L_match(yᵢ, ŷ_σ(i)).

Practical example

With γ=2, the loss of an example with p_t=0.9 falls a hundredfold compared with cross-entropy, so training concentrates on the hard examples.

Implementation steps

  1. Compare the RetinaNet, FCOS, DETR and RT-DETR diagrams in the models tab.
  2. In the detection lab, try changing the NMS threshold, then think: how does DETR avoid this step?
  3. Read the Mask2Former card and note how a single architecture produces three types of segmentation.

Expected results

You will know why FPN became a standard component, what each idea solves, and when to choose each family.

Common mistakes

Comparing AP numbers from different papers without accounting for the backbone, the training schedule and the input resolution; or assuming that the absence of NMS always means higher speed.

Practical applications

Self-driving, surveillance, industrial inspection, digital pathology and robotics.

References

  1. T.-Y. Lin, P. Dollár, R. Girshick, K. He, B. Hariharan, S. Belongie (2016).Feature Pyramid Networks for Object Detection. arXiv:1612.03144 (CVPR 2017). doi:10.48550/arXiv.1612.03144
  2. T.-Y. Lin, P. Goyal, R. Girshick, K. He, P. Dollár (2017).Focal Loss for Dense Object Detection. arXiv:1708.02002 (ICCV 2017). doi:10.48550/arXiv.1708.02002
  3. Z. Tian, C. Shen, H. Chen, T. He (2019).FCOS: Fully Convolutional One-Stage Object Detection. arXiv:1904.01355 (ICCV 2019). doi:10.48550/arXiv.1904.01355
  4. N. Carion, F. Massa, G. Synnaeve, N. Usunier, A. Kirillov, S. Zagoruyko (2020).End-to-End Object Detection with Transformers. arXiv:2005.12872 (ECCV 2020). doi:10.48550/arXiv.2005.12872
  5. Y. Zhao et al. (2023).DETRs Beat YOLOs on Real-time Object Detection. arXiv:2304.08069 (CVPR 2024). doi:10.48550/arXiv.2304.08069
  6. B. Cheng, I. Misra, A. G. Schwing, A. Kirillov, R. Girdhar (2021).Masked-attention Mask Transformer for Universal Image Segmentation. arXiv:2112.01527 (CVPR 2022). doi:10.48550/arXiv.2112.01527

Related algorithms

Try in object detection