Exercise: evaluation against ground truth

Goal

By the end of this exercise you should be able to:

  • fill in the confusion matrix of a predicted mask and compute precision, recall, FPR, accuracy, Dice and Jaccard,
  • explain why accuracy misleads when objects are rare,
  • trace ROC points by sweeping a threshold, and read the AUC,
  • predict how class imbalance changes the precision–recall curve but not the ROC curve,
  • say what pixel measures cannot see.

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

Measure Formula Meaning
Precision TP/(TP + FP) of the predicted object, the fraction that is object
Recall, TPR TP/(TP + FN) of the true object, the fraction found
FPR FP/(FP + TN) of the background, the fraction called object
Accuracy (TP + TN)/\text{all} fraction of pixels right
Dice (F1) 2TP/(2TP + FP + FN) overlap; harmonic mean of precision and recall
Jaccard (IoU) TP/(TP + FP + FN) intersection over union; J = D/(2 - D)
ROC curve (FPR, TPR) for every t AUC = probability that an object pixel is brighter than a background pixel
PR curve (recall, precision) for every t sensitive to how rare the objects are

By hand

B1. A 5 \times 5 image I and its ground truth G (1 = object): I = \begin{bmatrix} 10&20&30&20&10\\ 20&70&80&60&20\\ 30&80&90&40&50\\ 20&60&40&30&20\\ 10&20&30&70&10 \end{bmatrix}, \qquad G = \begin{bmatrix} 0&0&0&0&0\\ 0&1&1&1&0\\ 0&1&1&1&0\\ 0&1&0&0&0\\ 0&0&0&0&0 \end{bmatrix} (a) Threshold at t = 45 (object where I > 45). Mark TP, FP, FN, TN and fill in the confusion matrix. (b) Compute precision, recall, FPR, accuracy, Dice and Jaccard.

B2. The same image at t = 35, t = 55 and t = 75. (a) Compute TPR and FPR for each, and draw the three points with (0, 0) and (1, 1) on ROC axes. (b) Which of the four thresholds (35, 45, 55, 75) has the highest Dice? The highest TPR − FPR?

B3. A 256 \times 256 image in which 3 % of the pixels are object. (a) A method calls every pixel background. Compute its accuracy, recall and Dice. (b) Another method finds all objects but also marks 3 % of the background. Accuracy? Dice? (c) Which measure would you report, and why?

B4. A method has TPR 0.9 at FPR 0.05. Compute its precision when objects are 50 %, 10 % and 1 % of the pixels. Does its ROC point change? What does this mean for comparing methods on images with few objects?

B5. Two annotators outline the same round nucleus; one outline is 1 pixel larger in radius than the other. Compute Dice between their masks for a radius of 3, 10 and 20 pixels (use areas \pi r^2). What does this say about scoring small objects?

B6. Two touching nuclei, each 400 pixels, are segmented as one blob of 800 pixels that matches their union exactly except for 10 missed pixels along the line between them. (a) Compute pixel Dice. (b) How many objects does the segmentation report, and what are their areas? What would a cell count based on it say?

B1. (a) Object at t = 45: the 70, 80, 60 in row 2, the 80, 90 and the 50 in row 3, the 60 in row 4 and the 70 in row 5. TP = 6 (all object pixels except the 40 in row 3, column 4), FN = 1 (that 40), FP = 2 (the 50 at row 3, column 5 and the 70 at row 5, column 4), TN = 16. (b) Precision 6/8 = 0.750, recall 6/7 = 0.857, FPR 2/18 = 0.111, accuracy 22/25 = 0.880, Dice 12/15 = 0.800, Jaccard 6/9 = 0.667.

B2. (a) t = 35: TP 7, FP 3: TPR 1.000, FPR 3/18 = 0.167. t = 55: TP 6, FP 1: TPR 0.857, FPR 0.056. t = 75: TP 3, FP 0: TPR 0.429, FPR 0. The curve rises steeply: (0, 0) \to (0, 0.429) \to (0.056, 0.857) \to (0.111, 0.857) [t = 45] \to (0.167, 1) \to (1, 1). (b) Dice: 0.824 (t = 35), 0.800 (45), 0.857 (55), 0.600 (75). TPR − FPR: 0.833, 0.746, 0.802, 0.429: Youden’s index prefers t = 35, Dice prefers t = 55. Different criteria, different “best” thresholds.

