Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Reject mismatched input counts in many-to-many augmentation (#4461)

Sun Haoyuan5953acfc7c83
diff --git a/CHANGELOG.md b/CHANGELOG.md
index f3afbaadb..363c94547 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -392,6 +392,9 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Bug fixes
+* `ManyToManyAugmentationDispather` now rejects mismatched numbers of input bundles and
+ augmentations before applying any transformation, instead of silently dropping surplus inputs
+ or skipping augmentations. (#4461)
* `PatchSequential` now augments every patch of every image and runs the complete selected sequence.
The padding, reconstruction and parameter-generation behaviour changes are listed under
*Breaking changes*. Refs #4421. (#4460)
diff --git a/kornia/augmentation/container/dispatcher.py b/kornia/augmentation/container/dispatcher.py
index 72787a6e9..aae589272 100644
--- a/kornia/augmentation/container/dispatcher.py
+++ b/kornia/augmentation/container/dispatcher.py
@@ -67,7 +67,14 @@ class ManyToManyAugmentationDispather(nn.Module):
Returns:
Outputs from each augmentation, preserving input order.
+
+ Raises:
+ ValueError: If the number of input bundles differs from the number of augmentations.
"""
+ if len(input) != len(self.augmentations):
+ raise ValueError(
+ f"Expected {len(self.augmentations)} input bundles, one per augmentation, but got {len(input)}."
+ )
return [aug(*inp) for inp, aug in zip(input, self.augmentations)]
diff --git a/tests/augmentation/container/test_dispatcher.py b/tests/augmentation/container/test_dispatcher.py
index 1133c6891..dfd3c4da4 100644
--- a/tests/augmentation/container/test_dispatcher.py
+++ b/tests/augmentation/container/test_dispatcher.py
@@ -21,8 +21,49 @@ import torch
import kornia
import kornia.augmentation as K
+from testing.base import BaseTester
+
+
+class TestDispatcher(BaseTester):
+ @pytest.mark.parametrize("num_augmentations,num_inputs", [(2, 1), (2, 3), (2, 0), (0, 1)])
+ def test_many_to_many_input_count(self, num_augmentations, num_inputs, device, dtype):
+ image = torch.arange(48, device=device, dtype=dtype).reshape(2, 1, 4, 6)
+ mask = image.remainder(2)
+ augmentations = [
+ K.AugmentationSequential(K.RandomHorizontalFlip(p=1.0), data_keys=["input", "mask"])
+ for _ in range(num_augmentations)
+ ]
+ dispatcher = K.ManyToManyAugmentationDispather(*augmentations)
+
+ message = f"Expected {num_augmentations} input bundles, one per augmentation, but got {num_inputs}\\."
+ with pytest.raises(ValueError, match=message):
+ dispatcher(*[(image, mask)] * num_inputs)
+
+ # Validate before applying any augmentation or updating its replay parameters.
+ assert all(augmentation._params is None for augmentation in augmentations)
+
+ @pytest.mark.parametrize("bundle_type", [tuple, list])
+ def test_many_to_many_corresponding_inputs(self, bundle_type, device, dtype):
+ image_1 = torch.arange(48, device=device, dtype=dtype).reshape(2, 1, 4, 6)
+ image_2 = image_1 + 48
+ mask = image_2.remainder(3)
+ dispatcher = K.ManyToManyAugmentationDispather(
+ K.AugmentationSequential(K.RandomHorizontalFlip(p=1.0), data_keys=["input"]),
+ K.AugmentationSequential(K.RandomVerticalFlip(p=1.0), data_keys=["input", "mask"]),
+ )
+
+ output = dispatcher(bundle_type([image_1]), bundle_type([image_2, mask]))
+
+ assert len(output) == 2
+ self.assert_close(output[0], image_1.flip(-1), rtol=0, atol=0)
+ self.assert_close(output[1][0], image_2.flip(-2), rtol=0, atol=0)
+ self.assert_close(output[1][1], mask.flip(-2), rtol=0, atol=0)
+
+ @pytest.mark.device_agnostic
+ def test_many_to_many_empty(self):
+ # Preserve current behavior; rejecting an empty dispatcher remains under discussion in #4422.
+ assert K.ManyToManyAugmentationDispather()() == []
-class TestDispatcher:
def test_many_to_many(self, device, dtype):
input_1 = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)
input_2 = torch.randn(2, 3, 5, 6, device=device, dtype=dtype)