Exercise: matching objects
Before this unit: Day 12: Thresholding; Day 33: Random forests; Day 36: Neural networks.
See also: Day 14: Labelling; Day 15: Watershed.
Goal
By the end of this exercise you should be able to:
- compute the IoU of two objects and say why small objects lose IoU fast,
- match predicted to true objects from an IoU table at a threshold \tau, and count TP, FP and FN objects,
- compute object precision, recall and F1, and sketch F1 against \tau,
- recognise splits and merges in an IoU table,
- compute average precision from scored predictions, and tell it apart from the matching accuracy TP/(TP+FP+FN).
Do the pen-and-paper part first, then the demo. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Word | Meaning |
|---|---|
| IoU | |G \cap P| / |G \cup P| for a true object G and a predicted object P |
| Matching at \tau | one to one; a pair may match only if its IoU \ge \tau |
| TP, FP, FN | matched pairs; unmatched predictions; unmatched true objects (no TN) |
| Precision, recall | TP/(TP+FP), TP/(TP+FN) |
| F1 | 2\,TP/(2\,TP + FP + FN) |
| Matching accuracy | TP/(TP + FP + FN) (some papers call it “AP at \tau”) |
| AP | area under the precision–recall curve of predictions sorted by score, with precision made non-increasing from the right |
By hand
B1. On a pixel grid: (a) Two 4 \times 4 squares, one shifted by one pixel to the right. Compute the IoU. (b) Two 2 \times 2 squares, shifted the same way. Compute the IoU. (c) Which loses more for the same one-pixel error? What does that mean when you evaluate small objects (spots, small nuclei) at \tau = 0.5?
B2. An IoU table with 3 true objects and 4 predictions:
| P1 | P2 | P3 | P4 | |
|---|---|---|---|---|
| G1 | 0.82 | 0.05 | 0 | 0 |
| G2 | 0.10 | 0.45 | 0.38 | 0 |
| G3 | 0 | 0 | 0 | 0.62 |
- Match at \tau = 0.5. Give TP, FP, FN, precision, recall and F1.
- The same at \tau = 0.3 and at \tau = 0.7.
- What kind of error is G2? Sketch shapes that could give its row.
- Sketch F1 as a function of \tau from 0 to 1. Where are the steps?
B3. Greedy and optimal matching at \tau = 0.3:
| P1 | P2 | |
|---|---|---|
| G1 | 0.60 | 0.40 |
| G2 | 0.35 | 0 |
- Match greedily: take the largest IoU first, then the largest among the objects still free. How many matches?
- Find the matching with the most matches. What is its total IoU, compared with the greedy one?
- Draw two true objects and two predictions that could give this table.
- Could this happen at \tau = 0.55? Why not?
B4. Four true objects. A method predicts five objects; sorted by score they are TP, TP, FP, TP, FP (at \tau = 0.5). (a) Write down recall and precision after each prediction, and draw the precision–recall curve. (b) Make the precision non-increasing from the right and compute the area under it: the average precision. (c) Compute the matching accuracy TP/(TP+FP+FN) and the F1 for all five predictions. A paper reports “AP 0.5” for this method. Is it wrong? (d) A sixth prediction with the lowest score is added; it is an FP. What happens to AP, to precision and to F1?
B5. Predict, then explain. (a) A method finds every nucleus but every predicted outline is one pixel too large. Sketch its F1-vs-\tau curve. What happens to the pixel Dice? (b) A perfect method, except that one pair of touching nuclei is merged into one object. Sketch its F1-vs-\tau curve. Which is worse for counting cells, (a) or (b)? (c) Why can a method with pixel Dice 0.97 have a lower object F1 than a method with Dice 0.86?
B1. (a) Overlap 3 \times 4 = 12 pixels, union 16 + 16 - 12 = 20: IoU = 0.6. (b) Overlap 1 \times 2 = 2, union 4 + 4 - 2 = 6: IoU = 1/3. (c) The small square loses far more: a one-pixel error is a large fraction of a small object. At \tau = 0.5 the 2 \times 2 prediction is already an FP plus an FN. For small objects either use a lower \tau (or a distance between centres instead of IoU), or report the F1-vs-\tau curve so the reader sees it.
B2. (a) Pairs with IoU \ge 0.5: G1–P1 (0.82) and G3–P4 (0.62). TP 2, FP 2 (P2, P3), FN 1 (G2). Precision 2/4 = 0.5, recall 2/3 = 0.67, F1 = 4/(4 + 2 + 1) = 4/7 = 0.57. (b) \tau = 0.3: G2–P2 (0.45) also matches (P3 cannot, G2 is taken). TP 3, FP 1, FN 0: precision 0.75, recall 1, F1 = 6/7 = 0.86. \tau = 0.7: only G1–P1. TP 1, FP 3, FN 2: precision 0.25, recall 0.33, F1 = 2/7 = 0.29. (c) G2 is split into P2 and P3: two predictions that each cover part of it (IoU 0.45 and 0.38). For example a nucleus cut in two by a wrong watershed line, slightly unequal halves. (d) F1 = 6/7 for \tau \le 0.45, 4/7 for 0.45 < \tau \le 0.62, 2/7 for 0.62 < \tau \le 0.82, and 0 above 0.82. A staircase, stepping down at the IoU values of the matched pairs.
B3. (a) Greedy takes G1–P1 (0.60). G2’s only candidate P1 is taken, and P2’s only candidate G1 is taken: 1 match, total IoU 0.60. (b) G1–P2 and G2–P1: 2 matches, total IoU 0.40 + 0.35 = 0.75, more matches and also more IoU. (c) Two touching nuclei G1 (left) and G2 (right); P1 is a large object between them, covering most of G1 and part of G2; P2 is a smaller object inside the left part of G1. (The lecture’s figure has this geometry, with IoUs 0.44, 0.39, 0.31.) (d) No: above 0.5 every object has at most one partner (the lecture’s math slide), so each row and column has at most one allowed pair and there is no choice to get wrong. Here only G1–P1 would be allowed at 0.55.
B4. (a) Recall: 0.25, 0.5, 0.5, 0.75, 0.75; precision: 1, 1, 0.67, 0.75, 0.6. (b) Interpolated precision: 1, 1, 0.75, 0.75, 0.6. Recall increases by 0.25 at predictions 1, 2 and 4: AP = 0.25 \cdot 1 + 0.25 \cdot 1 + 0.25 \cdot 0.75 = 0.6875. (c) TP 3, FP 2, FN 1: matching accuracy 3/6 = 0.5; F1 = 6/9 = 0.67. The paper is not wrong if it means the matching accuracy, but its “AP” is a different number from the area under the PR curve (0.69). Compare only numbers with the same definition. (d) AP does not change: the new FP does not raise recall, so it adds an area of width 0. Precision drops to 3/6 = 0.5, F1 to 6/10 = 0.6.
B5. (a) A one-pixel dilation lowers the IoU of every object a little (more for small ones), but all IoUs stay well above 0.5: F1 = 1 up to a fairly high \tau (around 0.8 for nuclei of radius ~10 px), then it drops to 0 within a short range of \tau, because all objects have about the same IoU. The pixel Dice drops somewhat (every boundary pixel ring is an FP). (b) F1 is a little below 1 at every \tau: the merged object can match at most one of the two nuclei. If it matches one (IoU \ge \tau): 1 FN; otherwise 2 FN + 1 FP. The curve is a lower, flat plateau. For counting, (b) is worse: the count is wrong, while (a) counts correctly. (c) Pixel Dice weighs big objects and boundary pixels; it barely notices a merge (almost every pixel is right). Object F1 counts objects: every merge costs a TP. Many merges with tight outlines: high Dice, low F1. Sloppy outlines without merges: lower Dice, higher F1 at \tau = 0.5.
Math questions
B6. (math) Prove that IoU(A, B_1) > \frac12 and IoU(A, B_2) > \frac12 is impossible for disjoint B_1, B_2. Is it still impossible with IoU exactly \frac12? Give an example with IoU \ge \frac13 for both.
B7. (math) Write the matching of B3 as an assignment problem with unknowns x_{ij} \in \{0, 1\}. List all feasible x (row and column sums \le 1, only pairs with IoU \ge 0.3) and find the optimum (a) of the total IoU, (b) of “number of matches first, then IoU”. Do they agree here?
B8. (math) (a) Show that with every object matched or unmatched, F1 = 2\,TP/(n_G + n_P), where n_G, n_P are the numbers of true and predicted objects. (b) Show that F1(\tau) is non-increasing in \tau for the forced matching above 0.5. (c) Show that greedy matching has the same property at any \tau. (d) Show that the matching accuracy equals F1/(2 - \text{F1}).
B6. IoU(A, B) > \frac12 gives |A \cap B| > \frac12|A \cup B| \ge \frac12|A|. If both held, |A \cap B_1| + |A \cap B_2| > |A|; but A \cap B_1 and A \cap B_2 are disjoint subsets of A, so their sizes add to at most |A|. Contradiction. At exactly \frac12 it is possible: cut A into two equal halves B_1, B_2; then |A \cap B_i| = \frac12|A| and |A \cup B_i| = |A|, IoU = \frac12 for both. The same example has IoU \ge \frac13 for both; so does A cut into halves with each B_i a little larger than its half.
B7. Allowed pairs: G1–P1 (0.60), G1–P2 (0.40), G2–P1 (0.35). Feasible matchings: none (0); \{G1–P1\} (0.60); \{G1–P2\} (0.40); \{G2–P1\} (0.35); \{G1–P2, G2–P1\} (0.75). (\{G1–P1, G2–P1\} uses P1 twice, \{G1–P1, G1–P2\} uses G1 twice.) (a) Maximum total IoU: \{G1–P2, G2–P1\} with 0.75. (b) Most matches: the same, 2 matches. They agree here; they would differ if, say, G1–P1 were 0.80: total IoU would then prefer the single pair (0.80 > 0.75), “matches first” still the two pairs.
B8. (a) Every predicted object is TP or FP: n_P = TP + FP; every true object TP or FN: n_G = TP + FN. So 2\,TP + FP + FN = n_G + n_P. (b) For \tau > \frac12 the matched pairs are exactly the pairs with IoU \ge \tau (forced). Raising \tau can only remove pairs, so TP(\tau) is non-increasing, and with the fixed denominator n_G + n_P so is F1. (c) Greedy sorts all pairs by IoU and walks down the list. At a higher \tau it walks the same list but stops earlier; every decision it makes before stopping is the same, so its matches are those of the lower \tau with IoU \ge \tau: a subset. (d) acc = TP/(TP + FP + FN) = TP/(n_G + n_P - TP). With f = 2\,TP/(n_G + n_P): f/(2 - f) = 2\,TP/(2(n_G + n_P) - 2\,TP) = TP/(n_G + n_P - TP).
Explore
The image is a synthetic fluorescence image of 16 nuclei (6 touching pairs, 3 dim nuclei). True outlines are white. Predicted objects are filled green (TP) or red (FP) at the current \tau; true objects without a match get a dashed orange outline (FN). The error generator turns the truth into predictions: boundary jitter, an outline bias (too large or too small), merging of touching pairs, splitting, missing dim nuclei, and specks; every prediction gets a score (low for specks and dim nuclei). Edit by hand freezes the predictions as ellipses you can move, resize, rotate, add, delete, split and merge. The IoU table shows the objects near the yellow marker (click the image to move it): matched cells boxed, cells below \tau grey. The right panel shows F1 against \tau (with the pixel Dice as a dashed line) or the precision–recall curve at \tau.
E1. Preset a typical result (\tau = 0.5). Read TP, FP, FN, precision, recall, F1 and the pixel Dice. Which kinds of error do you see in the image, and how many of each?
E2. Preset Dice high, count wrong. Compare the pixel Dice with F1. How many merges are there, and how many nuclei does the method count? Explain why Dice hardly notices.
E3. Preset sloppy outlines. Read F1 at \tau = 0.5 and at \tau = 0.8 (move the slider, or read the curve). Compare with E2 at the same two thresholds. Which method is better, and for what purpose?
E4. Preset one split. Find the split nucleus (label “split”) and put the marker on it. Read its row in the IoU table. How does one split change TP, FP and FN?
E5. Preset greedy loses a match (\tau = 0.3). Read the IoU table at the marker. Compare TP with greedy and optimal matching. Then move \tau to 0.35 and to 0.4: what happens to the difference, and why?
E6. Preset specks. Read precision, recall and F1, then open the PR curve tab. What are AP at \tau and AP over 0.5–0.95? Set specks to 0: which numbers change and which do not? Why?
E7. Preset vs a second annotator. The grey curve is the F1 of annotator B against annotator A (the truth); the band above it is out of reach of any honest claim. Where does the prediction’s curve lie? At which \tau do the two annotators start to disagree strongly?
E8. In a typical result, choose edit by hand, click a correctly found nucleus and press split. Predict the change of TP, FP and FN at \tau = 0.5 before you look. Then try merge with nearest on another one, and move an ellipse until its IoU drops just below \tau.
E1. 16 true, 14 predicted objects: TP 12, FP 2, FN 4; precision 0.857, recall 0.750, F1 0.800, matching accuracy 0.667; pixel Dice 0.864. Two merges: each merged object matches the larger of its two nuclei (IoU 0.57) and leaves the other one unmatched (2 FN). Two dim nuclei are missed (2 FN), and the two specks are the 2 FP. The count is 14 instead of 16, and two of the 14 are not nuclei.
E2. Pixel Dice 0.971, but F1 0.692 at \tau = 0.5 (TP 9, FP 1, FN 7). Every touching pair is merged: 5 merged objects (one of them a chain of three nuclei), so the method counts 10 objects instead of 16. Almost every pixel is labelled correctly (only the thin gap between the nuclei of a pair is wrong), so Dice is high; but each merge loses one or two true objects.
E3. Sloppy outlines: F1 0.875 at 0.5, 0.250 at 0.8 (Dice 0.857). Merges (E2): 0.692 at 0.5, 0.385 at 0.8. For counting the sloppy method is better (it finds the objects); for measuring shapes and areas neither is good, and at strict \tau the merging method with tight outlines does better. One number at one \tau hides this.
E4. One nucleus is cut into two halves with IoU 0.41 and 0.40 with it (below 0.5). Its row has two non-zero cells, neither boxed. TP 15, FP 2, FN 1 (F1 0.909): one split costs one TP and adds two FP and one FN. The pixel Dice is still 0.978.
E5. At the marker: G1–P1 0.376 (largest), G1–P2 0.335, G2–P1 0.339. Greedy takes G1–P1 and leaves G2 and P2 unmatched: TP 15 (F1 0.938). Optimal pairs G1–P2 and G2–P1: TP 16 (F1 1.000). At \tau = 0.35 the pairs G1–P2 and G2–P1 are no longer allowed (both below 0.35), so both methods give TP 15; at 0.4 also G1–P1 drops out and both give TP 14. Greedy can only lose when there is a choice, which needs several allowed pairs in a row or column.
E6. Precision 0.571, recall 1.000, F1 0.727 (16 TP, 12 FP). AP at \tau = 0.5 is 1.000 and AP over 0.5–0.95 is 0.821. With specks set to 0: precision and F1 become 1.000, AP stays 1.000 and AP over 0.5–0.95 stays 0.821. The specks have the lowest scores, so they come last in the list, after all true objects are found; they do not raise recall, so they add no area. AP measures the ranking; F1 at a fixed cut-off (all predictions) does not use the scores.
E7. Up to \tau = 0.8 the prediction’s curve (F1 1.000) lies above annotator B’s curve (0.968), i.e. in the band; at 0.85 it drops to 0.875, below B (still 0.968), and at 0.9 both fall (0.625 and 0.710). Annotator B left out one disputed dim nucleus and draws slightly different outlines; above \tau = 0.85 the two annotators disagree strongly. The prediction was generated from annotator A, so “better than a human” here means “closer to annotator A”. On real data a score above the inter-annotator agreement says “as good as a human”, not “better”.
E8. A correctly found nucleus split into halves with a 2-pixel gap: each half has IoU just under 0.5 with it, so TP −1, FP +2 (net predicted objects +1), FN +1. Merging two correctly found separate nuclei: the merged object matches at most one of them; usually it is too large for either (IoU < 0.5): TP −2, FP +1, FN +2. Moving an ellipse: when its IoU crosses \tau its pair stops being a TP and becomes one FP plus one FN.
Code
Write the algorithms of this unit yourself, in Rust. Setup, and how to run and check your code: coding exercises: getting started. Your code goes in evaluation/src/object_eval/exercise.rs.
C1. iou_table(truth, pred): the IoU table of two label images (0 = background). One pass over the pixels counts every object’s area and every pair’s overlap:
for every pixel:
g = true label, p = predicted label
if g > 0: area_true[g] += 1
if p > 0: area_pred[p] += 1
if g > 0 and p > 0: overlap[g][p] += 1
IoU[g][p] = overlap[g][p] / (area_true[g] + area_pred[p] − overlap[g][p])
The checks are B1 (two squares shifted by one pixel) and a merge.
C2. match_greedy(iou, tau): of all pairs with IoU \ge \tau, take the largest IoU first, then the largest among the objects still free:
pairs = all (IoU, g, p) with IoU ≥ tau, sorted from the largest IoU down
for every pair (IoU, g, p) in that order:
if g and p are both still free: match them
The checks are B2(a, b) and B3(a), where greedy finds only one match.
C3. scores(n_true, n_pred, tp): FP, FN, precision, recall, F1 and the matching accuracy TP/(TP + FP + FN). The checks are B2 and B4(c), and that F1 never rises with \tau (B2(d)).
C4. average_precision(is_tp, n_true): the predictions sorted by score, each marked TP or FP. Recall and precision after every prediction; precision made non-increasing from the right; then the area under the curve, \sum_k (R_k - R_{k-1})\,P_k. The checks are B4(b) and B4(d).
Then cargo run -p evaluation -- object-eval and open the pictures in out/object-eval/. The program scores two segmentations of a synthetic image of 30 nuclei: a global threshold followed by connected-component labelling (threshold.png), and the true nuclei drawn one pixel too large. It prints TP, FP, FN, precision, recall and F1 for several \tau, and the pixel Dice. threshold-errors.png shows the threshold result at \tau = 0.5: matched objects green, unmatched predictions red, missed nuclei orange.
C5. Compare the pixel Dice of the threshold result with its F1 at \tau = 0.5. How many objects does it find, and what has happened to the others? Look at threshold-errors.png.
C6. Compare the F1-vs-\tau columns of the two segmentations with your sketches of B5(a) and B5(b). Which one would you use to count nuclei, and which to measure their areas?
C7. The program also sorts the threshold result’s objects by their mean brightness (as a score) and computes the average precision at \tau = 0.5. Why can it not be larger than the final recall?
The model solution is evaluation/src/object_eval/solution.rs in the download.
C5. Pixel Dice 0.988, but F1 only 0.489 at \tau = 0.5 (TP 11, FP 4, FN 19). The threshold finds 15 objects for 30 nuclei: almost every pixel is labelled correctly, but touching nuclei come out as one object. A merged object matches at most one of its nuclei; a large cluster matches none (IoU below 0.5 with each) and is an FP, its nuclei all FN. In the picture the red objects are such clusters of touching nuclei.
C6. Threshold: F1 0.667 for \tau \le 0.3 (each of the 15 objects matches one nucleus), 0.489 at 0.5 and 0.356 from 0.7 up: a low curve, because of the merges, as in B5(b) (but much lower: here many nuclei touch). One pixel too large: F1 1.000 up to \tau = 0.7, 0.967 at 0.8, then down to 0.367 at 0.9, the curve of B5(a): high and flat, then a steep drop where the IoU of the slightly too large outlines falls below \tau; its pixel Dice (0.944) is lower than the threshold’s. For counting: the one-pixel-too-large outlines (they count 30). For areas: neither as it is; the one-pixel bias could be corrected, the merges cannot.
C7. AP is 0.326 and the final recall 11/30 = 0.367. AP adds the precision (at most 1) over the recall steps, and the recall steps add up to the final recall: AP \le recall. Nuclei that no prediction matches can never be ranked; they cost AP whatever the scores are.
Where this leads
The same matching and AP are used for detection with boxes on the modern deep learning day. The next unit asks how good the truth is, and what to do when the outline or the count matters more than the match: boundary distances and agreement plots.