You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
DiceMetric: mean_batch and mean_channel reduction modes appear to be swapped #8602
Describe the bug
The mean_batch and mean_channel reduction modes in DiceMetric.aggregate() appear to be swapped. mean_batch returns one value per channel (instead of per batch), and mean_channel returns one value per batch (instead of per channel).
Is this the intended behavior, or am I misunderstanding how data should be accumulated or passed to DiceMetric? In the below example, the dimension of the data is BCHW.
To Reproduce
Run:
importtorchfrommonai.metricsimportDiceMetricn_batches=3batch_size, n_classes, h, w=4, 5, 128, 128y_pred_batches= [torch.rand(batch_size, n_classes, h, w) for_inrange(n_batches)]
y_batches= [torch.rand(batch_size, n_classes, h, w) for_inrange(n_batches)]
dm=DiceMetric()
foriinrange(n_batches):
dm(y_pred_batches[i], y_batches[i])
# Internal buffer has shape [12, 5] (12 samples total, 5 channels)print(f"Buffer shape: {dm.get_buffer().shape}") # [12, 5]# mean_batch returns 5 values (one per channel, not per batch)print(f"mean_batch: {dm.aggregate(reduction='mean_batch').shape}") # [5]# mean_channel returns 12 values (one per batch, not per channel) print(f"mean_channel: {dm.aggregate(reduction='mean_channel').shape}") # [12]
Expected behavior
mean_batch should average across channels and return one value per batch: shape [3]
mean_channel should average across batches and return one value per channel: shape [5]
Environment
Ensuring you use the relevant python executable, please paste the output of:
Additional context
The problem appears to be in monai/metrics/utils.py:do_metric_reduction() lines 113-121. I am not sure if the bug affects other metrics using this function too.
Hi @gauthamk02, thanks for reporting this.
In this repo, we have defined MEAN_BATCH to calculate the average across the batch dimension, and MEAN_CHANNEL to calculate the average across the channel dimension.
Activity
KumoLiu commentedon Oct 28, 2025
Hi @gauthamk02, thanks for reporting this.
In this repo, we have defined
MEAN_BATCHto calculate the average across the batch dimension, andMEAN_CHANNELto calculate the average across the channel dimension.MONAI/monai/metrics/utils.py
Line 113 in 69f3dd2
MONAI/monai/metrics/utils.py
Line 119 in 69f3dd2