π Geometric Computer Vision Library for Spatial AI
Python artificial-intelligencecomputer-visiondeep-learninghacktoberfestimage-processingmachine-learningneural-networkpythonpytorchroboticsspatial-ai
fix(feature): align SOLD2 CUDA normalization (#5646)
Karnajeet Gosavi7f0b8ba557b3
diff --git a/changelog.d/5646.fixed.md b/changelog.d/5646.fixed.mdnew file mode 100644index 000000000..63a43007f--- /dev/null+++ b/changelog.d/5646.fixed.md@@ -0,0 +1 @@+`keypoints_to_grid`, used by `WunschLineMatcher`, divides by the image size as a tensor. On CUDA it is now correctly rounded and matches CPU and the reference normalization; before, it multiplied by a rounded reciprocal and could differ in the last bit.diff --git a/kornia/feature/sold2/sold2.py b/kornia/feature/sold2/sold2.pyindex 410e713ca..561871e46 100644--- a/kornia/feature/sold2/sold2.py+++ b/kornia/feature/sold2/sold2.py@@ -355,9 +355,7 @@ def keypoints_to_grid(keypoints: torch.Tensor, img_size: Tuple[int, int]) -> tor"""KORNIA_CHECK_SHAPE(keypoints, ["N", "2"])n_points = len(keypoints)- xy = keypoints[:, [1, 0]]- grid_points = torch.stack((xy[:, 0] * 2.0 / img_size[1] - 1.0, xy[:, 1] * 2.0 / img_size[0] - 1.0), dim=-1)- return grid_points.view(-1, n_points, 1, 2)+ return (keypoints * 2.0 / torch.tensor(img_size, device=keypoints.device) - 1.0)[:, [1, 0]].view(-1, n_points, 1, 2)def batched_linspace(start: torch.Tensor, end: torch.Tensor, step: int, dim: int) -> torch.Tensor:diff --git a/tests/augmentation/test_augmentation_mix.py b/tests/augmentation/test_augmentation_mix.pyindex 92daad770..5616010d5 100644--- a/tests/augmentation/test_augmentation_mix.py+++ b/tests/augmentation/test_augmentation_mix.py@@ -519,9 +519,11 @@ class TestRandomCutMixV2(BaseTester):_, out_label = f(input, label)w, h = infer_bbox_shape(f._params["crop_src"][0])- expected_lambda = w.to(torch.float64) * h.to(torch.float64) / (15 * 17)+ expected_lambda = (+ w.to(device=device, dtype=torch.float64) * h.to(device=device, dtype=torch.float64) / (15 * 17)+ )- self.assert_close(out_label[0, :, 2], expected_lambda, rtol=0.0, atol=0.0)+ self.assert_close(out_label[0, :, 2], expected_lambda, rtol=0.0, atol=1e-15)def test_cutmix_prob_single_gate(self, device, dtype):# Regression test for #4649: p must not be applied twicediff --git a/tests/feature/test_local_features_orientation.py b/tests/feature/test_local_features_orientation.pyindex 91d9cd7f5..9ebae8e0f 100644--- a/tests/feature/test_local_features_orientation.py+++ b/tests/feature/test_local_features_orientation.py@@ -213,7 +213,7 @@ class TestOrientationHalfPrecisionIsFinite(BaseTester):if not supports_replicate_padding(device, torch.float16):pytest.skip(f"no float16 replicate-pad kernel on {device.type}")torch.manual_seed(0)- patches = torch.rand(4, 1, 32, 32, device=device)+ patches = torch.rand(4, 1, 32, 32).to(device)ref = PatchDominantGradientOrientation(32).to(device)(patches)out = PatchDominantGradientOrientation(32).to(device, torch.float16)(patches.half())self.assert_close(out.float(), ref, atol=5e-2, rtol=0)