Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): reject fit_line weights whose shape is not (B, N) (#6526)

Muhammad Talhaee9c0daf3402
diff --git a/changelog.d/6526.fixed.md b/changelog.d/6526.fixed.md
new file mode 100644
index 000000000..a6afd8183
--- /dev/null
+++ b/changelog.d/6526.fixed.md
@@ -0,0 +1 @@
+`fit_line` raises a `ShapeError` when `weights` does not have the shape `(B, N)` of the points, instead of accepting a `(B, 1)` tensor. Such weights broadcast in the weighted sums but not in the normaliser, so the centroid came out wrong (the sum of the points for `D >= 3`, the first point plus the sum of the offsets from it for `D = 2`), and they skipped the zero-weight-sum and negative-weight checks: a zero weight gave a NaN line in 2-D and a negative weight was accepted. Weights with another second dimension raised a `RuntimeError` from torch, and a mismatched batch size a bare `BaseError`; both now raise the same `ShapeError`. The sizes are compared as numbers, so the check also holds for a symbolic `N` under a dynamic `torch.export`.
diff --git a/kornia/geometry/line.py b/kornia/geometry/line.py
index 4f8fe5cb9..e379ceb3a 100644
--- a/kornia/geometry/line.py
+++ b/kornia/geometry/line.py
@@ -41,7 +41,7 @@ from typing import Iterator, Optional, Tuple, Union
import torch
from torch import nn
-from kornia.core.check import KORNIA_CHECK, KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPE, are_checks_enabled
+from kornia.core.check import KORNIA_CHECK_IS_TENSOR, KORNIA_CHECK_SHAPE, KORNIA_CHECK_SIZES, are_checks_enabled
from kornia.core.exceptions import ValueCheckError
from kornia.core.utils import _torch_svd_cast, is_compiling, register_module_state
from kornia.geometry.conversions import _normalize_last_dim
@@ -452,19 +452,15 @@ def fit_line(points: torch.Tensor, weights: Optional[torch.Tensor] = None) -> Pa
points: tensor containing a batch of sets of n-dimensional points. The expected
shape of the tensor is ``(B, N, D)``.
weights: per-point weights, used in the centroid and in the fit. The expected
- shape of the tensor is ``(B, N)``; pass that shape. For ``N > 1`` a ``(B, 1)`` tensor is not rejected:
- it broadcasts, skips the weight checks under Raises and in general gives a wrong line, wrong in a
- different way for ``D = 2`` and ``D >= 3`` (`#6525 <https://github.com/kornia/kornia/issues/6525>`_).
+ shape of the tensor is ``(B, N)``, with the same ``B`` and ``N`` as ``points``.
Return:
The fitted line, with origin and direction of shape ``(B, D)``.
Raises:
TypeCheckError: if ``points`` or ``weights`` is not a tensor.
- ShapeError: if ``points`` is not 3-D, or ``weights`` is not 2-D.
- BaseError: if ``weights`` has another batch size than ``points``.
- RuntimeError: if ``weights`` has another number of points than ``points``, other than one or zero, from torch.
- IndexError: if ``weights`` has zero points, from torch.
+ ShapeError: if ``points`` is not of shape ``(B, N, D)``, or ``weights`` is not of shape ``(B, N)`` with the
+ same ``B`` and ``N`` as ``points``, a ``(B, 1)`` tensor included.
ValueCheckError: if the points do not determine a line β€” fewer than two points,
all points identical, or (with weights) a zero weight sum, a negative weight, or all
points with positive weight identical. Like every kornia value check, these are skipped under
@@ -486,25 +482,25 @@ def fit_line(points: torch.Tensor, weights: Optional[torch.Tensor] = None) -> Pa
KORNIA_CHECK_IS_TENSOR(points, "points must be a tensor")
KORNIA_CHECK_SHAPE(points, ["B", "N", "D"])
- _B, _N, D = points.shape
+ B, N, D = points.shape
+
+ # The weights must have the points' own B and N, for both branches: a (B, 1) tensor would broadcast in the
+ # weighted sums but not in the normaliser. Compare the sizes as numbers; KORNIA_CHECK_SHAPE reads a string as a
+ # label, which a symbolic size under a dynamic torch.export would become.
+ if weights is not None:
+ KORNIA_CHECK_IS_TENSOR(weights, "weights must be a tensor")
+ KORNIA_CHECK_SIZES(weights, [B, N])
_reject_degenerate_line(points, weights)
# Fast path: closed-form total least squares for the 2D case
if D == 2:
if weights is not None:
- KORNIA_CHECK_IS_TENSOR(weights, "weights must be a tensor")
- KORNIA_CHECK_SHAPE(weights, ["B", "N"])
- KORNIA_CHECK(points.shape[0] == weights.shape[0])
return _fit_line_weighted_tls_2d(points, weights)
return _fit_line_tls_2d(points)
# The scatter matrix is quadratic in the coordinates. Form it from centred, unit-scale
# offsets in a compute dtype before SVD; casting an already-overflowed half matrix cannot recover it.
- if weights is not None:
- KORNIA_CHECK_IS_TENSOR(weights, "weights must be a tensor")
- KORNIA_CHECK_SHAPE(weights, ["B", "N"])
- KORNIA_CHECK(points.shape[0] == weights.shape[0])
# The output keeps the promoted dtype of points and weights, as in the D = 2 branch.
out_dtype = points.dtype if weights is None else torch.promote_types(points.dtype, weights.dtype)
compute_dtype = torch.float32 if out_dtype in (torch.float16, torch.bfloat16) else out_dtype
diff --git a/tests/geometry/test_line.py b/tests/geometry/test_line.py
index 06892eff9..b82d2304b 100644
--- a/tests/geometry/test_line.py
+++ b/tests/geometry/test_line.py
@@ -21,7 +21,7 @@ import pytest
import torch
from kornia.core.check import are_checks_enabled, disable_checks, enable_checks
-from kornia.core.exceptions import TypeCheckError, ValueCheckError
+from kornia.core.exceptions import ShapeError, TypeCheckError, ValueCheckError
from kornia.geometry.line import ParametrizedLine, fit_line
from kornia.geometry.plane import Hyperplane
from kornia.geometry.vector import Scalar, Vector3
@@ -985,6 +985,77 @@ class TestFitLine(BaseTester):
(line.direction * expected.direction).sum(-1).abs(), torch.ones(1, device=device, dtype=dtype)
)
+ def test_fit_line_weights_shape_must_match_points_6525(self, device, dtype):
+ # #6525: the shape check named the weights' dimensions "B" and "N" without tying them to the points', so
+ # (B, 1) weights passed. They broadcast in the weighted sums but not in the normaliser, so the centroid came
+ # out wrong: the sum of the points for D >= 3, and for D = 2 the first point plus the sum of the offsets
+ # from it. They also skipped the weight checks of _reject_degenerate_line, which compare the weights' shape
+ # with the points': zero weights gave a NaN line in 2-D, and a negative weight was accepted.
+ points_2d = torch.tensor([[[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [3.0, 0.0]]], device=device, dtype=dtype)
+ points_3d = torch.nn.functional.pad(points_2d, (0, 1))
+ for points in (points_2d, points_3d):
+ # Weights of the points' shape still give the weighted centroid, here the mean of the points.
+ line = fit_line(points, torch.ones(1, 4, device=device, dtype=dtype))
+ self.assert_close(line.origin, points.mean(-2))
+
+ # Any other second dimension is a shape error, (B, 1) included, whatever the weights' values.
+ for n in (1, 3, 5):
+ for value in (1.0, 0.0, -1.0):
+ with pytest.raises(ShapeError, match=r"expected \(1, 4\)"):
+ fit_line(points, torch.full((1, n), value, device=device, dtype=dtype))
+
+ # So is a weights tensor of the wrong rank, or the wrong batch size, and a (B, 1) tensor is no exception
+ # in a batch.
+ for weights in (torch.ones(4, device=device, dtype=dtype), torch.ones(1, 4, 1, device=device, dtype=dtype)):
+ with pytest.raises(ShapeError, match=r"expected \(1, 4\)"):
+ fit_line(points, weights)
+ batch = torch.cat([points, points])
+ for weights in (torch.ones(1, 4, device=device, dtype=dtype), torch.ones(2, 1, device=device, dtype=dtype)):
+ with pytest.raises(ShapeError, match=r"expected \(2, 4\)"):
+ fit_line(batch, weights)
+
+ # With N == 1 the (B, 1) weights have the points' shape, and the single point is rejected as before.
+ with pytest.raises(ValueCheckError, match="at least two points"):
+ fit_line(points[:, :1], torch.ones(1, 1, device=device, dtype=dtype))
+
+ def test_fit_line_weights_shape_2d_centroid_not_at_origin_6525(self, device, dtype):
+ # The first point is not at the origin here, so the 2-D branch's wrong centroid (the first point plus the sum
+ # of the offsets from it) differs from the sum of the points: (2, 0), (4, 0) with one weight gave (4, 0).
+ points = torch.tensor([[[2.0, 0.0], [4.0, 0.0]]], device=device, dtype=dtype)
+ with pytest.raises(ShapeError, match=r"expected \(1, 2\)"):
+ fit_line(points, torch.ones(1, 1, device=device, dtype=dtype))
+ self.assert_close(fit_line(points, torch.ones(1, 2, device=device, dtype=dtype)).origin, points.mean(-2))
+
+ @pytest.mark.parametrize("d", [2, 3])
+ def test_fit_line_weights_shape_with_dynamic_n_export_6525(self, device, d):
+ # Under a dynamic torch.export the point count N is symbolic. The weights' size is compared with it without
+ # specializing it, so weights that share the points' N still export, and the program runs for another N.
+ # Mismatched weights are not exported here: up to torch 2.12 a non-strict export that raises leaves a global
+ # buffer-registration hook installed, and later compiled tests in the process fail on it. The upper bound
+ # keeps torch 2.5.1 from guarding N against the largest int64 in the D >= 3 branch.
+ from torch.export import Dim
+
+ class FitLine(torch.nn.Module):
+ def forward(self, points, weights):
+ line = fit_line(points, weights)
+ return line.origin, line.direction
+
+ n = Dim("n", min=2, max=64)
+ points = torch.nn.functional.pad(
+ torch.tensor([[[0.0, 0.0], [1.0, 0.0], [2.0, 0.0], [3.0, 0.0]]], device=device), (0, d - 2)
+ )
+ program = torch.export.export(
+ FitLine(),
+ (points, torch.ones(1, 4, device=device)),
+ dynamic_shapes={"points": {1: n}, "weights": {1: n}},
+ strict=False,
+ )
+ longer = torch.nn.functional.pad(
+ torch.stack([torch.arange(6.0, device=device), torch.zeros(6, device=device)], -1)[None], (0, d - 2)
+ )
+ origin, _ = program.module()(longer, torch.ones(1, 6, device=device))
+ self.assert_close(origin, longer.mean(-2))
+
def test_dynamo_skips_degenerate_checks(self, device, dtype, torch_optimizer):
# The degeneracy checks depend on tensor values, so they are skipped under torch.compile: a compiled call
# on identical points returns what an eager call returns with checks disabled.