🐍 Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
Add support for class data keys in AugmentationSequential (#2536)
Miquel Martíad0927f04875
diff --git a/kornia/augmentation/container/augment.py b/kornia/augmentation/container/augment.pyindex 39b646dbe..3dcf1c6b5 100644--- a/kornia/augmentation/container/augment.py+++ b/kornia/augmentation/container/augment.py@@ -275,6 +275,8 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):inp.append(self._preproc_keypoints(arg, dcate))elif DataKey.get(dcate) in _BOXES_OPTIONS:inp.append(self._preproc_boxes(arg, dcate))+ elif DataKey.get(dcate) is DataKey.CLASS:+ inp.append(arg)else:raise NotImplementedError(f"input type of {dcate} is not implemented.")return inp@@ -307,6 +309,9 @@ class AugmentationSequential(TransformMatrixMinIn, ImageSequential):_out_b = _out_b.type(in_arg.dtype)out.append(_out_b)+ elif DataKey.get(dcate) is DataKey.CLASS:+ out.append(out_arg)+else:raise NotImplementedError(f"input type of {dcate} is not implemented.")diff --git a/kornia/augmentation/container/ops.py b/kornia/augmentation/container/ops.pyindex 85f14c040..3d8694f1c 100644--- a/kornia/augmentation/container/ops.py+++ b/kornia/augmentation/container/ops.py@@ -91,6 +91,8 @@ class AugmentationSequentialOps:return BoxSequentialOpsif data_key == DataKey.KEYPOINTS:return KeypointSequentialOps+ if data_key == DataKey.CLASS:+ return ClassSequentialOpsraise RuntimeError(f"Operation for `{data_key.name}` is not found.")def transform(@@ -195,6 +197,22 @@ class InputSequentialOps(SequentialOpsInterface[Tensor]):return input+class ClassSequentialOps(SequentialOpsInterface[Tensor]):+ """Apply and inverse transformations for class labels if needed."""++ @classmethod+ def transform(cls, input: Tensor, module: Module, param: ParamItem, extra_args: Dict[str, Any] = {}) -> Tensor:+ if isinstance(module, K.MixAugmentationBaseV2):+ raise NotImplementedError(+ "The support for class labels for mix augmentations that change the class label is not yet supported."+ )+ return input++ @classmethod+ def inverse(cls, input: Tensor, module: Module, param: ParamItem, extra_args: Dict[str, Any] = {}) -> Tensor:+ return input++class MaskSequentialOps(SequentialOpsInterface[Tensor]):"""Apply and inverse transformations for mask tensors."""diff --git a/test/augmentation/test_container.py b/test/augmentation/test_container.pyindex 1f7c09f16..33ac60702 100644--- a/test/augmentation/test_container.py+++ b/test/augmentation/test_container.py@@ -505,6 +505,14 @@ class TestAugmentationSequential:for i in range(len(bbox)):assert len(bboxes_transformed[i]) == len(bbox[i])+ def test_class(self, device, dtype):+ img = torch.zeros((5, 1, 5, 5))+ labels = torch.randint(0, 10, size=(5, 1))+ aug = K.AugmentationSequential(K.RandomCrop((3, 3), pad_if_needed=True), data_keys=['input', 'class'])++ _, out_labels = aug(img, labels)+ assert labels is out_labels+@pytest.mark.parametrize('random_apply', [1, (2, 2), (1, 2), (2,), 10, True, False])def test_forward_and_inverse(self, random_apply, device, dtype):inp = torch.randn(1, 3, 1000, 500, device=device, dtype=dtype)