CSE445Computer Vision
Tutorials/Object Detection/OD 8A: DETR Transformer Detection
OD-08AAdvanced 90 min

OD 8A: DETR Transformer Detection

Understand how DETR replaces anchors, NMS, and RPN with a set-prediction transformer: encoder-decoder architecture, learned object queries, and bipartite Hungarian matching.

PyTorchHugging Face TransformersMatplotlib

Code Structure Note

These notes are provided only to help students understand the code structure. Students should review, optimize, and adapt the code before using it in assignments, projects, or experiments.

Topics Covered
  1. 1DETR encoder: CNN backbone → flattened feature map → multi-head self-attention
  2. 2DETR decoder: N learnable object queries attending to encoder memory via cross-attention
  3. 3Hungarian matching: bipartite assignment between N predictions and M ground-truth boxes
  4. 4Set prediction loss: matched classification + L1 + GIoU box losses
  5. 5Deformable DETR: sparse attention on reference points — 10× faster convergence

Learning Objectives

  • Trace a full DETR forward pass: image → ResNet → transformer encoder → decoder → FFN
  • Explain how Hungarian matching eliminates NMS by treating detection as set prediction
  • Load `facebook/detr-resnet-50` from Hugging Face and visualise per-query attention maps
  • Compare DETR vs. Faster R-CNN on the same COCO images for speed and accuracy

Key Takeaways

  • DETR outputs exactly N predictions per image; Hungarian matching assigns each to a ground-truth or ∅
  • Object queries are randomly initialised embeddings that learn to specialise by position/size
  • DETR requires 500 training epochs to match Faster R-CNN — slow convergence is the main drawback
  • Deformable DETR fixes slow convergence with multi-scale deformable attention, converging in ~50 epochs

Tutorial Infographic

Notebook Resources

Jupyter Notebook

Student action area

Use this section to practice the tutorial code. Kaggle opens the prepared notebook, Colab lets you experiment in your own runtime, and the .ipynb download is best for local editing or submission.

Kaggle: follow the official tutorial notebook.
Colab: modify and rerun quickly.
IPYNB: keep a local copy.

Hands-On Exercises

  1. 1Load `facebook/detr-resnet-50` and run detection on 5 COCO val images; visualise boxes + labels
  2. 2Visualise attention maps for 3 object queries on an image with multiple objects
  3. 3Implement the bipartite matching cost matrix (class + L1 + GIoU) for a toy 4-box example
  4. 4Profile DETR encoder vs. decoder FLOPs using `torch.profiler` and compare their share

Tools & Libraries

PyTorchHugging Face TransformersMatplotlib