π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): honor ADA's shared batch probability gate (#5787)
A Aswanth Rajd715141d68fe
diff --git a/changelog.d/5787.fixed.md b/changelog.d/5787.fixed.mdnew file mode 100644index 000000000..cd14fd6cf--- /dev/null+++ b/changelog.d/5787.fixed.md@@ -0,0 +1 @@+Make `AdaptiveDiscriminatorAugmentation(same_on_batch=True)` share its global probability gate across the batch instead of independently selecting image rows.diff --git a/kornia/augmentation/presets/ada.py b/kornia/augmentation/presets/ada.pyindex 6a2a0d5ba..3a064c7ad 100644--- a/kornia/augmentation/presets/ada.py+++ b/kornia/augmentation/presets/ada.py@@ -288,7 +288,12 @@ class AdaptiveDiscriminatorAugmentation(AugmentationSequential):batch_size, device = self._get_inputs_metadata(inputs, data_keys=data_keys)- p_tensor = torch.bernoulli(torch.full((batch_size,), self.p, dtype=torch.float32, device=device)).bool()+ sample_size = min(batch_size, 1) if self.same_on_batch else batch_size+ p_tensor = (+ torch.bernoulli(torch.full((sample_size,), self.p, dtype=torch.float32, device=device))+ .bool()+ .expand(batch_size)+ )if not p_tensor.any():return inputsdiff --git a/tests/augmentation/test_presets.py b/tests/augmentation/test_presets.pyindex 26f56948a..501c8a8d1 100644--- a/tests/augmentation/test_presets.py+++ b/tests/augmentation/test_presets.py@@ -19,6 +19,7 @@import pytestimport torch+from kornia.augmentation import RandomHorizontalFlipfrom kornia.augmentation.presets.ada import AdaptiveDiscriminatorAugmentationfrom testing.base import BaseTester@@ -30,6 +31,39 @@ class PresetTests(BaseTester):@pytest.mark.usefixtures("device", "dtype")class TestAdaptiveDiscriminatorAugmentation(PresetTests):+ @pytest.mark.parametrize("seed", [0, 2])+ @pytest.mark.parametrize("direct_forward", [False, True])+ def test_shared_batch_gate(self, device, dtype, seed, direct_forward):+ inputs = torch.arange(4, device=device, dtype=dtype).reshape(1, 1, 1, 4).expand(8, 1, 1, 4).clone()+ expected_inputs = inputs.clone()+ ada = AdaptiveDiscriminatorAugmentation(RandomHorizontalFlip(p=1.0), initial_p=0.5, same_on_batch=True)+ torch.manual_seed(seed)+ actual = ada.forward(inputs) if direct_forward else ada(inputs)+ self.assert_close(actual, actual[:1].expand_as(actual), atol=0, rtol=0)+ assert torch.equal(actual, inputs) or torch.equal(actual, inputs.flip(-1))+ self.assert_close(inputs[0, 0, 0], torch.arange(4, device=device, dtype=dtype), atol=0, rtol=0)+ self.assert_close(inputs, expected_inputs, atol=0, rtol=0)++ @pytest.mark.parametrize("same_on_batch", [False, None])+ def test_independent_batch_gate(self, device, dtype, same_on_batch):+ inputs = torch.arange(4, device=device, dtype=dtype).reshape(1, 1, 1, 4).expand(8, 1, 1, 4).clone()+ ada = AdaptiveDiscriminatorAugmentation(RandomHorizontalFlip(p=1.0), initial_p=0.5, same_on_batch=same_on_batch)+ torch.manual_seed(0)+ selected = torch.bernoulli(torch.full((8,), 0.5, device=device)).bool()+ expected = torch.where(selected[:, None, None, None], inputs.flip(-1), inputs)+ torch.manual_seed(0)+ self.assert_close(ada(inputs), expected, atol=0, rtol=0)++ @pytest.mark.parametrize("same_on_batch", [True, False])+ def test_empty_batch_gate_consumes_no_draws(self, device, dtype, same_on_batch):+ inputs = torch.zeros(0, 1, 1, 4, device=device, dtype=dtype)+ ada = AdaptiveDiscriminatorAugmentation(RandomHorizontalFlip(p=1.0), initial_p=0.5, same_on_batch=same_on_batch)+ torch.manual_seed(0)+ expected = torch.rand(4, device=device)+ torch.manual_seed(0)+ self.assert_close(ada(inputs), inputs, atol=0, rtol=0)+ self.assert_close(torch.rand(4, device=device), expected, atol=0, rtol=0)+def test_initial_hyper_params(self):ada_preset = AdaptiveDiscriminatorAugmentation()assert ada_preset.adjustment_speed > 0