|
| 1 | +# LICENSE HEADER MANAGED BY add-license-header |
| 2 | +# |
| 3 | +# Copyright 2018 Kornia Team |
| 4 | +# |
| 5 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | +# you may not use this file except in compliance with the License. |
| 7 | +# You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, software |
| 12 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | +# See the License for the specific language governing permissions and |
| 15 | +# limitations under the License. |
| 16 | +# |
| 17 | + |
| 18 | +import pytest |
| 19 | +import torch |
| 20 | + |
| 21 | +import kornia |
| 22 | +from kornia.augmentation import RandomCrop, RandomCrop3D |
| 23 | + |
| 24 | +from testing.base import BaseTester |
| 25 | + |
| 26 | + |
| 27 | +class TestRandomCrop3D(BaseTester): |
| 28 | + # TODO: improve and implement more meaningful smoke tests e.g check for a consistent |
| 29 | + # return values such a torch.Tensor variable. |
| 30 | + @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.") |
| 31 | + def test_smoke(self): |
| 32 | + f = RandomCrop3D(size=(2, 3, 4), padding=(0, 1, 2), fill=10, pad_if_needed=False, p=1.0) |
| 33 | + repr = ( |
| 34 | + "RandomCrop3D(crop_size=(2, 3, 4), padding=(0, 1, 2), fill=10, pad_if_needed=False, " |
| 35 | + "padding_mode=constant, resample=BILINEAR, p=1.0, p_batch=1.0, same_on_batch=False, " |
| 36 | + "return_transform=None)" |
| 37 | + ) |
| 38 | + assert str(f) == repr |
| 39 | + |
| 40 | + @pytest.mark.parametrize("batch_size", [1, 2]) |
| 41 | + def test_no_padding(self, batch_size, device, dtype): |
| 42 | + torch.manual_seed(42) |
| 43 | + input_tensor = torch.tensor( |
| 44 | + [ |
| 45 | + [ |
| 46 | + [ |
| 47 | + [ |
| 48 | + [0.0, 1.0, 2.0, 3.0, 4.0], |
| 49 | + [5.0, 6.0, 7.0, 8.0, 9.0], |
| 50 | + [10, 11, 12, 13, 14], |
| 51 | + [15, 16, 17, 18, 19], |
| 52 | + [20, 21, 22, 23, 24], |
| 53 | + ] |
| 54 | + ] |
| 55 | + ] |
| 56 | + ], |
| 57 | + device=device, |
| 58 | + dtype=dtype, |
| 59 | + ).repeat(batch_size, 1, 5, 1, 1) |
| 60 | + f = RandomCrop3D(size=(2, 3, 4), padding=None, align_corners=True, p=1.0) |
| 61 | + out = f(input_tensor) |
| 62 | + if batch_size == 1: |
| 63 | + expected = torch.tensor( |
| 64 | + [[[[[11, 12, 13, 14], [16, 17, 18, 19], [21, 22, 23, 24]]]]], device=device, dtype=dtype |
| 65 | + ).repeat(batch_size, 1, 2, 1, 1) |
| 66 | + if batch_size == 2: |
| 67 | + expected = torch.tensor( |
| 68 | + [ |
| 69 | + [ |
| 70 | + [ |
| 71 | + [ |
| 72 | + [6.0000, 7.0000, 8.0000, 9.0000], |
| 73 | + [11.0000, 12.0000, 13.0000, 14.0000], |
| 74 | + [16.0000, 17.0000, 18.0000, 19.0000], |
| 75 | + ], |
| 76 | + [ |
| 77 | + [6.0000, 7.0000, 8.0000, 9.0000], |
| 78 | + [11.0000, 12.0000, 13.0000, 14.0000], |
| 79 | + [16.0000, 17.0000, 18.0000, 19.0000], |
| 80 | + ], |
| 81 | + ] |
| 82 | + ], |
| 83 | + [ |
| 84 | + [ |
| 85 | + [ |
| 86 | + [11.0000, 12.0000, 13.0000, 14.0000], |
| 87 | + [16.0000, 17.0000, 18.0000, 19.0000], |
| 88 | + [21.0000, 22.0000, 23.0000, 24.0000], |
| 89 | + ], |
| 90 | + [ |
| 91 | + [11.0000, 12.0000, 13.0000, 14.0000], |
| 92 | + [16.0000, 17.0000, 18.0000, 19.0000], |
| 93 | + [21.0000, 22.0000, 23.0000, 24.0000], |
| 94 | + ], |
| 95 | + ] |
| 96 | + ], |
| 97 | + ], |
| 98 | + device=device, |
| 99 | + dtype=dtype, |
| 100 | + ) |
| 101 | + |
| 102 | + self.assert_close(out, expected, atol=1e-4, rtol=1e-4) |
| 103 | + |
| 104 | + def test_same_on_batch(self, device, dtype): |
| 105 | + f = RandomCrop3D(size=(2, 3, 4), padding=None, align_corners=True, p=1.0, same_on_batch=True) |
| 106 | + input_tensor = ( |
| 107 | + torch.eye(6, device=device, dtype=dtype) |
| 108 | + .unsqueeze(dim=0) |
| 109 | + .unsqueeze(dim=0) |
| 110 | + .unsqueeze(dim=0) |
| 111 | + .repeat(2, 3, 5, 1, 1) |
| 112 | + ) |
| 113 | + res = f(input_tensor) |
| 114 | + self.assert_close(res[0], res[1]) |
| 115 | + |
| 116 | + @pytest.mark.parametrize("padding", [1, (1, 1, 1), (1, 1, 1, 1, 1, 1)]) |
| 117 | + def test_padding_batch(self, padding, device, dtype): |
| 118 | + torch.manual_seed(42) |
| 119 | + batch_size = 2 |
| 120 | + input_tensor = torch.tensor( |
| 121 | + [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]], device=device, dtype=dtype |
| 122 | + ).repeat(batch_size, 1, 3, 1, 1) |
| 123 | + expected = torch.tensor( |
| 124 | + [ |
| 125 | + [ |
| 126 | + [ |
| 127 | + [[0.0, 1.0, 2.0, 10.0], [3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0]], |
| 128 | + [[0.0, 1.0, 2.0, 10.0], [3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0]], |
| 129 | + ] |
| 130 | + ], |
| 131 | + [ |
| 132 | + [ |
| 133 | + [[3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0], [10, 10, 10, 10.0]], |
| 134 | + [[3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0], [10, 10, 10, 10.0]], |
| 135 | + ] |
| 136 | + ], |
| 137 | + ], |
| 138 | + device=device, |
| 139 | + dtype=dtype, |
| 140 | + ) |
| 141 | + f = RandomCrop3D(size=(2, 3, 4), fill=10.0, padding=padding, align_corners=True, p=1.0) |
| 142 | + out = f(input_tensor) |
| 143 | + |
| 144 | + self.assert_close(out, expected, atol=1e-4, rtol=1e-4) |
| 145 | + |
| 146 | + def test_pad_if_needed(self, device, dtype): |
| 147 | + torch.manual_seed(42) |
| 148 | + input_tensor = torch.tensor([[[0.0, 1.0, 2.0]]], device=device, dtype=dtype) |
| 149 | + expected = torch.tensor( |
| 150 | + [ |
| 151 | + [ |
| 152 | + [ |
| 153 | + [[9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0]], |
| 154 | + [[0.0, 1.0, 2.0, 9.0], [9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0]], |
| 155 | + ] |
| 156 | + ] |
| 157 | + ], |
| 158 | + device=device, |
| 159 | + dtype=dtype, |
| 160 | + ) |
| 161 | + rc = RandomCrop3D(size=(2, 3, 4), pad_if_needed=True, fill=9, align_corners=True, p=1.0) |
| 162 | + out = rc(input_tensor) |
| 163 | + |
| 164 | + self.assert_close(out, expected, atol=1e-4, rtol=1e-4) |
| 165 | + |
| 166 | + def test_gradcheck(self, device): |
| 167 | + torch.manual_seed(0) # for random reproductibility |
| 168 | + input_tensor = torch.rand((3, 3, 3), device=device, dtype=torch.float64) # 3 x 3 |
| 169 | + self.gradcheck(RandomCrop3D(size=(3, 3, 3), p=1.0), (input_tensor,)) |
| 170 | + |
| 171 | + @pytest.mark.skip("Need to fix Union type") |
| 172 | + def test_jit(self, device, dtype): |
| 173 | + # Define script |
| 174 | + op = RandomCrop(size=(3, 3), p=1.0).forward |
| 175 | + op_script = torch.jit.script(op) |
| 176 | + img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype) |
| 177 | + |
| 178 | + actual = op_script(img) |
| 179 | + expected = kornia.geometry.transform.center_crop3d(img) |
| 180 | + self.assert_close(actual, expected) |
| 181 | + |
| 182 | + @pytest.mark.skip("Need to fix Union type") |
| 183 | + def test_jit_trace(self, device, dtype): |
| 184 | + # Define script |
| 185 | + op = RandomCrop(size=(3, 3), p=1.0).forward |
| 186 | + op_script = torch.jit.script(op) |
| 187 | + # 1. Trace op |
| 188 | + img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype) |
| 189 | + |
| 190 | + op_trace = torch.jit.trace(op_script, (img,)) |
| 191 | + |
| 192 | + # 2. Generate new input |
| 193 | + img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype) |
| 194 | + |
| 195 | + # 3. Evaluate |
| 196 | + actual = op_trace(img) |
| 197 | + expected = op(img) |
| 198 | + self.assert_close(actual, expected) |
0 commit comments