Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(augmentation): insert only a mix member in random_apply's mix step (#6071)

Saksham Sharma945b20e3ae1d
diff --git a/changelog.d/6071.fixed.md b/changelog.d/6071.fixed.md
new file mode 100644
index 000000000..79e032942
--- /dev/null
+++ b/changelog.d/6071.fixed.md
@@ -0,0 +1 @@
+With `random_apply` and a mix augmentation, `ImageSequential`, `AugmentationSequential` and `VideoSequential` insert only a mix member at the mix-insertion step. That step drew among every member whose weight was 0, so an ordinary member given weight 0 in `random_apply_weights` could still be applied, and was reported as the added mix.
diff --git a/kornia/augmentation/container/image.py b/kornia/augmentation/container/image.py
index 3a777e09f..2d27c7475 100644
--- a/kornia/augmentation/container/image.py
+++ b/kornia/augmentation/container/image.py
@@ -264,7 +264,11 @@ class ImageSequential(ImageSequentialBase, ImageModuleForSequentialMixIn):
and len(mix_indices) != 0
and (torch.rand(1) < ((len(mix_indices) + len(indices)) / len(self))).item()
):
- mix_idx = torch.multinomial((~multinomial_weights.bool()).float(), 1)
+ # Draw among the mix members only: an ordinary member with weight 0 is also 0 in
+ # ``multinomial_weights`` and must not be inserted in their place.
+ mix_mask = torch.zeros_like(multinomial_weights, dtype=torch.float32)
+ mix_mask[mix_indices] = 1.0
+ mix_idx = torch.multinomial(mix_mask, 1)
if len(indices) == 0:
indices = mix_idx
else:
diff --git a/tests/augmentation/container/test_sequential_aug.py b/tests/augmentation/container/test_sequential_aug.py
index c1d84300a..01c973b55 100644
--- a/tests/augmentation/container/test_sequential_aug.py
+++ b/tests/augmentation/container/test_sequential_aug.py
@@ -176,6 +176,67 @@ class TestConventionImageSequential(BaseTester):
assert seq._params == []
self.assert_close(out, x, rtol=0.0, atol=0.0)
+ @pytest.mark.parametrize("container", [K.ImageSequential, K.AugmentationSequential])
+ def test_random_apply_mix_insertion_skips_zero_weight_members_6068(self, container, device, dtype):
+ # The mix-insertion step drew among every member whose weight was 0 once the mix weights were zeroed, which
+ # included ordinary members given weight 0, so RandomVerticalFlip was applied in about a third of the calls.
+ x = torch.rand(2, 3, 6, 8, device=device, dtype=dtype)
+ seq = container(
+ K.RandomHorizontalFlip(p=1.0),
+ K.RandomVerticalFlip(p=1.0),
+ K.RandomMixUpV2(p=1.0),
+ random_apply=1,
+ random_apply_weights=[1.0, 0.0, 1.0],
+ )
+ selected = set()
+ for seed in range(100):
+ torch.manual_seed(seed)
+ seq(x)
+ selected.add(tuple(p.name for p in seq._params))
+ assert selected == {("RandomHorizontalFlip_0",), ("RandomMixUpV2_2",)}
+
+ @pytest.mark.parametrize("container", [K.ImageSequential, K.AugmentationSequential])
+ def test_random_apply_mix_insertion_with_all_ordinary_weights_zero_6068(self, container, device, dtype):
+ # With every ordinary weight 0, nothing ordinary is drawn, and the insertion step may add only the mix member.
+ x = torch.rand(2, 3, 6, 8, device=device, dtype=dtype)
+ seq = container(
+ K.RandomHorizontalFlip(p=1.0),
+ K.RandomVerticalFlip(p=1.0),
+ K.RandomMixUpV2(p=1.0),
+ random_apply=(1, 2),
+ random_apply_weights=[0.0, 0.0, 1.0],
+ )
+ selected = set()
+ for seed in range(100):
+ torch.manual_seed(seed)
+ out = seq(x)
+ selected.add(tuple(p.name for p in seq._params))
+ if not seq._params:
+ self.assert_close(out, x, rtol=0.0, atol=0.0)
+ assert selected == {(), ("RandomMixUpV2_2",)}
+
+ @pytest.mark.parametrize("container", [K.ImageSequential, K.AugmentationSequential])
+ def test_random_apply_mix_insertion_draws_among_every_mix_member_6068(self, container, device, dtype):
+ # The insertion draws uniformly among the mix members and does not honor their own weights, as on main: both
+ # mix members are inserted although their weights are 0.
+ x = torch.rand(2, 3, 6, 8, device=device, dtype=dtype)
+ seq = container(
+ K.RandomHorizontalFlip(p=1.0),
+ K.RandomVerticalFlip(p=1.0),
+ K.RandomMixUpV2(p=1.0),
+ K.RandomMixUpV2(p=1.0),
+ random_apply=2,
+ random_apply_weights=[1.0, 1.0, 0.0, 0.0],
+ )
+ mixes = set()
+ for seed in range(30):
+ torch.manual_seed(seed)
+ seq(x)
+ names = [p.name for p in seq._params]
+ assert len(names) == 2
+ mixes.update(name for name in names if name.startswith("RandomMixUpV2"))
+ assert mixes == {"RandomMixUpV2_2", "RandomMixUpV2_3"}
+
def test_convention_slice_crop_inverse_restores_the_canvas(self, device, dtype):
seq = K.ImageSequential(K.CenterCrop((4, 6), p=1.0))
out = seq(torch.rand(1, 3, 6, 8, device=device, dtype=dtype))