File size: 6,519 Bytes
e4b9a7b | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | # Copyright 2020 MONAI Consortium
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
# http://www.apache.org/licenses/LICENSE-2.0
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import unittest
import numpy as np
import torch
from parameterized import parameterized
from monai.metrics import DiceMetric, compute_meandice
# keep background
TEST_CASE_1 = [ # y (1, 1, 2, 2), y_pred (1, 1, 2, 2), expected out (1, 1)
{
"y_pred": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]]]),
"y": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]),
"include_background": True,
"to_onehot_y": False,
"mutually_exclusive": False,
"logit_thresh": 0.5,
"sigmoid": True,
},
[[0.8]],
]
# remove background and not One-Hot target
TEST_CASE_2 = [ # y (2, 1, 2, 2), y_pred (2, 3, 2, 2), expected out (2, 2) (no background)
{
"y_pred": torch.tensor(
[
[[[-1.0, 3.0], [2.0, -4.0]], [[0.0, -1.0], [3.0, 2.0]], [[0.0, 1.0], [2.0, -1.0]]],
[[[-2.0, 0.0], [3.0, 1.0]], [[0.0, 2.0], [1.0, -2.0]], [[-1.0, 2.0], [4.0, 0.0]]],
]
),
"y": torch.tensor([[[[1.0, 2.0], [1.0, 0.0]]], [[[1.0, 1.0], [2.0, 0.0]]]]),
"include_background": False,
"to_onehot_y": True,
"mutually_exclusive": True,
},
[[0.5000, 0.0000], [0.6666, 0.6666]],
]
# should return Nan for all labels=0 case and skip for MeanDice
TEST_CASE_3 = [
{
"y_pred": torch.zeros(2, 3, 2, 2),
"y": torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]], [[[1.0, 0.0], [0.0, 1.0]]]]),
"include_background": True,
"to_onehot_y": True,
"mutually_exclusive": True,
},
[[False, True, True], [False, False, True]],
]
TEST_CASE_4 = [
{"include_background": True, "to_onehot_y": True, "reduction": "mean_batch"},
{
"y_pred": torch.tensor(
[
[[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]],
[[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]],
]
),
"y": torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]], [[[1.0, 1.0], [2.0, 0.0]]]]),
},
[0.6786, 0.4000, 0.6667],
]
TEST_CASE_5 = [
{"include_background": True, "to_onehot_y": True, "reduction": "mean"},
{
"y_pred": torch.tensor(
[
[[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]],
[[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]],
]
),
"y": torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]], [[[1.0, 1.0], [2.0, 0.0]]]]),
},
0.689683,
]
TEST_CASE_6 = [
{"include_background": True, "to_onehot_y": True, "reduction": "sum_batch"},
{
"y_pred": torch.tensor(
[
[[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]],
[[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]],
]
),
"y": torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]], [[[0.0, 0.0], [0.0, 0.0]]]]),
},
[1.7143, 0.0000, 0.0000],
]
TEST_CASE_7 = [
{"include_background": True, "to_onehot_y": True, "reduction": "mean"},
{
"y_pred": torch.tensor(
[
[[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]],
[[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]],
]
),
"y": torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]], [[[0.0, 0.0], [0.0, 0.0]]]]),
},
0.857143,
]
TEST_CASE_8 = [
{"to_onehot_y": True, "include_background": False, "reduction": "sum_batch"},
{
"y_pred": torch.tensor(
[
[[[1.0, 1.0], [1.0, 0.0]], [[0.0, 1.0], [0.0, 0.0]], [[0.0, 1.0], [1.0, 1.0]]],
[[[1.0, 0.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 1.0]], [[0.0, 1.0], [1.0, 0.0]]],
]
),
"y": torch.tensor([[[[0.0, 0.0], [0.0, 0.0]]], [[[0.0, 0.0], [0.0, 0.0]]]]),
},
[0.0000, 0.0000],
]
TEST_CASE_9 = [
{"y": torch.from_numpy(np.ones((2, 2, 3, 3))), "y_pred": torch.from_numpy(np.ones((2, 2, 3, 3)))},
[[1.0000, 1.0000], [1.0000, 1.0000]],
]
TEST_CASE_10 = [ # y (1, 1, 2, 2), y_pred (1, 1, 2, 2), expected out (1, 1)
{
"y_pred": torch.tensor([[[[1.0, -1.0], [-1.0, 1.0]]]]),
"y": torch.tensor([[[[1.0, 0.0], [1.0, 1.0]]]]),
"include_background": True,
"to_onehot_y": False,
"mutually_exclusive": False,
"logit_thresh": 0.0,
"other_act": torch.tanh,
},
[[0.8]],
]
class TestComputeMeanDice(unittest.TestCase):
@parameterized.expand([TEST_CASE_1, TEST_CASE_2, TEST_CASE_9, TEST_CASE_10])
def test_value(self, input_data, expected_value):
result = compute_meandice(**input_data)
np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4)
@parameterized.expand([TEST_CASE_3])
def test_nans(self, input_data, expected_value):
result = compute_meandice(**input_data)
self.assertTrue(np.allclose(np.isnan(result.cpu().numpy()), expected_value))
# DiceMetric class tests
@parameterized.expand([TEST_CASE_1, TEST_CASE_2])
def test_value_class(self, input_data, expected_value):
# same test as for compute_meandice
vals = dict()
vals["y_pred"] = input_data.pop("y_pred")
vals["y"] = input_data.pop("y")
dice_metric = DiceMetric(**input_data, reduction="none")
result = dice_metric(**vals)
np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4)
@parameterized.expand([TEST_CASE_4, TEST_CASE_5, TEST_CASE_6, TEST_CASE_7, TEST_CASE_8])
def test_nans_class(self, params, input_data, expected_value):
dice_metric = DiceMetric(**params)
result = dice_metric(**input_data)
np.testing.assert_allclose(result.cpu().numpy(), expected_value, atol=1e-4)
if __name__ == "__main__":
unittest.main()
|