Object Detection & Image Segmentation Basics
Understand boxes vs masks and the high-level families of detection/segmentation models.
In plain words
Detection draws a box around each thing in a picture and says what it is. Segmentation goes further and colours in exactly which dots of the picture belong to each thing.
Think of it like…
Marking up a photograph. Classification is writing a caption, detection is drawing rectangles around each item and naming it, and segmentation is tracing each item's exact outline with a pen.
Where you’ve seen it
A retailer wants to count products on shelves. A box overlap example shows IoU: a true box (0, 0, 10, 10) and predictions shifted by 1, 2 and 5 pixels have intersections of 9×9 = 81, 8×8 = 64 and 5×5 = 25 and unions of 119, 136 and 175, so IoU = 0.681, 0.471 and 0.143; at the usual 0.5 threshold only the first counts as a correct detection. A single-stage detector at 640×640 produces thousands of candidate boxes (for example 8,400 across three scales: 80×80 + 40×40 + 20×20), many on the same product, so non-maximum suppression (NMS) keeps the highest-scoring box (0.92) and removes overlapping boxes with IoU above 0.5 (a 0.85 box with IoU 0.68 is suppressed) while keeping a distant 0.60 box that overlaps by only 0.05. To score a class, detections are ranked by confidence: with 4 true objects and ranked outcomes TP, TP, FP, TP, FP the precision at each hit is 1.0, 1.0 and 0.75, recall steps by 0.25 each time and the average precision (area under the interpolated precision–recall curve) is 0.25 × 1.0 + 0.25 × 1.0 + 0.25 × 0.75 + 0 = 0.6875 because the last object was never found; mAP averages this over classes. For shelf-gap area they add a segmentation head: two masks of 100 pixels overlapping on 70 pixels have Dice = 2 × 70 / 200 = 0.70 and IoU = 70 / 130 = 0.54. Labelling boxes was fast enough to annotate 5,000 images, whereas masks were reserved for the 500 images where area mattered.
Words you’ll hear
- IoU
- Intersection area divided by union area of two regions.
- NMS
- Non-maximum suppression: removes overlapping duplicate boxes, keeping the highest score.
- AP / mAP
- Area under the precision–recall curve per class, and its mean over classes.
- Semantic vs instance segmentation
- Per-pixel class labels versus separate masks for each object.
Visual diagram
Theory & deep dive
Boxes, IoU, NMS and AP
IoU = area of overlap ÷ area of union. For boxes A = (0, 0, 10, 10) and B = (2, 2, 12, 12) the intersection is 8 × 8 = 64 and the union is 100 + 100 − 64 = 136, so IoU = 0.471, which fails a 0.5 threshold; shifting B to (1, 1, 11, 11) gives 81 / 119 = 0.681, a match. A prediction counts as a true positive when its class is right and IoU with an unmatched ground-truth box exceeds the threshold (0.5 for PASCAL VOC-style AP50; COCO averages thresholds from 0.5 to 0.95), otherwise it is a false positive, and unmatched ground-truth boxes are false negatives.
Detectors emit many overlapping candidates. NMS sorts by score, keeps the best box, discards boxes whose IoU with it exceeds a threshold (commonly 0.5), and repeats. Too low a threshold deletes true neighbours in crowded scenes; too high leaves duplicates. Average precision ranks detections by confidence and integrates precision over recall: with 4 true objects and the ranked outcomes TP, TP, FP, TP, FP, precision is 1/1, 2/2, 2/3, 3/4, 3/5 and recall is 0.25, 0.5, 0.5, 0.75, 0.75. Using the interpolated precision envelope (1.0, 1.0, 0.75 at the three recall levels reached, then 0 because recall 1.0 is never reached), AP = 0.25 + 0.25 + 0.1875 = 0.6875. mAP is the mean AP across classes.
A = (0, 0, 10, 10), B = (5, 5, 15, 15). Intersection: 5 × 5 = 25. Union: 100 + 100 − 25 = 175. IoU = 25 / 175 = 0.143, a miss. Move B to (1, 1, 11, 11): 81 / 119 = 0.681, a hit.
Model families, segmentation and practical choices
Two-stage detectors (Faster R-CNN) first propose regions and then classify and refine them; they are accurate and the basis of Mask R-CNN for instance segmentation, but slower. Single-stage detectors (YOLO, SSD) predict boxes and classes directly across a grid and are the usual choice for real-time use; transformer detectors (DETR and successors) predict a fixed set of objects with bipartite matching and avoid NMS. For semantic segmentation, encoder–decoder networks such as U-Net upsample features back to full resolution with skip connections; for instance segmentation, add a mask head per detected box; panoptic merges both. Segmentation is scored with mask IoU or the Dice coefficient (2|A∩B| / (|A| + |B|): masks of 100 pixels overlapping on 70 pixels give 0.70 Dice and 0.54 IoU).
As a data scientist you will usually fine-tune a pretrained model rather than design one. Decisions that matter: annotation cost (boxes are much quicker to draw than polygon masks), image resolution versus small-object recall, class imbalance (many background regions; detectors use focal loss or hard-negative mining), augmentation that moves boxes consistently (flips, crops, mosaic), confidence thresholds tuned for the cost of misses versus false alarms, and evaluation by slice (small objects, occlusion, lighting). Always inspect predictions visually; metric gains can hide systematic failures such as double counts or missed small items.
Two masks of 100 pixels each overlap on 70 pixels. Union = 130. IoU = 70 / 130 = 0.538. Dice = 2 × 70 / (100 + 100) = 0.700. Dice is always at least as large as IoU for the same pair.
How it works, step by step
Step 1 of 41. Define the output you need
Decide between image labels, boxes, instance masks or semantic masks, based on the business question.
- Cost
- boxes ≪ masks
When to use it & trade-offs
What it is
Object detection answers "what is in the image and where?" by predicting a set of bounding boxes (x1, y1, x2, y2), each with a class label and a confidence score. Segmentation answers "which pixels belong to what?": semantic segmentation labels every pixel with a class, instance segmentation separates individual objects of the same class and panoptic segmentation combines both. Detection quality is judged with Intersection over Union (IoU), the overlap between predicted and true regions divided by their union, and summarised with average precision (AP) and mean AP (mAP). Detectors fall into two-stage families (propose regions, then classify, as in Faster R-CNN and Mask R-CNN), single-stage families (predict boxes directly on a grid, as in YOLO and SSD) and transformer-based detectors such as DETR; U-Net style encoder–decoders dominate semantic segmentation.
Why it matters: Classification says only that an image contains a defect; detection says where, how many and how big, which is what counting, tracking, inspection and autonomous systems need. Segmentation gives pixel-accurate shapes for medical imaging, area measurement and cutting masks. The practical decisions are about labels (boxes are far cheaper than masks), metrics (IoU thresholds, mAP) and post-processing such as non-maximum suppression. Using a pretrained detector and fine-tuning it is almost always better than building one from scratch.
When to reach for it
- Product counting and shelf auditing with detection
- Defect area measurement with segmentation
- Medical and satellite imagery with U-Net style models
When to avoid or be careful
- Pixel masks when boxes answer the question
- Reporting AP at one IoU threshold without saying which
- Judging the model from the headline mAP without viewing errors
Vision task families
Strengths: Cheapest labels and models.
Watch out: No location or counts.
Strengths: Good accuracy for moderate labelling cost.
Watch out: Boxes are coarse for irregular shapes.
Strengths: Precise outlines.
Watch out: Expensive labels and compute.
Interview signals & numbers to know
When this topic comes up
- “How do you compute IoU?”
- What non-maximum suppression does
- mAP and the precision–recall curve
- Semantic versus instance segmentation
- Two-stage versus single-stage detectors
Benchmarks & rules of thumb
- 25 / 175 = 0.143
- IoU of two 10×10 boxes offset by 5 pixels
- 0.5
- usual IoU threshold for a correct detection and for NMS
- 0.6875
- AP of the example ranked detections
Common mistakes & follow-ups
Common interview mistakes
- Setting the NMS threshold too low in crowded scenes and deleting true objects.
- Comparing mAP numbers computed with different IoU thresholds.
- Augmenting images without transforming the boxes consistently.
Likely interviewer follow-ups
- How would you evaluate a detector for small objects?
- What are the trade-offs of Faster R-CNN versus YOLO?
- When is Dice preferred over IoU?
Knowledge check
Put it into practice
Practice related interview Q&A
Apply Object Detection & Image Segmentation Basics with data science interview Q&A and hands-on practice.