| |
| |
| |
| |
| |
| |
| |
| |
| |
| |
|
|
| import unittest |
|
|
| import numpy as np |
| import torch |
| from parameterized import parameterized |
|
|
| from monai.losses import DiceLoss |
|
|
| TEST_CASES = [ |
| [ |
| {"include_background": True, "sigmoid": True}, |
| { |
| "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]]]), |
| "target": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]), |
| "smooth": 1e-6, |
| }, |
| 0.307576, |
| ], |
| [ |
| {"include_background": True, "sigmoid": True}, |
| { |
| "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]], [[[1.0, -1.0], [-1.0, 1.0]]]]), |
| "target": torch.tensor([[[[1.0, 1.0], [1.0, 1.0]]], [[[1.0, 0.0], [1.0, 0.0]]]]), |
| "smooth": 1e-4, |
| }, |
| 0.416657, |
| ], |
| [ |
| {"include_background": False, "to_onehot_y": True}, |
| { |
| "input": torch.tensor([[[1.0, 1.0, 0.0], [0.0, 0.0, 1.0]], [[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]), |
| "target": torch.tensor([[[0.0, 0.0, 1.0]], [[0.0, 1.0, 0.0]]]), |
| "smooth": 0.0, |
| }, |
| 0.0, |
| ], |
| [ |
| {"include_background": True, "to_onehot_y": True, "sigmoid": True}, |
| { |
| "input": torch.tensor([[[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]), |
| "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| "smooth": 1e-4, |
| }, |
| 0.435050, |
| ], |
| [ |
| {"include_background": True, "to_onehot_y": True, "sigmoid": True, "reduction": "none"}, |
| { |
| "input": torch.tensor([[[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]), |
| "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| "smooth": 1e-4, |
| }, |
| [[0.296529, 0.415136], [0.599976, 0.428559]], |
| ], |
| [ |
| {"include_background": True, "to_onehot_y": True, "softmax": True}, |
| { |
| "input": torch.tensor([[[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]), |
| "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| "smooth": 1e-4, |
| }, |
| 0.383713, |
| ], |
| [ |
| {"include_background": True, "to_onehot_y": True, "softmax": True, "reduction": "sum"}, |
| { |
| "input": torch.tensor([[[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]), |
| "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| "smooth": 1e-4, |
| }, |
| 1.534853, |
| ], |
| [ |
| {"include_background": True, "sigmoid": True}, |
| { |
| "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]]]), |
| "target": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]), |
| "smooth": 1e-6, |
| }, |
| 0.307576, |
| ], |
| [ |
| {"include_background": True, "sigmoid": True, "squared_pred": True}, |
| { |
| "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]]]), |
| "target": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]), |
| "smooth": 1e-5, |
| }, |
| 0.178337, |
| ], |
| [ |
| {"include_background": True, "sigmoid": True, "jaccard": True}, |
| { |
| "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]]]), |
| "target": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]), |
| "smooth": 1e-5, |
| }, |
| 0.470451, |
| ], |
| [ |
| {"include_background": True, "other_act": torch.tanh}, |
| { |
| "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]], [[[1.0, -1.0], [-1.0, 1.0]]]]), |
| "target": torch.tensor([[[[1.0, 1.0], [1.0, 1.0]]], [[[1.0, 0.0], [1.0, 0.0]]]]), |
| "smooth": 1e-4, |
| }, |
| 0.999963, |
| ], |
| [ |
| {"include_background": True, "to_onehot_y": True, "other_act": lambda x: torch.log_softmax(x, dim=1)}, |
| { |
| "input": torch.tensor([[[-1.0, 0.0, 1.0], [1.0, 0.0, -1.0]], [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]), |
| "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| "smooth": 1e-4, |
| }, |
| -8.522593, |
| ], |
| ] |
|
|
|
|
| class TestDiceLoss(unittest.TestCase): |
| @parameterized.expand(TEST_CASES) |
| def test_shape(self, input_param, input_data, expected_val): |
| result = DiceLoss(**input_param).forward(**input_data) |
| np.testing.assert_allclose(result.detach().cpu().numpy(), expected_val, rtol=1e-5) |
|
|
| def test_ill_shape(self): |
| loss = DiceLoss() |
| with self.assertRaisesRegex(AssertionError, ""): |
| loss.forward(torch.ones((1, 2, 3)), torch.ones((4, 5, 6))) |
|
|
| def test_ill_opts(self): |
| with self.assertRaisesRegex(ValueError, ""): |
| DiceLoss(sigmoid=True, softmax=True) |
| chn_input = torch.ones((1, 1, 3)) |
| chn_target = torch.ones((1, 1, 3)) |
| with self.assertRaisesRegex(ValueError, ""): |
| DiceLoss(reduction="unknown")(chn_input, chn_target) |
| with self.assertRaisesRegex(ValueError, ""): |
| DiceLoss(reduction=None)(chn_input, chn_target) |
|
|
| def test_input_warnings(self): |
| chn_input = torch.ones((1, 1, 3)) |
| chn_target = torch.ones((1, 1, 3)) |
| with self.assertWarns(Warning): |
| loss = DiceLoss(include_background=False) |
| loss.forward(chn_input, chn_target) |
| with self.assertWarns(Warning): |
| loss = DiceLoss(softmax=True) |
| loss.forward(chn_input, chn_target) |
| with self.assertWarns(Warning): |
| loss = DiceLoss(to_onehot_y=True) |
| loss.forward(chn_input, chn_target) |
|
|
|
|
| if __name__ == "__main__": |
| unittest.main() |
|
|