Object Detection
Finding and labelling every object in a scene, from R-CNN to YOLO.
Advanced lesson, about 40 minutes, with interactive demos and a quiz.
What you will learn
- Bounding boxes and IoU
- Two-stage detectors
- YOLO
- Non-max suppression
What and where
A classifier looks at a photo and says dog. A detector says dog, here, and two people, here and here, and a traffic light, up there. That extra word, where, is what lets a car brake for the right pedestrian, a checkout camera charge for three apples instead of one, and a conservationist count animals in a million camera-trap photos.
What is it?
Object detection takes an image and returns a list of objects. Each entry has three parts: a class label (person, car, dog), a bounding box (the tightest axis-aligned rectangle around the object) and a confidence score. The list can be empty or contain hundreds of entries, and that is the first thing that makes detection harder than classification: the output has a variable size.
How does it work?
Every modern detector starts with a convolutional or transformer backbone that turns pixels into a grid of feature vectors, the same machinery you met in the computer vision lesson. What differs is the head bolted on top. It must answer two coupled questions at once: is there an object at this location, and if so, what are its exact edges? Detection is therefore both a classification problem and a regression problem, trained with a combined loss.
The difficulty compounds. Objects appear at any scale, from a traffic light a few pixels tall to a bus filling the frame. They overlap and hide each other. Most of any image is background, so the model must say “nothing here” thousands of times for every “dog here”. And several predictions often land on the same object, so something has to decide which one to keep.
Why does it matter?
Detection is the perception layer of most vision products. Driver-assistance systems detect vehicles, pedestrians and signs; factories detect defects on production lines; radiology tools flag nodules for a second look; retail systems detect items on shelves. In ecology, Microsoft's MegaDetector finds animals, people and vehicles in camera-trap images so that researchers only look at the frames that contain something. Detection also feeds other tasks: tracking links detections across video frames, and many segmentation and pose models start from a detected box.
Key takeaways
- Detection outputs a variable-length list of (class, box, score), not a single label.
- It combines classification with box regression, under heavy scale variation, occlusion and background imbalance.
- It is the perception layer behind driving, inspection, medical triage, retail and ecology.
Boxes and IoU
Before we can build a detector we need a way to say how right a box is. A box that is off by three pixels is nearly perfect; a box around the wrong half of the car is not. The standard ruler is a single number between 0 and 1.
What is it?
A bounding box is four numbers. Libraries disagree about which four: corners (x1, y1, x2, y2), corner plus size (x, y, w, h) as in COCO, or centre plus size (cx, cy, w, h) as in YOLO. Mixing formats is one of the most common bugs in detection code, so always check.
Intersection over union (IoU, also called the Jaccard index) compares two boxes by dividing the area they share by the area they cover together.
IoU(A, B) = area(A ∩ B) / area(A ∪ B) = |A ∩ B| / (|A| + |B| − |A ∩ B|)IoU is 1 for identical boxes and 0 for boxes that do not touch.
How does it work?
The intersection of two axis-aligned boxes is itself a box: its left edge is the larger of the two left edges, its right edge the smaller of the two right edges, and likewise for top and bottom. If the right edge ends up left of the left edge, there is no overlap. Four max and min calls and a subtraction, which is why IoU can be computed millions of times per second.
Because it divides by the union, IoU is scale invariant: a 3-pixel error on a tiny traffic light costs far more IoU than the same error on a bus. It also punishes boxes that are too big as well as too small. Try it.
Notice the presets. Shifting a perfect box sideways by just a quarter of its width already drops IoU to 0.6. A box twice the size of the object, fully containing it, scores exactly 0.5, and so does a box covering precisely half the object. The metric is strict, and it has to be: whatever threshold we pick decides what counts as a hit.
Why does it matter?
IoU shows up in three places. Evaluation uses it to decide whether a prediction matches a ground-truth object: PASCAL VOC counted a hit at IoU 0.5, and COCO averages performance over ten thresholds from 0.5 to 0.95, rewarding tight boxes. Training uses it to decide which anchors or predictions are responsible for which objects. And inference uses it in non-max suppression to recognise duplicates. Variants such as GIoU and CIoU extend it into loss functions that still give a useful gradient when boxes do not overlap at all.
Key takeaways
- IoU = intersection area divided by union area; 1 is perfect, 0 means no overlap.
- It is strict and scale-aware: small shifts on small objects cost a lot, and oversized boxes are punished too.
- The same number drives evaluation, training assignment and duplicate removal.
Windows, proposals and R-CNN
The obvious way to find objects is brute force: take a classifier and ask it about every rectangle in the image. The history of detection is largely the story of making that question cheaper to ask.
Sliding windows
Classic detectors did exactly that. The Viola-Jones face detector (2001) slid a window over the image at many scales and made it fast with a cascade: cheap tests reject most windows immediately and only promising ones reach the expensive stages. The HOG pedestrian detector (2005) described each window by histograms of edge directions and scored it with a linear SVM. Both worked because their per-window classifiers were tiny.
With a modest 32-pixel stride and three scales you already need a few hundred classifier calls, and a realistic setup with fine strides and several aspect ratios needs tens of thousands. That is fine for a tiny boosted classifier, but hopeless for a deep CNN. Sliding windows also waste effort: nearly every window contains background.
R-CNN: propose first, then classify
What is it?
R-CNN (2014) replaced the exhaustive scan with region proposals: about 2,000 candidate boxes per image from selective search, a classical algorithm that merges similar-looking superpixels into object-like regions. Each proposal was warped to a fixed size, passed through a CNN pretrained on ImageNet, and classified by per-class SVMs, with a regressor nudging the box edges. It lifted PASCAL VOC 2012 accuracy to 53.3% mAP, more than 30% better in relative terms than the previous best. Deep learning had arrived in detection.
How does it work (and get faster)?
R-CNN was slow because it ran the CNN 2,000 times per image. Fast R-CNN (2015) ran the CNN once over the whole image and then cut each proposal's features out of the shared feature map with RoI pooling, which max-pools any rectangle into a fixed 7 x 7 grid. With VGG16 it trained 9 times faster and tested 213 times faster than R-CNN.
The proposals were now the bottleneck, still computed by selective search on the CPU. Faster R-CNN (2015) made them learnable with a region proposal network (RPN): a small convolutional head that slides over the feature map and, at each position, scores a fixed set of reference boxes called anchors for “objectness” and regresses corrections to them. The best few hundred proposals go to the Fast R-CNN head. The whole system ran at about 5 frames per second on a GPU and became the reference two-stage design.
The rule is Faster R-CNN's: an anchor is positive if its IoU with some object is at least 0.7, or if it is the single best anchor for an object (so nothing goes unassigned); it is negative if its IoU with every object is below 0.3; everything in between is ignored during training. At 0.7 only the car has anchors that clear the bar. Every other object relies on the forced best-match rule, and the front person, tall and thin, gets a best anchor with an IoU of only 0.37. Drop the positive threshold to 0.5 and more anchors join in. The box regression head exists precisely to close this gap, but objects whose shapes the anchor set covers poorly get fewer and weaker training signals, which is one reason detectors have historically struggled with them, and why anchor sizes are tuned to the dataset.
Why does it matter?
Two-stage detectors are still the conceptual template. The split into “where might objects be” and “what exactly is each one” is intuitive, accurate, and easy to extend: Mask R-CNN added a mask branch to the second stage and became a standard for instance segmentation. The cost is speed and complexity: two networks, anchors to tune, and many hyperparameters.
Key takeaways
- Sliding windows ask a classifier about every rectangle; the count explodes with scales, strides and aspect ratios.
- R-CNN classified about 2,000 proposals; Fast R-CNN shared the CNN pass; Faster R-CNN learned the proposals with an RPN.
- Anchors are reference boxes labelled positive or negative by IoU with ground truth; their sizes bias what the model finds easy.
One look: YOLO and SSD
In 2015 a paper with a memorable title asked why detection needed two stages at all. You Only Look Once framed the whole problem as one regression from pixels to boxes, and ran it at video speed.
What is it?
A one-stage (or single-shot) detector predicts class scores and box coordinates directly from the backbone's feature map, at every location, in one forward pass. There is no separate proposal step. The original YOLO processed 45 frames per second, and a smaller variant 155, while two-stage detectors of the time managed single digits.
How does it work?
YOLOv1 divides the image into an S x S grid (S = 7). The cell that contains an object's centre is responsible for it. Each cell predicts B = 2 boxes, each as (x, y, w, h, confidence), plus one set of C class probabilities. The x and y are offsets inside the cell, and w and h are fractions of the image, so every number the network outputs is between 0 and 1. For PASCAL VOC with 20 classes, the whole output is a 7 x 7 x 30 tensor.
At S = 5 the two people fall into the same cell, which YOLOv1 can only give one class and two boxes. That is the design's known weakness: crowds and flocks of small objects. Later versions predict several anchors per cell, at several resolutions, and assign objects more flexibly.
SSD, feature pyramids and focal loss
SSD (2016) attached prediction heads to several feature maps of decreasing resolution: fine maps catch small objects, coarse maps large ones, each with its own default boxes. Feature pyramid networks later refined the idea by mixing high-level semantics back into the high-resolution maps, and nearly every modern detector uses some form of multi-scale head.
One-stage detectors still trailed two-stage ones in accuracy until RetinaNet (2017) diagnosed why. A dense detector evaluates on the order of 100,000 candidate locations per image, nearly all easy background. Their many small losses swamp the few informative ones. Focal loss multiplies the standard cross-entropy by (1 − p)γ, shrinking the loss on examples the model already gets right so training focuses on the hard ones. With it, a one-stage detector matched two-stage accuracy.
FL(p_t) = −αt (1 − pt)γ log(pt)Focal loss. p_t is the predicted probability of the true class; gamma = 2 in the paper. An easy example with p_t = 0.9 has its loss cut by a factor of 100.
Why does it matter?
Single-pass detection is what made real-time vision practical on drones, phones and cameras. The YOLO name became a family maintained by several groups, each version trading off speed, accuracy and ease of deployment. Many recent versions also dropped hand-designed anchors in favour of predicting boxes directly from each location (“anchor-free”), removing a set of hyperparameters that had to be tuned per dataset.
Key takeaways
- One-stage detectors predict classes and boxes at every location in a single pass, trading some accuracy for large speed gains.
- YOLOv1 makes the cell containing an object's centre responsible for it, which fails when many objects crowd one cell.
- Multi-scale heads handle object size; focal loss handles the flood of easy background examples.
Non-max suppression
Dense detectors are enthusiastic. Neighbouring cells and anchors all see the same car and all report it, so the raw output is a cloud of overlapping boxes. Non-max suppression is the short, greedy algorithm that turns that cloud into one box per object.
What is it?
Non-maximum suppression (NMS) keeps the highest-scoring box in each cluster of overlapping same-class boxes and discards the rest. It has two knobs: a score threshold that throws away low-confidence boxes first, and an IoU threshold above which two boxes are considered duplicates.
How does it work?
- Drop every box whose score is below the score threshold.
- Sort the remaining boxes by score, highest first.
- Take the top box and keep it.
- Remove every remaining box of the same class whose IoU with the kept box exceeds the IoU threshold.
- Repeat from step 3 until no boxes remain.
With the defaults you should get all five objects plus one false alarm, a low-confidence “person” on the traffic light pole. Raise the score threshold to 0.35 and it disappears. Now lower the IoU threshold to 0.25: every box on the person standing behind overlaps the front person's best box by more than that, so NMS deletes a real person. Raise it to 0.8 instead and duplicates of the car and the front person survive as false positives. There is no single right setting; crowded scenes want a high IoU threshold, sparse scenes a low one.
Why does it matter?
For a decade NMS was part of every detector's deployment and a frequent source of latency and bugs. That is why making detectors NMS-free became a major goal. YOLOv10 (2024) trained YOLO with a one-to-one assignment head so each object gets exactly one prediction, and Ultralytics YOLO26 (January 2026) made NMS-free output the default. The idea came from the transformer detectors in the next section.
Key takeaways
- NMS keeps the best box and deletes same-class boxes that overlap it beyond an IoU threshold, repeatedly.
- A low IoU threshold deletes real overlapping objects; a high one leaves duplicates.
- Modern end-to-end detectors learn to output one box per object and drop NMS entirely.
Transformers and open vocabulary
Anchors, grids and NMS are all ways of coping with one awkward fact: an image contains an unknown number of objects. In 2020 a team at Facebook AI tackled that fact head-on by predicting a set.
What is it?
DETR (DEtection TRansformer) feeds backbone features into a transformer encoder-decoder. The decoder receives a fixed number of learned object queries (100 in the paper), and each query outputs either one object (class and box) or “no object”. No anchors, no proposals, no NMS.
How does it work?
The trick is in training. With 100 predictions and, say, 5 real objects, which prediction should be compared with which object? DETR finds the one-to-one assignment with the lowest total cost using the Hungarian algorithm, where the cost mixes class probability and box distance. Matched predictions are trained towards their object; every unmatched prediction is trained towards “no object”. A duplicate is therefore explicitly penalised, and the queries learn, through self-attention, to coordinate and divide the objects among themselves.
DETR was elegant but slow to train and weak on small objects. A string of follow-ups fixed that: Deformable DETR attended to a few sampled points instead of the whole image, DINO improved the query design and training, and RT-DETR (2023) reached real-time speeds, directly competing with YOLO models. The line between “YOLO” and “transformer” detectors is now blurry: modern YOLOs borrow one-to-one matching and attention blocks, and DETRs borrow efficient convolutional backbones.
Open-vocabulary detection
What changes?
Every detector so far has a fixed list of classes baked into its final layer: COCO's 80, say. Ask it for a “forklift” and it has nothing to say. Open-vocabulary detectors replace that final layer with a comparison between region features and text embeddings, learned from large collections of image-caption pairs, the same idea that powers CLIP. The class list becomes an input you type at inference time.
Grounding DINO (2023) fuses the text prompt into a DETR-style detector at several stages, so a phrase like “the dog on the left” can select a specific instance. YOLO-World (2024) brings the idea to YOLO speeds by precomputing the text embeddings for a vocabulary once, so it runs in real time. Large multimodal models can also output box coordinates when asked where something is, which blurs detection into general visual question answering.
Multimodal models: how vision and language share one embedding space, and why that makes text-prompted detection possible.Why does it matter?
End-to-end set prediction removed the last hand-crafted stage from the pipeline, and open vocabularies removed the need to collect labelled boxes for every new category before you can detect it. In practice, teams now often prototype with a promptable open-vocabulary model, use it to pre-label data, and then train or fine-tune a small, fast closed-set detector for deployment.
Key takeaways
- DETR predicts a fixed-size set of objects and uses optimal bipartite matching in training, so no anchors or NMS are needed.
- RT-DETR and NMS-free YOLOs show that end-to-end detection now runs in real time.
- Open-vocabulary detectors compare regions with text embeddings, so new classes can be named at inference time.
Measuring detectors: mAP
Detection papers are ranked by one number, mean average precision. It sounds opaque, but it is built from two ideas you already know, precision and recall, plus the IoU rule for what counts as a hit.
What is it?
For one class, sort all detections across the test set by confidence. Walk down the list; each detection is a true positive if it matches a not-yet-matched ground-truth object with IoU above the threshold, and a false positive otherwise (a wrong location, or a duplicate). After each step, precision is TP / (TP + FP) and recall is TP / (number of ground-truth objects). Plotting precision against recall gives a curve; average precision (AP) is the area under it. mAP is the mean of AP over classes.
How does it work?
Follow the default table. The first two detections are correct, so precision is 1.0 and recall climbs to 2/6 = 0.33. The third is a false positive: precision falls to 0.67 while recall stays put. Precision zigzags down as false positives creep in; recall only ever rises. The model finds 5 of the 6 people, so recall never exceeds 0.83 and the missing sixth person caps the area.
Before integrating, the zigzag is smoothed: at each recall level we take the best precision achievable at that recall or higher. That is the teal envelope. Its area here is 0.674. Try flipping the third detection to TP: the envelope jumps and AP rises, because high-confidence mistakes are the most costly. Flipping the last detection barely moves anything.
APCOCO = mean over t ∈ {0.50, 0.55, …, 0.95} of AP at IoU threshold tCOCO-style AP averages over ten IoU thresholds, so it rewards precise localisation, not just rough hits.
Why does it matter?
mAP summarises the whole confidence range, so it does not depend on a deployment threshold. But be careful when reading numbers. “AP50” (VOC style) and “AP” (COCO style, 0.5:0.95) are very different scales; COCO also reports AP for small, medium and large objects, and small objects are usually far worse. A single average can hide a class the product depends on. In deployment you also pick one operating point on the curve: a security camera might favour recall, a product that pages a human favours precision. The same trade-off is explored with classifiers in the model evaluation lesson.
Key takeaways
- A detection is a true positive only if it matches an unclaimed object above the IoU threshold; duplicates are false positives.
- AP is the area under the interpolated precision-recall curve for one class; mAP averages it over classes.
- COCO AP averages over IoU 0.5 to 0.95 and rewards tight boxes; always check which AP a paper reports.
In the real world
A detector with excellent mAP on a benchmark can still fail badly on the street. The gap between benchmark and deployment is where most of the real engineering happens.
Where is it used?
Driving and roboticsVehicles, pedestrians, cyclists, signs; fused with lidar and radar and tracked over time. Latency budgets are tight.Industry and retailDefects on production lines, items on shelves, packages on conveyors. Controlled lighting helps; rare defects do not.MedicineNodules, polyps and fractures flagged for a clinician to review. Recall matters, and every flag costs human time.Science and conservationAnimals in camera traps, cells in microscopy, ships and buildings in satellite imagery, often with very small objects.How does it fail?
- Small objects. A distant pedestrian may occupy a few dozen pixels, less than one cell of a coarse feature map. Small-object AP is routinely a fraction of large-object AP.
- Occlusion and crowds. Partially hidden objects look unlike training examples, and heavily overlapping instances collide with NMS, as the demo showed.
- Domain shift. Night, rain, a new camera, a new country's road signs. Performance measured on one distribution does not transfer automatically to another.
- Rare classes and the long tail. A model trained mostly on cars and people may see a mobility scooter or a horse-drawn cart only a handful of times.
- Adversarial inputs. A printed patch held in front of the body was shown to substantially reduce a person detector's ability to find the person holding it.
- Uneven error rates. If some groups of people are under-represented in training data, detection recall can differ between them, a fairness problem when detection feeds safety systems. See the AI ethics lesson.
Why does it matter?
A missed detection is invisible: nothing is drawn, so nobody notices until something goes wrong. Robust systems therefore evaluate on data from the deployment conditions, break metrics down by object size, class and condition, track objects over time so a single missed frame does not matter, and keep a human in the loop where errors are costly.
Try a real detector: the Object Detection Lab runs COCO-SSD in your browser on your camera or sample images. Watch how scores and boxes change as you move, partially hide an object, or step into poor light.Next in the Seeing path: image segmentation, where boxes become pixel-accurate masks.Key takeaways
- Detection is deployed in driving, industry, medicine and science, each with its own latency and error-cost profile.
- Small objects, occlusion, domain shift, rare classes and adversarial inputs are the classic failure modes.
- Evaluate on deployment-like data, report metrics per size and condition, and design for the cost of a missed object.
Check your understanding
Seven scenarios drawn from real detection projects. Each explanation adds something the lesson only touched on.
Question 1 of 7A warehouse robot must count parcels on a shelf. Your model outputs "parcel: 0.97" for the whole image and nothing else. What task is it actually solving, and what do you need instead?
References
The original papers are unusually readable. If you read two, make them YOLO for the single-pass idea and DETR for set prediction; the COCO paper explains why the evaluation protocol looks the way it does.
References
- [1]
Efficient Pipeline for Camera Trap Image Review(opens in a new tab)
Beery, S., Morris, D., Yang, S., 2019
Introduces MegaDetector, a detector for animals, people and vehicles in camera-trap images used by conservation projects.
- [2]
Microsoft COCO: Common Objects in Context(opens in a new tab)
Lin, T.-Y., Maire, M., Belongie, S., et al., 2014
The benchmark dataset of 80 object categories whose evaluation protocol (AP averaged over IoU 0.5 to 0.95) became the field standard.
- [3]
Rapid Object Detection using a Boosted Cascade of Simple Features(opens in a new tab)
Viola, P., Jones, M., 2001
CVPR 2001. Real-time face detection with Haar-like features, the integral image and an attentional cascade.
- [4]
Histograms of Oriented Gradients for Human Detection(opens in a new tab)
Dalal, N., Triggs, B., 2005
CVPR 2005. HOG features with a linear SVM, scanned over every window position and scale.
- [5]
Rich Feature Hierarchies for Accurate Object Detection and Semantic Segmentation(opens in a new tab)
Girshick, R., Donahue, J., Darrell, T., Malik, J., 2014
R-CNN. Runs a CNN on about 2,000 warped proposals per image; 53.3% mAP on PASCAL VOC 2012, over 30% relative improvement.
- [6]
Selective Search for Object Recognition(opens in a new tab)
Uijlings, J. R. R., van de Sande, K. E. A., Gevers, T., Smeulders, A. W. M., 2013
IJCV 2013. Hierarchical grouping of superpixels into a few thousand class-agnostic region proposals.
- [7]
Fast R-CNN(opens in a new tab)
Girshick, R., 2015
Shares one convolutional pass across all proposals with RoI pooling; 9x faster training and 213x faster testing than R-CNN with VGG16.
- [8]
Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks(opens in a new tab)
Ren, S., He, K., Girshick, R., Sun, J., 2015
Introduces the region proposal network and anchor boxes; about 5 frames per second with VGG-16 on a GPU.
- [9]
You Only Look Once: Unified, Real-Time Object Detection(opens in a new tab)
Redmon, J., Divvala, S., Girshick, R., Farhadi, A., 2016
Detection as a single regression from a 7x7 grid; 45 frames per second, 155 for the smaller Fast YOLO.
- [10]
SSD: Single Shot MultiBox Detector(opens in a new tab)
Liu, W., Anguelov, D., Erhan, D., et al., 2016
One-stage detection with default boxes on several feature maps of different resolution.
- [11]
Focal Loss for Dense Object Detection(opens in a new tab)
Lin, T.-Y., Goyal, P., Girshick, R., He, K., Dollar, P., 2017
RetinaNet. Identifies extreme foreground-background imbalance as the reason one-stage detectors lagged, and fixes it with focal loss.
- [12]
YOLOv10: Real-Time End-to-End Object Detection(opens in a new tab)
Wang, A., Chen, H., Liu, L., et al., 2024
NMS-free training for YOLO using consistent dual label assignments.
- [13]
Ultralytics YOLO26 documentation(opens in a new tab)
Ultralytics, 2026
Released January 2026. Detection models are NMS-free by default, outputting final boxes directly.
- [14]
End-to-End Object Detection with Transformers(opens in a new tab)
Carion, N., Massa, F., Synnaeve, G., Usunier, N., Kirillov, A., Zagoruyko, S., 2020
DETR. Treats detection as direct set prediction with a transformer and bipartite matching loss; removes anchors and NMS.
- [15]
DETRs Beat YOLOs on Real-time Object Detection(opens in a new tab)
Zhao, Y., Lv, W., Xu, S., et al., 2023
RT-DETR, a real-time end-to-end transformer detector (CVPR 2024).
- [16]
Liu, S., Zeng, Z., Ren, T., et al., 2023
Detects arbitrary objects described by text by fusing language and vision inside a DETR-style detector.
- [17]
YOLO-World: Real-Time Open-Vocabulary Object Detection(opens in a new tab)
Cheng, T., Song, L., Ge, Y., Liu, W., Wang, X., Shan, Y., 2024
Adds vision-language pretraining to a YOLO detector so the class list can be given as text at inference time (CVPR 2024).
- [18]
Thys, S., Van Ranst, W., Goedeme, T., 2019
A printed patch held by a person substantially lowers a YOLOv2 person detector's ability to find them.
Related
- Builds on: Computer Vision
- Practise in the lab: Object Detection Lab