|
| 1 | +# Copyright (c) MONAI Consortium |
| 2 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | +# you may not use this file except in compliance with the License. |
| 4 | +# You may obtain a copy of the License at |
| 5 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | +# Unless required by applicable law or agreed to in writing, software |
| 7 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 8 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 9 | +# See the License for the specific language governing permissions and |
| 10 | +# limitations under the License. |
| 11 | + |
| 12 | +import unittest |
| 13 | + |
| 14 | +import numpy as np |
| 15 | +import torch |
| 16 | +from parameterized import parameterized |
| 17 | + |
| 18 | +from monai.losses import DeepSupervisionLoss, DiceCELoss, DiceFocalLoss, DiceLoss |
| 19 | +from tests.utils import SkipIfBeforePyTorchVersion, test_script_save |
| 20 | + |
| 21 | +TEST_CASES_DICECE = [ |
| 22 | + [ |
| 23 | + {"to_onehot_y": True}, |
| 24 | + {}, |
| 25 | + { |
| 26 | + "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]]]]), |
| 27 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 28 | + }, |
| 29 | + 0.606557, |
| 30 | + ] |
| 31 | +] |
| 32 | + |
| 33 | +TEST_CASES_DICECE2 = [ |
| 34 | + [ |
| 35 | + {"to_onehot_y": True}, |
| 36 | + {}, |
| 37 | + { |
| 38 | + "input": [ |
| 39 | + 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]]]]), |
| 40 | + torch.tensor([[[[1.0, 1.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]]]]), |
| 41 | + torch.tensor([[[[1.0], [0.0]], [[1.0], [0.0]]]]), |
| 42 | + ], |
| 43 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 44 | + }, |
| 45 | + 1.78144, |
| 46 | + ], |
| 47 | + [ |
| 48 | + {"to_onehot_y": True}, |
| 49 | + {"weight_mode": "same"}, |
| 50 | + { |
| 51 | + "input": [ |
| 52 | + 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]]]]), |
| 53 | + torch.tensor([[[[1.0, 1.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]]]]), |
| 54 | + torch.tensor([[[[1.0], [0.0]], [[1.0], [0.0]]]]), |
| 55 | + ], |
| 56 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 57 | + }, |
| 58 | + 3.5529, |
| 59 | + ], |
| 60 | + [ |
| 61 | + {"to_onehot_y": True}, |
| 62 | + {"weight_mode": "two"}, |
| 63 | + { |
| 64 | + "input": [ |
| 65 | + 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]]]]), |
| 66 | + torch.tensor([[[[1.0, 1.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]]]]), |
| 67 | + torch.tensor([[[[1.0], [0.0]], [[1.0], [0.0]]]]), |
| 68 | + ], |
| 69 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 70 | + }, |
| 71 | + 2.07973, |
| 72 | + ], |
| 73 | + [ |
| 74 | + {"to_onehot_y": True}, |
| 75 | + {"weights": [0.1, 0.2, 0.3]}, |
| 76 | + { |
| 77 | + "input": [ |
| 78 | + 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]]]]), |
| 79 | + torch.tensor([[[[1.0, 1.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]]]]), |
| 80 | + torch.tensor([[[[1.0], [0.0]], [[1.0], [0.0]]]]), |
| 81 | + ], |
| 82 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 83 | + }, |
| 84 | + 0.76924, |
| 85 | + ], |
| 86 | +] |
| 87 | + |
| 88 | + |
| 89 | +TEST_CASES_DICE = [ |
| 90 | + [ |
| 91 | + {"to_onehot_y": True}, |
| 92 | + { |
| 93 | + "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]]]]), |
| 94 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 95 | + }, |
| 96 | + 0.166666, # the result equals to -1 + np.log(1 + np.exp(1)) |
| 97 | + ], |
| 98 | + [ |
| 99 | + {"to_onehot_y": True}, |
| 100 | + { |
| 101 | + "input": [ |
| 102 | + 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]]]]), |
| 103 | + torch.tensor([[[[1.0, 1.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]]]]), |
| 104 | + torch.tensor([[[[1.0], [0.0]], [[1.0], [0.0]]]]), |
| 105 | + ], |
| 106 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 107 | + }, |
| 108 | + 0.666665, |
| 109 | + ], |
| 110 | +] |
| 111 | + |
| 112 | +TEST_CASES_DICEFOCAL = [ |
| 113 | + [ |
| 114 | + {"to_onehot_y": True}, |
| 115 | + { |
| 116 | + "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]]]]), |
| 117 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 118 | + }, |
| 119 | + 0.32124, # the result equals to -1 + np.log(1 + np.exp(1)) |
| 120 | + ], |
| 121 | + [ |
| 122 | + {"to_onehot_y": True}, |
| 123 | + { |
| 124 | + "input": [ |
| 125 | + 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]]]]), |
| 126 | + torch.tensor([[[[1.0, 1.0], [0.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]]]]), |
| 127 | + torch.tensor([[[[1.0], [0.0]], [[1.0], [0.0]]]]), |
| 128 | + ], |
| 129 | + "target": torch.tensor([[[[1.0, 0.0, 1.0], [0.0, 1.0, 0.0]]]]), |
| 130 | + }, |
| 131 | + 1.06452, |
| 132 | + ], |
| 133 | +] |
| 134 | + |
| 135 | + |
| 136 | +class TestDSLossDiceCE(unittest.TestCase): |
| 137 | + @parameterized.expand(TEST_CASES_DICECE) |
| 138 | + def test_result(self, input_param, input_param2, input_data, expected_val): |
| 139 | + diceceloss = DeepSupervisionLoss(DiceCELoss(**input_param), **input_param2) |
| 140 | + result = diceceloss(**input_data) |
| 141 | + np.testing.assert_allclose(result.detach().cpu().numpy(), expected_val, atol=1e-4, rtol=1e-4) |
| 142 | + |
| 143 | + def test_ill_shape(self): |
| 144 | + loss = DeepSupervisionLoss(DiceCELoss()) |
| 145 | + with self.assertRaisesRegex(ValueError, ""): |
| 146 | + loss(torch.ones((1, 2, 3)), torch.ones((1, 1, 2, 3))) |
| 147 | + |
| 148 | + def test_ill_reduction(self): |
| 149 | + with self.assertRaisesRegex(ValueError, ""): |
| 150 | + loss = DeepSupervisionLoss(DiceCELoss(reduction="none")) |
| 151 | + loss(torch.ones((1, 2, 3)), torch.ones((1, 1, 2, 3))) |
| 152 | + |
| 153 | + @SkipIfBeforePyTorchVersion((1, 10)) |
| 154 | + def test_script(self): |
| 155 | + loss = DeepSupervisionLoss(DiceCELoss()) |
| 156 | + test_input = torch.ones(2, 1, 8, 8) |
| 157 | + test_script_save(loss, test_input, test_input) |
| 158 | + |
| 159 | + |
| 160 | +@SkipIfBeforePyTorchVersion((1, 11)) |
| 161 | +class TestDSLossDiceCE2(unittest.TestCase): |
| 162 | + @parameterized.expand(TEST_CASES_DICECE2) |
| 163 | + def test_result(self, input_param, input_param2, input_data, expected_val): |
| 164 | + diceceloss = DeepSupervisionLoss(DiceCELoss(**input_param), **input_param2) |
| 165 | + result = diceceloss(**input_data) |
| 166 | + np.testing.assert_allclose(result.detach().cpu().numpy(), expected_val, atol=1e-4, rtol=1e-4) |
| 167 | + |
| 168 | + |
| 169 | +@SkipIfBeforePyTorchVersion((1, 11)) |
| 170 | +class TestDSLossDice(unittest.TestCase): |
| 171 | + @parameterized.expand(TEST_CASES_DICE) |
| 172 | + def test_result(self, input_param, input_data, expected_val): |
| 173 | + loss = DeepSupervisionLoss(DiceLoss(**input_param)) |
| 174 | + result = loss(**input_data) |
| 175 | + np.testing.assert_allclose(result.detach().cpu().numpy(), expected_val, atol=1e-4, rtol=1e-4) |
| 176 | + |
| 177 | + |
| 178 | +@SkipIfBeforePyTorchVersion((1, 11)) |
| 179 | +class TestDSLossDiceFocal(unittest.TestCase): |
| 180 | + @parameterized.expand(TEST_CASES_DICEFOCAL) |
| 181 | + def test_result(self, input_param, input_data, expected_val): |
| 182 | + loss = DeepSupervisionLoss(DiceFocalLoss(**input_param)) |
| 183 | + result = loss(**input_data) |
| 184 | + np.testing.assert_allclose(result.detach().cpu().numpy(), expected_val, atol=1e-4, rtol=1e-4) |
| 185 | + |
| 186 | + |
| 187 | +if __name__ == "__main__": |
| 188 | + unittest.main() |
0 commit comments