Skip to content

DiceMetric does not return NaN for empty classes  #7504

@benolmbrt

Description

@benolmbrt

Describe the bug
It is expected that the DiceMetric returns NaN values for classes that are not present in the ground truth, if the option ignore_empty is set to True. However, the metric returns instead a value of 0.

To Reproduce
Here is a minimal example to reproduce the bug. I'm creating two random segmentations with 8 classes,
and then manually remove classes 2 and 5 from the ground truth.

from monai.metrics import DiceMetric
import torch
from monai.networks import one_hot

# init metric
metric = DiceMetric(ignore_empty=True, get_not_nans=True, reduction='mean_batch')

# create ground truth and pred for a 8-classes problem
y = torch.randint(0, 8, (1, 1, 64, 64, 64))
y_hat = torch.randint(0, 8, (1, 1, 64, 64, 64))
y1h = one_hot(y, 8)
pred1h = one_hot(y_hat, 8)

# remove classes 2 and 5 from ground truth
y1h[:, 2] = torch.zeros(1, 64, 64, 64)
y1h[:, 5] = torch.zeros(1, 64, 64, 64)

# compute dice
metric(pred1h, y1h)
class_dices, not_nans_count = metric.aggregate()

print(class_dices)
print(not_nans_count)

Output :

class_dices = [0.1273, 0.1231, 0.0000, 0.1277, 0.1249, 0.0000, 0.1233, 0.1278]
not_nans_count = [1., 1., 0., 1., 1., 0., 1., 1.]

--> The Dice scores for class 2 and 5 are set to 0, not NaN.

Expected behavior
It is expected that the Dice values for class 2 and 5 are NaN, but it is 0.

Environment

I am using Monai 1.3.0 with Python 3.11.5

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