Skip to content

Confusion matrix and F-beta lose false positives and negatives above 2**24 voxels #9270

Open

Description

@pmxlr8

Describe the bug

get_confusion_matrix and get_f_beta_score cast tp, tn and p to float32 before they compute fn = p - tp and fp = n - tn. float32 holds integers exactly only up to 2**24 (16,777,216). In a sample with more voxels per channel, n and tn round to the float32 step, so fp and fn round too.

This is a follow-up to #7186. #7187 moved the sums to integers, but the counts are still cast to float32 before the subtractions.

To Reproduce

Synthetic masks, one CT-sized sample (512 x 512 x 300 = 78,643,200 voxels):

import torch
from monai.metrics import compute_confusion_matrix_metric, get_confusion_matrix

y = torch.zeros(1, 1, 512, 512, 300, dtype=torch.uint8)
y_pred = torch.zeros_like(y)
y[0, 0, 0, 0, :52] = 1       # 52 lesion voxels
y_pred[0, 0, 0, 0, :50] = 1  # 50 found: tp 50, fn 2
y_pred[0, 0, 1, 0, 0] = 1    # 1 false positive

cm = get_confusion_matrix(y_pred, y)
print(cm[0, 0].tolist())                                         # [50.0, 8.0, 78643144.0, 2.0]
print(compute_confusion_matrix_metric("precision", cm).item())   # 0.8621

Expected behavior

fp is 1 and precision is 50/51 = 0.9804. In this volume fp can only be 0 or a multiple of 8 (the float32 step between 226 and 227). With 0 to 6 real false positives, dev reports 0, 8, 8, 8, 8, 8, 8.

This affects ConfusionMatrixMetric (all metric names) and FBetaScore for samples above 2**24 voxels, for example whole-body CT and large 2D pathology tiles (4096 x 4098 is already above the limit).

Environment

MONAI dev at 846ae34, torch 2.15.0, Python 3.11.6, macOS 15.6.1 arm64 (CPU).

Additional context

I have a fix with tests and will open a PR that links this issue.

AI disclosure: this issue was found and written with Claude Code (an AI coding tool).

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions