Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix AugmentationSequential inverse for mix augmentations (#4698)

NiteeshGowda72b89d89acc3dd
diff --git a/changelog.d/4698.fixed.md b/changelog.d/4698.fixed.md
new file mode 100644
index 000000000..56992313a
--- /dev/null
+++ b/changelog.d/4698.fixed.md
@@ -0,0 +1 @@
+Fixed `AugmentationSequential.inverse()` silently ignoring unsupported mix augmentations.
diff --git a/kornia/augmentation/container/ops.py b/kornia/augmentation/container/ops.py
index c7d4f44a7..9996554b2 100644
--- a/kornia/augmentation/container/ops.py
+++ b/kornia/augmentation/container/ops.py
@@ -329,6 +329,8 @@ class InputSequentialOps(SequentialOpsInterface[torch.Tensor]):
extra_args = {}
if isinstance(module, K.GeometricAugmentationBase2D):
input = module.inverse(input, params=cls.get_instance_module_param(param), **extra_args)
+ elif isinstance(module, K.MixAugmentationBaseV2):
+ input = module.inverse(**extra_args)
elif isinstance(module, (K.GeometricAugmentationBase3D,)):
raise NotImplementedError(
"The support for 3d inverse operations are not yet supported. You are welcome to file a PR in our repo."
diff --git a/tests/augmentation/container/test_augmentation_sequential.py b/tests/augmentation/container/test_augmentation_sequential.py
index 9a8378592..78ed3e035 100644
--- a/tests/augmentation/container/test_augmentation_sequential.py
+++ b/tests/augmentation/container/test_augmentation_sequential.py
@@ -747,6 +747,46 @@ class TestConventionAugmentationSequential(BaseTester):
self.assert_close(out_masks[1], output[:1])
assert not torch.equal(out_masks[1], output[1:])
+ def test_mix_augmentation_inverse_raises_4693(self, device, dtype):
+ image = torch.stack([torch.full((2, 4, 6), float(i + 1), device=device, dtype=dtype) for i in range(3)])
+ mask = torch.zeros(3, 4, 6, device=device, dtype=torch.long)
+
+ for i in range(3):
+ mask[i, :2, :3] = i + 1
+
+ seq = K.AugmentationSequential(
+ K.RandomTransplantation(p=1.0),
+ data_keys=["input", "mask"],
+ )
+
+ torch.manual_seed(7)
+ output = seq(image, mask)
+
+ assert not torch.equal(output[0], image)
+
+ with pytest.raises(RuntimeError, match="Inverse for RandomTransplantation is not supported"):
+ seq.inverse(*output)
+
+ def test_mix_augmentation_3d_inverse_raises_4693(self, device, dtype):
+ image = torch.stack([torch.full((2, 3, 4, 5), float(i + 1), device=device, dtype=dtype) for i in range(3)])
+ mask = torch.zeros(3, 3, 4, 5, device=device, dtype=torch.long)
+
+ for i in range(3):
+ mask[i, :2, :2, :3] = i + 1
+
+ seq = K.AugmentationSequential(
+ K.RandomTransplantation3D(p=1.0),
+ data_keys=["input", "mask"],
+ )
+
+ torch.manual_seed(7)
+ output = seq(image, mask)
+
+ assert not torch.equal(output[0], image)
+
+ with pytest.raises(RuntimeError, match="Inverse for RandomTransplantation3D is not supported"):
+ seq.inverse(*output)
+
def test_mix_children_dispatch_annotation_keys_4493(self, device, dtype):
# Regression (#4493): mix children used to fall through Mask/Box/KeypointSequentialOps to a silent
# passthrough, so annotations desynchronized from the mixed image. The container now dispatches to
diff --git a/tests/augmentation/container/test_sequential_aug.py b/tests/augmentation/container/test_sequential_aug.py
index 64384dfd0..de4b3ba3c 100644
--- a/tests/augmentation/container/test_sequential_aug.py
+++ b/tests/augmentation/container/test_sequential_aug.py
@@ -70,7 +70,6 @@ class TestSequential:
)
out = aug(inp)
assert out.shape == inp.shape
- aug.inverse(inp)
reproducibility_test(inp, aug)