π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix: correct Thin Plate Spline warp normalization for grid_sample (#3468)
Dheeraj Alamuri33307959b1f4
diff --git a/kornia/geometry/transform/thin_plate_spline.py b/kornia/geometry/transform/thin_plate_spline.pyindex 0777888e7..76f202f7e 100644--- a/kornia/geometry/transform/thin_plate_spline.py+++ b/kornia/geometry/transform/thin_plate_spline.py@@ -248,7 +248,7 @@ def warp_image_tps(raise ValueError(f"Invalid shape for affine_weights, expected BxNx2. Got {affine_weights.shape}")batch_size, _, h, w = image.shape- coords: torch.Tensor = create_meshgrid(h, w, device=image.device, dtype=image.dtype)+ coords: torch.Tensor = create_meshgrid(h, w, device=image.device, dtype=image.dtype, normalized_coordinates=True)coords = coords.reshape(-1, 2).expand(batch_size, -1, -1)warped: torch.Tensor = warp_points_tps(coords, kernel_centers, kernel_weights, affine_weights)warped = warped.view(-1, h, w, 2)diff --git a/tests/geometry/transform/test_thin_plate_spline.py b/tests/geometry/transform/test_thin_plate_spline.pyindex 22e6e69ea..c405daf01 100644--- a/tests/geometry/transform/test_thin_plate_spline.py+++ b/tests/geometry/transform/test_thin_plate_spline.py@@ -250,3 +250,15 @@ class TestWarpImage:op = kornia.geometry.transform.warp_image_tpsop_jit = torch.jit.script(op)assert_close(op(image, dst, kernel, affine), op_jit(image, dst, kernel, affine), rtol=1e-4, atol=1e-4)++ @pytest.mark.parametrize("batch_size", [1])+ def test_identity_warp_align_corners(self, batch_size, device, dtype):+ image = torch.arange(9.0, device=device, dtype=dtype).reshape(1, 1, 3, 3)+ dst = 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,+ ).repeat(batch_size, 1, 1)+ kernel, affine = kornia.geometry.transform.get_tps_transform(dst, dst)+ warped = kornia.geometry.transform.warp_image_tps(image, dst, kernel, affine, align_corners=True)+ assert_close(warped, image, atol=1e-4, rtol=1e-4)