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
Is your feature request related to a problem? Please describe.
The docstring descriptions for DiceLoss, DiceMetric, DiceHelper, and compute_dice should include more information about the expectations of inputs, implications of various arguments, and better examples. There's been some confusion about why results from the loss don't match expectations given the value of metrics, this is related to the extra features in the loss such as the smoothing values as well as misunderstanding about activation or thresholding/one-hot formatting.
Describe the solution you'd like
A few things should be changed or added:
Docstring for DiceMetric should make it clear the ground truth and prediction can be either single-channel label maps or multi-channel one-hot tensors.
The values of the inputs do matter since no thresholding is done in the metrics themselves.
The softmax parameter of DiceHelper is confusing in that it appears to indicate softmax will be applied if it's true, rather than the expectation that this was already done to the prediction. This should be renamed to argmax and the old name deprecated. It should also be noted that this parameter becomes the opposite value to sigmoid if not given a value.
Using compute_dice probably should be discouraged in favour of the DiceMetric class, and it should be mentioned that they do the same computation. It's possible this function isn't used by anyone and can be deprecated entirely.
An example in the docstring of how to use the metric and the loss to get expected results should be included.
I would also like to know the formula used in DiceMetric.
I'm not totally sure how to demonstrate based off your code snippet, but I will be updating the comments with something like the following:
importtorchfrommonai.metricsimportDiceMetricfrommonai.networks.utilsimportone_hotbatch_size, n_classes, h, w=7, 5, 128, 128y_pred=torch.rand(batch_size, n_classes, h, w) # network predictionsy_pred=torch.argmax(y_pred, 1, True) # convert to label mapy_pred=one_hot(y_pred, 5) # and then to one-hot# ground truth as one_hoty=torch.randint(0, 2, size=(batch_size, n_classes, h, w))
dm=DiceMetric(reduction="mean_batch")
raw_scores=dm(y_pred, y) # dice scores with shape (batch_size, n_classes)print(raw_scores)
# compute the equivalent metric valueequivalent=torch.sum(y*y_pred, axis=(2, 3)).mul(2) /torch.sum(y+y_pred, axis=(2, 3))
print(equivalent) # same as raw_scores
This shows what the computation is for a specific case with one-hot prediction and ground truth. The actual computation is done a per-item and per-channel basis here.
Activity
innat commentedon Mar 12, 2025
+1.
I would also like to know the formula used in DiceMetric. Say, if
y_truecontains sparse vector (1, 2, 3) and model gives logits, then:I tried to compare results between DiceMetric and above approach, didn't match.
ericspod commentedon Mar 12, 2025
I'm not totally sure how to demonstrate based off your code snippet, but I will be updating the comments with something like the following:
This shows what the computation is for a specific case with one-hot prediction and ground truth. The actual computation is done a per-item and per-channel basis here.
Update Dice Metric Docs (Project-MONAI#8388)