π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): align empty mask shapes with images (#5706)
Manu-2717b9d71f9728f4
diff --git a/changelog.d/5706.breaking.md b/changelog.d/5706.breaking.mdnew file mode 100644index 000000000..f88a032f8--- /dev/null+++ b/changelog.d/5706.breaking.md@@ -0,0 +1,8 @@+On an empty batch, an always-applied (`p=1`) shape-changing 2D augmentation now gives a mask the height and width it+gives the image. It used to return the mask at its input size: `AugmentationSequential(Resize((4, 5)),+data_keys=["input", "mask"])` on a `(0, 3, 8, 10)` image and a `(0, 1, 8, 10)` mask gave a `(0, 3, 4, 5)` image next+to a `(0, 1, 8, 10)` mask, and passing that pair to `AugmentationSequential.inverse` raised `ValueError`. The mask is+now `(0, 1, 4, 5)` and the inverse restores `(0, 1, 8, 10)`. This covers `Resize`, `LongestMaxSize`,+`SmallestMaxSize`, `CenterCrop`, `RandomCrop`, `RandomResizedCrop` and `PadTo`, in `AugmentationSequential` (also with+dictionary inputs and inside `VideoSequential`) and in a direct `transform_masks` call. With `p < 1` an empty image and+mask both keep their input size, as before. Non-empty batches are unchanged. Part of #4429.diff --git a/kornia/augmentation/base.py b/kornia/augmentation/base.pyindex 7fc21aeef..f37861ef9 100644--- a/kornia/augmentation/base.py+++ b/kornia/augmentation/base.py@@ -481,7 +481,12 @@ class _AugmentationBase(_BasicAugmentationBase):output_transformed = self.apply_transform_mask(in_tensor, params, flags, transform=transform)output_not_transformed = self.apply_non_transform_mask(in_tensor, params, flags, transform=transform)- output = self._blend_by_prob(output_transformed, output_not_transformed, to_apply)+ # With an empty, statically always-applied batch, the image path takes the transformed shape.+ # An empty gate has no rows to blend, so keep the mask on that same canvas (#4429).+ if self.p == 1.0 and self.p_batch == 1.0 and batch_prob.numel() == 0:+ output = output_transformed+ else:+ output = self._blend_by_prob(output_transformed, output_not_transformed, to_apply)if not self.keepdim:return outputdiff --git a/tests/augmentation/container/test_augmentation_sequential.py b/tests/augmentation/container/test_augmentation_sequential.pyindex 5bbe52e06..791b8616a 100644--- a/tests/augmentation/container/test_augmentation_sequential.py+++ b/tests/augmentation/container/test_augmentation_sequential.py@@ -1873,6 +1873,51 @@ class TestConventionAugmentationSequential(BaseTester):assert restored_image.shape == restored_mask.shape == (0, 1, 2, 2)assert restored_mask.dtype == mask.dtype+ @pytest.mark.parametrize(+ "make_op", [lambda: K.Resize((4, 5)), lambda: K.CenterCrop((4, 5)), lambda: K.RandomCrop((4, 5), p=1.0)]+ )+ @pytest.mark.parametrize("as_dict", [False, True])+ @pytest.mark.parametrize("mask_dtype", [torch.int64, torch.bool])+ def test_empty_batch_shape_changing_mask_follows_image_4429(self, make_op, as_dict, mask_dtype, device, dtype):+ image = torch.empty(0, 3, 8, 10, device=device, dtype=dtype)+ mask = torch.empty(0, 1, 8, 10, device=device, dtype=mask_dtype)+ seq = K.AugmentationSequential(make_op(), data_keys=None if as_dict else ["input", "mask"])++ if as_dict:+ result = seq({"input": image, "mask": mask})+ out_image, out_mask = result["input"], result["mask"]+ else:+ out_image, out_mask = seq(image, mask)++ assert out_image.shape == (0, 3, 4, 5)+ assert out_mask.shape == (0, 1, 4, 5)+ assert out_image.device == out_mask.device == device+ assert out_image.dtype == dtype+ assert out_mask.dtype == mask_dtype++ @pytest.mark.parametrize(+ "make_op",+ [+ lambda p: K.Resize((4, 5), p=p),+ lambda p: K.CenterCrop((4, 5), cropping_mode="resample", p=p),+ lambda p: K.RandomCrop((4, 5), cropping_mode="resample", p=p),+ ],+ ids=["resize", "center-crop", "crop"],+ )+ @pytest.mark.parametrize("p", [0.5, 1.0])+ def test_empty_batch_mask_round_trip_follows_image_4429(self, make_op, p, device, dtype):+ # The mask takes the image's canvas under every gate, and the inverse restores both. With p < 1 the empty+ # image keeps its input size (the blend fallback tracked in #4429), so only the parity is pinned here.+ image = torch.empty(0, 3, 8, 10, device=device, dtype=dtype)+ mask = torch.empty(0, 1, 8, 10, device=device, dtype=torch.int64)+ seq = K.AugmentationSequential(make_op(p), data_keys=["input", "mask"])+ out_image, out_mask = seq(image, mask)+ assert out_mask.shape == (0, 1, *out_image.shape[-2:])+ restored_image, restored_mask = seq.inverse(out_image, out_mask)+ assert restored_image.shape == image.shape+ assert restored_mask.shape == mask.shape+ assert restored_mask.dtype == mask.dtype+def test_dictionary_preserves_metadata_and_input_4483(self, device, dtype):seq = K.AugmentationSequential(K.RandomHorizontalFlip(p=1.0), data_keys=None)metadata = {"imagenet_id": 7, "maskrcnn_boxes": "unchanged", "labelled_image": None, "note": "retained"}