B3. (a) Accuracy 0.97, recall 0, Dice 0. (b) TP = 3 % of the pixels, FP = 3 % of the 97 % background = 2.91 %: accuracy 1 - 0.0291 = 0.971, Dice = 2 \cdot 3/(2 \cdot 3 + 2.91) = 0.673. (c) Dice (or precision and recall): accuracy barely distinguishes a useless method (0.970) from a useful one (0.971).

B4. Precision = \frac{\pi \cdot 0.9}{\pi \cdot 0.9 + (1 - \pi) \cdot 0.05} for object fraction \pi: 0.947 at 50 %, 0.667 at 10 %, 0.154 at 1 %. The ROC point (0.05, 0.9) is the same in all three cases: the ROC curve is blind to imbalance. With few objects, compare methods on the PR curve or Dice, where the flood of false positives shows.

B5. Dice = 2\pi r^2/(\pi r^2 + \pi(r + 1)^2): 0.720 for r = 3, 0.905 for r = 10, 0.951 for r = 20. A one-pixel boundary disagreement, which is within annotation noise, costs a small object far more: Dice of small objects cannot be measured precisely, and averages over objects of mixed size are dominated by the small ones.

B6. (a) TP = 790, FN = 10, FP = 0: Dice = 1580/1590 = 0.994. (b) One object of 790 pixels instead of two of 400: the count is wrong by one (50 %), and the measured area is double the true area of each cell. Pixel Dice sees none of this; object-level evaluation (day 38) does.

Math questions

B7. (math) Show that Dice is the harmonic mean of precision and recall, and that J = D/(2 - D).

B8. (math) Let h^1_i and h^0_i be the histograms of the object and background pixels. Show that TPR(t) = 1 - P_1(t) and FPR(t) = 1 - P_0(t), with P_k the normalised cumulative histograms. With two normal classes \mathcal N(\mu_k, \sigma^2), write TPR and FPR with the normal cumulative distribution \Phi.

B9. (math) Show that the area under the ROC curve equals the probability that a randomly chosen object pixel is brighter than a randomly chosen background pixel (ties counting one half). Check it for B1: the object pixels are 40, 60, 60, 70, 80, 80, 90; the background pixels are fifteen values \le 30 and one each of 40, 50 and 70.

B10. (math) Express precision in terms of TPR, FPR and the object fraction \pi, and show that for fixed TPR and FPR it tends to 0 as \pi \to 0.

B7. \frac{2}{1/p + 1/r} = \frac{2}{(TP + FP)/TP + (TP + FN)/TP} = \frac{2TP}{2TP + FP + FN} = D. With s = FP + FN: \frac{D}{2 - D} = \frac{2TP/(2TP + s)}{(4TP + 2s - 2TP)/(2TP + s)} = \frac{2TP}{2TP + 2s} = \frac{TP}{TP + s} = J.

B8. The object pixels called object at threshold t are those with grey level > t: TPR = \frac{1}{n_1}\sum_{i > t} h^1_i = 1 - P_1(t); likewise FPR = 1 - P_0(t). For normal classes: TPR(t) = 1 - \Phi\big(\frac{t - \mu_1}{\sigma}\big), FPR(t) = 1 - \Phi\big(\frac{t - \mu_0}{\sigma}\big): the tail areas of the minimum-error picture.

