Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Splitting the tests in test_augmentation_3d.py and test_container.py into individual test files. (#3143)

abdulrahman_riyadb0cf376f12dc
diff --git a/testing/augmentation/utils.py b/testing/augmentation/utils.py
new file mode 100644
index 000000000..e9e64170c
--- /dev/null
+++ b/testing/augmentation/utils.py
@@ -0,0 +1,45 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 torch
+
+from testing.base import assert_close
+
+
+def reproducibility_test(input, seq):
+ """Any tests failed here indicate the output cannot be reproduced by the same params."""
+ if isinstance(input, (tuple, list)):
+ output_1 = seq(*input)
+ output_2 = seq(*input, params=seq._params)
+ else:
+ output_1 = seq(input)
+ output_2 = seq(input, params=seq._params)
+
+ if isinstance(output_1, (tuple, list)) and isinstance(output_2, (tuple, list)):
+ [
+ assert_close(o1, o2)
+ for o1, o2 in zip(output_1, output_2)
+ if isinstance(o1, (torch.Tensor,)) and isinstance(o2, (torch.Tensor,))
+ ]
+ elif isinstance(output_1, (tuple, list)) and isinstance(output_2, (torch.Tensor,)):
+ assert_close(output_1[0], output_2)
+ elif isinstance(output_2, (tuple, list)) and isinstance(output_1, (torch.Tensor,)):
+ assert_close(output_1, output_2[0])
+ elif isinstance(output_2, (torch.Tensor,)) and isinstance(output_1, (torch.Tensor,)):
+ assert_close(output_1, output_2, msg=f"{seq._params}")
+ else:
+ raise AssertionError(f"cannot compare {type(output_1)} and {type(output_2)}")
diff --git a/tests/augmentation/_3d/test_center_crop_3d.py b/tests/augmentation/_3d/test_center_crop_3d.py
new file mode 100644
index 000000000..63dc6a428
--- /dev/null
+++ b/tests/augmentation/_3d/test_center_crop_3d.py
@@ -0,0 +1,45 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 torch
+
+from kornia.augmentation import CenterCrop3D
+
+from testing.base import BaseTester
+
+
+class TestCenterCrop3D(BaseTester):
+ def test_no_transform(self, device, dtype):
+ inp = torch.rand(1, 2, 4, 4, 4, device=device, dtype=dtype)
+ out = CenterCrop3D(2)(inp)
+ assert out.shape == (1, 2, 2, 2, 2)
+
+ def test_transform(self, device, dtype):
+ inp = torch.rand(1, 2, 5, 4, 8, device=device, dtype=dtype)
+ aug = CenterCrop3D(2)
+ out = aug(inp)
+ assert out.shape == (1, 2, 2, 2, 2)
+ assert aug.transform_matrix.shape == (1, 4, 4)
+
+ def test_no_transform_tuple(self, device, dtype):
+ inp = torch.rand(1, 2, 5, 4, 8, device=device, dtype=dtype)
+ out = CenterCrop3D((3, 4, 5))(inp)
+ assert out.shape == (1, 2, 3, 4, 5)
+
+ def test_gradcheck(self, device):
+ input_tensor = torch.rand(1, 2, 3, 4, 5, device=device, dtype=torch.float64)
+ self.gradcheck(CenterCrop3D(3), (input_tensor,))
diff --git a/tests/augmentation/_3d/test_random_affine_3d.py b/tests/augmentation/_3d/test_random_affine_3d.py
new file mode 100644
index 000000000..41e1a42c2
--- /dev/null
+++ b/tests/augmentation/_3d/test_random_affine_3d.py
@@ -0,0 +1,52 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+from kornia.augmentation import RandomAffine3D
+
+from testing.base import BaseTester
+
+
+class TestRandomAffine3D(BaseTester):
+ def test_batch_random_affine_3d(self, device, dtype):
+ # TODO(jian): cuda and fp64
+ if "cuda" in str(device) and dtype == torch.float64:
+ pytest.skip("AssertionError: assert tensor(False, device='cuda:0')")
+
+ f = RandomAffine3D((0, 0, 0), p=1.0) # No rotation
+ tensor = torch.tensor(
+ [[[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]]], device=device, dtype=dtype
+ ) # 1 x 1 x 1 x 3 x 3
+
+ expected = torch.tensor(
+ [[[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]]], device=device, dtype=dtype
+ ) # 1 x 1 x 1 x 3 x 3
+
+ expected_transform = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
+ device=device,
+ dtype=dtype,
+ ) # 1 x 4 x 4
+
+ tensor = tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
+
+ self.assert_close(f(tensor), expected)
+ self.assert_close(f.transform_matrix, expected_transform)
diff --git a/tests/augmentation/_3d/test_random_crop_3d.py b/tests/augmentation/_3d/test_random_crop_3d.py
new file mode 100644
index 000000000..9b20d30c8
--- /dev/null
+++ b/tests/augmentation/_3d/test_random_crop_3d.py
@@ -0,0 +1,198 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+import kornia
+from kornia.augmentation import RandomCrop, RandomCrop3D
+
+from testing.base import BaseTester
+
+
+class TestRandomCrop3D(BaseTester):
+ # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
+ # return values such a torch.Tensor variable.
+ @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
+ def test_smoke(self):
+ f = RandomCrop3D(size=(2, 3, 4), padding=(0, 1, 2), fill=10, pad_if_needed=False, p=1.0)
+ repr = (
+ "RandomCrop3D(crop_size=(2, 3, 4), padding=(0, 1, 2), fill=10, pad_if_needed=False, "
+ "padding_mode=constant, resample=BILINEAR, p=1.0, p_batch=1.0, same_on_batch=False, "
+ "return_transform=None)"
+ )
+ assert str(f) == repr
+
+ @pytest.mark.parametrize("batch_size", [1, 2])
+ def test_no_padding(self, batch_size, device, dtype):
+ torch.manual_seed(42)
+ input_tensor = torch.tensor(
+ [
+ [
+ [
+ [
+ [0.0, 1.0, 2.0, 3.0, 4.0],
+ [5.0, 6.0, 7.0, 8.0, 9.0],
+ [10, 11, 12, 13, 14],
+ [15, 16, 17, 18, 19],
+ [20, 21, 22, 23, 24],
+ ]
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ).repeat(batch_size, 1, 5, 1, 1)
+ f = RandomCrop3D(size=(2, 3, 4), padding=None, align_corners=True, p=1.0)
+ out = f(input_tensor)
+ if batch_size == 1:
+ expected = torch.tensor(
+ [[[[[11, 12, 13, 14], [16, 17, 18, 19], [21, 22, 23, 24]]]]], device=device, dtype=dtype
+ ).repeat(batch_size, 1, 2, 1, 1)
+ if batch_size == 2:
+ expected = torch.tensor(
+ [
+ [
+ [
+ [
+ [6.0000, 7.0000, 8.0000, 9.0000],
+ [11.0000, 12.0000, 13.0000, 14.0000],
+ [16.0000, 17.0000, 18.0000, 19.0000],
+ ],
+ [
+ [6.0000, 7.0000, 8.0000, 9.0000],
+ [11.0000, 12.0000, 13.0000, 14.0000],
+ [16.0000, 17.0000, 18.0000, 19.0000],
+ ],
+ ]
+ ],
+ [
+ [
+ [
+ [11.0000, 12.0000, 13.0000, 14.0000],
+ [16.0000, 17.0000, 18.0000, 19.0000],
+ [21.0000, 22.0000, 23.0000, 24.0000],
+ ],
+ [
+ [11.0000, 12.0000, 13.0000, 14.0000],
+ [16.0000, 17.0000, 18.0000, 19.0000],
+ [21.0000, 22.0000, 23.0000, 24.0000],
+ ],
+ ]
+ ],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ self.assert_close(out, expected, atol=1e-4, rtol=1e-4)
+
+ def test_same_on_batch(self, device, dtype):
+ f = RandomCrop3D(size=(2, 3, 4), padding=None, align_corners=True, p=1.0, same_on_batch=True)
+ input_tensor = (
+ torch.eye(6, device=device, dtype=dtype)
+ .unsqueeze(dim=0)
+ .unsqueeze(dim=0)
+ .unsqueeze(dim=0)
+ .repeat(2, 3, 5, 1, 1)
+ )
+ res = f(input_tensor)
+ self.assert_close(res[0], res[1])
+
+ @pytest.mark.parametrize("padding", [1, (1, 1, 1), (1, 1, 1, 1, 1, 1)])
+ def test_padding_batch(self, padding, device, dtype):
+ torch.manual_seed(42)
+ batch_size = 2
+ input_tensor = torch.tensor(
+ [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]], device=device, dtype=dtype
+ ).repeat(batch_size, 1, 3, 1, 1)
+ expected = torch.tensor(
+ [
+ [
+ [
+ [[0.0, 1.0, 2.0, 10.0], [3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0]],
+ [[0.0, 1.0, 2.0, 10.0], [3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0]],
+ ]
+ ],
+ [
+ [
+ [[3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0], [10, 10, 10, 10.0]],
+ [[3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0], [10, 10, 10, 10.0]],
+ ]
+ ],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ f = RandomCrop3D(size=(2, 3, 4), fill=10.0, padding=padding, align_corners=True, p=1.0)
+ out = f(input_tensor)
+
+ self.assert_close(out, expected, atol=1e-4, rtol=1e-4)
+
+ def test_pad_if_needed(self, device, dtype):
+ torch.manual_seed(42)
+ input_tensor = torch.tensor([[[0.0, 1.0, 2.0]]], device=device, dtype=dtype)
+ expected = torch.tensor(
+ [
+ [
+ [
+ [[9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0]],
+ [[0.0, 1.0, 2.0, 9.0], [9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0]],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+ rc = RandomCrop3D(size=(2, 3, 4), pad_if_needed=True, fill=9, align_corners=True, p=1.0)
+ out = rc(input_tensor)
+
+ self.assert_close(out, expected, atol=1e-4, rtol=1e-4)
+
+ def test_gradcheck(self, device):
+ torch.manual_seed(0) # for random reproductibility
+ input_tensor = torch.rand((3, 3, 3), device=device, dtype=torch.float64) # 3 x 3
+ self.gradcheck(RandomCrop3D(size=(3, 3, 3), p=1.0), (input_tensor,))
+
+ @pytest.mark.skip("Need to fix Union type")
+ def test_jit(self, device, dtype):
+ # Define script
+ op = RandomCrop(size=(3, 3), p=1.0).forward
+ op_script = torch.jit.script(op)
+ img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype)
+
+ actual = op_script(img)
+ expected = kornia.geometry.transform.center_crop3d(img)
+ self.assert_close(actual, expected)
+
+ @pytest.mark.skip("Need to fix Union type")
+ def test_jit_trace(self, device, dtype):
+ # Define script
+ op = RandomCrop(size=(3, 3), p=1.0).forward
+ op_script = torch.jit.script(op)
+ # 1. Trace op
+ img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype)
+
+ op_trace = torch.jit.trace(op_script, (img,))
+
+ # 2. Generate new input
+ img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype)
+
+ # 3. Evaluate
+ actual = op_trace(img)
+ expected = op(img)
+ self.assert_close(actual, expected)
diff --git a/tests/augmentation/_3d/test_random_depthical_flip_3d.py b/tests/augmentation/_3d/test_random_depthical_flip_3d.py
new file mode 100644
index 000000000..d56f430d6
--- /dev/null
+++ b/tests/augmentation/_3d/test_random_depthical_flip_3d.py
@@ -0,0 +1,157 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+from kornia.augmentation import RandomDepthicalFlip3D
+from kornia.augmentation.container.augment import AugmentationSequential
+
+from testing.base import BaseTester
+
+
+class TestRandomDepthicalFlip3D(BaseTester):
+ # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
+ # return values such a torch.Tensor variable.
+ @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
+ def test_smoke(self):
+ f = RandomDepthicalFlip3D(0.5)
+ repr = "RandomDepthicalFlip3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
+ assert str(f) == repr
+
+ def test_random_dflip(self, device, dtype):
+ f = RandomDepthicalFlip3D(p=1.0)
+ f1 = RandomDepthicalFlip3D(p=0.0)
+
+ input_tensor = torch.tensor(
+ [
+ [
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ) # 2 x 3 x 4
+
+ expected = torch.tensor(
+ [
+ [
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ) # 2 x 3 x 4
+
+ expected_transform = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]],
+ device=device,
+ dtype=dtype,
+ ) # 4 x 4
+
+ identity = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
+ device=device,
+ dtype=dtype,
+ ) # 4 x 4
+
+ self.assert_close(f(input_tensor), expected)
+ self.assert_close(f.transform_matrix, expected_transform)
+ self.assert_close(f1(input_tensor), input_tensor)
+ self.assert_close(f1.transform_matrix, identity)
+
+ def test_batch_random_dflip(self, device):
+ f = RandomDepthicalFlip3D(p=1.0)
+ f1 = RandomDepthicalFlip3D(p=0.0)
+
+ input_tensor = torch.tensor(
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
+ ]
+ ) # 2 x 3 x 4
+
+ input_tensor = input_tensor.to(device)
+
+ expected = torch.tensor(
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
+ ]
+ ) # 2 x 3 x 4
+ expected = expected.to(device)
+
+ expected_transform = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ expected_transform = expected_transform.to(device)
+
+ identity = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ identity = identity.to(device)
+
+ input_tensor = input_tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
+ identity = identity.repeat(5, 1, 1) # 5 x 4 x 4
+
+ self.assert_close(f(input_tensor), expected)
+ self.assert_close(f.transform_matrix, expected_transform)
+ self.assert_close(f1(input_tensor), input_tensor)
+ self.assert_close(f1.transform_matrix, identity)
+
+ def test_same_on_batch(self, device):
+ f = RandomDepthicalFlip3D(p=0.5, same_on_batch=True)
+ input_tensor = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 2, 1, 1)
+ res = f(input_tensor)
+ self.assert_close(res[0], res[1])
+
+ def test_sequential(self, device):
+ f = AugmentationSequential(RandomDepthicalFlip3D(p=1.0), RandomDepthicalFlip3D(p=1.0))
+
+ input_tensor = torch.tensor(
+ [
+ [
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
+ ]
+ ]
+ ]
+ ) # 2 x 3 x 4
+ input_tensor = input_tensor.to(device)
+
+ expected_transform = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ expected_transform = expected_transform.to(device)
+
+ expected_transform_1 = expected_transform @ expected_transform
+
+ self.assert_close(f(input_tensor), input_tensor)
+ self.assert_close(f.transform_matrix, expected_transform_1)
+
+ def test_gradcheck(self, device):
+ input_tensor = torch.rand((1, 3, 3)).to(device) # 4 x 4
+ self.gradcheck(RandomDepthicalFlip3D(p=1.0), (input_tensor,))
diff --git a/tests/augmentation/_3d/test_random_equalize_3d.py b/tests/augmentation/_3d/test_random_equalize_3d.py
new file mode 100644
index 000000000..f7d49683c
--- /dev/null
+++ b/tests/augmentation/_3d/test_random_equalize_3d.py
@@ -0,0 +1,99 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+import kornia
+from kornia.augmentation import RandomEqualize3D
+
+from testing.base import BaseTester
+
+
+class TestRandomEqualize3D(BaseTester):
+ # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
+ # return values such a torch.Tensor variable.
+ @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
+ def test_smoke(self, device, dtype):
+ f = RandomEqualize3D(p=0.5)
+ repr = "RandomEqualize3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
+ assert str(f) == repr
+
+ def test_random_equalize(self, device, dtype):
+ f = RandomEqualize3D(p=1.0)
+ f1 = RandomEqualize3D(p=0.0)
+
+ bs, channels, depth, height, width = 1, 3, 6, 10, 10
+
+ inputs3d = self.build_input(channels, depth, height, width, bs, device=device, dtype=dtype)
+
+ row_expected = torch.tensor(
+ [0.0000, 0.11764, 0.2353, 0.3529, 0.4706, 0.5882, 0.7059, 0.8235, 0.9412, 1.0000],
+ device=device,
+ dtype=dtype,
+ )
+ expected = self.build_input(channels, depth, height, width, bs=1, row=row_expected, device=device, dtype=dtype)
+
+ identity = kornia.eye_like(4, expected)
+
+ self.assert_close(f(inputs3d), expected, rtol=1e-4, atol=1e-4)
+ self.assert_close(f.transform_matrix, identity, rtol=1e-4, atol=1e-4)
+ self.assert_close(f1(inputs3d), inputs3d, rtol=1e-4, atol=1e-4)
+ self.assert_close(f1.transform_matrix, identity, rtol=1e-4, atol=1e-4)
+
+ def test_batch_random_equalize(self, device, dtype):
+ f = RandomEqualize3D(p=1.0)
+ f1 = RandomEqualize3D(p=0.0)
+
+ bs, channels, depth, height, width = 2, 3, 6, 10, 10
+
+ inputs3d = self.build_input(channels, depth, height, width, bs, device=device, dtype=dtype)
+
+ row_expected = torch.tensor([0.0000, 0.11764, 0.2353, 0.3529, 0.4706, 0.5882, 0.7059, 0.8235, 0.9412, 1.0000])
+ expected = self.build_input(channels, depth, height, width, bs, row=row_expected, device=device, dtype=dtype)
+
+ identity = kornia.eye_like(4, expected) # 2 x 4 x 4
+
+ self.assert_close(f(inputs3d), expected, rtol=1e-4, atol=1e-4)
+ self.assert_close(f.transform_matrix, identity, rtol=1e-4, atol=1e-4)
+ self.assert_close(f1(inputs3d), inputs3d, rtol=1e-4, atol=1e-4)
+ self.assert_close(f1.transform_matrix, identity, rtol=1e-4, atol=1e-4)
+
+ def test_same_on_batch(self, device, dtype):
+ f = RandomEqualize3D(p=0.5, same_on_batch=True)
+ input_tensor = torch.eye(4, device=device, dtype=dtype)
+ input_tensor = input_tensor.unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 2, 1, 1)
+ res = f(input_tensor)
+ self.assert_close(res[0], res[1])
+
+ def test_gradcheck(self, device, dtype):
+ torch.manual_seed(0) # for random reproductibility
+
+ inputs3d = torch.rand((3, 3, 3), device=device, dtype=dtype) # 3 x 3 x 3
+ self.gradcheck(RandomEqualize3D(p=0.5), (inputs3d,))
+
+ @staticmethod
+ def build_input(channels, depth, height, width, bs=1, row=None, device="cpu", dtype=torch.float32):
+ if row is None:
+ row = torch.arange(width, device=device, dtype=dtype) / float(width)
+
+ channel = torch.stack([row] * height)
+ image = torch.stack([channel] * channels)
+ image3d = torch.stack([image] * depth).transpose(0, 1)
+ batch = torch.stack([image3d] * bs)
+
+ return batch.to(device, dtype)
diff --git a/tests/augmentation/_3d/test_random_horizontal_flip_3d.py b/tests/augmentation/_3d/test_random_horizontal_flip_3d.py
new file mode 100644
index 000000000..0f5833d6e
--- /dev/null
+++ b/tests/augmentation/_3d/test_random_horizontal_flip_3d.py
@@ -0,0 +1,127 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+from kornia.augmentation import (
+ RandomHorizontalFlip3D,
+)
+from kornia.augmentation.container.augment import AugmentationSequential
+
+from testing.base import BaseTester
+
+
+class TestRandomHorizontalFlip3D(BaseTester):
+ # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
+ # return values such a torch.Tensor variable.
+ @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
+ def test_smoke(self):
+ f = RandomHorizontalFlip3D(0.5)
+ repr = "RandomHorizontalFlip3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
+ assert str(f) == repr
+
+ def test_random_hflip(self, device):
+ f = RandomHorizontalFlip3D(p=1.0, keepdim=True)
+ f1 = RandomHorizontalFlip3D(p=0.0, keepdim=True)
+
+ input_tensor = torch.tensor(
+ [
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ ]
+ ],
+ device=device,
+ ) # 1 x 2 x 3 x 4
+
+ expected = torch.tensor(
+ [
+ [
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
+ [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
+ ]
+ ],
+ device=device,
+ ) # 1 x 2 x 3 x 4
+
+ expected_transform = torch.tensor(
+ [[[-1.0, 0.0, 0.0, 3.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]], device=device
+ ) # 1 x 4 x 4
+
+ identity = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]], device=device
+ ) # 1 x 4 x 4
+
+ self.assert_close(f(input_tensor), expected)
+ self.assert_close(f.transform_matrix, expected_transform)
+ self.assert_close(f1(input_tensor), input_tensor)
+ self.assert_close(f1.transform_matrix, identity)
+
+ def test_batch_random_hflip(self, device):
+ f = RandomHorizontalFlip3D(p=1.0)
+
+ input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 3 x 3
+ input_tensor = input_tensor.to(device)
+
+ expected = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 0.0]]]]]) # 1 x 1 x 1 x 3 x 3
+ expected = expected.to(device)
+
+ expected_transform = torch.tensor(
+ [[[-1.0, 0.0, 0.0, 2.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ expected_transform = expected_transform.to(device)
+
+ identity = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ identity = identity.to(device)
+
+ input_tensor = input_tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
+ identity = identity.repeat(5, 1, 1) # 5 x 4 x 4
+
+ self.assert_close(f(input_tensor), expected)
+ self.assert_close(f.transform_matrix, expected_transform)
+
+ def test_same_on_batch(self, device):
+ f = RandomHorizontalFlip3D(p=0.5, same_on_batch=True)
+ input_tensor = torch.eye(3, device=device).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1, 1)
+ res = f(input_tensor)
+ self.assert_close(res[0], res[1])
+
+ def test_sequential(self, device):
+ f = AugmentationSequential(RandomHorizontalFlip3D(p=1.0), RandomHorizontalFlip3D(p=1.0))
+
+ input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 3 x 3
+ input_tensor = input_tensor.to(device)
+
+ expected_transform = torch.tensor(
+ [[[-1.0, 0.0, 0.0, 2.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ expected_transform = expected_transform.to(device)
+
+ expected_transform_1 = expected_transform @ expected_transform
+ expected_transform_1 = expected_transform_1.to(device)
+
+ self.assert_close(f(input_tensor), input_tensor)
+ self.assert_close(f.transform_matrix, expected_transform_1)
+
+ def test_gradcheck(self, device):
+ input_tensor = torch.rand((1, 3, 3)).to(device) # 3 x 3
+ self.gradcheck(RandomHorizontalFlip3D(p=1.0), (input_tensor,))
diff --git a/tests/augmentation/_3d/test_random_rotation_3d.py b/tests/augmentation/_3d/test_random_rotation_3d.py
new file mode 100644
index 000000000..87163e21a
--- /dev/null
+++ b/tests/augmentation/_3d/test_random_rotation_3d.py
@@ -0,0 +1,268 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+from kornia.augmentation import RandomRotation3D
+from kornia.augmentation.container.augment import AugmentationSequential
+
+from testing.base import BaseTester
+
+
+class TestRandomRotation3D(BaseTester):
+ torch.manual_seed(0) # for random reproductibility
+
+ # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
+ # return values such a torch.Tensor variable.
+ @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
+ def test_smoke(self):
+ f = RandomRotation3D(degrees=45.5)
+ repr = (
+ """RandomRotation3D(degrees=tensor([[-45.5000, 45.5000],
+ [-45.5000, 45.5000],
+ [-45.5000, 45.5000]]), resample=BILINEAR, align_corners=False, p=0.5, """
+ """p_batch=1.0, same_on_batch=False, return_transform=None)"""
+ )
+ assert str(f) == repr
+
+ def test_random_rotation(self, device, dtype):
+ # This is included in doctest
+ torch.manual_seed(0) # for random reproductibility
+
+ f = RandomRotation3D(degrees=45.0)
+
+ input_tensor = torch.tensor(
+ [
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ ],
+ device=device,
+ dtype=dtype,
+ ) # 3 x 4 x 4
+
+ expected = torch.tensor(
+ [
+ [
+ [
+ [
+ [0.0000, 0.0000, 0.6810, 0.5250],
+ [0.5052, 0.0000, 0.0000, 0.0613],
+ [0.1159, 0.1072, 0.5324, 0.0870],
+ [0.0000, 0.0000, 0.1927, 0.0000],
+ ],
+ [
+ [0.0000, 0.1683, 0.6963, 0.1131],
+ [0.0566, 0.0000, 0.5215, 0.2796],
+ [0.0694, 0.6039, 1.4519, 1.1240],
+ [0.0000, 0.1325, 0.1542, 0.2510],
+ ],
+ [
+ [0.0000, 0.2054, 0.0000, 0.0000],
+ [0.0026, 0.6088, 0.7358, 0.2319],
+ [0.1261, 1.0830, 1.3687, 1.4940],
+ [0.0000, 0.0416, 0.2012, 0.3124],
+ ],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ expected_transform = torch.tensor(
+ [
+ [
+ [0.6523, 0.3666, -0.6635, 0.6352],
+ [-0.6185, 0.7634, -0.1862, 1.4689],
+ [0.4382, 0.5318, 0.7247, -1.1797],
+ [0.0000, 0.0000, 0.0000, 1.0000],
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ out = f(input_tensor)
+ self.assert_close(out, expected, rtol=1e-6, atol=1e-4)
+ self.assert_close(f.transform_matrix, expected_transform, rtol=1e-6, atol=1e-4)
+
+ def test_batch_random_rotation(self, device, dtype):
+ torch.manual_seed(24) # for random reproductibility
+
+ f = RandomRotation3D(degrees=45.0)
+
+ input_tensor = torch.tensor(
+ [
+ [
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ) # 1 x 1 x 4 x 4
+
+ expected = torch.tensor(
+ [
+ [
+ [
+ [
+ [1.0000, 0.0000, 0.0000, 2.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 1.0000, 2.0000, 0.0000],
+ [0.0000, 0.0000, 1.0000, 2.0000],
+ ],
+ [
+ [1.0000, 0.0000, 0.0000, 2.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 1.0000, 2.0000, 0.0000],
+ [0.0000, 0.0000, 1.0000, 2.0000],
+ ],
+ [
+ [1.0000, 0.0000, 0.0000, 2.0000],
+ [0.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 1.0000, 2.0000, 0.0000],
+ [0.0000, 0.0000, 1.0000, 2.0000],
+ ],
+ ]
+ ],
+ [
+ [
+ [
+ [0.0000, 0.0726, 0.0000, 0.0000],
+ [0.1038, 1.0134, 0.5566, 0.1519],
+ [0.0000, 1.0849, 1.1068, 0.0000],
+ [0.1242, 1.1065, 0.9681, 0.0000],
+ ],
+ [
+ [0.0000, 0.0047, 0.0166, 0.0000],
+ [0.0579, 0.4459, 0.0000, 0.4728],
+ [0.1864, 1.3349, 0.7530, 0.3251],
+ [0.1431, 1.2481, 0.4471, 0.0000],
+ ],
+ [
+ [0.0000, 0.4840, 0.2314, 0.0000],
+ [0.0000, 0.0328, 0.0000, 0.1434],
+ [0.1899, 0.5580, 0.0000, 0.9170],
+ [0.0000, 0.2042, 0.1571, 0.0855],
+ ],
+ ]
+ ],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ expected_transform = torch.tensor(
+ [
+ [
+ [1.0000, 0.0000, 0.0000, 0.0000],
+ [0.0000, 1.0000, 0.0000, 0.0000],
+ [0.0000, 0.0000, 1.0000, 0.0000],
+ [0.0000, 0.0000, 0.0000, 1.0000],
+ ],
+ [
+ [0.7522, -0.6326, -0.1841, 1.5047],
+ [0.6029, 0.5482, 0.5796, -0.8063],
+ [-0.2657, -0.5470, 0.7938, 1.4252],
+ [0.0000, 0.0000, 0.0000, 1.0000],
+ ],
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ input_tensor = input_tensor.repeat(2, 1, 1, 1, 1) # 5 x 4 x 4 x 3
+
+ out = f(input_tensor)
+ self.assert_close(out, expected, rtol=1e-6, atol=1e-4)
+ self.assert_close(f.transform_matrix, expected_transform, rtol=1e-6, atol=1e-4)
+
+ def test_same_on_batch(self, device, dtype):
+ f = RandomRotation3D(degrees=40, same_on_batch=True)
+ input_tensor = torch.eye(6, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 6, 1, 1)
+ res = f(input_tensor)
+ self.assert_close(res[0], res[1])
+
+ def test_sequential(self, device, dtype):
+ torch.manual_seed(24) # for random reproductibility
+
+ f = AugmentationSequential(RandomRotation3D(torch.tensor([-45.0, 90])), RandomRotation3D(10.4))
+ input_tensor = torch.tensor(
+ [
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
+ ],
+ device=device,
+ dtype=dtype,
+ ) # 3 x 4 x 4
+
+ expected = torch.tensor(
+ [
+ [
+ [
+ [
+ [0.3431, 0.1239, 0.0000, 1.0348],
+ [0.0000, 0.2035, 0.1139, 0.1770],
+ [0.0789, 0.9057, 1.7780, 0.0000],
+ [0.0000, 0.2286, 1.2498, 1.2643],
+ ],
+ [
+ [0.5460, 0.2131, 0.0000, 1.1453],
+ [0.0000, 0.0899, 0.0000, 0.4293],
+ [0.0797, 1.0193, 1.6677, 0.0000],
+ [0.0000, 0.2458, 1.2765, 1.0920],
+ ],
+ [
+ [0.6322, 0.2614, 0.0000, 0.9207],
+ [0.0000, 0.0037, 0.0000, 0.6551],
+ [0.0689, 0.9251, 1.3442, 0.0000],
+ [0.0000, 0.2449, 0.9856, 0.6862],
+ ],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ expected_transform = torch.tensor(
+ [
+ [
+ [0.9857, -0.1686, -0.0019, 0.2762],
+ [0.1668, 0.9739, 0.1538, -0.3650],
+ [-0.0241, -0.1520, 0.9881, 0.2760],
+ [0.0000, 0.0000, 0.0000, 1.0000],
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ )
+
+ out = f(input_tensor)
+ self.assert_close(out, expected, rtol=1e-6, atol=1e-4)
+ self.assert_close(f.transform_matrix, expected_transform, rtol=1e-6, atol=1e-4)
+
+ def test_gradcheck(self, device):
+ torch.manual_seed(0) # for random reproductibility
+
+ input_tensor = torch.rand((3, 3, 3), device=device, dtype=torch.float64) # 3 x 3 x 3
+ self.gradcheck(RandomRotation3D(degrees=(15.0, 15.0), p=1.0), (input_tensor,))
diff --git a/tests/augmentation/_3d/test_random_vertical_flip_3d.py b/tests/augmentation/_3d/test_random_vertical_flip_3d.py
new file mode 100644
index 000000000..ab4a0032b
--- /dev/null
+++ b/tests/augmentation/_3d/test_random_vertical_flip_3d.py
@@ -0,0 +1,139 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+from kornia.augmentation import (
+ RandomVerticalFlip3D,
+)
+from kornia.augmentation.container.augment import AugmentationSequential
+
+from testing.base import BaseTester
+
+
+class TestRandomVerticalFlip3D(BaseTester):
+ # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
+ # return values such a torch.Tensor variable.
+ @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
+ def test_smoke(self):
+ f = RandomVerticalFlip3D(0.5)
+ repr = "RandomVerticalFlip3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
+ assert str(f) == repr
+
+ def test_random_vflip(self, device, dtype):
+ f = RandomVerticalFlip3D(p=1.0)
+ f1 = RandomVerticalFlip3D(p=0.0)
+
+ input_tensor = torch.tensor(
+ [
+ [
+ [
+ [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]],
+ [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ) # 1 x 1 x 2 x 3 x 3
+
+ expected = torch.tensor(
+ [
+ [
+ [
+ [[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
+ [[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
+ ]
+ ]
+ ],
+ device=device,
+ dtype=dtype,
+ ) # 1 x 1 x 2 x 3 x 3
+
+ expected_transform = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
+ device=device,
+ dtype=dtype,
+ ) # 4 x 4
+
+ identity = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
+ device=device,
+ dtype=dtype,
+ ) # 1 x 4 x 4
+
+ self.assert_close(f(input_tensor), expected)
+ self.assert_close(f.transform_matrix, expected_transform)
+ self.assert_close(f1(input_tensor), input_tensor)
+ self.assert_close(f1.transform_matrix, identity)
+
+ def test_batch_random_vflip(self, device):
+ f = RandomVerticalFlip3D(p=1.0)
+ f1 = RandomVerticalFlip3D(p=0.0)
+
+ input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 3 x 3
+ input_tensor = input_tensor.to(device)
+
+ expected = torch.tensor([[[[[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]]]) # 1 x 1 x 1 x 3 x 3
+ expected = expected.to(device)
+
+ expected_transform = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ expected_transform = expected_transform.to(device)
+
+ identity = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ identity = identity.to(device)
+
+ input_tensor = input_tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
+ expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
+ identity = identity.repeat(5, 1, 1) # 5 x 4 x 4
+
+ self.assert_close(f(input_tensor), expected)
+ self.assert_close(f.transform_matrix, expected_transform)
+ self.assert_close(f1(input_tensor), input_tensor)
+ self.assert_close(f1.transform_matrix, identity)
+
+ def test_same_on_batch(self, device):
+ f = RandomVerticalFlip3D(p=0.5, same_on_batch=True)
+ input_tensor = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1, 1)
+ res = f(input_tensor)
+ self.assert_close(res[0], res[1])
+
+ def test_sequential(self, device):
+ f = AugmentationSequential(RandomVerticalFlip3D(p=1.0), RandomVerticalFlip3D(p=1.0))
+
+ input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 4 x 4
+ input_tensor = input_tensor.to(device)
+
+ expected_transform = torch.tensor(
+ [[[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
+ ) # 1 x 4 x 4
+ expected_transform = expected_transform.to(device)
+
+ expected_transform_1 = expected_transform @ expected_transform
+
+ self.assert_close(f(input_tensor), input_tensor)
+ self.assert_close(f.transform_matrix, expected_transform_1)
+
+ def test_gradcheck(self, device):
+ input_tensor = torch.rand((1, 3, 3)).to(device) # 4 x 4
+ self.gradcheck(RandomVerticalFlip3D(p=1.0), (input_tensor,))
diff --git a/tests/augmentation/test_container.py b/tests/augmentation/container/test_augmentation_sequential.py
similarity index 51%
rename from tests/augmentation/test_container.py
rename to tests/augmentation/container/test_augmentation_sequential.py
index d89e40128..8903845cd 100644
--- a/tests/augmentation/test_container.py
+++ b/tests/augmentation/container/test_augmentation_sequential.py
@@ -26,232 +26,11 @@ import kornia.augmentation as K
from kornia.augmentation.container.base import ParamItem
from kornia.constants import BorderType
from kornia.geometry.bbox import bbox_to_mask
-from kornia.geometry.boxes import Boxes
+from testing.augmentation.utils import reproducibility_test
from testing.base import assert_close
-def reproducibility_test(input, seq):
- """Any tests failed here indicate the output cannot be reproduced by the same params."""
- if isinstance(input, (tuple, list)):
- output_1 = seq(*input)
- output_2 = seq(*input, params=seq._params)
- else:
- output_1 = seq(input)
- output_2 = seq(input, params=seq._params)
-
- if isinstance(output_1, (tuple, list)) and isinstance(output_2, (tuple, list)):
- [
- assert_close(o1, o2)
- for o1, o2 in zip(output_1, output_2)
- if isinstance(o1, (torch.Tensor,)) and isinstance(o2, (torch.Tensor,))
- ]
- elif isinstance(output_1, (tuple, list)) and isinstance(output_2, (torch.Tensor,)):
- assert_close(output_1[0], output_2)
- elif isinstance(output_2, (tuple, list)) and isinstance(output_1, (torch.Tensor,)):
- assert_close(output_1, output_2[0])
- elif isinstance(output_2, (torch.Tensor,)) and isinstance(output_1, (torch.Tensor,)):
- assert_close(output_1, output_2, msg=f"{seq._params}")
- elif isinstance(output_1, dict) and isinstance(output_2, dict):
- [
- assert_close(o1, o2)
- for o1, o2 in zip(output_1.values(), output_2.values())
- if isinstance(o1, (torch.Tensor,)) and isinstance(o2, (torch.Tensor,))
- ]
- else:
- raise AssertionError(f"cannot compare {type(output_1)} {type(output_2)}")
-
-
-class TestVideoSequential:
- def test_smoke(self, device, dtype):
- input_1 = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 3, 1, 1)
- input_2 = torch.randn(4, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 3, 1, 1)
- aug_list = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1))
- aug_list(input_1)
- aug_list(input_2)
-
- @pytest.mark.parametrize("shape", [(3, 4), (2, 3, 4), (2, 3, 5, 6), (2, 3, 4, 5, 6, 7)])
- @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
- def test_exception(self, shape, data_format, device, dtype):
- aug_list = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1), data_format=data_format, same_on_frame=True)
- with pytest.raises(AssertionError):
- img = torch.randn(*shape, device=device, dtype=dtype)
- aug_list(img)
-
- @pytest.mark.parametrize(
- "augmentation",
- [
- K.RandomAffine(360, p=1.0),
- K.CenterCrop((3, 3), p=1.0),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- K.RandomCrop((5, 5), p=1.0),
- K.RandomErasing(p=1.0),
- K.RandomGrayscale(p=1.0),
- K.RandomHorizontalFlip(p=1.0),
- K.RandomVerticalFlip(p=1.0),
- K.RandomPerspective(p=1.0),
- K.RandomResizedCrop((5, 5), p=1.0),
- K.RandomRotation(360.0, p=1.0),
- K.RandomSolarize(p=1.0),
- K.RandomPosterize(p=1.0),
- K.RandomSharpness(p=1.0),
- K.RandomEqualize(p=1.0),
- K.RandomMotionBlur(3, 35.0, 0.5, p=1.0),
- K.Normalize(torch.tensor([0.5, 0.5, 0.5]), torch.tensor([0.5, 0.5, 0.5]), p=1.0),
- K.Denormalize(torch.tensor([0.5, 0.5, 0.5]), torch.tensor([0.5, 0.5, 0.5]), p=1.0),
- K.RandomAutoContrast(p=1.0),
- K.RandomShear((10.0, 10.0), p=1.0),
- K.RandomTranslate((0.5, 0.5), p=1.0),
- ],
- )
- @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
- def test_augmentation(self, augmentation, data_format, device, dtype):
- input = torch.randint(255, (1, 3, 3, 5, 6), device=device, dtype=dtype).repeat(2, 1, 1, 1, 1) / 255.0
- torch.manual_seed(21)
- aug_list = K.VideoSequential(augmentation, data_format=data_format, same_on_frame=True)
- reproducibility_test(input, aug_list)
-
- @pytest.mark.parametrize(
- "augmentations",
- [
- [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0)],
- [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)],
- [K.RandomAffine(360, p=1.0), kornia.color.BgrToRgb()],
- [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.0), K.RandomAffine(360, p=0.0)],
- [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.0)],
- [K.RandomAffine(360, p=0.0)],
- # NOTE: RandomMixUpV2 failed occasionally but always passed in the debugger. Unable to debug now.
- # [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0), K.RandomMixUpV2(p=1.0)],
- ],
- )
- @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
- @pytest.mark.parametrize("random_apply", [1, (1, 1), (1,), 10, True, False])
- def test_same_on_frame(self, augmentations, data_format, random_apply, device, dtype):
- aug_list = K.VideoSequential(
- *augmentations, data_format=data_format, same_on_frame=True, random_apply=random_apply
- )
-
- if data_format == "BCTHW":
- input = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1)
- output = aug_list(input)
- assert_close(output[:, :, 0], output[:, :, 1])
- assert_close(output[:, :, 1], output[:, :, 2])
- assert_close(output[:, :, 2], output[:, :, 3])
- if data_format == "BTCHW":
- input = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1)
- output = aug_list(input)
- assert_close(output[:, 0], output[:, 1])
- assert_close(output[:, 1], output[:, 2])
- assert_close(output[:, 2], output[:, 3])
- reproducibility_test(input, aug_list)
-
- @pytest.mark.parametrize(
- "augmentations",
- [
- [K.RandomAffine(360, p=1.0)],
- [K.RandomCrop((2, 2), padding=2)],
- [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)],
- [K.RandomAffine(360, p=0.0), K.ImageSequential(K.RandomAffine(360, p=0.0))],
- ],
- )
- @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
- def test_against_sequential(self, augmentations, data_format, device, dtype):
- aug_list_1 = K.VideoSequential(*augmentations, data_format=data_format, same_on_frame=False)
- aug_list_2 = torch.nn.Sequential(*augmentations)
-
- if data_format == "BCTHW":
- input = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1)
- if data_format == "BTCHW":
- input = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1)
-
- torch.manual_seed(0)
- output_1 = aug_list_1(input)
-
- torch.manual_seed(0)
- if data_format == "BCTHW":
- input = input.transpose(1, 2)
- output_2 = aug_list_2(input.reshape(-1, 3, 5, 6))
- if any(isinstance(a, K.RandomCrop) for a in augmentations):
- output_2 = output_2.view(2, 4, 3, 2, 2)
- else:
- output_2 = output_2.view(2, 4, 3, 5, 6)
- if data_format == "BCTHW":
- output_2 = output_2.transpose(1, 2)
- assert_close(output_1, output_2)
-
- @pytest.mark.jit()
- @pytest.mark.skip(reason="turn off due to Union Type")
- def test_jit(self, device, dtype):
- B, C, D, H, W = 2, 3, 5, 4, 4
- img = torch.ones(B, C, D, H, W, device=device, dtype=dtype)
- op = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1), same_on_frame=True)
- op_jit = torch.jit.script(op)
- assert_close(op(img), op_jit(img))
-
- @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
- def test_autocast(self, data_format, device, dtype):
- if not hasattr(torch, "autocast"):
- pytest.skip("PyTorch version without autocast support")
-
- tfs = (K.RandomAffine(0.5, (0.1, 0.5), (0.5, 1.5), 1.2, p=1.0), K.RandomGaussianBlur((3, 3), (0.1, 3), p=1))
- aug = K.VideoSequential(*tfs, data_format=data_format, random_apply=True)
- if data_format == "BCTHW":
- imgs = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1)
- elif data_format == "BTCHW":
- imgs = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1)
-
- with torch.autocast(device.type):
- output = aug(imgs)
-
- assert output.dtype == dtype, "Output image dtype should match the input dtype"
-
-
-class TestSequential:
- @pytest.mark.parametrize("random_apply_weights", [None, [0.8, 0.9]])
- def test_exception(self, random_apply_weights, device, dtype):
- inp = torch.randn(1, 3, 30, 30, device=device, dtype=dtype)
- with pytest.raises(Exception): # AssertError and NotImplementedError
- K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), random_apply_weights=random_apply_weights
- ).inverse(inp)
-
- @pytest.mark.parametrize("same_on_batch", [True, False, None])
- @pytest.mark.parametrize("keepdim", [True, False, None])
- @pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 20, True, False])
- def test_construction(self, same_on_batch, keepdim, random_apply):
- aug = K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- K.RandomAffine(360, p=1.0),
- K.RandomMixUpV2(p=1.0),
- same_on_batch=same_on_batch,
- keepdim=keepdim,
- random_apply=random_apply,
- )
- aug.same_on_batch = True
- aug.keepdim = True
- for m in aug.children():
- assert m.same_on_batch is True, m.same_on_batch
- assert m.keepdim is True, m.keepdim
-
- @pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 10, True, False])
- def test_forward(self, random_apply, device, dtype):
- inp = torch.randn(1, 3, 30, 30, device=device, dtype=dtype)
- aug = K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- kornia.filters.MedianBlur((3, 3)),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)),
- K.ImageSequential(K.RandomAffine(360, p=1.0)),
- K.RandomAffine(360, p=1.0),
- K.RandomMixUpV2(p=1.0),
- random_apply=random_apply,
- )
- out = aug(inp)
- assert out.shape == inp.shape
- aug.inverse(inp)
- reproducibility_test(inp, aug)
-
-
class TestAugmentationSequential:
@pytest.mark.parametrize(
"data_keys", ["input", "image", ["mask", "input"], ["input", "bbox_yxyx"], [0, 10], [BorderType.REFLECT]]
@@ -531,57 +310,22 @@ class TestAugmentationSequential:
for i in range(len(bbox)):
assert len(bboxes_transformed[i]) == len(bbox[i])
- @pytest.mark.parametrize("class_data_key", ["class", "label"])
- def test_class(self, class_data_key, device, dtype):
+ def test_class(self, device, dtype):
img = torch.zeros((5, 1, 5, 5))
labels = torch.randint(0, 10, size=(5, 1))
- aug = K.AugmentationSequential(K.RandomCrop((3, 3), pad_if_needed=True), data_keys=["input", class_data_key])
+ aug = K.AugmentationSequential(K.RandomCrop((3, 3), pad_if_needed=True), data_keys=["input", "class"])
_, out_labels = aug(img, labels)
assert labels is out_labels
- @pytest.mark.parametrize("B", [None, 1, 3, 5])
- @pytest.mark.parametrize("C_i", [1, 3])
- @pytest.mark.parametrize("C_m", [None, 1, 3, 4])
- @pytest.mark.parametrize("keepdim", [True, False])
- def test_masks_without_channel_dim(self, device, dtype, B, C_i, C_m, keepdim):
- img_shape = (B, C_i, 1000, 500) if B else (C_i, 1000, 500)
- mask_shape = tuple(x for x in (B, C_m, *img_shape[-2:]) if x)
-
- inp = torch.randn(*img_shape, device=device, dtype=dtype)
- mask = torch.randint(0, 5, mask_shape, device=device, dtype=dtype)
-
- aug = K.AugmentationSequential(
- # K.ImageSequential(K.RandomAffine(360, p=1.0)),
- # K.AugmentationSequential(
- # K.RandomAffine(360, p=1.0),
- # data_keys=["input", "mask"],
- # ),
- K.RandomAffine(360, p=1.0),
- K.RandomMixUpV2(p=1.0),
- data_keys=["input", "mask"],
- keepdim=keepdim,
- )
-
- out = aug(inp, mask)
- if keepdim:
- assert out[0].shape == img_shape
- assert out[1].shape == mask_shape
- else:
- assert (*(1,) * (4 - len(img_shape)), *img_shape) == out[0].shape
-
- out_mask_shape = tuple(x or 1 for x in (B, C_m, *img_shape[-2:]))
- assert out[1].shape == out_mask_shape
-
@pytest.mark.slow
@pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 10, True, False])
- @pytest.mark.parametrize("mask_dtype", [torch.int32, torch.int64, torch.float32])
- def test_forward_and_inverse(self, random_apply, device, dtype, mask_dtype):
+ def test_forward_and_inverse(self, random_apply, device, dtype):
inp = torch.randn(1, 3, 1000, 500, device=device, dtype=dtype)
bbox = torch.tensor([[[355, 10], [660, 10], [660, 250], [355, 250]]], device=device, dtype=dtype)
keypoints = torch.tensor([[[465, 115], [545, 116]]], device=device, dtype=dtype)
mask = bbox_to_mask(
- torch.tensor([[[155, 0], [900, 0], [900, 400], [155, 400]]], device=device, dtype=mask_dtype), 1000, 500
+ torch.tensor([[[155, 0], [900, 0], [900, 400], [155, 400]]], device=device, dtype=dtype), 1000, 500
)[:, None]
aug = K.AugmentationSequential(
K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0)),
@@ -731,88 +475,6 @@ class TestAugmentationSequential:
if random_apply is False:
reproducibility_test((inp, mask, bbox, keypoints, bbox_2, bbox_wh, bbox_wh_2), aug)
- @pytest.mark.parametrize("mask_dtype", [torch.int32, torch.int64, torch.float32])
- def test_transform_list_of_masks_and_boxes(self, device, dtype, mask_dtype):
- input = torch.randn(2, 3, 256, 256, device=device, dtype=dtype)
- mask = [
- torch.ones(1, 3, 256, 256, device=device, dtype=mask_dtype),
- torch.ones(1, 2, 256, 256, device=device, dtype=mask_dtype),
- ]
-
- bbox = [
- torch.tensor(
- [[28.0, 53.0, 143.0, 164.0], [254.0, 158.0, 364.0, 290.0], [307.0, 204.0, 413.0, 350.0]],
- device=device,
- dtype=dtype,
- ),
- torch.tensor([[254.0, 158.0, 364.0, 290.0], [307.0, 204.0, 413.0, 350.0]], device=device, dtype=dtype),
- ]
-
- bbox = [Boxes.from_tensor(i).data for i in bbox]
-
- aug_list = K.AugmentationSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- K.RandomHorizontalFlip(p=1.0),
- K.ImageSequential(K.RandomHorizontalFlip(p=1.0)),
- K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)),
- data_keys=["input", "mask", "bbox"],
- same_on_batch=False,
- random_apply=10,
- )
- out = aug_list(input, mask, bbox)
-
- assert out[0].shape == input.shape
- assert len(out[1]) == len(bbox)
- assert len(out[2]) == len(mask)
-
- @pytest.mark.slow
- @pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 10, True, False])
- @pytest.mark.parametrize("bbox_key", ["bbox", "bbox_xyxy", "bbox_xywh"])
- def test_dict_as_input_forward_and_inverse(self, random_apply, bbox_key, device, dtype):
- inp = torch.randn(1, 3, 1000, 500, device=device, dtype=dtype)
- bbox = torch.tensor([[[355, 10], [660, 10], [660, 250], [355, 250]]], device=device, dtype=dtype)
- if bbox_key == "bbox_xyxy":
- bbox = Boxes(bbox).to_tensor(mode="xyxy_plus")
- if bbox_key == "bbox_xywh":
- bbox = Boxes(bbox).to_tensor(mode="xywh")
- keypoints = torch.tensor([[[465, 115], [545, 116]]], device=device, dtype=dtype)
- mask = bbox_to_mask(
- torch.tensor([[[155, 0], [900, 0], [900, 400], [155, 400]]], device=device, dtype=dtype), 1000, 500
- )[:, None]
- aug = K.AugmentationSequential(
- K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0)),
- K.AugmentationSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- K.RandomAffine(360, p=1.0),
- K.RandomAffine(360, p=1.0),
- data_keys=["input", "mask", bbox_key, "keypoints"],
- ),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- K.RandomAffine(360, p=1.0),
- data_keys=None,
- random_apply=random_apply,
- )
-
- data = {"input": inp, "mask": mask, bbox_key: bbox, "keypoints": keypoints, "id": 45}
- out = aug(data)
- assert out["input"].shape == inp.shape
- assert out["mask"].shape == mask.shape
- assert out[bbox_key].shape == bbox.shape
- assert out["keypoints"].shape == keypoints.shape
- assert set(out["mask"].unique().tolist()).issubset(set(mask.unique().tolist()))
- assert out["id"] == 45
-
- out_inv = aug.inverse(out)
- assert out_inv["input"].shape == inp.shape
- assert out_inv["mask"].shape == mask.shape
- assert out_inv[bbox_key].shape == bbox.shape
- assert out_inv["keypoints"].shape == keypoints.shape
- assert set(out_inv["mask"].unique().tolist()).issubset(set(mask.unique().tolist()))
- assert out_inv["id"] == 45
-
- if random_apply is False:
- reproducibility_test(data, aug)
-
@pytest.mark.jit()
@pytest.mark.skip(reason="turn off due to Union Type")
def test_jit(self, device, dtype):
@@ -874,179 +536,3 @@ class TestAugmentationSequential:
assert outputs[1].dtype == dtype, "Output mask dtype should match the input dtype"
assert outputs[2].dtype == dtype, "Output box dtype should match the input dtype"
assert outputs[3].dtype == dtype, "Output keypoints dtype should match the input dtype"
-
-
-class TestPatchSequential:
- @pytest.mark.parametrize(
- "error_param",
- [
- {"random_apply": False, "patchwise_apply": True, "grid_size": (2, 3)},
- {"random_apply": 2, "patchwise_apply": True},
- {"random_apply": (2, 3), "patchwise_apply": True},
- ],
- )
- def test_exception(self, error_param):
- with pytest.raises(Exception): # AssertError and NotImplementedError
- K.PatchSequential(
- K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
- K.RandomPerspective(0.2, p=0.5),
- K.RandomSolarize(0.1, 0.1, p=0.5),
- ),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
- K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
- K.RandomPerspective(0.2, p=0.5),
- K.RandomSolarize(0.1, 0.1, p=0.5),
- ),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
- **error_param,
- )
-
- @pytest.mark.parametrize("shape", [(2, 3, 24, 24)])
- @pytest.mark.parametrize("padding", ["same", "valid"])
- @pytest.mark.parametrize("patchwise_apply", [True, False])
- @pytest.mark.parametrize("same_on_batch", [True, False, None])
- @pytest.mark.parametrize("keepdim", [True, False, None])
- @pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 10, True, False])
- def test_forward(self, shape, padding, patchwise_apply, same_on_batch, keepdim, random_apply, device, dtype):
- torch.manual_seed(11)
- try: # skip wrong param settings.
- seq = K.PatchSequential(
- K.color.RgbToBgr(),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
- K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
- K.RandomPerspective(0.2, p=0.5),
- K.RandomSolarize(0.1, 0.1, p=0.5),
- ),
- K.RandomMixUpV2(p=1.0),
- grid_size=(2, 2),
- padding=padding,
- patchwise_apply=patchwise_apply,
- same_on_batch=same_on_batch,
- keepdim=keepdim,
- random_apply=random_apply,
- )
- # TODO: improve me and remove the exception.
- except Exception:
- return
-
- input = torch.randn(*shape, device=device, dtype=dtype)
- out = seq(input)
- assert out.shape[-3:] == input.shape[-3:]
-
- reproducibility_test(input, seq)
-
- def test_intensity_only(self):
- seq = K.PatchSequential(
- K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
- K.RandomPerspective(0.2, p=0.5),
- K.RandomSolarize(0.1, 0.1, p=0.5),
- ),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
- K.ImageSequential(
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
- K.RandomPerspective(0.2, p=0.5),
- K.RandomSolarize(0.1, 0.1, p=0.5),
- ),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
- grid_size=(2, 2),
- )
- assert not seq.is_intensity_only()
-
- seq = K.PatchSequential(
- K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5)),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
- K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
- grid_size=(2, 2),
- )
- assert seq.is_intensity_only()
-
- def test_autocast(self, device, dtype):
- if not hasattr(torch, "autocast"):
- pytest.skip("PyTorch version without autocast support")
-
- tfs = (K.RandomAffine(0.5, (0.1, 0.5), (0.5, 1.5), 1.2, p=1.0), K.RandomGaussianBlur((3, 3), (0.1, 3), p=1))
- aug = K.PatchSequential(*tfs, grid_size=(2, 2), random_apply=True)
- imgs = torch.rand(2, 3, 7, 4, dtype=dtype, device=device)
-
- with torch.autocast(device.type):
- output = aug(imgs)
-
- assert output.dtype == dtype, "Output image dtype should match the input dtype"
-
-
-class TestDispatcher:
- def test_many_to_many(self, device, dtype):
- input_1 = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)
- input_2 = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)
- mask_1 = torch.ones(2, 1, 5, 6, device=device, dtype=dtype)
- mask_2 = torch.ones(2, 1, 5, 6, device=device, dtype=dtype)
- aug_list = K.ManyToManyAugmentationDispather(
- K.AugmentationSequential(
- kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- kornia.augmentation.RandomAffine(360, p=1.0),
- data_keys=["input", "mask"],
- ),
- K.AugmentationSequential(
- kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- kornia.augmentation.RandomAffine(360, p=1.0),
- data_keys=["input", "mask"],
- ),
- )
- output = aug_list((input_1, mask_1), (input_2, mask_2))
-
- assert output[0][0].shape == input_1.shape
- assert output[1][0].shape == input_2.shape
- assert output[0][1].shape == mask_1.shape
- assert output[1][1].shape == mask_2.shape
-
- @pytest.mark.parametrize("strict", [True, False])
- def test_many_to_one(self, strict, device, dtype):
- input = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)
- mask = torch.ones(2, 1, 5, 6, device=device, dtype=dtype)
- aug_list = K.ManyToOneAugmentationDispather(
- K.AugmentationSequential(
- kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- kornia.augmentation.RandomAffine(360, p=1.0),
- data_keys=["input", "mask"],
- ),
- K.AugmentationSequential(
- kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- kornia.augmentation.RandomAffine(360, p=1.0),
- data_keys=["input", "mask"],
- ),
- strict=strict,
- )
- output = aug_list(input, mask)
-
- assert output[0][0].shape == input.shape
- assert output[1][0].shape == input.shape
- assert output[0][1].shape == mask.shape
- assert output[1][1].shape == mask.shape
-
- @pytest.mark.parametrize("strict", [True, False])
- def test_many_to_one_strict_mode(self, strict):
- def _init_many_to_one(strict):
- K.ManyToOneAugmentationDispather(
- K.AugmentationSequential(
- kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- kornia.augmentation.RandomAffine(360, p=1.0),
- data_keys=["input"],
- ),
- K.AugmentationSequential(
- kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
- kornia.augmentation.RandomAffine(360, p=1.0),
- data_keys=["input", "mask"],
- ),
- strict=strict,
- )
-
- if strict:
- with pytest.raises(RuntimeError):
- _init_many_to_one(strict) # fails
- else:
- _init_many_to_one(strict) # passes
diff --git a/tests/augmentation/container/test_dispatcher.py b/tests/augmentation/container/test_dispatcher.py
new file mode 100644
index 000000000..1133c6891
--- /dev/null
+++ b/tests/augmentation/container/test_dispatcher.py
@@ -0,0 +1,95 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+import kornia
+import kornia.augmentation as K
+
+
+class TestDispatcher:
+ def test_many_to_many(self, device, dtype):
+ input_1 = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)
+ input_2 = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)
+ mask_1 = torch.ones(2, 1, 5, 6, device=device, dtype=dtype)
+ mask_2 = torch.ones(2, 1, 5, 6, device=device, dtype=dtype)
+ aug_list = K.ManyToManyAugmentationDispather(
+ K.AugmentationSequential(
+ kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ kornia.augmentation.RandomAffine(360, p=1.0),
+ data_keys=["input", "mask"],
+ ),
+ K.AugmentationSequential(
+ kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ kornia.augmentation.RandomAffine(360, p=1.0),
+ data_keys=["input", "mask"],
+ ),
+ )
+ output = aug_list((input_1, mask_1), (input_2, mask_2))
+
+ assert output[0][0].shape == input_1.shape
+ assert output[1][0].shape == input_2.shape
+ assert output[0][1].shape == mask_1.shape
+ assert output[1][1].shape == mask_2.shape
+
+ @pytest.mark.parametrize("strict", [True, False])
+ def test_many_to_one(self, strict, device, dtype):
+ input = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)
+ mask = torch.ones(2, 1, 5, 6, device=device, dtype=dtype)
+ aug_list = K.ManyToOneAugmentationDispather(
+ K.AugmentationSequential(
+ kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ kornia.augmentation.RandomAffine(360, p=1.0),
+ data_keys=["input", "mask"],
+ ),
+ K.AugmentationSequential(
+ kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ kornia.augmentation.RandomAffine(360, p=1.0),
+ data_keys=["input", "mask"],
+ ),
+ strict=strict,
+ )
+ output = aug_list(input, mask)
+
+ assert output[0][0].shape == input.shape
+ assert output[1][0].shape == input.shape
+ assert output[0][1].shape == mask.shape
+ assert output[1][1].shape == mask.shape
+
+ @pytest.mark.parametrize("strict", [True, False])
+ def test_many_to_one_strict_mode(self, strict):
+ def _init_many_to_one(strict):
+ K.ManyToOneAugmentationDispather(
+ K.AugmentationSequential(
+ kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ kornia.augmentation.RandomAffine(360, p=1.0),
+ data_keys=["input"],
+ ),
+ K.AugmentationSequential(
+ kornia.augmentation.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ kornia.augmentation.RandomAffine(360, p=1.0),
+ data_keys=["input", "mask"],
+ ),
+ strict=strict,
+ )
+
+ if strict:
+ with pytest.raises(RuntimeError):
+ _init_many_to_one(strict) # fails
+ else:
+ _init_many_to_one(strict) # passes
diff --git a/tests/augmentation/container/test_patch_sequential.py b/tests/augmentation/container/test_patch_sequential.py
new file mode 100644
index 000000000..411cf1728
--- /dev/null
+++ b/tests/augmentation/container/test_patch_sequential.py
@@ -0,0 +1,126 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+import kornia.augmentation as K
+
+from testing.augmentation.utils import reproducibility_test
+
+
+class TestPatchSequential:
+ @pytest.mark.parametrize(
+ "error_param",
+ [
+ {"random_apply": False, "patchwise_apply": True, "grid_size": (2, 3)},
+ {"random_apply": 2, "patchwise_apply": True},
+ {"random_apply": (2, 3), "patchwise_apply": True},
+ ],
+ )
+ def test_exception(self, error_param):
+ with pytest.raises(Exception): # AssertError and NotImplementedError
+ K.PatchSequential(
+ K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
+ K.RandomPerspective(0.2, p=0.5),
+ K.RandomSolarize(0.1, 0.1, p=0.5),
+ ),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
+ K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
+ K.RandomPerspective(0.2, p=0.5),
+ K.RandomSolarize(0.1, 0.1, p=0.5),
+ ),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
+ **error_param,
+ )
+
+ @pytest.mark.parametrize("shape", [(2, 3, 24, 24)])
+ @pytest.mark.parametrize("padding", ["same", "valid"])
+ @pytest.mark.parametrize("patchwise_apply", [True, False])
+ @pytest.mark.parametrize("same_on_batch", [True, False, None])
+ @pytest.mark.parametrize("keepdim", [True, False, None])
+ @pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 10, True, False])
+ def test_forward(self, shape, padding, patchwise_apply, same_on_batch, keepdim, random_apply, device, dtype):
+ torch.manual_seed(11)
+ try: # skip wrong param settings.
+ seq = K.PatchSequential(
+ K.color.RgbToBgr(),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
+ K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
+ K.RandomPerspective(0.2, p=0.5),
+ K.RandomSolarize(0.1, 0.1, p=0.5),
+ ),
+ K.RandomMixUpV2(p=1.0),
+ grid_size=(2, 2),
+ padding=padding,
+ patchwise_apply=patchwise_apply,
+ same_on_batch=same_on_batch,
+ keepdim=keepdim,
+ random_apply=random_apply,
+ )
+ # TODO: improve me and remove the exception.
+ except Exception:
+ return
+
+ input = torch.randn(*shape, device=device, dtype=dtype)
+ out = seq(input)
+ assert out.shape[-3:] == input.shape[-3:]
+
+ reproducibility_test(input, seq)
+
+ def test_intensity_only(self):
+ seq = K.PatchSequential(
+ K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
+ K.RandomPerspective(0.2, p=0.5),
+ K.RandomSolarize(0.1, 0.1, p=0.5),
+ ),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
+ K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
+ K.RandomPerspective(0.2, p=0.5),
+ K.RandomSolarize(0.1, 0.1, p=0.5),
+ ),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
+ grid_size=(2, 2),
+ )
+ assert not seq.is_intensity_only()
+
+ seq = K.PatchSequential(
+ K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5)),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.5),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1),
+ grid_size=(2, 2),
+ )
+ assert seq.is_intensity_only()
+
+ def test_autocast(self, device, dtype):
+ if not hasattr(torch, "autocast"):
+ pytest.skip("PyTorch version without autocast support")
+
+ tfs = (K.RandomAffine(0.5, (0.1, 0.5), (0.5, 1.5), 1.2, p=1.0), K.RandomGaussianBlur((3, 3), (0.1, 3), p=1))
+ aug = K.PatchSequential(*tfs, grid_size=(2, 2), random_apply=True)
+ imgs = torch.rand(2, 3, 7, 4, dtype=dtype, device=device)
+
+ with torch.autocast(device.type):
+ output = aug(imgs)
+
+ assert output.dtype == dtype, "Output image dtype should match the input dtype"
diff --git a/tests/augmentation/container/test_sequential_aug.py b/tests/augmentation/container/test_sequential_aug.py
new file mode 100644
index 000000000..2e34d3efe
--- /dev/null
+++ b/tests/augmentation/container/test_sequential_aug.py
@@ -0,0 +1,70 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+import kornia
+import kornia.augmentation as K
+
+from testing.augmentation.utils import reproducibility_test
+
+
+class TestSequential:
+ @pytest.mark.parametrize("random_apply_weights", [None, [0.8, 0.9]])
+ def test_exception(self, random_apply_weights, device, dtype):
+ inp = torch.randn(1, 3, 30, 30, device=device, dtype=dtype)
+ with pytest.raises(Exception): # AssertError and NotImplementedError
+ K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), random_apply_weights=random_apply_weights
+ ).inverse(inp)
+
+ @pytest.mark.parametrize("same_on_batch", [True, False, None])
+ @pytest.mark.parametrize("keepdim", [True, False, None])
+ @pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 20, True, False])
+ def test_construction(self, same_on_batch, keepdim, random_apply):
+ aug = K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ K.RandomAffine(360, p=1.0),
+ K.RandomMixUpV2(p=1.0),
+ same_on_batch=same_on_batch,
+ keepdim=keepdim,
+ random_apply=random_apply,
+ )
+ aug.same_on_batch = True
+ aug.keepdim = True
+ for m in aug.children():
+ assert m.same_on_batch is True, m.same_on_batch
+ assert m.keepdim is True, m.keepdim
+
+ @pytest.mark.parametrize("random_apply", [1, (2, 2), (1, 2), (2,), 10, True, False])
+ def test_forward(self, random_apply, device, dtype):
+ inp = torch.randn(1, 3, 30, 30, device=device, dtype=dtype)
+ aug = K.ImageSequential(
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ kornia.filters.MedianBlur((3, 3)),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ K.ImageSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)),
+ K.ImageSequential(K.RandomAffine(360, p=1.0)),
+ K.RandomAffine(360, p=1.0),
+ K.RandomMixUpV2(p=1.0),
+ random_apply=random_apply,
+ )
+ out = aug(inp)
+ assert out.shape == inp.shape
+ aug.inverse(inp)
+ reproducibility_test(inp, aug)
diff --git a/tests/augmentation/container/test_video_sequential.py b/tests/augmentation/container/test_video_sequential.py
new file mode 100644
index 000000000..fc74fe57b
--- /dev/null
+++ b/tests/augmentation/container/test_video_sequential.py
@@ -0,0 +1,168 @@
+# LICENSE HEADER MANAGED BY add-license-header
+#
+# Copyright 2018 Kornia Team
+#
+# 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 pytest
+import torch
+
+import kornia
+import kornia.augmentation as K
+
+from testing.augmentation.utils import assert_close, reproducibility_test
+
+
+class TestVideoSequential:
+ def test_smoke(self, device, dtype):
+ input_1 = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 3, 1, 1)
+ input_2 = torch.randn(4, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 3, 1, 1)
+ aug_list = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1))
+ aug_list(input_1)
+ aug_list(input_2)
+
+ @pytest.mark.parametrize("shape", [(3, 4), (2, 3, 4), (2, 3, 5, 6), (2, 3, 4, 5, 6, 7)])
+ @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
+ def test_exception(self, shape, data_format, device, dtype):
+ aug_list = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1), data_format=data_format, same_on_frame=True)
+ with pytest.raises(AssertionError):
+ img = torch.randn(*shape, device=device, dtype=dtype)
+ aug_list(img)
+
+ @pytest.mark.parametrize(
+ "augmentation",
+ [
+ K.RandomAffine(360, p=1.0),
+ K.CenterCrop((3, 3), p=1.0),
+ K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0),
+ K.RandomCrop((5, 5), p=1.0),
+ K.RandomErasing(p=1.0),
+ K.RandomGrayscale(p=1.0),
+ K.RandomHorizontalFlip(p=1.0),
+ K.RandomVerticalFlip(p=1.0),
+ K.RandomPerspective(p=1.0),
+ K.RandomResizedCrop((5, 5), p=1.0),
+ K.RandomRotation(360.0, p=1.0),
+ K.RandomSolarize(p=1.0),
+ K.RandomPosterize(p=1.0),
+ K.RandomSharpness(p=1.0),
+ K.RandomEqualize(p=1.0),
+ K.RandomMotionBlur(3, 35.0, 0.5, p=1.0),
+ K.Normalize(torch.tensor([0.5, 0.5, 0.5]), torch.tensor([0.5, 0.5, 0.5]), p=1.0),
+ K.Denormalize(torch.tensor([0.5, 0.5, 0.5]), torch.tensor([0.5, 0.5, 0.5]), p=1.0),
+ K.RandomAutoContrast(p=1.0),
+ K.RandomShear((10.0, 10.0), p=1.0),
+ K.RandomTranslate((0.5, 0.5), p=1.0),
+ ],
+ )
+ @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
+ def test_augmentation(self, augmentation, data_format, device, dtype):
+ input = torch.randint(255, (1, 3, 3, 5, 6), device=device, dtype=dtype).repeat(2, 1, 1, 1, 1) / 255.0
+ torch.manual_seed(21)
+ aug_list = K.VideoSequential(augmentation, data_format=data_format, same_on_frame=True)
+ reproducibility_test(input, aug_list)
+
+ @pytest.mark.parametrize(
+ "augmentations",
+ [
+ [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0)],
+ [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)],
+ [K.RandomAffine(360, p=1.0), kornia.color.BgrToRgb()],
+ [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.0), K.RandomAffine(360, p=0.0)],
+ [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=0.0)],
+ [K.RandomAffine(360, p=0.0)],
+ # NOTE: RandomMixUpV2 failed occasionally but always passed in the debugger. Unable to debug now.
+ # [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0), K.RandomAffine(360, p=1.0), K.RandomMixUpV2(p=1.0)],
+ ],
+ )
+ @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
+ @pytest.mark.parametrize("random_apply", [1, (1, 1), (1,), 10, True, False])
+ def test_same_on_frame(self, augmentations, data_format, random_apply, device, dtype):
+ aug_list = K.VideoSequential(
+ *augmentations, data_format=data_format, same_on_frame=True, random_apply=random_apply
+ )
+
+ if data_format == "BCTHW":
+ input = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1)
+ output = aug_list(input)
+ assert_close(output[:, :, 0], output[:, :, 1])
+ assert_close(output[:, :, 1], output[:, :, 2])
+ assert_close(output[:, :, 2], output[:, :, 3])
+ if data_format == "BTCHW":
+ input = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1)
+ output = aug_list(input)
+ assert_close(output[:, 0], output[:, 1])
+ assert_close(output[:, 1], output[:, 2])
+ assert_close(output[:, 2], output[:, 3])
+ reproducibility_test(input, aug_list)
+
+ @pytest.mark.parametrize(
+ "augmentations",
+ [
+ [K.RandomAffine(360, p=1.0)],
+ [K.RandomCrop((2, 2), padding=2)],
+ [K.ColorJiggle(0.1, 0.1, 0.1, 0.1, p=1.0)],
+ [K.RandomAffine(360, p=0.0), K.ImageSequential(K.RandomAffine(360, p=0.0))],
+ ],
+ )
+ @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
+ def test_against_sequential(self, augmentations, data_format, device, dtype):
+ aug_list_1 = K.VideoSequential(*augmentations, data_format=data_format, same_on_frame=False)
+ aug_list_2 = torch.nn.Sequential(*augmentations)
+
+ if data_format == "BCTHW":
+ input = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1)
+ if data_format == "BTCHW":
+ input = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1)
+
+ torch.manual_seed(0)
+ output_1 = aug_list_1(input)
+
+ torch.manual_seed(0)
+ if data_format == "BCTHW":
+ input = input.transpose(1, 2)
+ output_2 = aug_list_2(input.reshape(-1, 3, 5, 6))
+ if any(isinstance(a, K.RandomCrop) for a in augmentations):
+ output_2 = output_2.view(2, 4, 3, 2, 2)
+ else:
+ output_2 = output_2.view(2, 4, 3, 5, 6)
+ if data_format == "BCTHW":
+ output_2 = output_2.transpose(1, 2)
+ assert_close(output_1, output_2)
+
+ @pytest.mark.jit()
+ @pytest.mark.skip(reason="turn off due to Union Type")
+ def test_jit(self, device, dtype):
+ B, C, D, H, W = 2, 3, 5, 4, 4
+ img = torch.ones(B, C, D, H, W, device=device, dtype=dtype)
+ op = K.VideoSequential(K.ColorJiggle(0.1, 0.1, 0.1, 0.1), same_on_frame=True)
+ op_jit = torch.jit.script(op)
+ assert_close(op(img), op_jit(img))
+
+ @pytest.mark.parametrize("data_format", ["BCTHW", "BTCHW"])
+ def test_autocast(self, data_format, device, dtype):
+ if not hasattr(torch, "autocast"):
+ pytest.skip("PyTorch version without autocast support")
+
+ tfs = (K.RandomAffine(0.5, (0.1, 0.5), (0.5, 1.5), 1.2, p=1.0), K.RandomGaussianBlur((3, 3), (0.1, 3), p=1))
+ aug = K.VideoSequential(*tfs, data_format=data_format, random_apply=True)
+ if data_format == "BCTHW":
+ imgs = torch.randn(2, 3, 1, 5, 6, device=device, dtype=dtype).repeat(1, 1, 4, 1, 1)
+ elif data_format == "BTCHW":
+ imgs = torch.randn(2, 1, 3, 5, 6, device=device, dtype=dtype).repeat(1, 4, 1, 1, 1)
+
+ with torch.autocast(device.type):
+ output = aug(imgs)
+
+ assert output.dtype == dtype, "Output image dtype should match the input dtype"
diff --git a/tests/augmentation/test_augmentation_3d.py b/tests/augmentation/test_augmentation_3d.py
deleted file mode 100644
index c7e2483fe..000000000
--- a/tests/augmentation/test_augmentation_3d.py
+++ /dev/null
@@ -1,927 +0,0 @@
-# LICENSE HEADER MANAGED BY add-license-header
-#
-# Copyright 2018 Kornia Team
-#
-# 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 pytest
-import torch
-
-import kornia
-from kornia.augmentation import (
- CenterCrop3D,
- RandomAffine3D,
- RandomCrop,
- RandomCrop3D,
- RandomDepthicalFlip3D,
- RandomEqualize3D,
- RandomHorizontalFlip3D,
- RandomRotation3D,
- RandomVerticalFlip3D,
-)
-from kornia.augmentation.container.augment import AugmentationSequential
-
-from testing.base import BaseTester
-
-
-class TestRandomHorizontalFlip3D(BaseTester):
- # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
- # return values such a torch.Tensor variable.
- @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
- def test_smoke(self):
- f = RandomHorizontalFlip3D(0.5)
- repr = "RandomHorizontalFlip3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
- assert str(f) == repr
-
- def test_random_hflip(self, device):
- f = RandomHorizontalFlip3D(p=1.0, keepdim=True)
- f1 = RandomHorizontalFlip3D(p=0.0, keepdim=True)
-
- input_tensor = torch.tensor(
- [
- [
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- ]
- ],
- device=device,
- ) # 1 x 2 x 3 x 4
-
- expected = torch.tensor(
- [
- [
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [2.0, 1.0, 0.0, 0.0]],
- ]
- ],
- device=device,
- ) # 1 x 2 x 3 x 4
-
- expected_transform = torch.tensor(
- [[[-1.0, 0.0, 0.0, 3.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]], device=device
- ) # 1 x 4 x 4
-
- identity = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]], device=device
- ) # 1 x 4 x 4
-
- self.assert_close(f(input_tensor), expected)
- self.assert_close(f.transform_matrix, expected_transform)
- self.assert_close(f1(input_tensor), input_tensor)
- self.assert_close(f1.transform_matrix, identity)
-
- def test_batch_random_hflip(self, device):
- f = RandomHorizontalFlip3D(p=1.0)
-
- input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 3 x 3
- input_tensor = input_tensor.to(device)
-
- expected = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [1.0, 1.0, 0.0]]]]]) # 1 x 1 x 1 x 3 x 3
- expected = expected.to(device)
-
- expected_transform = torch.tensor(
- [[[-1.0, 0.0, 0.0, 2.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- expected_transform = expected_transform.to(device)
-
- identity = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- identity = identity.to(device)
-
- input_tensor = input_tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
- identity = identity.repeat(5, 1, 1) # 5 x 4 x 4
-
- self.assert_close(f(input_tensor), expected)
- self.assert_close(f.transform_matrix, expected_transform)
-
- def test_same_on_batch(self, device):
- f = RandomHorizontalFlip3D(p=0.5, same_on_batch=True)
- input_tensor = torch.eye(3, device=device).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1, 1)
- res = f(input_tensor)
- self.assert_close(res[0], res[1])
-
- def test_sequential(self, device):
- f = AugmentationSequential(RandomHorizontalFlip3D(p=1.0), RandomHorizontalFlip3D(p=1.0))
-
- input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 3 x 3
- input_tensor = input_tensor.to(device)
-
- expected_transform = torch.tensor(
- [[[-1.0, 0.0, 0.0, 2.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- expected_transform = expected_transform.to(device)
-
- expected_transform_1 = expected_transform @ expected_transform
- expected_transform_1 = expected_transform_1.to(device)
-
- self.assert_close(f(input_tensor), input_tensor)
- self.assert_close(f.transform_matrix, expected_transform_1)
-
- def test_gradcheck(self, device):
- input_tensor = torch.rand((1, 3, 3)).to(device) # 3 x 3
- self.gradcheck(RandomHorizontalFlip3D(p=1.0), (input_tensor,))
-
-
-class TestRandomVerticalFlip3D(BaseTester):
- # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
- # return values such a torch.Tensor variable.
- @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
- def test_smoke(self):
- f = RandomVerticalFlip3D(0.5)
- repr = "RandomVerticalFlip3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
- assert str(f) == repr
-
- def test_random_vflip(self, device, dtype):
- f = RandomVerticalFlip3D(p=1.0)
- f1 = RandomVerticalFlip3D(p=0.0)
-
- input_tensor = torch.tensor(
- [
- [
- [
- [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]],
- [[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- ) # 1 x 1 x 2 x 3 x 3
-
- expected = torch.tensor(
- [
- [
- [
- [[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
- [[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- ) # 1 x 1 x 2 x 3 x 3
-
- expected_transform = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
- device=device,
- dtype=dtype,
- ) # 4 x 4
-
- identity = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
- device=device,
- dtype=dtype,
- ) # 1 x 4 x 4
-
- self.assert_close(f(input_tensor), expected)
- self.assert_close(f.transform_matrix, expected_transform)
- self.assert_close(f1(input_tensor), input_tensor)
- self.assert_close(f1.transform_matrix, identity)
-
- def test_batch_random_vflip(self, device):
- f = RandomVerticalFlip3D(p=1.0)
- f1 = RandomVerticalFlip3D(p=0.0)
-
- input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 3 x 3
- input_tensor = input_tensor.to(device)
-
- expected = torch.tensor([[[[[0.0, 1.0, 1.0], [0.0, 0.0, 0.0], [0.0, 0.0, 0.0]]]]]) # 1 x 1 x 1 x 3 x 3
- expected = expected.to(device)
-
- expected_transform = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- expected_transform = expected_transform.to(device)
-
- identity = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- identity = identity.to(device)
-
- input_tensor = input_tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
- identity = identity.repeat(5, 1, 1) # 5 x 4 x 4
-
- self.assert_close(f(input_tensor), expected)
- self.assert_close(f.transform_matrix, expected_transform)
- self.assert_close(f1(input_tensor), input_tensor)
- self.assert_close(f1.transform_matrix, identity)
-
- def test_same_on_batch(self, device):
- f = RandomVerticalFlip3D(p=0.5, same_on_batch=True)
- input_tensor = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 1, 1, 1)
- res = f(input_tensor)
- self.assert_close(res[0], res[1])
-
- def test_sequential(self, device):
- f = AugmentationSequential(RandomVerticalFlip3D(p=1.0), RandomVerticalFlip3D(p=1.0))
-
- input_tensor = torch.tensor([[[[[0.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 1.0, 1.0]]]]]) # 1 x 1 x 1 x 4 x 4
- input_tensor = input_tensor.to(device)
-
- expected_transform = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, -1.0, 0.0, 2.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- expected_transform = expected_transform.to(device)
-
- expected_transform_1 = expected_transform @ expected_transform
-
- self.assert_close(f(input_tensor), input_tensor)
- self.assert_close(f.transform_matrix, expected_transform_1)
-
- def test_gradcheck(self, device):
- input_tensor = torch.rand((1, 3, 3)).to(device) # 4 x 4
- self.gradcheck(RandomVerticalFlip3D(p=1.0), (input_tensor,))
-
-
-class TestRandomDepthicalFlip3D(BaseTester):
- # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
- # return values such a torch.Tensor variable.
- @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
- def test_smoke(self):
- f = RandomDepthicalFlip3D(0.5)
- repr = "RandomDepthicalFlip3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
- assert str(f) == repr
-
- def test_random_dflip(self, device, dtype):
- f = RandomDepthicalFlip3D(p=1.0)
- f1 = RandomDepthicalFlip3D(p=0.0)
-
- input_tensor = torch.tensor(
- [
- [
- [
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- ) # 2 x 3 x 4
-
- expected = torch.tensor(
- [
- [
- [
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- ) # 2 x 3 x 4
-
- expected_transform = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]],
- device=device,
- dtype=dtype,
- ) # 4 x 4
-
- identity = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
- device=device,
- dtype=dtype,
- ) # 4 x 4
-
- self.assert_close(f(input_tensor), expected)
- self.assert_close(f.transform_matrix, expected_transform)
- self.assert_close(f1(input_tensor), input_tensor)
- self.assert_close(f1.transform_matrix, identity)
-
- def test_batch_random_dflip(self, device):
- f = RandomDepthicalFlip3D(p=1.0)
- f1 = RandomDepthicalFlip3D(p=0.0)
-
- input_tensor = torch.tensor(
- [
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
- ]
- ) # 2 x 3 x 4
-
- input_tensor = input_tensor.to(device)
-
- expected = torch.tensor(
- [
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
- ]
- ) # 2 x 3 x 4
- expected = expected.to(device)
-
- expected_transform = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- expected_transform = expected_transform.to(device)
-
- identity = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- identity = identity.to(device)
-
- input_tensor = input_tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
- identity = identity.repeat(5, 1, 1) # 5 x 4 x 4
-
- self.assert_close(f(input_tensor), expected)
- self.assert_close(f.transform_matrix, expected_transform)
- self.assert_close(f1(input_tensor), input_tensor)
- self.assert_close(f1.transform_matrix, identity)
-
- def test_same_on_batch(self, device):
- f = RandomDepthicalFlip3D(p=0.5, same_on_batch=True)
- input_tensor = torch.eye(3).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 2, 1, 1)
- res = f(input_tensor)
- self.assert_close(res[0], res[1])
-
- def test_sequential(self, device):
- f = AugmentationSequential(RandomDepthicalFlip3D(p=1.0), RandomDepthicalFlip3D(p=1.0))
-
- input_tensor = torch.tensor(
- [
- [
- [
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 1.0]],
- [[0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 0.0], [0.0, 0.0, 0.0, 2.0]],
- ]
- ]
- ]
- ) # 2 x 3 x 4
- input_tensor = input_tensor.to(device)
-
- expected_transform = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, -1.0, 1.0], [0.0, 0.0, 0.0, 1.0]]]
- ) # 1 x 4 x 4
- expected_transform = expected_transform.to(device)
-
- expected_transform_1 = expected_transform @ expected_transform
-
- self.assert_close(f(input_tensor), input_tensor)
- self.assert_close(f.transform_matrix, expected_transform_1)
-
- def test_gradcheck(self, device):
- input_tensor = torch.rand((1, 3, 3)).to(device) # 4 x 4
- self.gradcheck(RandomDepthicalFlip3D(p=1.0), (input_tensor,))
-
-
-class TestRandomRotation3D(BaseTester):
- torch.manual_seed(0) # for random reproductibility
-
- # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
- # return values such a torch.Tensor variable.
- @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
- def test_smoke(self):
- f = RandomRotation3D(degrees=45.5)
- repr = (
- """RandomRotation3D(degrees=tensor([[-45.5000, 45.5000],
- [-45.5000, 45.5000],
- [-45.5000, 45.5000]]), resample=BILINEAR, align_corners=False, p=0.5, """
- """p_batch=1.0, same_on_batch=False, return_transform=None)"""
- )
- assert str(f) == repr
-
- def test_random_rotation(self, device, dtype):
- # This is included in doctest
- torch.manual_seed(0) # for random reproductibility
-
- f = RandomRotation3D(degrees=45.0)
-
- input_tensor = torch.tensor(
- [
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- ],
- device=device,
- dtype=dtype,
- ) # 3 x 4 x 4
-
- expected = torch.tensor(
- [
- [
- [
- [
- [0.0000, 0.0000, 0.6810, 0.5250],
- [0.5052, 0.0000, 0.0000, 0.0613],
- [0.1159, 0.1072, 0.5324, 0.0870],
- [0.0000, 0.0000, 0.1927, 0.0000],
- ],
- [
- [0.0000, 0.1683, 0.6963, 0.1131],
- [0.0566, 0.0000, 0.5215, 0.2796],
- [0.0694, 0.6039, 1.4519, 1.1240],
- [0.0000, 0.1325, 0.1542, 0.2510],
- ],
- [
- [0.0000, 0.2054, 0.0000, 0.0000],
- [0.0026, 0.6088, 0.7358, 0.2319],
- [0.1261, 1.0830, 1.3687, 1.4940],
- [0.0000, 0.0416, 0.2012, 0.3124],
- ],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- expected_transform = torch.tensor(
- [
- [
- [0.6523, 0.3666, -0.6635, 0.6352],
- [-0.6185, 0.7634, -0.1862, 1.4689],
- [0.4382, 0.5318, 0.7247, -1.1797],
- [0.0000, 0.0000, 0.0000, 1.0000],
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- out = f(input_tensor)
- self.assert_close(out, expected, rtol=1e-6, atol=1e-4)
- self.assert_close(f.transform_matrix, expected_transform, rtol=1e-6, atol=1e-4)
-
- def test_batch_random_rotation(self, device, dtype):
- torch.manual_seed(24) # for random reproductibility
-
- f = RandomRotation3D(degrees=45.0)
-
- input_tensor = torch.tensor(
- [
- [
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- ]
- ],
- device=device,
- dtype=dtype,
- ) # 1 x 1 x 4 x 4
-
- expected = torch.tensor(
- [
- [
- [
- [
- [1.0000, 0.0000, 0.0000, 2.0000],
- [0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 1.0000, 2.0000, 0.0000],
- [0.0000, 0.0000, 1.0000, 2.0000],
- ],
- [
- [1.0000, 0.0000, 0.0000, 2.0000],
- [0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 1.0000, 2.0000, 0.0000],
- [0.0000, 0.0000, 1.0000, 2.0000],
- ],
- [
- [1.0000, 0.0000, 0.0000, 2.0000],
- [0.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 1.0000, 2.0000, 0.0000],
- [0.0000, 0.0000, 1.0000, 2.0000],
- ],
- ]
- ],
- [
- [
- [
- [0.0000, 0.0726, 0.0000, 0.0000],
- [0.1038, 1.0134, 0.5566, 0.1519],
- [0.0000, 1.0849, 1.1068, 0.0000],
- [0.1242, 1.1065, 0.9681, 0.0000],
- ],
- [
- [0.0000, 0.0047, 0.0166, 0.0000],
- [0.0579, 0.4459, 0.0000, 0.4728],
- [0.1864, 1.3349, 0.7530, 0.3251],
- [0.1431, 1.2481, 0.4471, 0.0000],
- ],
- [
- [0.0000, 0.4840, 0.2314, 0.0000],
- [0.0000, 0.0328, 0.0000, 0.1434],
- [0.1899, 0.5580, 0.0000, 0.9170],
- [0.0000, 0.2042, 0.1571, 0.0855],
- ],
- ]
- ],
- ],
- device=device,
- dtype=dtype,
- )
-
- expected_transform = torch.tensor(
- [
- [
- [1.0000, 0.0000, 0.0000, 0.0000],
- [0.0000, 1.0000, 0.0000, 0.0000],
- [0.0000, 0.0000, 1.0000, 0.0000],
- [0.0000, 0.0000, 0.0000, 1.0000],
- ],
- [
- [0.7522, -0.6326, -0.1841, 1.5047],
- [0.6029, 0.5482, 0.5796, -0.8063],
- [-0.2657, -0.5470, 0.7938, 1.4252],
- [0.0000, 0.0000, 0.0000, 1.0000],
- ],
- ],
- device=device,
- dtype=dtype,
- )
-
- input_tensor = input_tensor.repeat(2, 1, 1, 1, 1) # 5 x 4 x 4 x 3
-
- out = f(input_tensor)
- self.assert_close(out, expected, rtol=1e-6, atol=1e-4)
- self.assert_close(f.transform_matrix, expected_transform, rtol=1e-6, atol=1e-4)
-
- def test_same_on_batch(self, device, dtype):
- f = RandomRotation3D(degrees=40, same_on_batch=True)
- input_tensor = torch.eye(6, device=device, dtype=dtype).unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 3, 6, 1, 1)
- res = f(input_tensor)
- self.assert_close(res[0], res[1])
-
- def test_sequential(self, device, dtype):
- torch.manual_seed(24) # for random reproductibility
-
- f = AugmentationSequential(RandomRotation3D(torch.tensor([-45.0, 90])), RandomRotation3D(10.4))
- input_tensor = torch.tensor(
- [
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- [[1.0, 0.0, 0.0, 2.0], [0.0, 0.0, 0.0, 0.0], [0.0, 1.0, 2.0, 0.0], [0.0, 0.0, 1.0, 2.0]],
- ],
- device=device,
- dtype=dtype,
- ) # 3 x 4 x 4
-
- expected = torch.tensor(
- [
- [
- [
- [
- [0.3431, 0.1239, 0.0000, 1.0348],
- [0.0000, 0.2035, 0.1139, 0.1770],
- [0.0789, 0.9057, 1.7780, 0.0000],
- [0.0000, 0.2286, 1.2498, 1.2643],
- ],
- [
- [0.5460, 0.2131, 0.0000, 1.1453],
- [0.0000, 0.0899, 0.0000, 0.4293],
- [0.0797, 1.0193, 1.6677, 0.0000],
- [0.0000, 0.2458, 1.2765, 1.0920],
- ],
- [
- [0.6322, 0.2614, 0.0000, 0.9207],
- [0.0000, 0.0037, 0.0000, 0.6551],
- [0.0689, 0.9251, 1.3442, 0.0000],
- [0.0000, 0.2449, 0.9856, 0.6862],
- ],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- expected_transform = torch.tensor(
- [
- [
- [0.9857, -0.1686, -0.0019, 0.2762],
- [0.1668, 0.9739, 0.1538, -0.3650],
- [-0.0241, -0.1520, 0.9881, 0.2760],
- [0.0000, 0.0000, 0.0000, 1.0000],
- ]
- ],
- device=device,
- dtype=dtype,
- )
-
- out = f(input_tensor)
- self.assert_close(out, expected, rtol=1e-6, atol=1e-4)
- self.assert_close(f.transform_matrix, expected_transform, rtol=1e-6, atol=1e-4)
-
- def test_gradcheck(self, device):
- torch.manual_seed(0) # for random reproductibility
-
- input_tensor = torch.rand((3, 3, 3), device=device, dtype=torch.float64) # 3 x 3 x 3
- self.gradcheck(RandomRotation3D(degrees=(15.0, 15.0), p=1.0), (input_tensor,))
-
-
-class TestRandomCrop3D(BaseTester):
- # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
- # return values such a torch.Tensor variable.
- @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
- def test_smoke(self):
- f = RandomCrop3D(size=(2, 3, 4), padding=(0, 1, 2), fill=10, pad_if_needed=False, p=1.0)
- repr = (
- "RandomCrop3D(crop_size=(2, 3, 4), padding=(0, 1, 2), fill=10, pad_if_needed=False, "
- "padding_mode=constant, resample=BILINEAR, p=1.0, p_batch=1.0, same_on_batch=False, "
- "return_transform=None)"
- )
- assert str(f) == repr
-
- @pytest.mark.parametrize("batch_size", [1, 2])
- def test_no_padding(self, batch_size, device, dtype):
- torch.manual_seed(42)
- input_tensor = torch.tensor(
- [
- [
- [
- [
- [0.0, 1.0, 2.0, 3.0, 4.0],
- [5.0, 6.0, 7.0, 8.0, 9.0],
- [10, 11, 12, 13, 14],
- [15, 16, 17, 18, 19],
- [20, 21, 22, 23, 24],
- ]
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- ).repeat(batch_size, 1, 5, 1, 1)
- f = RandomCrop3D(size=(2, 3, 4), padding=None, align_corners=True, p=1.0)
- out = f(input_tensor)
- if batch_size == 1:
- expected = torch.tensor(
- [[[[[11, 12, 13, 14], [16, 17, 18, 19], [21, 22, 23, 24]]]]], device=device, dtype=dtype
- ).repeat(batch_size, 1, 2, 1, 1)
- if batch_size == 2:
- expected = torch.tensor(
- [
- [
- [
- [
- [6.0000, 7.0000, 8.0000, 9.0000],
- [11.0000, 12.0000, 13.0000, 14.0000],
- [16.0000, 17.0000, 18.0000, 19.0000],
- ],
- [
- [6.0000, 7.0000, 8.0000, 9.0000],
- [11.0000, 12.0000, 13.0000, 14.0000],
- [16.0000, 17.0000, 18.0000, 19.0000],
- ],
- ]
- ],
- [
- [
- [
- [11.0000, 12.0000, 13.0000, 14.0000],
- [16.0000, 17.0000, 18.0000, 19.0000],
- [21.0000, 22.0000, 23.0000, 24.0000],
- ],
- [
- [11.0000, 12.0000, 13.0000, 14.0000],
- [16.0000, 17.0000, 18.0000, 19.0000],
- [21.0000, 22.0000, 23.0000, 24.0000],
- ],
- ]
- ],
- ],
- device=device,
- dtype=dtype,
- )
-
- self.assert_close(out, expected, atol=1e-4, rtol=1e-4)
-
- def test_same_on_batch(self, device, dtype):
- f = RandomCrop3D(size=(2, 3, 4), padding=None, align_corners=True, p=1.0, same_on_batch=True)
- input_tensor = (
- torch.eye(6, device=device, dtype=dtype)
- .unsqueeze(dim=0)
- .unsqueeze(dim=0)
- .unsqueeze(dim=0)
- .repeat(2, 3, 5, 1, 1)
- )
- res = f(input_tensor)
- self.assert_close(res[0], res[1])
-
- @pytest.mark.parametrize("padding", [1, (1, 1, 1), (1, 1, 1, 1, 1, 1)])
- def test_padding_batch(self, padding, device, dtype):
- torch.manual_seed(42)
- batch_size = 2
- input_tensor = torch.tensor(
- [[[[0.0, 1.0, 2.0], [3.0, 4.0, 5.0], [6.0, 7.0, 8.0]]]], device=device, dtype=dtype
- ).repeat(batch_size, 1, 3, 1, 1)
- expected = torch.tensor(
- [
- [
- [
- [[0.0, 1.0, 2.0, 10.0], [3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0]],
- [[0.0, 1.0, 2.0, 10.0], [3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0]],
- ]
- ],
- [
- [
- [[3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0], [10, 10, 10, 10.0]],
- [[3.0, 4.0, 5.0, 10.0], [6.0, 7.0, 8.0, 10.0], [10, 10, 10, 10.0]],
- ]
- ],
- ],
- device=device,
- dtype=dtype,
- )
- f = RandomCrop3D(size=(2, 3, 4), fill=10.0, padding=padding, align_corners=True, p=1.0)
- out = f(input_tensor)
-
- self.assert_close(out, expected, atol=1e-4, rtol=1e-4)
-
- def test_pad_if_needed(self, device, dtype):
- torch.manual_seed(42)
- input_tensor = torch.tensor([[[0.0, 1.0, 2.0]]], device=device, dtype=dtype)
- expected = torch.tensor(
- [
- [
- [
- [[9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0]],
- [[0.0, 1.0, 2.0, 9.0], [9.0, 9.0, 9.0, 9.0], [9.0, 9.0, 9.0, 9.0]],
- ]
- ]
- ],
- device=device,
- dtype=dtype,
- )
- rc = RandomCrop3D(size=(2, 3, 4), pad_if_needed=True, fill=9, align_corners=True, p=1.0)
- out = rc(input_tensor)
-
- self.assert_close(out, expected, atol=1e-4, rtol=1e-4)
-
- def test_gradcheck(self, device):
- torch.manual_seed(0) # for random reproductibility
- input_tensor = torch.rand((3, 3, 3), device=device, dtype=torch.float64) # 3 x 3
- self.gradcheck(RandomCrop3D(size=(3, 3, 3), p=1.0), (input_tensor,))
-
- @pytest.mark.skip("Need to fix Union type")
- def test_jit(self, device, dtype):
- # Define script
- op = RandomCrop(size=(3, 3), p=1.0).forward
- op_script = torch.jit.script(op)
- img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype)
-
- actual = op_script(img)
- expected = kornia.geometry.transform.center_crop3d(img)
- self.assert_close(actual, expected)
-
- @pytest.mark.skip("Need to fix Union type")
- def test_jit_trace(self, device, dtype):
- # Define script
- op = RandomCrop(size=(3, 3), p=1.0).forward
- op_script = torch.jit.script(op)
- # 1. Trace op
- img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype)
-
- op_trace = torch.jit.trace(op_script, (img,))
-
- # 2. Generate new input
- img = torch.ones(1, 1, 5, 6, device=device, dtype=dtype)
-
- # 3. Evaluate
- actual = op_trace(img)
- expected = op(img)
- self.assert_close(actual, expected)
-
-
-class TestCenterCrop3D(BaseTester):
- def test_no_transform(self, device, dtype):
- inp = torch.rand(1, 2, 4, 4, 4, device=device, dtype=dtype)
- out = CenterCrop3D(2)(inp)
- assert out.shape == (1, 2, 2, 2, 2)
-
- def test_transform(self, device, dtype):
- inp = torch.rand(1, 2, 5, 4, 8, device=device, dtype=dtype)
- aug = CenterCrop3D(2)
- out = aug(inp)
- assert out.shape == (1, 2, 2, 2, 2)
- assert aug.transform_matrix.shape == (1, 4, 4)
-
- def test_no_transform_tuple(self, device, dtype):
- inp = torch.rand(1, 2, 5, 4, 8, device=device, dtype=dtype)
- out = CenterCrop3D((3, 4, 5))(inp)
- assert out.shape == (1, 2, 3, 4, 5)
-
- def test_gradcheck(self, device):
- input_tensor = torch.rand(1, 2, 3, 4, 5, device=device, dtype=torch.float64)
- self.gradcheck(CenterCrop3D(3), (input_tensor,))
-
-
-class TestRandomEqualize3D(BaseTester):
- # TODO: improve and implement more meaningful smoke tests e.g check for a consistent
- # return values such a torch.Tensor variable.
- @pytest.mark.xfail(reason="might fail under windows OS due to printing preicision.")
- def test_smoke(self, device, dtype):
- f = RandomEqualize3D(p=0.5)
- repr = "RandomEqualize3D(p=0.5, p_batch=1.0, same_on_batch=False, return_transform=None)"
- assert str(f) == repr
-
- def test_random_equalize(self, device, dtype):
- f = RandomEqualize3D(p=1.0)
- f1 = RandomEqualize3D(p=0.0)
-
- bs, channels, depth, height, width = 1, 3, 6, 10, 10
-
- inputs3d = self.build_input(channels, depth, height, width, bs, device=device, dtype=dtype)
-
- row_expected = torch.tensor(
- [0.0000, 0.11764, 0.2353, 0.3529, 0.4706, 0.5882, 0.7059, 0.8235, 0.9412, 1.0000],
- device=device,
- dtype=dtype,
- )
- expected = self.build_input(channels, depth, height, width, bs=1, row=row_expected, device=device, dtype=dtype)
-
- identity = kornia.eye_like(4, expected)
-
- self.assert_close(f(inputs3d), expected, rtol=1e-4, atol=1e-4)
- self.assert_close(f.transform_matrix, identity, rtol=1e-4, atol=1e-4)
- self.assert_close(f1(inputs3d), inputs3d, rtol=1e-4, atol=1e-4)
- self.assert_close(f1.transform_matrix, identity, rtol=1e-4, atol=1e-4)
-
- def test_batch_random_equalize(self, device, dtype):
- f = RandomEqualize3D(p=1.0)
- f1 = RandomEqualize3D(p=0.0)
-
- bs, channels, depth, height, width = 2, 3, 6, 10, 10
-
- inputs3d = self.build_input(channels, depth, height, width, bs, device=device, dtype=dtype)
-
- row_expected = torch.tensor([0.0000, 0.11764, 0.2353, 0.3529, 0.4706, 0.5882, 0.7059, 0.8235, 0.9412, 1.0000])
- expected = self.build_input(channels, depth, height, width, bs, row=row_expected, device=device, dtype=dtype)
-
- identity = kornia.eye_like(4, expected) # 2 x 4 x 4
-
- self.assert_close(f(inputs3d), expected, rtol=1e-4, atol=1e-4)
- self.assert_close(f.transform_matrix, identity, rtol=1e-4, atol=1e-4)
- self.assert_close(f1(inputs3d), inputs3d, rtol=1e-4, atol=1e-4)
- self.assert_close(f1.transform_matrix, identity, rtol=1e-4, atol=1e-4)
-
- def test_same_on_batch(self, device, dtype):
- f = RandomEqualize3D(p=0.5, same_on_batch=True)
- input_tensor = torch.eye(4, device=device, dtype=dtype)
- input_tensor = input_tensor.unsqueeze(dim=0).unsqueeze(dim=0).repeat(2, 1, 2, 1, 1)
- res = f(input_tensor)
- self.assert_close(res[0], res[1])
-
- def test_gradcheck(self, device, dtype):
- torch.manual_seed(0) # for random reproductibility
-
- inputs3d = torch.rand((3, 3, 3), device=device, dtype=dtype) # 3 x 3 x 3
- self.gradcheck(RandomEqualize3D(p=0.5), (inputs3d,))
-
- @staticmethod
- def build_input(channels, depth, height, width, bs=1, row=None, device="cpu", dtype=torch.float32):
- if row is None:
- row = torch.arange(width, device=device, dtype=dtype) / float(width)
-
- channel = torch.stack([row] * height)
- image = torch.stack([channel] * channels)
- image3d = torch.stack([image] * depth).transpose(0, 1)
- batch = torch.stack([image3d] * bs)
-
- return batch.to(device, dtype)
-
-
-class TestRandomAffine3D(BaseTester):
- def test_batch_random_affine_3d(self, device, dtype):
- # TODO(jian): cuda and fp64
- if "cuda" in str(device) and dtype == torch.float64:
- pytest.skip("AssertionError: assert tensor(False, device='cuda:0')")
-
- f = RandomAffine3D((0, 0, 0), p=1.0) # No rotation
- tensor = torch.tensor(
- [[[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]]], device=device, dtype=dtype
- ) # 1 x 1 x 1 x 3 x 3
-
- expected = torch.tensor(
- [[[[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0], [7.0, 8.0, 9.0]]]]], device=device, dtype=dtype
- ) # 1 x 1 x 1 x 3 x 3
-
- expected_transform = torch.tensor(
- [[[1.0, 0.0, 0.0, 0.0], [0.0, 1.0, 0.0, 0.0], [0.0, 0.0, 1.0, 0.0], [0.0, 0.0, 0.0, 1.0]]],
- device=device,
- dtype=dtype,
- ) # 1 x 4 x 4
-
- tensor = tensor.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected = expected.repeat(5, 3, 1, 1, 1) # 5 x 3 x 3 x 3 x 3
- expected_transform = expected_transform.repeat(5, 1, 1) # 5 x 4 x 4
-
- self.assert_close(f(tensor), expected)
- self.assert_close(f.transform_matrix, expected_transform)
diff --git a/tests/augmentation/test_auto_operation.py b/tests/augmentation/test_auto_operation.py
index 5900359db..099a1d91f 100644
--- a/tests/augmentation/test_auto_operation.py
+++ b/tests/augmentation/test_auto_operation.py
@@ -29,10 +29,9 @@ from kornia.augmentation.auto.trivial_augment import TrivialAugment
from kornia.augmentation.container import AugmentationSequential
from kornia.geometry.bbox import bbox_to_mask
+from testing.augmentation.utils import reproducibility_test
from testing.base import BaseTester
-from tests.augmentation.test_container import reproducibility_test
-
def _find_all_ops() -> List[OperationBase]:
_ops = [op for _, op in inspect.getmembers(ops, inspect.isclass)]