π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): use compiled ColorJitter helpers (#4047)
Puttala Jeevan Kumarf772cb308ddd
diff --git a/kornia/augmentation/_2d/intensity/color_jitter.py b/kornia/augmentation/_2d/intensity/color_jitter.pyindex a50530231..0c96d0189 100644--- a/kornia/augmentation/_2d/intensity/color_jitter.py+++ b/kornia/augmentation/_2d/intensity/color_jitter.py@@ -168,7 +168,7 @@ class ColorJitter(IntensityAugmentationBase2D):options: Optional[Dict[Any, Any]] = None,disable: bool = False,) -> "ColorJitter":- self.brightness_fn = torch.compile(+ self._brightness_fn = torch.compile(self._brightness_fn,fullgraph=fullgraph,dynamic=dynamic,@@ -177,7 +177,7 @@ class ColorJitter(IntensityAugmentationBase2D):options=options,disable=disable,)- self.contrast_fn = torch.compile(+ self._contrast_fn = torch.compile(self._contrast_fn,fullgraph=fullgraph,dynamic=dynamic,@@ -186,7 +186,7 @@ class ColorJitter(IntensityAugmentationBase2D):options=options,disable=disable,)- self.saturation_fn = torch.compile(+ self._saturation_fn = torch.compile(self._saturation_fn,fullgraph=fullgraph,dynamic=dynamic,@@ -195,7 +195,7 @@ class ColorJitter(IntensityAugmentationBase2D):options=options,disable=disable,)- self.hue_fn = torch.compile(+ self._hue_fn = torch.compile(self._hue_fn,fullgraph=fullgraph,dynamic=dynamic,diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.pyindex 94b74d4ee..8997ebdac 100644--- a/tests/augmentation/test_augmentation.py+++ b/tests/augmentation/test_augmentation.py@@ -2114,6 +2114,38 @@ class TestColorJitter(BaseTester):self.assert_close(f(input), expected)self.assert_close(f.transform_matrix, expected_transform)+ @pytest.mark.parametrize(+ "jitter_kwargs,order",+ [+ ({"brightness": (1.2, 1.2)}, (0,)),+ ({"contrast": (1.2, 1.2)}, (1,)),+ ({"saturation": (1.2, 1.2)}, (2,)),+ ({"hue": (0.1, 0.1)}, (3,)),+ ],+ ids=["brightness", "contrast", "saturation", "hue"],+ )+ def test_compile_uses_helpers(self, device, dtype, jitter_kwargs, order):+ compiled_graphs = []++ def backend(graph_module, _example_inputs):+ compiled_graphs.append(graph_module)+ return graph_module.forward++ input = torch.tensor(+ [[[[0.1, 0.2], [0.3, 0.4]], [[0.2, 0.4], [0.6, 0.8]], [[0.9, 0.7], [0.5, 0.3]]]],+ device=device,+ dtype=dtype,+ )+ f = ColorJitter(**jitter_kwargs, p=1.0, order=order)+ params = f.forward_parameters(input.shape)+ expected = f(input, params=params)+ assert not torch.equal(expected, input)++ torch._dynamo.reset()+ f.compile(fullgraph=True, backend=backend)+ self.assert_close(f(input, params=params), expected)+ assert len(compiled_graphs) == 1+@pytest.mark.slowdef test_dynamo(self, device, dtype, torch_optimizer):input = torch.rand((1, 3, 5, 5), device=device, dtype=dtype)