B9. Lower the threshold one grey level at a time, from the top. When it passes level i, FPR increases by h^0_i/n_0, and TPR is then the fraction of object pixels above i (plus half of those at i, if the step is drawn as a straight segment). The area of that strip is \frac{h^0_i}{n_0}\cdot\frac{\#\{\text{obj} > i\} + \frac12\#\{\text{obj} = i\}}{n_1}: the fraction of (object, background) pairs with this background pixel at level i in which the object pixel is brighter. Summing over i counts all pairs: AUC = P(I_\text{obj} > I_\text{bg}) + \frac12 P(=). For B1: 7 object and 18 background pixels, 126 pairs. Object 90, 80, 80: brighter than all 18 (54 pairs). 70: brighter than 17, tie with one 70: 17.5. Each 60: brighter than 17 (all but the 70): 34. 40: brighter than the 15 values \le 30, tie with one 40: 15.5. Total 121/126 = 0.960.

B10. Precision = \frac{TP}{TP + FP} = \frac{\pi\,\text{TPR}}{\pi\,\text{TPR} + (1 - \pi)\,\text{FPR}} (with TP = \pi N\,\text{TPR} and FP = (1 - \pi)N\,\text{FPR}). For fixed TPR and FPR > 0 the second term in the denominator dominates as \pi \to 0, and precision \approx \pi\,\text{TPR}/\text{FPR} \to 0.

Explore

A synthetic image with bright blobs (the ground truth is known). Left: the image coloured by the four cases: TP green, FP red, FN blue, TN grey. Middle: the ROC curve (left plot) and the precision–recall curve (right plot), traced as t sweeps; the orange dot is the current t; purple = Otsu, brown = minimum error, green = the threshold with the best Dice. Below: the confusion matrix (counts and percentages of all pixels) and the measures, with the AUC. objects sets the object fraction, contrast, noise σ and light ramp (an illumination gradient) the image; smooth first applies a Gaussian blur (σ = 1.5) before thresholding; local threshold evaluates a local threshold (local mean + 5 in a 51 \times 51 window) instead: one point, no curve.

E1. Preset balanced classes (50 % objects; t = Otsu). Read the confusion matrix, precision, recall, Dice and AUC. Press t = max Dice: how much better is it? Where is the minimum-error marker, and what went wrong there?

E2. Preset 3 % objects: accuracy lies. Read accuracy and Dice. Then press t = Otsu and t = max Dice, and compare precision, recall, accuracy and Dice.

E3. Preset Otsu vs max Dice (10 % objects). Compare the two thresholds on the ROC curve and on the PR curve. Which plot shows the difference better? Why?

E4. Set objects to 50 %, 25 % and 3 % (contrast 50, noise 20, no light ramp) and watch the two curves. Which curve changes shape, and which stays (nearly) the same? Relate to B4.

E5. Preset uneven illumination. Read AUC and the best Dice. Tick local threshold: where is its point, and what is its Dice? Then tick smooth first as well.

E6. Preset noise: smooth first (noise 40, contrast 40). Read AUC and the best Dice; then tick smooth first. What happens to the ROC curve, and why?

E1. Otsu: t = 95; TP 28 074, FP 4388, FN 4693, TN 28 381; precision 0.865, recall 0.857, Dice 0.861, AUC 0.937. Max Dice is at t = 92 with 0.864: almost no gain, because with balanced classes of equal noise Otsu’s assumptions hold. The minimum-error marker sits at t = 165, near the top end of the curves (Dice 0.020): the blurred edges put many pixels between the peaks, so the histogram is not two normal peaks, and the two-Gaussian fit lands at a split far out in the upper tail of the object peak.

E2. At t = 254 nothing is object: accuracy 0.970, recall 0, Dice 0. With Otsu (t = 72): recall 0.967 but precision 0.062: Otsu falls into the large background peak and marks 45.5 % of the background (28 931 false positives); accuracy drops to 0.558, Dice 0.116. With max Dice (t = 115): precision 0.519, recall 0.458, accuracy 0.971, Dice 0.487. Accuracy hardly separates “nothing” (0.970) from the best threshold (0.971).

E3. Otsu (t = 87): TPR 0.978, FPR 0.138, precision 0.440, Dice 0.607. Max Dice (t = 105): TPR 0.846, FPR 0.014, precision 0.869, Dice 0.857. On the ROC curve both points look close to the top-left corner; on the PR curve Otsu sits far down at precision 0.44. With 10 % objects, an FPR of 0.138 means 8161 false pixels against 6411 true ones: the PR curve shows it, the ROC curve’s FPR axis compresses it.

E4. The ROC curve stays nearly the same (AUC 0.937 at 50 %, 0.935 at 25 %, 0.926 at 3 %): it uses rates within each class. The PR curve sinks as objects get rarer: at 3 % the precision stays well below 1 except at the highest thresholds, and the best Dice falls to 0.487. The same false-positive rate means more and more false pixels per true one (B4).

E5. AUC 0.762 (against 0.935 without the ramp), best Dice 0.576 at t = 115: on the bright side the background is brighter than the objects on the dim side, so the two class histograms overlap and no threshold is good: the ROC curve flattens. The local threshold gives one point with TPR 0.845, FPR 0.242, Dice 0.657: better than any global threshold. With smooth first as well, the local threshold reaches Dice 0.919, while the best global threshold only gets 0.612.

E6. Without smoothing: AUC 0.734, best Dice 0.506 (t = 92). With smoothing: AUC 0.989, best Dice 0.916 (t = 90). Smoothing narrows both class histograms (noise divided by about the square root of the number of pixels averaged), so they overlap much less and the whole ROC curve moves towards the top-left corner.

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 thresholding/src/seg_eval/exercise.rs.

C1. confusion(pred, truth): count TP, FP, FN and TN over all pixels. C2. The six measures of the Background table (precision, recall, FPR, accuracy, Dice, Jaccard) from the four counts. The checks are B1 (a) and (b), and B3 (a).

C3. roc(img, truth, bins): the point (FPR, TPR) for every threshold t (object where the bin of the grey value is above t), from two histograms, without thresholding the image again:

h1, h0 = histograms of the object pixels and of the background pixels
above1 = n1;  above0 = n0                  # everything is above "t = −1"
for t = 0, 1, …, bins − 1:
    above1 = above1 − h1[t];   above0 = above0 − h0[t]
    point(t) = (above0 / n0, above1 / n1)  # (FPR, TPR)

The check is B2 (a); it stores the grey levels of B1 as i/128, so that bin t of 128 bins is grey level t.

C4. auc(points): the area under the ROC curve, with the corners (1, 1) and (0, 0) added, as a sum of trapezoids:

area = 0
for each pair of neighbouring points (f0, t0), (f1, t1):
    area = area + (f0 − f1) · (t0 + t1) / 2

The check is B9: 121/126 = 0.960.

C5. best_dice(img, truth, bins): the threshold with the highest Dice, and that Dice. The check is B2 (b): Dice 0.857 (from t = 50 to 59, the smallest wins).

Then cargo run -p thresholding -- seg-eval and open the pictures in out/seg-eval/. The program makes synthetic nuclei with low contrast and noise, once with many nuclei (16.6 % of the pixels) and once with three (1.6 %). For each it evaluates “all background”, a threshold at the mean grey value and the best-Dice threshold, draws the four kinds of pixels (TP green, FP red, FN blue, TN grey) and plots the ROC curve (-roc.png, FPR across, TPR up) and the precision–recall curve (-pr.png, recall across, precision up).

C6. For the image with three nuclei, compare “all background” with the best-Dice threshold: accuracy and Dice. Which measure would you report? (B3)

C7. Compare the AUC and the two curves of many and few. Which curve changes, and which hardly does? Relate it to B4.

The model solution is thresholding/src/seg_eval/solution.rs in the download.

C6. All background: accuracy 0.984, Dice 0. Best Dice (t = 144): accuracy 0.988, Dice 0.589 (precision 0.654, recall 0.536). Accuracy hardly separates a method that finds nothing from one that finds half of the nuclei; Dice (or precision and recall) does. The threshold at the mean grey value is worse than nothing by accuracy (0.541) because it calls 46.6 % of the background object: precision 0.033.

C7. The AUC is almost the same, 0.970 with many nuclei and 0.964 with three, and the two ROC curves look alike: they use rates within each class. The precision–recall curves differ: with many nuclei precision stays high up to a recall of about 0.8, with three it falls steadily as the recall rises, and the best Dice drops from 0.824 to 0.589. The same false-positive rate means ten times as many false pixels per true one when the objects are ten times rarer (B4).

Where this leads

Pixel scores judge masks. Counting and measuring cells needs objects: connected components (labelling, day 14), splitting touching objects (watershed, day 15), and object-level evaluation with matching, F1 and average precision (evaluation, day 38). The learning days use Dice and the ROC curve to compare classifiers (SVM pixel classification, day 34), and Dice itself as a training loss (segmentation networks, day 36). Next day: morphology cleans up the binary masks made today.