Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): preserve points dtype in _fit_line_ols_2d vertical fallback (#5015) (#5026)

yi11136d8afdb85f5
diff --git a/changelog.d/5026.fixed.md b/changelog.d/5026.fixed.md
new file mode 100644
index 000000000..e379ccd1c
--- /dev/null
+++ b/changelog.d/5026.fixed.md
@@ -0,0 +1,3 @@
+`fit_line` now returns the direction in the input's dtype for unweighted 2-D `float16` and `bfloat16` points.
+Its vertical-line fallback constant had no dtype, so `torch.where` promoted the direction to `float32` while the
+origin kept the input's dtype.
diff --git a/kornia/geometry/line.py b/kornia/geometry/line.py
index fe183fb6a..2c41b1311 100644
--- a/kornia/geometry/line.py
+++ b/kornia/geometry/line.py
@@ -202,7 +202,7 @@ def _fit_line_ols_2d(points: torch.Tensor) -> ParametrizedLine:
direction = torch.where(
denom > 1e-8,
torch.cat([torch.ones_like(slope), slope], dim=-1),
- torch.tensor([0.0, 1.0], device=points.device).expand(points.shape[0], 2),
+ torch.tensor([0.0, 1.0], device=points.device, dtype=points.dtype).expand(points.shape[0], 2),
)
direction = direction / direction.norm(dim=-1, keepdim=True)
diff --git a/testing/half_precision_xfails/cpu_bfloat16.txt b/testing/half_precision_xfails/cpu_bfloat16.txt
index a0c58aa82..9ab4f0990 100644
--- a/testing/half_precision_xfails/cpu_bfloat16.txt
+++ b/testing/half_precision_xfails/cpu_bfloat16.txt
@@ -365,7 +365,6 @@ call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomograp
call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points_iter[cpu-bfloat16-1]
call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points_iter[cpu-bfloat16-2]
call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points_iter[cpu-bfloat16-5]
-call builtins.AssertionError tests/geometry/test_line.py::TestFitLine::test_fit_line2[cpu-bfloat16]
call builtins.RuntimeError tests/geometry/test_pose.py::TestNamedPose::test_from_matrix[cpu-bfloat16]
call builtins.RuntimeError tests/geometry/test_pose.py::TestNamedPose::test_from_rt[cpu-bfloat16]
call builtins.AssertionError tests/geometry/test_quaternion.py::TestQuaternion::test_from_to_euler_values[cpu-bfloat16]
diff --git a/testing/half_precision_xfails/cpu_float16.txt b/testing/half_precision_xfails/cpu_float16.txt
index a2ffb9c0c..9ae0a075d 100644
--- a/testing/half_precision_xfails/cpu_float16.txt
+++ b/testing/half_precision_xfails/cpu_float16.txt
@@ -371,7 +371,6 @@ call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomograp
call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points[cpu-float16-5]
call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points_iter[cpu-float16-2]
call builtins.AssertionError tests/geometry/test_homography.py::TestFindHomographyFromLinesDLT::test_clean_points_iter[cpu-float16-5]
-call builtins.AssertionError tests/geometry/test_line.py::TestFitLine::test_fit_line2[cpu-float16]
call builtins.AssertionError tests/geometry/test_quaternion.py::TestQuaternion::test_axis_angle[cpu-float16-1]
call builtins.AssertionError tests/geometry/test_quaternion.py::TestQuaternion::test_axis_angle[cpu-float16-5]
call builtins.AssertionError tests/geometry/test_quaternion.py::TestQuaternion::test_axis_angle[cpu-float16-None]
diff --git a/tests/geometry/test_line.py b/tests/geometry/test_line.py
index 4f63038f8..74162b8e4 100644
--- a/tests/geometry/test_line.py
+++ b/tests/geometry/test_line.py
@@ -285,6 +285,13 @@ class TestFitLine(BaseTester):
assert angle_est.abs() > 0.998
+ def test_fit_line_vertical_dtype(self, device, dtype):
+ pts = torch.tensor([[[0.0, 0.0], [0.0, 1.0], [0.0, 2.0]]], device=device, dtype=dtype)
+ line = fit_line(pts)
+ assert line.origin.dtype == dtype
+ assert line.direction.dtype == dtype
+ self.assert_close(line.direction, torch.tensor([[0.0, 1.0]], device=device, dtype=dtype))
+
@pytest.mark.skip(reason="numerical do not match with analytical")
def test_gradcheck(self, device):
def proxy_func(pts, weights):