Repository navigation
fix(metrics): count true positives for bool inputs in confusion matrix and F-beta score - #9189
tritsystem wants to merge 2 commits into
Conversation
|
No actionable comments were generated in the recent review.
|
…atrix get_confusion_matrix finds true positives with (y_pred + y) == 2. For bool tensors that sum is a logical OR, which is never 2, so bool masks such as pred > 0.5 lost every true positive: a perfect prediction scored sensitivity 0 and F1 0. Cast bool inputs to float before counting; float and integer inputs are unchanged. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: tritsystem <gbranaa4@gmail.com>
get_f_beta_score has the same (y_pred + y) == 2 true-positive test as get_confusion_matrix; both lost their float casts in Project-MONAI#7187. For bool masks the sum is a logical OR, so every true positive was dropped and a perfect prediction scored F-beta 0. Cast bool inputs to float before counting, as in the previous commit; float and integer inputs are unchanged. Assisted-by: Claude Opus 5.5 <noreply@anthropic.com> Signed-off-by: tritsystem <gbranaa4@gmail.com>
ba59ee7 to
67a70ae
Compare
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are reported for the boolean-mask metric counting fix.
No open issues 🎉
What changed in this PR
Restores correct true-positive counting for boolean segmentation masks in MONAI’s confusion-matrix and F-beta metrics, addressing the regression introduced by removing float casts in #7187.
Changes:
- Convert boolean inputs to float before counting, leaving float and integer inputs unchanged.
- Add boolean/float equivalence tests with background included and excluded, plus a perfect-prediction F-beta check.
| File | Description |
|---|---|
tests/metrics/test_compute_f_beta.py |
Tests boolean/float equivalence and a perfect boolean prediction. |
tests/metrics/test_compute_confusion_matrix.py |
Tests boolean/float count equivalence with both background settings. |
monai/metrics/f_beta_score.py |
Casts boolean inputs to float to preserve true-positive counts. |
monai/metrics/confusion_matrix.py |
Casts boolean inputs to float to correct confusion-matrix counts. |
Review effort: Balanced
|
Hi @tritsystem sorry for the delay. I wonder about the issue this raises with float precision, which is already present. The lines tp = (y_pred + y) == 2
tn = (y_pred + y) == 0rely on the tensors having exact values of 2 or 0, there's no margin for floating point error. Typically we don't have any after one-hot encoding but it's possible. Can we tweak these lines in both places to use The alternative is always convert tp = (y_pred & y)
tn = (~(y_pred | y))but we'd have to validate this is correct. Thanks! |
Files:
monai/metrics/confusion_matrix.py(get_confusion_matrix) andmonai/metrics/f_beta_score.py(get_f_beta_score). Both count true positives with(y_pred + y) == 2, which works for float/int tensors. If bothy_predandyare bool tensors, PyTorch's+does logical OR, not addition, soTrue + TruegivesTrue(1), never 2 -- the true-positive count comes out as zero every time for bool inputs.Impact: a perfect bool prediction (identical bool masks) scores sensitivity 0, precision NaN, F1 0, F-beta 0 -- a perfect match graded as a total failure. The same perfect prediction in float form correctly scores 1.0. This only triggers when both inputs are bool; if either is float, PyTorch promotes the other and it works. Checked every other metric in MONAI (Dice, MeanIoU, GeneralizedDice, Hausdorff, surface distance) -- none have this pattern, so it's isolated to these two functions.
This is a regression: #7187 (merged Nov 2023) removed the
.float()casts from both functions as "unnecessary".Fix: cast bool inputs to float at the top of both functions; float/int inputs are unchanged.
Types of changes
Fixes # .