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
…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>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
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 # .