π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): prevent double application of probability p in RandomMixUpV2 and RandomCutMixV2 (#4649) (#4685)
Prakhar Sainia04938fac872
diff --git a/changelog.d/4685.fixed.md b/changelog.d/4685.fixed.mdnew file mode 100644index 000000000..861a2c071--- /dev/null+++ b/changelog.d/4685.fixed.md@@ -0,0 +1 @@+Fix double application of probability `p` in `RandomMixUpV2` and `RandomCutMixV2`.diff --git a/kornia/augmentation/_2d/mix/cutmix.py b/kornia/augmentation/_2d/mix/cutmix.pyindex 0688aec75..8dbdb7f2a 100644--- a/kornia/augmentation/_2d/mix/cutmix.py+++ b/kornia/augmentation/_2d/mix/cutmix.py@@ -122,7 +122,7 @@ class RandomCutMixV2(MixAugmentationBaseV2):use_correct_lambda: bool = False,) -> None:super().__init__(p=1.0, p_batch=p, same_on_batch=same_on_batch, keepdim=keepdim, data_keys=data_keys)- self._param_generator: rg.CutmixGenerator = rg.CutmixGenerator(cut_size, beta, num_mix, p=p)+ self._param_generator: rg.CutmixGenerator = rg.CutmixGenerator(cut_size, beta, num_mix, p=1.0)self.use_correct_lambda = use_correct_lambdaif not self.use_correct_lambda:diff --git a/kornia/augmentation/_2d/mix/mixup.py b/kornia/augmentation/_2d/mix/mixup.pyindex 5a1dca463..18d017a00 100644--- a/kornia/augmentation/_2d/mix/mixup.py+++ b/kornia/augmentation/_2d/mix/mixup.py@@ -120,7 +120,7 @@ class RandomMixUpV2(MixAugmentationBaseV2):data_keys: Optional[List[Union[str, int, DataKey]]] = None,) -> None:super().__init__(p=1.0, p_batch=p, same_on_batch=same_on_batch, keepdim=keepdim, data_keys=data_keys)- self._param_generator = rg.MixupGenerator(lambda_val, p=p)+ self._param_generator = rg.MixupGenerator(lambda_val, p=1.0)def apply_transform(self, input: torch.Tensor, params: Dict[str, torch.Tensor], maybe_flags: Optional[Dict[str, Any]] = Nonediff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.pyindex f3c7e1ecb..048707d97 100644--- a/tests/augmentation/test_augmentation_mix.py+++ b/tests/augmentation/test_augmentation_mix.py@@ -277,6 +277,20 @@ class TestRandomMixUpV2(BaseTester):if untouched.any():self.assert_close(output[untouched], input[untouched])+ def test_mixup_prob_single_gate(self, device, dtype):+ # Regression test for #4649: p must not be applied twice+ torch.manual_seed(42)+ x = torch.rand(4, 1, 8, 8, device=device, dtype=dtype)+ aug = RandomMixUpV2(p=0.5)+ assert aug._param_generator.p == 1.0++ for _ in range(20):+ aug(x)+ bp = aug._params["batch_prob"]+ if bp[0] > 0:+ # All rows in a selected batch must be mixed+ assert (aug._params["mixup_lambdas"] > 0).all()+class TestRandomCutMixV2(BaseTester):def test_smoke(self):@@ -492,6 +506,24 @@ class TestRandomCutMixV2(BaseTester):self.assert_close(out_label[0, :, 2], expected_lambda, rtol=0.0, atol=0.0)+ def test_cutmix_prob_single_gate(self, device, dtype):+ # Regression test for #4649: p must not be applied twice+ from kornia.geometry.bbox import infer_bbox_shape++ torch.manual_seed(42)+ x = torch.rand(4, 1, 16, 16, device=device, dtype=dtype)+ aug = RandomCutMixV2(p=0.5, cut_size=(0.2, 0.8), use_correct_lambda=True)+ assert aug._param_generator.p == 1.0++ for _ in range(20):+ aug(x)+ bp = aug._params["batch_prob"]+ if bp[0] > 0:+ # All rows in a selected batch must have non-empty crops+ w, h = infer_bbox_shape(aug._params["crop_src"][0])+ assert (w > 0).all()+ assert (h > 0).all()+class TestRandomMosaic(BaseTester):def test_non_square_input_preserves_hw_4438(self):