|
| 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 | +from __future__ import annotations |
| 13 | + |
| 14 | +import unittest |
| 15 | + |
| 16 | +import numpy as np |
| 17 | +import torch |
| 18 | +from parameterized import parameterized |
| 19 | + |
| 20 | +from monai.losses import MCCLoss |
| 21 | +from tests.test_utils import test_script_save |
| 22 | + |
| 23 | +TEST_CASES = [ |
| 24 | + [ # shape: (1, 1, 2, 2), (1, 1, 2, 2), sigmoid |
| 25 | + {"include_background": True, "sigmoid": True}, |
| 26 | + {"input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]]]), "target": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]])}, |
| 27 | + 0.733197, |
| 28 | + ], |
| 29 | + [ # shape: (2, 1, 2, 2), (2, 1, 2, 2), sigmoid |
| 30 | + {"include_background": True, "sigmoid": True}, |
| 31 | + { |
| 32 | + "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]], [[[1.0, -1.0], [-1.0, 1.0]]]]), |
| 33 | + "target": torch.tensor([[[[1.0, 1.0], [1.0, 1.0]]], [[[1.0, 0.0], [1.0, 0.0]]]]), |
| 34 | + }, |
| 35 | + 1.0, |
| 36 | + ], |
| 37 | + [ # shape: (1, 1, 2, 2), (1, 1, 2, 2), perfect prediction |
| 38 | + {"include_background": True}, |
| 39 | + {"input": torch.tensor([[[[1.0, 0.0], [0.0, 1.0]]]]), "target": torch.tensor([[[[1.0, 0.0], [0.0, 1.0]]]])}, |
| 40 | + 0.0, |
| 41 | + ], |
| 42 | + [ # shape: (1, 1, 2, 2), (1, 1, 2, 2), worst case (inverted) |
| 43 | + {"include_background": True}, |
| 44 | + {"input": torch.tensor([[[[0.0, 1.0], [1.0, 0.0]]]]), "target": torch.tensor([[[[1.0, 0.0], [0.0, 1.0]]]])}, |
| 45 | + 2.0, |
| 46 | + ], |
| 47 | + [ # shape: (2, 2, 3), (2, 1, 3), multi-class, exclude background, one-hot |
| 48 | + {"include_background": False, "to_onehot_y": True}, |
| 49 | + { |
| 50 | + "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]]]), |
| 51 | + "target": torch.tensor([[[0.0, 0.0, 1.0]], [[0.0, 1.0, 0.0]]]), |
| 52 | + }, |
| 53 | + 0.0, |
| 54 | + ], |
| 55 | + [ # shape: (2, 2, 3), (2, 1, 3), multi-class, sigmoid, one-hot |
| 56 | + {"include_background": True, "to_onehot_y": True, "sigmoid": True}, |
| 57 | + { |
| 58 | + "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]]]), |
| 59 | + "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| 60 | + }, |
| 61 | + 0.836617, |
| 62 | + ], |
| 63 | + [ # shape: (2, 2, 3), (2, 1, 3), multi-class, sigmoid, one-hot, batch=True |
| 64 | + {"include_background": True, "to_onehot_y": True, "sigmoid": True, "batch": True}, |
| 65 | + { |
| 66 | + "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]]]), |
| 67 | + "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| 68 | + }, |
| 69 | + 0.845961, |
| 70 | + ], |
| 71 | + [ # shape: (2, 2, 3), (2, 1, 3), multi-class, sigmoid, one-hot, reduction=sum |
| 72 | + {"include_background": True, "to_onehot_y": True, "sigmoid": True, "reduction": "sum"}, |
| 73 | + { |
| 74 | + "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]]]), |
| 75 | + "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| 76 | + }, |
| 77 | + 3.346468, |
| 78 | + ], |
| 79 | + [ # shape: (2, 2, 3), (2, 1, 3), multi-class, softmax, one-hot |
| 80 | + {"include_background": True, "to_onehot_y": True, "softmax": True}, |
| 81 | + { |
| 82 | + "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]]]), |
| 83 | + "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| 84 | + }, |
| 85 | + 0.730736, |
| 86 | + ], |
| 87 | + [ # shape: (2, 2, 3), (2, 1, 3), multi-class, softmax, one-hot, reduction=none |
| 88 | + {"include_background": True, "to_onehot_y": True, "softmax": True, "reduction": "none"}, |
| 89 | + { |
| 90 | + "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]]]), |
| 91 | + "target": torch.tensor([[[1.0, 0.0, 0.0]], [[1.0, 1.0, 0.0]]]), |
| 92 | + }, |
| 93 | + [[0.461472, 0.461472], [1.0, 1.0]], |
| 94 | + ], |
| 95 | + [ # shape: (1, 1, 3, 3), (1, 1, 3, 3), all-ones perfect prediction |
| 96 | + {"include_background": True}, |
| 97 | + {"input": torch.ones(1, 1, 3, 3), "target": torch.ones(1, 1, 3, 3)}, |
| 98 | + 0.0, |
| 99 | + ], |
| 100 | + [ # shape: (1, 1, 3, 3), (1, 1, 3, 3), all-zeros perfect prediction |
| 101 | + {"include_background": True}, |
| 102 | + {"input": torch.zeros(1, 1, 3, 3), "target": torch.zeros(1, 1, 3, 3)}, |
| 103 | + 0.0, |
| 104 | + ], |
| 105 | + [ # shape: (2, 1, 2, 2), (2, 1, 2, 2), other_act=torch.tanh |
| 106 | + {"include_background": True, "other_act": torch.tanh}, |
| 107 | + { |
| 108 | + "input": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]], [[[1.0, -1.0], [-1.0, 1.0]]]]), |
| 109 | + "target": torch.tensor([[[[1.0, 1.0], [1.0, 1.0]]], [[[1.0, 0.0], [1.0, 0.0]]]]), |
| 110 | + }, |
| 111 | + 1.0, |
| 112 | + ], |
| 113 | +] |
| 114 | + |
| 115 | + |
| 116 | +class TestMCCLoss(unittest.TestCase): |
| 117 | + @parameterized.expand(TEST_CASES) |
| 118 | + def test_shape(self, input_param, input_data, expected_val): |
| 119 | + result = MCCLoss(**input_param).forward(**input_data) |
| 120 | + np.testing.assert_allclose(result.detach().cpu().numpy(), expected_val, rtol=1e-4) |
| 121 | + |
| 122 | + def test_ill_shape(self): |
| 123 | + loss = MCCLoss() |
| 124 | + with self.assertRaisesRegex(AssertionError, ""): |
| 125 | + loss.forward(torch.ones((2, 2, 3)), torch.ones((4, 5, 6))) |
| 126 | + chn_input = torch.ones((1, 1, 3)) |
| 127 | + chn_target = torch.ones((1, 1, 3)) |
| 128 | + with self.assertRaisesRegex(ValueError, ""): |
| 129 | + MCCLoss(reduction="unknown")(chn_input, chn_target) |
| 130 | + with self.assertRaisesRegex(ValueError, ""): |
| 131 | + MCCLoss(reduction=None)(chn_input, chn_target) |
| 132 | + |
| 133 | + def test_ill_opts(self): |
| 134 | + with self.assertRaisesRegex(ValueError, ""): |
| 135 | + MCCLoss(sigmoid=True, softmax=True) |
| 136 | + with self.assertRaisesRegex(TypeError, ""): |
| 137 | + MCCLoss(other_act="tanh") |
| 138 | + |
| 139 | + @parameterized.expand([(False, False, False), (False, True, False), (False, False, True)]) |
| 140 | + def test_input_warnings(self, include_background, softmax, to_onehot_y): |
| 141 | + chn_input = torch.ones((1, 1, 3)) |
| 142 | + chn_target = torch.ones((1, 1, 3)) |
| 143 | + with self.assertWarns(Warning): |
| 144 | + loss = MCCLoss(include_background=include_background, softmax=softmax, to_onehot_y=to_onehot_y) |
| 145 | + loss.forward(chn_input, chn_target) |
| 146 | + |
| 147 | + def test_script(self): |
| 148 | + loss = MCCLoss() |
| 149 | + test_input = torch.ones(2, 1, 8, 8) |
| 150 | + test_script_save(loss, test_input, test_input) |
| 151 | + |
| 152 | + |
| 153 | +if __name__ == "__main__": |
| 154 | + unittest.main() |
0 commit comments