π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(augmentation): name the intensity box handlers what transform_boxes calls (#4549)
Dipak Chaudharibe2f52a8401b
diff --git a/changelog.d/4549.fixed.md b/changelog.d/4549.fixed.mdnew file mode 100644index 000000000..953dcb7d6--- /dev/null+++ b/changelog.d/4549.fixed.md@@ -0,0 +1 @@+Calling `transform_boxes` directly on a 2D intensity augmentation returns the boxes unchanged instead of raising `NotImplementedError`: `IntensityAugmentationBase2D` now defines its box pass-through handlers under the names the dispatcher calls.diff --git a/docs/source/augmentation.base.rst b/docs/source/augmentation.base.rstindex 326745c62..0dcb2f902 100644--- a/docs/source/augmentation.base.rst+++ b/docs/source/augmentation.base.rst@@ -57,8 +57,7 @@ Custom Augmentation Classes`IntensityAugmentationBase2D` supplies an identity matrix and default passthrough handlers for most annotations.Subclasses can override these defaults: `RandomErasing` zero-fills the erased mask region. Direct intensity-`transform_boxes` calls currently raise because the base's box-handler names do not match the dispatcher-(`#4480 <https://github.com/kornia/kornia/issues/4480>`_); the container skips intensity transforms for boxes.+`transform_boxes` calls return the boxes unchanged; the container skips intensity transforms for boxes.`GeometricAugmentationBase2D` supplies the dispatch used for geometric coordinate transformations.For a geometric operation, implement `compute_transformation` and `apply_transform`. Supporting image inversiondiff --git a/kornia/augmentation/_2d/intensity/base.py b/kornia/augmentation/_2d/intensity/base.pyindex 18e47963e..19b071c3a 100644--- a/kornia/augmentation/_2d/intensity/base.py+++ b/kornia/augmentation/_2d/intensity/base.py@@ -40,10 +40,8 @@ class IntensityAugmentationBase2D(RigidAffineAugmentationBase2D):Convention:- an intensity augmentation moves no image pixel, so this base supplies ``compute_transformation``- itself and reports the identity matrix. Its direct mask and keypoint handlers pass their inputs through,- except where a subclass overrides them: :class:`RandomErasing` also erases masks. Direct box dispatch- is currently unsupported despite the similarly named plural helpers below; see- `#4480 <https://github.com/kornia/kornia/issues/4480>`_.+ itself and reports the identity matrix. Its direct mask, box and keypoint handlers pass their inputs+ through, except where a subclass overrides them: :class:`RandomErasing` also erases masks.A subclass supplies ``apply_transform`` and, where it draws anything, its ``_param_generator``.- the matrix is built lazily, on the first read of ``transform_matrix``.- this base adds no ``inverse``. When@@ -72,12 +70,12 @@ class IntensityAugmentationBase2D(RigidAffineAugmentationBase2D):) -> Tensor:return input- def apply_non_transform_boxes(+ def apply_non_transform_box(self, input: Boxes, params: Dict[str, Tensor], flags: Dict[str, Any], transform: Optional[Tensor] = None) -> Boxes:return input- def apply_transform_boxes(+ def apply_transform_box(self, input: Boxes, params: Dict[str, Tensor], flags: Dict[str, Any], transform: Optional[Tensor] = None) -> Boxes:return inputdiff --git a/tests/augmentation/test_base.py b/tests/augmentation/test_base.pyindex b4cdcd514..cc44fbd45 100644--- a/tests/augmentation/test_base.py+++ b/tests/augmentation/test_base.py@@ -799,13 +799,15 @@ class TestConventionAugmentationBase2D(BaseTester):float_mask.bool(), augmentation._params, augmentation.flags, transform=augmentation.transform_matrix)- def test_wart_intensity_container_boxes_passthrough_but_direct_dispatch_raises_4480(self, device, dtype):+ @pytest.mark.parametrize("p", [0.0, 1.0])+ def test_intensity_boxes_pass_through_direct_dispatch_and_container_4480(self, device, dtype, p):image = torch.ones(1, 1, 4, 4, device=device, dtype=dtype)boxes = Boxes.from_tensor(torch.tensor([[[0.0, 0.0, 2.0, 2.0]]], device=device, dtype=dtype), mode="xyxy")- augmentation = K.RandomInvert(p=1.0)+ augmentation = K.RandomInvert(p=p)augmentation(image)- with pytest.raises(NotImplementedError):- augmentation.transform_boxes(boxes, augmentation._params, augmentation.flags)+ direct = augmentation.transform_boxes(boxes, augmentation._params, augmentation.flags)+ assert direct.mode == boxes.mode+ self.assert_close(direct.data, boxes.data)container = K.AugmentationSequential(K.RandomInvert(p=1.0), data_keys=["input", "bbox_xyxy"])_, output_boxes = container(image, boxes)self.assert_close(output_boxes.data, boxes.data)