|
| 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.metrics import MeanIoU, compute_meaniou |
| 19 | + |
| 20 | +# keep background |
| 21 | +TEST_CASE_1 = [ # y (1, 1, 2, 2), y_pred (1, 1, 2, 2), expected out (1, 1) |
| 22 | + { |
| 23 | + "y_pred": torch.tensor([[[[1.0, 0.0], [0.0, 1.0]]]]), |
| 24 | + "y": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]), |
| 25 | + "include_background": True, |
| 26 | + }, |
| 27 | + [[0.6667]], |
| 28 | +] |
| 29 | + |
| 30 | +# remove background and not One-Hot target |
| 31 | +TEST_CASE_2 = [ # y (2, 3, 2, 2), y_pred (2, 3, 2, 2), expected out (2, 2) (no background) |
| 32 | + { |
| 33 | + "y_pred": torch.tensor( |
| 34 | + [ |
| 35 | + [[[0.0, 1.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 1.0]], [[1.0, 0.0], [0.0, 0.0]]], |
| 36 | + [[[0.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 0.0]]], |
| 37 | + ] |
| 38 | + ), |
| 39 | + "y": torch.tensor( |
| 40 | + [ |
| 41 | + [[[0.0, 0.0], [0.0, 1.0]], [[1.0, 0.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]]], |
| 42 | + [[[0.0, 0.0], [0.0, 1.0]], [[1.0, 1.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], |
| 43 | + ] |
| 44 | + ), |
| 45 | + "include_background": False, |
| 46 | + }, |
| 47 | + [[0.3333, 0.0000], [0.5000, 0.5000]], |
| 48 | +] |
| 49 | + |
| 50 | +# should return Nan for all labels=0 case and skip for MeanIoU |
| 51 | +TEST_CASE_3 = [ |
| 52 | + { |
| 53 | + "y_pred": torch.tensor( |
| 54 | + [ |
| 55 | + [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]], [[1.0, 1.0], [1.0, 1.0]]], |
| 56 | + [[[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]], [[1.0, 1.0], [1.0, 1.0]]], |
| 57 | + ] |
| 58 | + ), |
| 59 | + "y": torch.tensor( |
| 60 | + [ |
| 61 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 62 | + [[[0.0, 1.0], [1.0, 0.0]], [[1.0, 0.0], [0.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 63 | + ] |
| 64 | + ), |
| 65 | + "include_background": True, |
| 66 | + }, |
| 67 | + [[False, True, True], [False, False, True]], |
| 68 | +] |
| 69 | + |
| 70 | +TEST_CASE_4 = [ |
| 71 | + {"include_background": True, "reduction": "mean_batch", "get_not_nans": True}, |
| 72 | + { |
| 73 | + "y_pred": torch.tensor( |
| 74 | + [ |
| 75 | + [[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]], |
| 76 | + [[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]], |
| 77 | + ] |
| 78 | + ), |
| 79 | + "y": torch.tensor( |
| 80 | + [ |
| 81 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 82 | + [[[0.0, 0.0], [0.0, 1.0]], [[1.0, 1.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], |
| 83 | + ] |
| 84 | + ), |
| 85 | + }, |
| 86 | + [0.5416, 0.2500, 0.5000], |
| 87 | +] |
| 88 | + |
| 89 | +TEST_CASE_5 = [ |
| 90 | + {"include_background": True, "reduction": "mean", "get_not_nans": True}, |
| 91 | + { |
| 92 | + "y_pred": torch.tensor( |
| 93 | + [ |
| 94 | + [[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]], |
| 95 | + [[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]], |
| 96 | + ] |
| 97 | + ), |
| 98 | + "y": torch.tensor( |
| 99 | + [ |
| 100 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 101 | + [[[0.0, 0.0], [0.0, 1.0]], [[1.0, 1.0], [0.0, 0.0]], [[0.0, 0.0], [1.0, 0.0]]], |
| 102 | + ] |
| 103 | + ), |
| 104 | + }, |
| 105 | + 0.5555, |
| 106 | +] |
| 107 | + |
| 108 | +TEST_CASE_6 = [ |
| 109 | + {"include_background": True, "reduction": "sum_batch", "get_not_nans": True}, |
| 110 | + { |
| 111 | + "y_pred": torch.tensor( |
| 112 | + [ |
| 113 | + [[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]], |
| 114 | + [[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]], |
| 115 | + ] |
| 116 | + ), |
| 117 | + "y": torch.tensor( |
| 118 | + [ |
| 119 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 120 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 121 | + ] |
| 122 | + ), |
| 123 | + }, |
| 124 | + [1.5000, 0.0000, 0.0000], |
| 125 | +] |
| 126 | + |
| 127 | +TEST_CASE_7 = [ |
| 128 | + {"include_background": True, "reduction": "mean", "get_not_nans": True}, |
| 129 | + { |
| 130 | + "y_pred": torch.tensor( |
| 131 | + [ |
| 132 | + [[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]], |
| 133 | + [[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]], |
| 134 | + ] |
| 135 | + ), |
| 136 | + "y": torch.tensor( |
| 137 | + [ |
| 138 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 139 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 140 | + ] |
| 141 | + ), |
| 142 | + }, |
| 143 | + 0.7500, |
| 144 | +] |
| 145 | + |
| 146 | +TEST_CASE_8 = [ |
| 147 | + {"include_background": False, "reduction": "sum_batch", "get_not_nans": True}, |
| 148 | + { |
| 149 | + "y_pred": torch.tensor( |
| 150 | + [ |
| 151 | + [[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]], |
| 152 | + [[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]], |
| 153 | + ] |
| 154 | + ), |
| 155 | + "y": torch.tensor( |
| 156 | + [ |
| 157 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 158 | + [[[1.0, 1.0], [1.0, 1.0]], [[0.0, 0.0], [0.0, 0.0]], [[0.0, 0.0], [0.0, 0.0]]], |
| 159 | + ] |
| 160 | + ), |
| 161 | + }, |
| 162 | + [0.0000, 0.0000], |
| 163 | +] |
| 164 | + |
| 165 | +TEST_CASE_9 = [ |
| 166 | + {"y": torch.ones((2, 2, 3, 3)), "y_pred": torch.ones((2, 2, 3, 3))}, |
| 167 | + [[1.0000, 1.0000], [1.0000, 1.0000]], |
| 168 | +] |
| 169 | + |
| 170 | +TEST_CASE_10 = [ |
| 171 | + {"y": [torch.ones((2, 3, 3)), torch.ones((2, 3, 3))], "y_pred": [torch.ones((2, 3, 3)), torch.ones((2, 3, 3))]}, |
| 172 | + [[1.0000, 1.0000], [1.0000, 1.0000]], |
| 173 | +] |
| 174 | + |
| 175 | +TEST_CASE_11 = [ |
| 176 | + {"y": torch.zeros((2, 2, 3, 3)), "y_pred": torch.zeros((2, 2, 3, 3)), "ignore_empty": False}, |
| 177 | + [[1.0000, 1.0000], [1.0000, 1.0000]], |
| 178 | +] |
| 179 | + |
| 180 | +TEST_CASE_12 = [ |
| 181 | + {"y": torch.zeros((2, 2, 3, 3)), "y_pred": torch.ones((2, 2, 3, 3)), "ignore_empty": False}, |
| 182 | + [[0.0000, 0.0000], [0.0000, 0.0000]], |
| 183 | +] |
| 184 | + |
| 185 | + |
| 186 | +class TestComputeMeanIoU(unittest.TestCase): |
| 187 | + @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_9, TEST_CASE_11, TEST_CASE_12]) |
| 188 | + def test_value(self, input_data, expected_value): |
| 189 | + result = compute_meaniou(**input_data) |
| 190 | + np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4) |
| 191 | + |
| 192 | + @parameterized.expand([TEST_CASE_3]) |
| 193 | + def test_nans(self, input_data, expected_value): |
| 194 | + result = compute_meaniou(**input_data) |
| 195 | + self.assertTrue(np.allclose(np.isnan(result.cpu().numpy()), expected_value)) |
| 196 | + |
| 197 | + # MeanIoU class tests |
| 198 | + @parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_10]) |
| 199 | + def test_value_class(self, input_data, expected_value): |
| 200 | + |
| 201 | + # same test as for compute_meaniou |
| 202 | + vals = {} |
| 203 | + vals["y_pred"] = input_data.pop("y_pred") |
| 204 | + vals["y"] = input_data.pop("y") |
| 205 | + iou_metric = MeanIoU(**input_data) |
| 206 | + iou_metric(**vals) |
| 207 | + result = iou_metric.aggregate(reduction="none") |
| 208 | + np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4) |
| 209 | + |
| 210 | + @parameterized.expand([TEST_CASE_4, TEST_CASE_5, TEST_CASE_6, TEST_CASE_7, TEST_CASE_8]) |
| 211 | + def test_nans_class(self, params, input_data, expected_value): |
| 212 | + |
| 213 | + iou_metric = MeanIoU(**params) |
| 214 | + iou_metric(**input_data) |
| 215 | + result, _ = iou_metric.aggregate() |
| 216 | + np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4) |
| 217 | + |
| 218 | + |
| 219 | +if __name__ == "__main__": |
| 220 | + unittest.main() |
0 commit comments