Exercise: boundaries, counts and the uncertain ground truth
Before this unit: Matching objects: IoU, F1 and average precision (previous 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 directed distances, the Hausdorff distance and the average surface distance of two small point sets, and say which error each one reacts to,
- draw and read a Bland–Altman plot: bias, limits of agreement, proportional bias,
- explain why correlation is not agreement, and why a one-pixel outline error is a large relative error for small objects,
- judge a score against the agreement of two annotators,
- estimate how many annotated objects and images an evaluation needs.
Do the pen-and-paper part first, then the two demos. Questions marked (math) are for those who want the derivations; the concept questions do not depend on them.
Background
| Word | Meaning |
|---|---|
| d(a, \partial B) | distance from a boundary point a of A to the nearest boundary point of B |
| Directed distance h(A, B) | the largest d(a, \partial B) over the boundary points of A |
| Hausdorff H | \max(h(A, B), h(B, A)): the worst boundary distance |
| ASD | mean of all d(a, \partial B) and d(b, \partial A) together |
| HD95 | like Hausdorff, but the 95th percentile instead of the maximum |
| Bland–Altman | difference (method − reference) against the mean of the two, per item |
| Bias, limits of agreement | mean difference \bar d; \bar d \pm 1.96\,s_d |
| SE of a proportion | \sqrt{p(1-p)/n} for n independent items |
By hand
B1. Two point sets (boundary points): A = \{(0,0), (1,0), (2,0)\} and B = \{(0,1), (1,1), (2,1), (1,4)\}. (a) For every point, the distance to the nearest point of the other set. (b) The directed distances h(A, B) and h(B, A), the Hausdorff distance and the ASD. Which point is responsible for the Hausdorff distance? (c) The ASD without the point (1, 4). If A and B were the outlines of two large regions and (1, 4) the tip of a thin spike, what would IoU say?
B2. Bland–Altman by hand. Manual counts of six images: 20, 35, 50, 65, 80, 100; automated counts: 19, 33, 46, 60, 72, 88. (a) Compute the differences (auto − manual) and the means. (b) Compute the bias, the SD of the differences and the limits of agreement (\pm 1.96 SD). (c) Sketch the plot. Why are these limits misleading here, and what would you plot instead? (d) Estimate the correlation of the two lists without computing it. What does it tell you about agreement?
B3. Two annotators reach an object F1 of 0.86 against each other at \tau = 0.5. Method X scores 0.84 against annotator A, method Y 0.91. (a) What can you conclude about X? (b) A paper presents Y as “better than human”. What would you check first? (c) Name two ways to make the ground truth less uncertain.
B4. A recall of 0.80 is measured on 25 objects. (a) Roughly how wide is its ±2 SE interval? (b) How many objects would you need for ±0.05? (c) Why could even that be optimistic if all objects come from two images?
B5. Predict, then explain. (a) An outline is 2 px too large everywhere. Hausdorff, ASD, and IoU for a nucleus of radius 8 px and one of radius 40 px? (b) An automated area measurement uses outlines that are 1 px too large. For which objects is the relative error largest, and what does the ratio auto/manual look like as a function of size? (c) Method A’s counts are always 10 % too low; method B’s are unbiased but scatter by ±10 %. Which one would you rather use to compare treated with control wells, and why?
B1. (a) Each point of A is at distance 1 from the point of B above it: 1, 1, 1. From B: (0,1), (1,1), (2,1) at distance 1; (1, 4) is 4 from (1, 0). (b) h(A, B) = 1, h(B, A) = 4, Hausdorff H = 4, caused by (1, 4). ASD = (1 + 1 + 1 + 1 + 1 + 1 + 4)/7 = 10/7 = 1.43. (c) Without (1, 4): ASD = 6/6 = 1. The spike adds one far point: it dominates the Hausdorff distance and raises the ASD a little, while IoU of the regions would hardly change (a thin spike has almost no area).
B2. (a) Differences: −1, −2, −4, −5, −8, −12. Means: 19.5, 34, 48, 62.5, 76, 94. (b) Bias = -32/6 = -5.33. SD = 4.08. Limits: -5.33 \pm 1.96 \cdot 4.08 = −13.3 to +2.7. (c) The differences fall steadily with the mean: the method undercounts more in dense images (proportional bias, typical of merges). The limits assume one spread for all sizes; here they are too wide for small counts and too narrow for large ones. Plot the ratio auto/manual (0.95, 0.94, 0.92, 0.92, 0.90, 0.88) against the mean, or the differences of logarithms; or report the bias as a function of the count. (d) Very close to 1 (it is 0.9995): the points lie almost exactly on a straight line, just not the identity line. Correlation says nothing about agreement.
B3. (a) X is about as good as a second human, within the uncertainty of both numbers (0.84 vs 0.86). It is not clearly worse. (b) Y agrees with annotator A better than annotator B does. Check whether Y was trained on (or tuned to) annotations by A, or by the same protocol: then Y has learned A’s style (where A draws boundaries, which dim objects A counts). Evaluate against B, or against a consensus. “Better than human” needs an independent truth. (c) Several annotators and a consensus (majority vote, or averaging the outlines); a clear annotation protocol; an “ignore” label for ambiguous objects; synthetic images where the truth is known by construction.
B4. (a) SE = \sqrt{0.8 \cdot 0.2/25} = 0.08: the interval is 0.80 \pm 0.16, from 0.64 to 0.96. (b) Need 2\,\text{SE} = 0.05, i.e. \text{SE} = 0.025: n = 0.16/0.025^2 = 256 objects. (c) The formula assumes independent objects. Objects of the same image share illumination, density and staining; if the method works badly on one of the two images, a whole block of objects fails together. The effective number of independent observations is much smaller (closer to the number of images), so the real uncertainty is larger.
B5. (a) Every boundary point is 2 px from the other outline: Hausdorff = ASD = 2 for both. IoU depends on the size: for a disc of radius r, r^2/(r+2)^2: 0.64 for r = 8, 0.91 for r = 40 (the demo’s nucleus-shaped outline gives 0.64 and 0.91). (b) Largest for the smallest objects: a disc of radius r gets the area \pi(r+1)^2 instead of \pi r^2, ratio (1 + 1/r)^2: 1.78 for r = 3, 1.21 for r = 10. The ratio falls towards 1 as the size grows, like 1 + 2/r. (c) Method A: a constant relative bias cancels when comparing two conditions measured the same way (a 10 % drop is still a 10 % drop), as long as the bias does not depend on the condition. B’s scatter adds noise to every well and needs more wells to see the same effect. (If treated wells are denser and A undercounts dense wells more, A’s bias would no longer cancel.)
Math questions
B6. (math) Show that the Hausdorff distance satisfies the triangle inequality H(A, C) \le H(A, B) + H(B, C) for non-empty finite (or compact) sets. Is the ASD a metric?
B7. (math) Suppose auto = a \cdot manual + b exactly. Show that the Bland–Altman points lie on a straight line and find its slope in terms of a. When is the slope zero? What is the correlation of auto and manual?
B8. (math) Show that \sqrt{p(1-p)/n} is largest at p = \frac12, and find the n needed for a 95 % interval of half-width 0.05 in the worst case.
B6. For a \in A, let b \in B be its nearest point: d(a, B) \le h(A, B). Then for any c \in C, \|a - c\| \le \|a - b\| + \|b - c\|, so d(a, C) \le \|a - b\| + d(b, C) \le h(A, B) + h(B, C). Taking the maximum over a: h(A, C) \le h(A, B) + h(B, C) \le H(A, B) + H(B, C). The same with the roles of A and C swapped gives h(C, A) \le H(C, B) + H(B, A); together H(A, C) \le H(A, B) + H(B, C). (H is also symmetric, and H(A, B) = 0 only if A = B for closed sets, so it is a metric.) The ASD is symmetric and zero for equal sets, but it is not a metric: it violates the triangle inequality. Example in 1D: A = \{0\}, B = \{0, 1\}, C = \{1\}: ASD(A, B) = (0 + 0 + 1)/3 = \frac13, ASD(B, C) = \frac13, but ASD(A, C) = 1 > \frac23.
B7. With y = ax + b: d = y - x = (a - 1)x + b and m = (x + y)/2 = \frac{a+1}{2}x + \frac b2. Eliminate x = (2m - b)/(a + 1): d = \frac{2(a-1)}{a+1}m + b\,\frac{2}{a+1}, a straight line with slope \frac{2(a-1)}{a+1}. The slope is zero only for a = 1 (then all points sit at d = b: a pure offset). For a = 0.8: slope -0.22. The correlation is r = 1 for every a > 0 and every b.
B8. f(p) = p(1 - p) has f'(p) = 1 - 2p = 0 at p = \frac12 and f'' = -2 < 0: a maximum, f(\frac12) = \frac14. So SE \le \frac{1}{2\sqrt n}. A 95 % interval of half-width w needs 1.96\,\text{SE} \le w: n \ge 1.96^2/(4w^2) = 384.1 for w = 0.05, so 385 objects (400 with the rule of thumb 2 SE). These are independent objects.
Explore: boundary distances
The grey outline is the truth, the blue one the prediction; drag the round handles to deform the prediction (each handle moves the outline locally). The square handle is the tip of an optional thin spike, the dashed circle an optional stray fragment. Whiskers join boundary points to the nearest point of the other outline, coloured by length; the red whisker is the Hausdorff distance. The object radius slider scales the object while the pixel errors stay the same.
C1. Preset one-pixel dilation. Read IoU, Hausdorff, HD95 and ASD. Why are the three distances equal?
C2. Preset one thin spike (14 px). Read the five numbers. Now drag the spike tip to make it about 28 px long, and then about 7 px. Which numbers change a lot, which hardly at all?
C3. Preset a stray fragment. Drag the fragment closer to the object, and then into the middle of the object. What happens to Hausdorff, HD95, ASD and IoU?
C4. Preset same error, small object (radius 8 px, 1 px too large). Compare IoU with C1. Move the radius slider from 5 to 50 and note IoU at a few sizes. Which metric would you use to compare outline quality across small and large objects?
C5. Choose a typical outline (radius 40 px) and read the numbers. Then set the radius to 10. Which numbers stay and which change?
C1. IoU 0.951, Dice 0.975, Hausdorff 1.00, HD95 1.00, ASD 1.00. Every boundary point of either outline is exactly 1 px from the other outline, so the maximum, the 95th percentile and the mean of the distances are all 1.
C2. At 14 px: IoU 0.994, Hausdorff 14.0, HD95 7.48, ASD 0.40. At 28 px: IoU 0.987, Hausdorff 28.0, HD95 20.5, ASD 1.48. At 7 px: IoU 0.997, Hausdorff 7.0, HD95 0.93, ASD 0.12. Hausdorff follows the tip exactly; HD95 reacts once the spike is long enough to make up more than 5 % of the predicted outline; ASD grows with the spike’s length (it is an average over all points); IoU barely moves (the spike is 3 px wide).
C3. In its starting position: Hausdorff 37.8, HD95 0.00, ASD 0.87, IoU 0.998. Near the object (just outside): Hausdorff about 6 and ASD about 0.1; in the middle of the object Hausdorff is about 34 (its distance to the true outline) and IoU 1.000 (it lies inside the object anyway). HD95 stays 0: the fragment is less than 5 % of the outline points. Hausdorff measures the worst point, wherever it is.
C4. Radius 8 px: IoU 0.787, Dice 0.881, while Hausdorff = HD95 = ASD = 1.00 as in C1 (IoU 0.951 there). One pixel too large gives IoU 0.691 at radius 5, 0.787 at 8, 0.877 at 15, 0.905 at 20, 0.951 at 40, 0.960 at 50. To compare outline quality across sizes, a distance in pixels (or µm), such as ASD, says the same thing for every size; IoU mixes outline error and object size.
C5. Radius 40: IoU 0.940, Hausdorff 2.50, HD95 2.20, ASD 1.21. Radius 10: Hausdorff and ASD hardly change (2.50 and 1.23: the pixel offsets are the same), but IoU drops to 0.777.
Explore: agreement of counts
40 synthetic images with manual counts (annotator A) and automated counts from a model: scale (auto ≈ scale × manual), a constant offset, an undercount that grows with the count (density undercount, as from merges) and random scatter. Top left: automated against manual with the identity line (dashed) and the correlation r. Bottom left: the ratio auto/manual against the mean. Right: the Bland–Altman plot with the bias (red), the limits of agreement (orange) and a fitted line (green dots). Areas switches to one point per object (150 objects of radius 2.5–15 px) with an outline offset in pixels; annotator B compares a second manual annotation with annotator A.
A1. Preset r = 0.99, still 20 % off. Read r, the bias, the limits of agreement and the mean ratio. Why is r so high?
A2. Preset merges at high density. Describe the Bland–Altman cloud and read the slope of the fitted line. Why are the limits of agreement misleading here? What does the ratio panel show?
A3. Preset scatter only. Compare bias and limits of agreement with A1. Which of the two methods could you fix with a calibration factor, and which not?
A4. Preset small objects: area bias (outline 1 px too large). Read the mean ratio. In the ratio panel, what is the ratio for the smallest objects and for the largest? Set the outline offset to −1 px: what changes?
A5. Preset two annotators. Read the bias and the limits of agreement. What does this mean for the methods of A1–A3? Then tick areas: how well do two annotators agree on object areas?
A6. Start from a typical method, set density undercount and scatter to 0, and the scale to 1.1. Predict the Bland–Altman plot before you look (bias sign, slope sign). Compare the slope with B7.
A1. r = 0.997, bias −16.2, limits of agreement −32.4 to 0.0, mean ratio 0.81. The automated counts are 80 % of the manual ones, so all points lie close to a straight line of slope 0.8: correlation measures closeness to a line, not to the identity line, and the wide range of counts (17 to 146) makes r high regardless.
A2. The differences fall with the mean: almost no error for small counts, an undercount of 30–45 for the densest images; the fitted slope is −0.38 (r = 0.990). The limits (−41.2 to +8.9) pretend that a sparse image can be off by −41, which never happens, and that a dense image is within the limits, which is only barely true. The ratio panel shows a ratio near 1 for low counts dropping to about 0.7 for the highest: report the bias as a function of the count, or as a ratio.
A3. Scatter only: bias 1.2 (almost none), but limits −16.5 to +18.9, wider than A1’s spread. A1 can be calibrated (multiply by 1/0.81, after checking that the factor is stable); A3’s random scatter cannot be calibrated away, only averaged over more images.
A4. Mean ratio 1.26 (bias +63.6 px²). Objects below 50 px² have a ratio of about 1.66, objects above 400 px² about 1.17: the same 1-px error is a much larger fraction of a small area. With −1 px the ratios fall below 1 (about 0.49 for objects below 50 px², 0.86 above 400 px²; mean 0.77): again the small objects suffer most.
A5. Two annotators: bias 0.55, limits −3.2 to +4.3. A method cannot be expected to do better than this; A1–A3 are far outside it, so they are clearly worse than a second human. For areas, the two annotators have bias 0.59 px² and limits −43.5 to +44.7 px²: their outlines differ by a fraction of a pixel, which on areas of 25–730 px² is a scatter of tens of px².
A6. Bias positive (+8.7: auto too high), and the cloud slopes up (slope 0.088): the overcount grows with the count. B7 predicts 2(a-1)/(a+1) = 0.095 for a = 1.1; the demo’s value is a little lower because the manual counts themselves have a small random error.
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/agreement/exercise.rs.
C1. boundary(mask): the boundary pixels of a mask, as points: object pixels with at least one of their 4 neighbours outside the object (or outside the image). The check: a 4 \times 4 square has 12.
C2. nearest_distances(a, b): for every point of A, the distance to the nearest point of B, by trying them all. The check is B1(a).
C3. boundary_scores(a, b): Hausdorff, HD95 and ASD from the nearest distances in both directions:
dA = nearest distances from A to B, dB = from B to A
Hausdorff = largest value in dA and dB
HD95 = larger of (95th percentile of dA, 95th percentile of dB)
ASD = (sum of dA + sum of dB) / (number of points of A + of B)
The 95th percentile of n sorted values is value number \lceil 0.95\,n \rceil. The checks are B1(b, c) and a far single point that HD95 ignores.
C4. bland_altman(reference, method): the differences method − reference, their mean (the bias), their standard deviation (divide by n - 1) and the limits of agreement bias \pm 1.96\,SD. The check is B2(b).
C5. correlation(x, y): the correlation coefficient r. The checks are B2(d), and that “80 % of the manual count” has r = 1.
Then cargo run -p evaluation -- agreement. It prints three tables: a disc of radius 3 to 40 pixels against the same disc drawn one pixel larger; a disc of radius 30 with a thin spike (3 px wide, 7, 14 and 28 px long; out/agreement/spike-outlines.png shows the 14 px case, truth white, prediction blue); and the counts of 40 simulated images by three automated methods against the manual count.
C6. In the disc table, which numbers depend on the size of the disc and which do not? Why are some distances 1.41 instead of 1?
C7. In the spike table, which numbers follow the length of the spike, and which hardly notice it?
C8. In the count table, rank the three methods by r and then by the width of the limits of agreement. Which method could you fix with a calibration factor? What do the two mean differences printed for the merges tell you?
The model solution is evaluation/src/agreement/solution.rs in the download.
C6. IoU grows with the size: 0.592 at radius 3, 0.717 at 5, 0.779 at 8, 0.890 at 15, 0.940 at 30, 0.955 at 40; the area ratio falls from 1.69 to 1.05 (B5(b)). The boundary distances stay at about one pixel for every size (ASD 1.00 to 1.07): the error is the same one pixel, and the distances measure it in pixels. On a pixel grid a “one pixel larger” outline is sometimes one pixel away diagonally, \sqrt 2 = 1.41, which shows in the Hausdorff distance of some sizes.
C7. Spike of 7, 14, 28 px: Hausdorff 6.1, 13.0, 27.0 (it follows the tip exactly), HD95 2.2, 9.1, 22.0 (it reacts once the spike’s points are more than 5 % of the outline), ASD 0.16, 0.56, 2.03 (an average: it grows with the length, but stays small). IoU hardly notices: 0.993, 0.986, 0.971, because the spike has almost no area. Compare C2 of the demo.
C8. By r: 80 % of the count 0.997, merges 0.989, scattered 0.968: the scattered, unbiased method looks worst. By the limits of agreement: 80 % of the count -31.9 to -2.6 (bias -17.3, the narrowest band, but nowhere near 0), merges -41.7 to 8.9, scattered -16.7 to 17.5 (bias 0.4). The first one can be calibrated (divide by 0.8): its error is systematic. The scatter of the third cannot. For the merges the mean difference is -5.3 in images below 80 cells and -25.5 above: the error grows with the density, so one bias and one pair of limits describe none of the images well; report the error against the count, or the ratio (A2 of the demo).
Where this leads
Every number in this unit came with an uncertainty that depends on how many independent images or experiments went into it. The next unit asks how to split data into training and test sets so that the test is honest; the last one how to turn measurements into claims about a treatment.