Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

use resample instead of mode argument in RandomElasticTransform per default (#2017)

Jan Sellnerb1fe045aac88
diff --git a/kornia/augmentation/_2d/geometric/elastic_transform.py b/kornia/augmentation/_2d/geometric/elastic_transform.py
index eef793301..853db9f07 100644
--- a/kornia/augmentation/_2d/geometric/elastic_transform.py
+++ b/kornia/augmentation/_2d/geometric/elastic_transform.py
@@ -1,9 +1,11 @@
-from typing import Any, Dict, Optional, Tuple
+import warnings
+from typing import Any, Dict, Optional, Tuple, Union
import torch
from torch import Tensor
from kornia.augmentation._2d.geometric.base import GeometricAugmentationBase2D
+from kornia.constants import Resample
from kornia.geometry.transform import elastic_transform2d
@@ -19,7 +21,9 @@ class RandomElasticTransform(GeometricAugmentationBase2D):
alpha: The scaling factor that controls the intensity of the deformation
in the y and x directions, respectively.
align_corners: Interpolation flag used by `grid_sample`.
- mode: Interpolation mode used by `grid_sample`. Either 'bilinear' or 'nearest'.
+ 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.
@@ -49,7 +53,8 @@ class RandomElasticTransform(GeometricAugmentationBase2D):
sigma: Tuple[float, float] = (32.0, 32.0),
alpha: Tuple[float, float] = (1.0, 1.0),
align_corners: bool = False,
- mode: str = "bilinear",
+ resample: Union[str, int, Resample] = Resample.BILINEAR.name,
+ mode: Optional[str] = None,
padding_mode: str = "zeros",
same_on_batch: bool = False,
p: float = 0.5,
@@ -59,12 +64,21 @@ class RandomElasticTransform(GeometricAugmentationBase2D):
super().__init__(
p=p, return_transform=return_transform, same_on_batch=same_on_batch, p_batch=1.0, keepdim=keepdim
)
+ if mode is not None:
+ resample = mode
+ if not torch.jit.is_scripting():
+ warnings.warn(
+ "`mode` argument has been deprecated. Please use the `resample` argument instead",
+ DeprecationWarning,
+ stacklevel=2,
+ )
+
self.flags = dict(
kernel_size=kernel_size,
sigma=sigma,
alpha=alpha,
align_corners=align_corners,
- mode=mode,
+ resample=Resample.get(resample),
padding_mode=padding_mode,
)
@@ -90,6 +104,6 @@ class RandomElasticTransform(GeometricAugmentationBase2D):
flags["sigma"],
flags["alpha"],
flags["align_corners"],
- flags["mode"],
+ flags["resample"].name.lower(),
flags["padding_mode"],
)
diff --git a/test/augmentation/test_augmentation.py b/test/augmentation/test_augmentation.py
index 58456b606..fce1e8e3b 100644
--- a/test/augmentation/test_augmentation.py
+++ b/test/augmentation/test_augmentation.py
@@ -3580,6 +3580,24 @@ class TestRandomElasticTransform:
res = f(input)
assert (res[0] == res[1]).all()
+ def test_mask_transform(self, device, dtype):
+ torch.manual_seed(0)
+ features = torch.rand(1, 1, 4, 4, dtype=dtype, device=device)
+ labels = torch.ones(1, 1, 4, 4, dtype=dtype, device=device) * 10
+ labels[:, :, :, :2] = 0
+ labels[:, :, :2, :] = 0
+
+ compose = AugmentationSequential(RandomElasticTransform(alpha=(10, 10)))
+
+ # Use an example which would produce invalid label values
+ torch.manual_seed(0)
+ labels_transformed = compose(features, labels, data_keys=["input", "input"])[1]
+ assert len(labels_transformed.unique()) > 2
+
+ # The transformed values are fine if we use mask input type
+ labels_transformed = compose(features, labels, data_keys=["input", "mask"])[1]
+ assert torch.all(labels_transformed.unique() == torch.tensor([0, 10], dtype=dtype, device=device))
+
class TestRandomThinPlateSpline:
def test_smoke(self, device, dtype):