Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix elastic transformation if only partially applied (#2303)

Jan Sellner9ed192a19625
diff --git a/kornia/augmentation/_2d/geometric/elastic_transform.py b/kornia/augmentation/_2d/geometric/elastic_transform.py
index 0841fa412..f3ff07101 100644
--- a/kornia/augmentation/_2d/geometric/elastic_transform.py
+++ b/kornia/augmentation/_2d/geometric/elastic_transform.py
@@ -22,8 +22,6 @@ class RandomElasticTransform(AugmentationBase2D):
in the y and x directions, respectively.
align_corners: Interpolation flag used by `grid_sample`.
resample: Interpolation mode used by `grid_sample`. Either 'nearest' (0) or 'bilinear' (1).
- mode: Deprecated: Interpolation mode used by `grid_sample`. Either 'bilinear' or 'nearest'.
- Please use the `resample` argument instead.
padding_mode: The padding used by ```grid_sample```. Either 'zeros', 'border' or 'refection'.
same_on_batch: apply the same transformation across the batch.
p: probability of applying the transformation.
@@ -92,6 +90,11 @@ class RandomElasticTransform(AugmentationBase2D):
flags["padding_mode"],
)
+ def apply_non_transform_mask(
+ self, input: Tensor, params: Dict[str, Tensor], flags: Dict[str, Any], transform: Optional[Tensor] = None
+ ) -> Tensor:
+ return input
+
def apply_transform_mask(
self, input: Tensor, params: Dict[str, Tensor], flags: Dict[str, Any], transform: Optional[Tensor] = None
) -> Tensor:
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index b1fbd35b6..b04375ec9 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -1,4 +1,5 @@
from typing import Any, Dict, Optional, Tuple, Type
+from unittest.mock import patch
import pytest
import torch
@@ -3704,6 +3705,29 @@ class TestRandomElasticTransform:
labels_transformed = compose(features, labels, data_keys=["input", "mask"])[1]
assert_close(labels_transformed.unique(), torch.tensor([0, 10], dtype=dtype, device=device))
+ @pytest.mark.parametrize("batch_prob", [[True, True], [False, True], [False, False]])
+ def test_apply(self, batch_prob, device, dtype):
+ torch.manual_seed(0)
+
+ aug_list = AugmentationSequential(RandomElasticTransform(sigma=(2, 2), alpha=(2, 2)))
+ features = torch.rand(2, 3, 10, 10, dtype=dtype, device=device)
+ labels = torch.randint(0, 10, (2, 1, 10, 10), dtype=dtype, device=device)
+
+ # Make sure the transformation works correctly even if only applied to some images in the batch
+ to_apply = torch.tensor(batch_prob, device=device)
+ with patch.object(aug_list[0], '__batch_prob_generator__', return_value=to_apply):
+ features_transformed, labels_transformed = aug_list(features, labels, data_keys=["input", "mask"])
+ assert_close(aug_list._params[0].data["batch_prob"], to_apply)
+
+ # Images should remain unchanged if the transformation is not applied
+ assert_close(features_transformed[~to_apply], features[~to_apply])
+ assert_close(labels_transformed[~to_apply], labels[~to_apply])
+
+ # At least one value in the images should change if the transformation is applied
+ if to_apply.any():
+ assert features_transformed[to_apply].ne(features[to_apply]).any()
+ assert labels_transformed[to_apply].ne(labels[to_apply]).any()
+
class TestRandomThinPlateSpline:
def test_smoke(self, device, dtype):