Worked examples — Object detection (R-CNN, YOLO, SSD)
This page is a problem clinic for Object detection (R-CNN, YOLO, SSD). The parent note gave you the formulas. Here we use them on every kind of input they can face — including the weird ones (zero-size boxes, boxes that don't touch, perfect overlaps, tiny-vs-huge objects). Nothing new is assumed: every symbol is rebuilt from a picture the moment it appears.
Before we compute anything, we fix the one picture the whole page rests on.
The figure below shows both namings on the same rectangle — the coral dots are the two corners of corner form, the mint square is the center of center form, and are its width and height. Every later example silently uses one of these two conventions, so pin this picture down first.
The scenario matrix
Every worked example below is tagged with the cell of this matrix it covers. Together they touch every cell.
| # | Case class | What makes it tricky | Example |
|---|---|---|---|
| C1 | Boxes partially overlap (generic) | The everyday IoU computation | Ex 1 |
| C2 | Boxes do not touch (degenerate, ) | Must clamp negative overlap to zero | Ex 2 |
| C3 | Boxes identical / one inside other (limiting IoU=1 and nested) | Sanity checks at the extremes | Ex 3 |
| C4 | NMS with several boxes, some suppressed | Ordering by score, threshold logic | Ex 4 |
| C5 | Box regression targets — normal + degenerate log | The and transforms; what if ? | Ex 5 |
| C6 | YOLO cell decoding — center in a cell, all four quadrant offsets | Relative-to-cell vs relative-to-image | Ex 6 |
| C7 | YOLO confidence with and with object present | The multiplicative confidence, zero case | Ex 7 |
| C8 | Scale mismatch — tiny box vs huge box, why | Word-problem: small-object penalty | Ex 8 |
| C9 | Exam twist — count SSD/anchor outputs; grid arithmetic | Shapes and tensor sizes | Ex 9 |
Ex 1 — Generic partial overlap (C1)
- Areas. . . Why this step? IoU's denominator needs each box's area; area width height in corner form.
- Intersection corners. Overlap left edge , right edge , top edge , bottom edge . Why this step? The overlap is itself a rectangle: its left is the rightmost of the two lefts, its right is the leftmost of the two rights. encode that.
- Intersection area. width , height , so . Why this step? Once we have the overlap rectangle's four edges (step 2), its area is just (its width)(its height), exactly like any rectangle.
- Union. . Why this step? Adding double-counts the overlap once, so we subtract back out.
- IoU. . Why this step? This is the definition itself — overlap area over jointly-covered area — turning the two shapes into a single "how aligned are they?" number.
Verify: ✓, and ✓. The overlap patch () is clearly small compared to the combined footprint, matching the ~0.17 answer.
Ex 2 — Boxes that never touch (C2, degenerate )
- Naive intersection edges. left , right , top , bottom . Why this step? We apply the same overlap-edge rule as Ex 1 ( of lefts, of rights) blindly, to see what the formula does when there is no real overlap.
- Width/height come out negative. width , height . Why this step? A negative width is nature's way of shouting "these don't overlap." A rectangle can't have negative size.
- Clamp to zero. . Why this step? This clamp is the single most-forgotten line in IoU code. Without it you'd multiply two negatives and get a fake positive area — a classic bug.
- IoU . Why this step? With zero overlap the numerator is , so the fraction is regardless of the boxes' sizes — which is exactly the "no alignment at all" answer we want.
Verify: No overlap ⇒ IoU by definition ✓. If you had skipped the clamp you'd get — a negative IoU, which is impossible and flags the missing clamp.
Ex 3 — Identical and nested boxes (C3, limiting cases)
- (a) Identical. , . IoU . Why this step? When the boxes coincide, intersection union, so the fraction is exactly — the ceiling of IoU.
- (b) Nested. , . Overlap edges: left , right , top , bottom , so . Why this step? Running the same overlap-edge rule shows the intersection rectangle is — i.e. all of . When one rectangle sits entirely inside the other, their intersection is the smaller rectangle.
- (b) IoU . Why this step? Because , the union , so IoU collapses to the pure area ratio — a clean sanity formula for nested boxes.
Verify: IoU is the maximum possible ✓. For (b), IoU whenever — a nested box's IoU is just the area ratio, and is tiny because the small box barely fills the big one ✓.
Ex 4 — Non-Maximum Suppression, several boxes (C4)
- Sort by score. Order: . Why this step? NMS keeps the most confident box in each cluster and deletes weaker near-duplicates, so we process highest-first.
- Keep . Compare it to the rest. : overlap ; areas and ; IoU . Why this step? , so is a duplicate of → remove .
- Check vs . overlap ; area ; IoU → remove .
- Check vs . is far away (corners –) → IoU → keep for now.
- Move to next surviving box . Nothing left below it to compare → keep .
Verify: Survivors — the two genuinely different objects, exactly as the picture (one cluster + one lone box) demands ✓. Every removed box had IoU with a higher-scoring keeper ✓.
Ex 5 — Box-regression targets, normal and degenerate (C5)
We use the target formulas restated at the top of this page — in words: are how far to shift the center, measured in box-widths/heights; are how much to stretch, measured in log-scale.
- Center shifts. . . Why this step? Dividing by makes the target scale-free: shifting a big box 10px is "small," shifting a tiny box 10px is "big," and the fraction captures that.
- Width scale. . Why this step? This is the degenerate log case. When target width equals proposal width the ratio is and — the network should predict "no width change." is chosen precisely so "no change" maps to the neutral value , and positive/negative stretch sit symmetrically around it.
- Height scale. . Why this step? The box must double in height; in log-space doubling is a fixed step () regardless of the starting size, which keeps learning linear.
Verify: Invert the transforms (the formulas at the top), treating the predicted transform as exactly on target, . ✓. ✓. And ⇒ width unchanged ✓.
Ex 6 — YOLO cell decoding, all offset directions (C6)
- Cell origin. Column 3 starts at px; row 4 starts at px. Why this step? are given relative to the cell, so we need the cell's top-left in pixels first. With zero-based indexing, column begins at .
- Center in pixels. . . Why this step? Because means " across this cell," we add . Since , the center sits in the right half — matching the forecast.
- Box size in pixels. are relative to the whole image: , . Why this step? YOLO deliberately measures center relative to the cell (small range, easy to learn) but size relative to the image (an object can be bigger than one cell).
- Corner form. , ; , . Why this step? To draw or evaluate the box against IoU we convert center form to corners: corner center half-width (or half-height), because the center sits exactly midway between the two edges.
Verify: Center must lie inside cell , i.e. : ✓; : ✓. Corners stay inside the image ✓.
Ex 7 — Confidence with and without an object (C7)
Recall from the parent note: box confidence , and the final class-specific score is .
- Case (a) box confidence. . Why this step? The confidence is a product, and one factor is . This is exactly why we want a multiplicative form: "no object" mathematically forces the score to vanish, no matter how the box is placed.
- Case (b) box confidence. . Why this step? With an object present, , so confidence collapses to how well the box fits — its IoU.
- Case (b) final class score. . Why this step? This chains "is something here?" () with "if so, is it a dog?" () into one number the detector can threshold.
Verify: All results in ✓. Case (a) ✓. Case (b) final — multiplying two probabilities can only shrink the score, and it does ✓.
Ex 8 — Why : tiny box vs huge box (C8, word problem)
Recall the two size-loss terms from the parent's YOLO loss: plain MSE would use , but YOLO actually uses .
- Raw MSE on width. Sign: predicted , error . Truck: predicted , error . Why this step? Plain squared error treats both 8px mistakes identically — it can't tell the sign disaster from the trivial truck slip.
- Square-root MSE (YOLO). Sign: . Truck: . Why this step? Taking first compresses large values: the derivative of shrinks as grows, so the same absolute error produces a much smaller gap between the square roots of two large numbers than between two small ones. That turns the sign's error () into something far bigger than the truck's ().
- Ratio of penalties. Raw scheme ratio . Root scheme ratio . Why this step? The makes YOLO care roughly 15× more about the small-object mistake — exactly the priority we wanted.
Verify: square ✓. square ✓. Root penalty for the small box that for the large box ✓, matching the intuition.
Ex 9 — Exam twist: counting anchor / grid outputs (C9)
- (a) Per-cell vector length. numbers (5 = ). Why this step? Each box needs 4 coordinates + 1 confidence; class probs are shared per cell, so we add once.
- (a) Total. numbers. Why this step? The output is a tensor; its element count is the product of its three dimensions.
- (b) Feature-map size. stride ⇒ one position per input pixels, so width , height . Why this step? The backbone downsamples the image by the stride, so dividing image size by the stride gives how many feature-map positions (and thus anchor centers) exist along each axis.
- (b) Total anchors. proposals. Why this step? Every one of the positions plants anchors of assorted scale/ratio, so we multiply positions by anchors-per-position.
Verify: (a) ✓. (b) , ✓ — matching the parent note's worked figure. The anchor grid yields far more raw candidates than YOLO's -cell grid, which is why R-CNN needs a heavier NMS prune (down to 300) ✓.
Wrapping up
We have now walked every cell of the scenario matrix: generic overlap (C1), the degenerate no-touch case with its mandatory clamp (C2), the limiting identical/nested boxes (C3), a full Non-Maximum Suppression pass (C4), the regression targets including the degenerate (C5), YOLO grid-to-pixel decoding (C6), the zero-object confidence (C7), the small-vs-large scale word problem (C8), and the exam-style tensor arithmetic (C9). Nothing in the parent's formula box is left untested by a picture and a number.
Recall Quick self-test
IoU of two disjoint boxes ::: (after clamping the negative overlap to zero). IoU of two identical boxes ::: (intersection equals union). If ground-truth width equals proposal width, the target is ::: (predict no change). In NMS, a box is removed when ::: its IoU with a higher-scoring kept box exceeds the threshold . YOLO uses in the loss because ::: it makes an equal pixel error penalise small boxes more than large ones. YOLO confidence when no object is present ::: , because confidence and .
Related: Anchor Boxes · Non-Maximum Suppression · Intersection over Union (IoU) · Image Classification · Semantic Segmentation · Instance Segmentation · Transfer Learning · Data Augmentation · Batch Normalization.