π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
[Bug] Fix/pipelining multiple augmentations return wrong transformation matrix (#645)
FGerib2129a48194b
diff --git a/kornia/augmentation/augmentation.py b/kornia/augmentation/augmentation.pyindex 8c157f013..7466b6db7 100644--- a/kornia/augmentation/augmentation.py+++ b/kornia/augmentation/augmentation.py@@ -58,7 +58,7 @@ class AugmentationBase(nn.Module):output = self.apply_transform(input[0], self._params)transformation_matrix = self.compute_transformation(input[0], self._params)if return_transform:- return output, input[1] @ transformation_matrix+ return output, transformation_matrix @ input[1]else:return output, input[1]diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.pyindex 97e756746..6f60576c6 100644--- a/test/augmentation/test_augmentation.py+++ b/test/augmentation/test_augmentation.py@@ -1,3 +1,4 @@+from unittest.mock import patchfrom typing import Union, Tupleimport pytest@@ -10,10 +11,81 @@ from torch.autograd import gradcheckimport korniaimport kornia.testing as utils # test utilsfrom kornia.constants import pi-from kornia.augmentation import RandomHorizontalFlip, RandomVerticalFlip, ColorJitter, \+from kornia.augmentation import AugmentationBase, RandomHorizontalFlip, RandomVerticalFlip, ColorJitter, \RandomErasing, RandomGrayscale, RandomRotation, RandomCrop, RandomResizedCrop, RandomMotionBlur+class TestAugmentationBase:++ def test_forward(self, device, dtype):+ torch.manual_seed(42)+ input = torch.rand((2, 3, 4, 5), device=device, dtype=dtype)+ input_transform = torch.rand((2, 3, 3), device=device, dtype=dtype)+ expected_output = torch.rand((2, 3, 4, 5), device=device, dtype=dtype)+ expected_transform = torch.rand((2, 3, 3), device=device, dtype=dtype)+ augmentation = AugmentationBase(return_transform=False)++ with patch.object(augmentation, "apply_transform", autospec=True) as apply_transform, \+ patch.object(augmentation, "generate_parameters", autospec=True) as generate_parameters, \+ patch.object(augmentation, "compute_transformation", autospec=True) as compute_transformation:++ # Calling the augmentation with a single tensor shall return the expected tensor using the generated params.+ params = {"foo": 0}+ generate_parameters.return_value = params+ apply_transform.return_value = expected_output+ compute_transformation.return_value = expected_transform+ output = augmentation(input)+ apply_transform.assert_called_once_with(input, params)+ assert output is expected_output++ # Calling the augmentation with a tensor and set return_transform shall+ # return the expected tensor and transformation.+ output, transformation = augmentation(input, return_transform=True)+ assert output is expected_output+ assert transformation is expected_transform++ # Calling the augmentation with a tensor and params shall return the expected tensor using the given params.+ params = {"bar": 1}+ apply_transform.reset_mock()+ generate_parameters.return_value = None+ output = augmentation(input, params=params)+ apply_transform.assert_called_once_with(input, params)+ assert output is expected_output++ # Calling the augmentation with a tensor,a transformation and set+ # return_transform shall return the expected tensor and the proper+ # transformation matrix.+ expected_final_transformation = expected_transform @ input_transform+ output, transformation = augmentation((input, input_transform), return_transform=True)+ assert output is expected_output+ assert torch.allclose(expected_final_transformation, transformation)+ assert transformation.shape[0] == input.shape[0]++ def test_gradcheck(self, device, dtype):+ torch.manual_seed(42)++ input = torch.rand((1, 1, 3, 3), device=device, dtype=dtype)+ output = torch.rand((1, 1, 3, 3), device=device, dtype=dtype)+ input_transform = torch.rand((1, 3, 3), device=device, dtype=dtype)+ other_transform = torch.rand((1, 3, 3), device=device, dtype=dtype)++ input = utils.tensor_to_gradcheck_var(input) # to var+ input_transform = utils.tensor_to_gradcheck_var(input_transform) # to var+ output = utils.tensor_to_gradcheck_var(output) # to var+ other_transform = utils.tensor_to_gradcheck_var(other_transform) # to var++ augmentation = AugmentationBase(return_transform=True)++ with patch.object(augmentation, "apply_transform", autospec=True) as apply_transform, \+ patch.object(augmentation, "generate_parameters", autospec=True) as generate_parameters, \+ patch.object(augmentation, "compute_transformation", autospec=True) as compute_transformation:++ apply_transform.return_value = output+ compute_transformation.return_value = other_transform++ assert gradcheck(augmentation, ((input, input_transform)), raise_exception=True)++class TestRandomHorizontalFlip:def smoke_test(self, device):