Repository: Project-MONAI/MONAI

Issue:
Soft Dice Metric request
**Is your feature request related to a problem? Please describe.**
For segmentation tasks where the ground truth labels are continuous rather than binary, the DiceMetric fails. 

**Describe the solution you'd like**
Simply put, a version of DiceMetric that allows for continuous labels to be input as y, instead of just binaries.

**Describe alternatives you've considered**
This would in essence function as MONAI's dice loss, so being able to use losses as metrics would be a suitable workaround.



Discussion:
Hi @yiheng-wang-nv do you have time to help review all the metrics? ideally I think we can remove `raise ValueError("y should be a binarized tensor.")` if it's not a hard requirement by the underlying algo.?
Hi @wyli ,

For `DiceMetric`, the calculation in [here](https://github.com/Project-MONAI/MONAI/blob/dev/monai/metrics/meandice.py#L128) requires the input are binarized, otherwise the logic is wrong.

For `ConfusionMatrixMetric`, the requirement is also necessary, please check the logic [here](https://github.com/Project-MONAI/MONAI/blob/dev/monai/metrics/confusion_matrix.py#L163).


Thanks, agree that the confusion matrix would require binary inputs at the moment. Perhaps for the Dice we can change it to a warning instead?

Resolving patch:
diff --git a/monai/metrics/hausdorff_distance.py b/monai/metrics/hausdorff_distance.py
--- a/monai/metrics/hausdorff_distance.py
+++ b/monai/metrics/hausdorff_distance.py
@@ -85,7 +85,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor):  # type: ignor
         if not torch.all(y_pred.byte() == y_pred):
             warnings.warn("y_pred should be a binarized tensor.")
         if not torch.all(y.byte() == y):
-            raise ValueError("y should be a binarized tensor.")
+            warnings.warn("y should be a binarized tensor.")
         dims = y_pred.ndimension()
         if dims < 3:
             raise ValueError("y_pred should have at least three dimensions.")
diff --git a/monai/metrics/meandice.py b/monai/metrics/meandice.py
--- a/monai/metrics/meandice.py
+++ b/monai/metrics/meandice.py
@@ -72,7 +72,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor):  # type: ignor
         if not torch.all(y_pred.byte() == y_pred):
             warnings.warn("y_pred should be a binarized tensor.")
         if not torch.all(y.byte() == y):
-            raise ValueError("y should be a binarized tensor.")
+            warnings.warn("y should be a binarized tensor.")
         dims = y_pred.ndimension()
         if dims < 3:
             raise ValueError("y_pred should have at least three dimensions.")
diff --git a/monai/metrics/surface_distance.py b/monai/metrics/surface_distance.py
--- a/monai/metrics/surface_distance.py
+++ b/monai/metrics/surface_distance.py
@@ -78,7 +78,7 @@ def _compute_tensor(self, y_pred: torch.Tensor, y: torch.Tensor):  # type: ignor
         if not torch.all(y_pred.byte() == y_pred):
             warnings.warn("y_pred should be a binarized tensor.")
         if not torch.all(y.byte() == y):
-            raise ValueError("y should be a binarized tensor.")
+            warnings.warn("y should be a binarized tensor.")
         dims = y_pred.ndimension()
         if dims < 3:
             raise ValueError("y_pred should have at least three dimensions.")


Test patch: