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.
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
| Year | Idea | Model | What it solved |
|---|---|---|---|
| 2017 | Feature pyramid | FPN | Objects of different sizes without an image pyramid |
| 2017 | Focal loss | RetinaNet | Imbalance in dense detectors |
| 2019 | Anchor-free (per pixel) | FCOS | Hand-set anchor parameters |
| 2019 | Objects as points | CenterNet | Simplicity and extension to other tasks |
| 2020 | Set prediction | DETR | The need for NMS and anchors |
| 2022 | Mask classification + masked attention | Mask2Former | Unifying the types of segmentation |
| 2024 | Real-time DETR | RT-DETR | The 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:
| Method | Criterion | Needs NMS? |
|---|---|---|
| Anchors (SSD, RetinaNet, Faster R-CNN) | IoU between the anchor and the object above a threshold | Yes |
| Locations (FCOS) | The pixel is inside the box and at the right level | Yes |
| Centres (CenterNet) | The peak of the heat map at the object's centre | Simple max pooling |
| Sets (DETR) | One-to-one Hungarian matching | No |
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
- Compute the effect of focal loss if
\gamma=1in the example. - Why does DETR not need NMS while FCOS does?
- 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
- Compare the RetinaNet, FCOS, DETR and RT-DETR diagrams in the models tab.
- In the detection lab, try changing the NMS threshold, then think: how does DETR avoid this step?
- 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
- 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
- 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
- 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
- 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
- Y. Zhao et al. (2023).DETRs Beat YOLOs on Real-time Object Detection. arXiv:2304.08069 (CVPR 2024). doi:10.48550/arXiv.2304.08069
- 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
