|
| 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 torch |
| 15 | +from parameterized import parameterized |
| 16 | + |
| 17 | +from monai.transforms import SobelGradientsd |
| 18 | +from tests.utils import assert_allclose |
| 19 | + |
| 20 | +IMAGE = torch.zeros(1, 1, 16, 16, dtype=torch.float32) |
| 21 | +IMAGE[0, 0, 8, :] = 1 |
| 22 | +OUTPUT_3x3 = torch.zeros(2, 16, 16, dtype=torch.float32) |
| 23 | +OUTPUT_3x3[0, 7, :] = 2.0 |
| 24 | +OUTPUT_3x3[0, 9, :] = -2.0 |
| 25 | +OUTPUT_3x3[0, 7, 0] = OUTPUT_3x3[0, 7, -1] = 1.5 |
| 26 | +OUTPUT_3x3[0, 9, 0] = OUTPUT_3x3[0, 9, -1] = -1.5 |
| 27 | +OUTPUT_3x3[1, 7, 0] = OUTPUT_3x3[1, 9, 0] = 0.5 |
| 28 | +OUTPUT_3x3[1, 8, 0] = 1.0 |
| 29 | +OUTPUT_3x3[1, 8, -1] = -1.0 |
| 30 | +OUTPUT_3x3[1, 7, -1] = OUTPUT_3x3[1, 9, -1] = -0.5 |
| 31 | +OUTPUT_3x3 = OUTPUT_3x3.unsqueeze(1) |
| 32 | + |
| 33 | +TEST_CASE_0 = [{"image": IMAGE}, {"keys": "image", "kernel_size": 3, "dtype": torch.float32}, {"image": OUTPUT_3x3}] |
| 34 | +TEST_CASE_1 = [{"image": IMAGE}, {"keys": "image", "kernel_size": 3, "dtype": torch.float64}, {"image": OUTPUT_3x3}] |
| 35 | +TEST_CASE_2 = [ |
| 36 | + {"image": IMAGE}, |
| 37 | + {"keys": "image", "kernel_size": 3, "dtype": torch.float32, "new_key_prefix": "sobel_"}, |
| 38 | + {"sobel_image": OUTPUT_3x3}, |
| 39 | +] |
| 40 | + |
| 41 | +TEST_CASE_KERNEL_0 = [ |
| 42 | + {"keys": "image", "kernel_size": 3, "dtype": torch.float64}, |
| 43 | + torch.tensor([[-0.5, 0.0, 0.5], [-1.0, 0.0, 1.0], [-0.5, 0.0, 0.5]], dtype=torch.float64), |
| 44 | +] |
| 45 | +TEST_CASE_KERNEL_1 = [ |
| 46 | + {"keys": "image", "kernel_size": 5, "dtype": torch.float64}, |
| 47 | + torch.tensor( |
| 48 | + [ |
| 49 | + [-0.25, -0.2, 0.0, 0.2, 0.25], |
| 50 | + [-0.4, -0.5, 0.0, 0.5, 0.4], |
| 51 | + [-0.5, -1.0, 0.0, 1.0, 0.5], |
| 52 | + [-0.4, -0.5, 0.0, 0.5, 0.4], |
| 53 | + [-0.25, -0.2, 0.0, 0.2, 0.25], |
| 54 | + ], |
| 55 | + dtype=torch.float64, |
| 56 | + ), |
| 57 | +] |
| 58 | +TEST_CASE_KERNEL_2 = [ |
| 59 | + {"keys": "image", "kernel_size": 7, "dtype": torch.float64}, |
| 60 | + torch.tensor( |
| 61 | + [ |
| 62 | + [-3.0 / 18.0, -2.0 / 13.0, -1.0 / 10.0, 0.0, 1.0 / 10.0, 2.0 / 13.0, 3.0 / 18.0], |
| 63 | + [-3.0 / 13.0, -2.0 / 8.0, -1.0 / 5.0, 0.0, 1.0 / 5.0, 2.0 / 8.0, 3.0 / 13.0], |
| 64 | + [-3.0 / 10.0, -2.0 / 5.0, -1.0 / 2.0, 0.0, 1.0 / 2.0, 2.0 / 5.0, 3.0 / 10.0], |
| 65 | + [-3.0 / 9.0, -2.0 / 4.0, -1.0 / 1.0, 0.0, 1.0 / 1.0, 2.0 / 4.0, 3.0 / 9.0], |
| 66 | + [-3.0 / 10.0, -2.0 / 5.0, -1.0 / 2.0, 0.0, 1.0 / 2.0, 2.0 / 5.0, 3.0 / 10.0], |
| 67 | + [-3.0 / 13.0, -2.0 / 8.0, -1.0 / 5.0, 0.0, 1.0 / 5.0, 2.0 / 8.0, 3.0 / 13.0], |
| 68 | + [-3.0 / 18.0, -2.0 / 13.0, -1.0 / 10.0, 0.0, 1.0 / 10.0, 2.0 / 13.0, 3.0 / 18.0], |
| 69 | + ], |
| 70 | + dtype=torch.float64, |
| 71 | + ), |
| 72 | +] |
| 73 | +TEST_CASE_ERROR_0 = [{"keys": "image", "kernel_size": 2, "dtype": torch.float32}] |
| 74 | + |
| 75 | + |
| 76 | +class SobelGradientTests(unittest.TestCase): |
| 77 | + backend = None |
| 78 | + |
| 79 | + @parameterized.expand([TEST_CASE_0]) |
| 80 | + def test_sobel_gradients(self, image_dict, arguments, expected_grad): |
| 81 | + sobel = SobelGradientsd(**arguments) |
| 82 | + grad = sobel(image_dict) |
| 83 | + key = "image" if "new_key_prefix" not in arguments else arguments["new_key_prefix"] + arguments["keys"] |
| 84 | + assert_allclose(grad[key], expected_grad[key]) |
| 85 | + |
| 86 | + @parameterized.expand([TEST_CASE_KERNEL_0, TEST_CASE_KERNEL_1, TEST_CASE_KERNEL_2]) |
| 87 | + def test_sobel_kernels(self, arguments, expected_kernel): |
| 88 | + sobel = SobelGradientsd(**arguments) |
| 89 | + self.assertTrue(sobel.transform.kernel.dtype == expected_kernel.dtype) |
| 90 | + assert_allclose(sobel.transform.kernel, expected_kernel) |
| 91 | + |
| 92 | + @parameterized.expand([TEST_CASE_ERROR_0]) |
| 93 | + def test_sobel_gradients_error(self, arguments): |
| 94 | + with self.assertRaises(ValueError): |
| 95 | + SobelGradientsd(**arguments) |
| 96 | + |
| 97 | + |
| 98 | +if __name__ == "__main__": |
| 99 | + unittest.main() |
0 commit comments