TL;DR
Object detection takes an image and returns a list of objects. Each entry gives a class label, such as "cat", and a rectangle, called a bounding box, that tightly encloses the object. A single image can contain zero, one or dozens of such entries.
It differs from image classification, which gives one label for the whole picture. A detector has to answer two questions for each object: what is it, and where is it. It does both with the same network, usually with a convolutional backbone followed by a head that scores candidate boxes and then cleans up duplicates.
Builds on: Convolutional Neural Network, Supervised Learning, Intersection over Union, Non-Maximum Suppression, Bounding-Box Regression · Leads to: Image Segmentation, You Only Look Once: Unified, Real-Time Object Detection, Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks
Where it's used
Driving assistance and self-driving cars. A car's cameras must find pedestrians, cyclists and other vehicles in every frame, and the box tells the planner how far away each one is and how much room it takes up. Detectors in this setting must run many times per second on onboard hardware, which is why one-stage designs such as YOLO were developed.
Phone cameras and photo apps. Face detection in a camera preview draws a box around each face so that focus and exposure can be set on it. Early practical systems in the early 2000s used boosted classifiers over sliding windows; modern phones run small convolutional detectors.
Retail shelves and warehouses. A camera pointed at a shelf can detect every product facing, and a count of boxes per product tells staff when a shelf is empty or mis-stocked. The task needs a model trained on the specific products, because general-purpose detectors know only their training categories.
How it works
We follow one image with a cat and a dog through a toy detector. The detector has already been trained, and it produces candidate boxes with a score for each one.
- Input. A photo of 100 by 100 pixels showing a cat near the top left and a dog near the bottom right.
- Backbone. A Convolutional Neural Network reads the image and produces a smaller grid of feature vectors. Each grid cell summarises a patch of the picture. Check: the grid is coarser than the pixels, so each cell covers several pixels.
- Candidates. Each grid cell proposes a few candidate boxes. The network predicts, for each candidate, a class probability vector and a small correction to the candidate's position and size (see Bounding-Box Regression). Here we get three useful candidates: box A on the cat with cat score 0.90, box B also on the cat with cat score 0.75, and box C on the dog with dog score 0.60.
- Threshold. Drop every candidate whose best class score is below 0.50. All three survive.
- Clean up duplicates. Boxes A and B cover almost the same region. Non-Maximum Suppression keeps the higher scoring one, A, and removes B because the two boxes overlap strongly (their IoU is about 0.82, defined in the math section). C does not overlap A, so it stays. Check: the output must contain at most one box per object.
- Output. Two labelled boxes: cat with box A, and dog with box C.
flowchart LR I[Image] --> B[CNN backbone] B --> H[Head: class scores and box offsets] H --> T[Keep scores above threshold] T --> N[Non-maximum suppression] N --> O[Labelled boxes]
- IoU of two equal boxes
The math
Step 2 in symbols: the backbone
Let be the input image and the feature grid produced by the backbone with weights . Each cell of is a vector, and it is the input to the head.
Step 3 in symbols: the head
For each candidate with anchor box , the head outputs class probabilities , with one entry per class plus background, and four offsets. The offsets are the standard parameterisation from R-CNN:
where is the ground-truth box. The network predicts these four numbers, and at inference it turns them back into a box. The class probabilities come from a Softmax over the class scores, and they are trained with Cross-Entropy Loss. A common training objective adds the two losses:
where is the true class, the true offsets, a weight between the two terms, and the indicator makes the box term apply only to candidates that match an object.
Step 4 in symbols: thresholding
Let be the best object score of candidate . Keep the candidates with , where is a threshold chosen by the user. In the walkthrough, .
Step 5 in symbols: suppression
Non-maximum suppression, for one class, works as follows. Sort the kept boxes by score. Repeatedly take the top box, add it to the output, and delete every remaining box whose IoU with it exceeds a threshold . In the walkthrough, .
Overlap is measured by IoU. For two boxes and , : the shared area over the total area they cover together. It is 1 for identical boxes and 0 for boxes that do not touch.
Worked examples
Box covers and box covers .
- Area of : . Area of : .
- Shared region: from 1 to 2 (width 1) and from 1 to 2 (height 1). Shared area .
- Union: .
- IoU: .
- Check: this is below a typical NMS threshold of 0.5, so suppression would keep both boxes.
Interpretation: two boxes that overlap by a corner are different objects as far as NMS is concerned.
Candidates after thresholding: A cat with score 0.90; B cat with score 0.75; C dog with score 0.60. Threshold for suppression . Suppression compares only boxes of the same class.
- Areas: each box is .
- IoU(A, B): shared region has area . Union . IoU .
- IoU(A, C) would be 0, because the boxes share no values. C is a dog, so it is not compared with the cat boxes at all.
- Sort by score: A (0.90), B (0.75), C (0.60).
- Keep A. Since IoU(A, B) , delete B.
- Next is C, the only dog. Nothing of its class has been kept, so C is kept.
- Output: A (cat) and C (dog).
Interpretation: the algorithm returns one box per object. The cat box B is removed because it duplicates A, and the dog box C survives at the same threshold because it is a different object.
A street camera sees two pedestrians side by side. The detector returns two boxes, P for the left person and Q for the right person , with scores 0.88 and 0.81. Their shared region is .
- Areas: and each have area .
- Shared area: .
- Union: .
- IoU: .
- With , the IoU is below the threshold, so both people survive. With , Q would be deleted, and one person would vanish from the output.
- Lesson: the suppression threshold is a tradeoff. A low value cleans up duplicates but merges neighbours in a crowd. A high value keeps every box and leaves duplicates. Crowded scenes need a higher threshold than sparse ones.
Try it: a third box R covers , and it sits between P and Q. With score 0.70, after P is kept, does R survive NMS at ? Answer: no, it is deleted. The shared region has area . The union has area . So IoU , and R is treated as a duplicate of the left person.
Common mistakes and tradeoffs
It is tempting to think that a detector is a classifier run on many crops. In fact the most successful detectors share one backbone across the whole image and predict all boxes in one pass. Running a classifier on each crop separately is far slower, and it is the approach that early systems used before shared features were common.
It is tempting to think that a higher score always means a better box. In fact the score measures confidence that an object is present and of the given class, not how well the box fits. A poorly placed box can score highly, which is why the regression offsets and the IoU threshold for "correct" matches are both needed.
It is tempting to think that non-maximum suppression is optional once the network is good. In fact most dense detectors produce several overlapping candidates per object, and without suppression the output contains duplicates. Some newer detectors avoid the step by learning to predict one box per object directly, but they add other training machinery.
When to use it: whenever you need the location and count of objects, not only their presence. Use a one-stage detector such as YOLO when speed matters; use a two-stage detector such as Faster R-CNN when accuracy matters more than latency.
When not to use it: if you only need one label per image, use classification; if you need the exact outline of each object, use Image Segmentation, which labels every pixel.
Alternatives: the sliding-window detectors of the early 2000s are simple but slow. Transformer-based detectors remove hand-designed anchors and suppression, at the cost of longer training.
History
Before deep learning, the standard approach was a sliding window: test a fixed-size classifier at every position and scale. Viola and Jones (2001) made this fast enough for face detection on ordinary hardware by using cascades of simple features. Dalal and Triggs (2005) brought histograms of gradients to pedestrian detection, and detectors built on these hand-made features were the state of the art for about a decade.
Girshick and colleagues (2014) introduced R-CNN, which ran a convolutional network on a few thousand region proposals per image. It was far more accurate than the older detectors, but slow. Faster R-CNN (Ren and colleagues, 2015) replaced the external proposal method with a learned region proposal network that shares features with the detector. YOLO (Redmon and colleagues, 2016) went the other way: it predicted boxes from the whole image in one network pass, trading some accuracy for speed. The COCO benchmark (Lin and colleagues, 2014), with 80 object categories, became the common test for comparing these systems.
Recap
- Detection outputs a set of labelled boxes, one for each object, from a single image.
- A convolutional backbone and a head predict, for each candidate, a class probability and a box correction.
- Candidates are thresholded by score, then non-maximum suppression removes duplicates based on IoU.
- The main tradeoff is between speed (one-stage) and accuracy (two-stage), and the suppression threshold trades duplicates against crowded scenes.
Read next: You Only Look Once: Unified, Real-Time Object Detection for the one-stage design and Faster R-CNN: Towards Real-Time Object Detection with Region Proposal Networks for the two-stage design, and Image Segmentation for the per-pixel version of the same question.
Check yourself
QuestionRecall: what is the difference between image classification and object detection in what the output contains?Show answer
Classification returns one label for the whole image. Detection returns a list of entries, each with a class label and a bounding box, and an image can have any number of them.
QuestionCompute: two boxes and . What is their IoU?Show answer
The shared region is with area . Each box has area 16, so the union is . IoU .
QuestionWhy does a detector need non-maximum suppression even when its classifier is accurate?Show answer
The detector scores many overlapping candidate boxes for the same object, because nearby grid cells and anchors all see part of it. Each of them can pass the score threshold. Suppression keeps the best box per object and removes the rest, so the output has one box per object.
QuestionApplied: a parking-lot camera returns many boxes for densely parked cars, and suppression deletes some real cars. What should you change, and what is the risk?Show answer
Raise the suppression threshold so that boxes with partial overlap survive. The risk is that duplicates of the same car then also survive. Tune the threshold on labelled images from the same camera, using a metric that counts both missed and duplicated cars.