Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

Fix RandomThinPlateSpline same_on_batch behaviour and add unit tests (#3465)

Dheeraj Alamurif99683dfebda
diff --git a/kornia/augmentation/_2d/geometric/thin_plate_spline.py b/kornia/augmentation/_2d/geometric/thin_plate_spline.py
index 87acc3a25..d21ee534d 100644
--- a/kornia/augmentation/_2d/geometric/thin_plate_spline.py
+++ b/kornia/augmentation/_2d/geometric/thin_plate_spline.py
@@ -73,8 +73,25 @@ class RandomThinPlateSpline(AugmentationBase2D):
def generate_parameters(self, shape: Tuple[int, ...]) -> Dict[str, torch.Tensor]:
B, _, _, _ = shape
- src = torch.tensor([[[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0], [0.0, 0.0]]]).expand(B, 5, 2) # Bx5x2
- dst = src + self.dist.rsample(src.shape)
+
+ device = self.device
+ dtype = self.dtype
+
+ # 5 TPS control points in normalized coordinates
+ src = torch.tensor(
+ [[[-1.0, -1.0], [-1.0, 1.0], [1.0, -1.0], [1.0, 1.0], [0.0, 0.0]]],
+ device=device,
+ dtype=dtype,
+ ).expand(B, 5, 2)
+
+ if self.same_on_batch:
+ noise = self.dist.rsample((1, 5, 2)).to(device=device, dtype=dtype)
+ noise = noise.expand(B, 5, 2)
+ else:
+ noise = self.dist.rsample((B, 5, 2)).to(device=device, dtype=dtype)
+
+ dst = src + noise
+
return {"src": src, "dst": dst}
def apply_transform(
diff --git a/tests/augmentation/test_augmentation.py b/tests/augmentation/test_augmentation.py
index 7e66aecef..564c894b7 100644
--- a/tests/augmentation/test_augmentation.py
+++ b/tests/augmentation/test_augmentation.py
@@ -4588,13 +4588,6 @@ class TestRandomElasticTransform(BaseTester):
assert labels_transformed[to_apply].ne(labels[to_apply]).any()
-class TestRandomThinPlateSpline:
- def test_smoke(self, device, dtype):
- img = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)
- aug = RandomThinPlateSpline(p=1.0)
- assert img.shape == aug(img).shape
-
-
class TestRandomBoxBlur:
def test_smoke(self, device, dtype):
img = torch.rand(1, 1, 2, 2, device=device, dtype=dtype)
@@ -5255,3 +5248,52 @@ class TestRandomDissolving(BaseTester):
aug = RandomDissolving(p=1.0, keepdim=True, version="1.5", cache_dir="weights/")
images_aug = aug(images)
assert images_aug.shape == images.shape
+
+
+class TestRandomThinPlateSpline(CommonTests):
+ possible_params: Dict["str", Tuple] = {}
+ _augmentation_cls = RandomThinPlateSpline
+ _default_param_set: Dict["str", Any] = {}
+
+ @pytest.fixture(params=[_default_param_set], scope="class")
+ def param_set(self, request):
+ return request.param
+
+ # Disable unsupported base tests
+ def test_smoke(self, param_set):
+ pytest.skip("RandomThinPlateSpline does not implement compute_transformation")
+
+ def test_module(self):
+ pytest.skip("RandomThinPlateSpline does not expose transform_matrix")
+
+ def test_random_p_1(self):
+ pytest.skip("RandomThinPlateSpline does not expose transform_matrix")
+
+ def test_inverse_coordinate_check(self):
+ pytest.skip("RandomThinPlateSpline does not expose transform_matrix")
+
+ def test_exception(self):
+ pytest.skip("RandomThinPlateSpline does not expose transform_matrix")
+
+ def test_batch(self):
+ pytest.skip("RandomThinPlateSpline does not expose transform_matrix")
+
+ def test_same_on_batch_true(self):
+ torch.manual_seed(0)
+ x = torch.randn(4, 3, 64, 64, device=self.device, dtype=self.dtype)
+ aug = self._augmentation_cls(p=1.0, same_on_batch=True)
+ _ = aug(x)
+ params = aug._params
+ assert (params["src"][0] == params["src"][1]).all()
+ for j in range(1, 4):
+ torch.testing.assert_close(params["dst"][0], params["dst"][j])
+
+ def test_same_on_batch_false(self):
+ torch.manual_seed(0)
+ x = torch.randn(4, 3, 64, 64, device=self.device, dtype=self.dtype)
+ aug = self._augmentation_cls(p=1.0, same_on_batch=False)
+ _ = aug(x)
+ params = aug._params
+ diffs = [(params["dst"][0] - params["dst"][j]).abs().sum().item() for j in range(1, 4)]
+
+ assert any(d > 0 for d in diffs)