π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(geometry): harmonise dtypes in transform_points bmm (#3707)
funnym0nk3y1a3af2a34165
diff --git a/kornia/geometry/linalg.py b/kornia/geometry/linalg.pyindex 111297c25..7e14f4c6f 100644--- a/kornia/geometry/linalg.py+++ b/kornia/geometry/linalg.py@@ -218,15 +218,19 @@ def transform_points(trans_01: torch.Tensor, points_1: torch.Tensor) -> torch.Tetrans_01 = torch.repeat_interleave(trans_01, repeats=int(points_1.shape[0] // trans_01.shape[0]), dim=0)# to homogeneouspoints_1_h = convert_points_to_homogeneous(points_1) # BxNxD+1+ # `torch.bmm` requires both operands to share a scalar type. Callers may pass coordinate+ # tensors in a different dtype than the transform matrix (e.g. fp32 bbox + fp16 image), so+ # harmonise at the bmm site and restore the caller's coordinate dtype on return.+ points_dtype = points_1_h.dtype# transform coordinates- points_0_h = torch.bmm(points_1_h, trans_01.permute(0, 2, 1))+ points_0_h = torch.bmm(points_1_h.to(trans_01.dtype), trans_01.permute(0, 2, 1))# to euclideanpoints_0 = convert_points_from_homogeneous(points_0_h) # BxNxD# reshape to the input shapeshape_inp[-2] = points_0.shape[-2]shape_inp[-1] = points_0.shape[-1]points_0 = points_0.reshape(shape_inp)- return points_0+ return points_0.to(points_dtype)def point_line_distance(point: torch.Tensor, line: torch.Tensor, eps: float = 1e-9) -> torch.Tensor:diff --git a/tests/augmentation/container/test_augmentation_sequential.py b/tests/augmentation/container/test_augmentation_sequential.pyindex f23acf6a0..d6205b4ca 100644--- a/tests/augmentation/container/test_augmentation_sequential.py+++ b/tests/augmentation/container/test_augmentation_sequential.py@@ -107,6 +107,24 @@ class TestAugmentationSequential:out = aug_list(input)assert out.shape == input.shape+ @pytest.mark.parametrize("image_dtype", [torch.float16, torch.float32, torch.float64, torch.bfloat16])+ def test_mixed_image_bbox_dtypes(self, device, image_dtype):+ # Regression test for https://github.com/kornia/kornia/issues/3705 and #3706:+ # bbox stays in fp32 while the image uses a half/double compute dtype.+ torch.manual_seed(0)+ img = torch.rand(2, 3, 32, 32, device=device, dtype=image_dtype)+ bb = torch.tensor(+ [[[[4.0, 4.0], [12.0, 4.0], [12.0, 12.0], [4.0, 12.0]]]] * 2,+ device=device,+ dtype=torch.float32,+ )+ aug = K.AugmentationSequential(K.RandomAffine(degrees=10, p=1.0), data_keys=["image", "bbox"])+ out_img, out_bb = aug(img, bb)+ assert out_img.dtype == image_dtype+ assert out_bb.dtype == torch.float32+ assert out_img.shape == img.shape+ assert out_bb.shape == bb.shape+def test_random_flips(self, device, dtype):inp = torch.randn(1, 3, 510, 1020, device=device, dtype=dtype)bbox = torch.tensor([[[355, 10], [660, 10], [660, 250], [355, 250]]], device=device, dtype=dtype)diff --git a/tests/geometry/test_linalg.py b/tests/geometry/test_linalg.pyindex 0d4577ce3..a0d7ef116 100644--- a/tests/geometry/test_linalg.py+++ b/tests/geometry/test_linalg.py@@ -67,6 +67,16 @@ class TestTransformPoints(BaseTester):expected = op(transform, points)self.assert_close(actual, expected, atol=1e-4, rtol=1e-4)+ @pytest.mark.parametrize("trans_dtype", [torch.float16, torch.float32, torch.float64])+ @pytest.mark.parametrize("points_dtype", [torch.float16, torch.float32, torch.float64])+ def test_mixed_dtypes(self, device, trans_dtype, points_dtype):+ # Regression test for https://github.com/kornia/kornia/issues/3705+ points_src = torch.rand(2, 3, 2, device=device, dtype=points_dtype)+ trans = kornia.core.ops.eye_like(3, points_src).to(trans_dtype)+ out = kgl.transform_points(trans, points_src)+ assert out.dtype == points_dtype+ self.assert_close(out.to(torch.float32), points_src.to(torch.float32), atol=1e-2, rtol=1e-2)+class TestComposeTransforms(BaseTester):def test_smoke(self, device, dtype):