Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

37.7 KB
Raw Blame
# LICENSE HEADER MANAGED BY add-license-header
#
# Copyright 2018 Kornia Team
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

from functools import partial

import pytest
import torch

import kornia.geometry.epipolar as epi
from kornia.geometry.epipolar._metrics import (
    _sampson_epipolar_distance_manual_impl_,
    _sampson_epipolar_distance_matmul_impl_,
    _sampson_epipolar_distance_shared_impl_,
    _sampson_errors,
    _sampson_from_quadratic_basis,
    _sampson_quadratic_basis,
    _symmetrical_epipolar_distance_manual_impl_,
    _symmetrical_epipolar_distance_matmul_impl_,
)
from kornia.geometry.epipolar.fundamental import _rank2_projection

from testing.base import BaseTester
from testing.geometry.create import create_random_fundamental_matrix
from testing.two_view import two_view_scene


class TestSymmetricalEpipolarDistance(BaseTester):
    def test_smoke(self, device, dtype):
        pts1 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)
        assert epi.symmetrical_epipolar_distance(pts1, pts2, Fm).shape == (1, 4)

    def test_batch(self, device, dtype):
        batch_size = 5
        pts1 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)
        assert epi.symmetrical_epipolar_distance(pts1, pts2, Fm).shape == (5, 4)

    def test_frames(self, device, dtype):
        batch_size, num_frames, num_points = 5, 3, 4
        pts1 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        Fm = torch.stack(
            [create_random_fundamental_matrix(1, dtype=dtype, device=device) for _ in range(num_frames)], dim=1
        )
        dist_frame_by_frame = torch.stack(
            [
                epi.symmetrical_epipolar_distance(pts1[:, t, ...], pts2[:, t, ...], Fm[:, t, ...])
                for t in range(num_frames)
            ],
            dim=1,
        )
        dist_all_frames = epi.symmetrical_epipolar_distance(pts1, pts2, Fm)
        self.assert_close(dist_frame_by_frame, dist_all_frames, atol=1e-6, rtol=1e-6)

    def test_gradcheck(self, device):
        # generate input data
        batch_size, num_points, num_dims = 2, 3, 2
        points1 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64, requires_grad=True)
        points2 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64)
        Fm = create_random_fundamental_matrix(batch_size, dtype=torch.float64, device=device)

        self.gradcheck(epi.symmetrical_epipolar_distance, (points1, points2, Fm), requires_grad=(True, False, False))

    def test_shift(self, device, dtype):
        pts1 = torch.zeros(3, 2, device=device, dtype=dtype)[None]
        pts2 = torch.tensor([[2, 0.0], [2, 1], [2, 2.0]], device=device, dtype=dtype)[None]
        Fm = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]], dtype=dtype, device=device)[None]
        expected = torch.tensor([0.0, 2.0, 8.0], device=device, dtype=dtype)[None]
        self.assert_close(epi.symmetrical_epipolar_distance(pts1, pts2, Fm), expected, atol=1e-4, rtol=1e-4)


class TestSampsonEpipolarDistance(BaseTester):
    def test_smoke(self, device, dtype):
        pts1 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)

        assert epi.sampson_epipolar_distance(pts1, pts2, Fm).shape == (1, 4)

    def test_batch(self, device, dtype):
        batch_size = 5
        pts1 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)
        assert epi.sampson_epipolar_distance(pts1, pts2, Fm).shape == (5, 4)

    def test_frames(self, device, dtype):
        batch_size, num_frames, num_points = 5, 3, 4
        pts1 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        Fm = torch.stack(
            [create_random_fundamental_matrix(1, dtype=dtype, device=device) for _ in range(num_frames)], dim=1
        )
        dist_frame_by_frame = torch.stack(
            [epi.sampson_epipolar_distance(pts1[:, t, ...], pts2[:, t, ...], Fm[:, t, ...]) for t in range(num_frames)],
            dim=1,
        )
        dist_all_frames = epi.sampson_epipolar_distance(pts1, pts2, Fm)
        self.assert_close(dist_frame_by_frame, dist_all_frames, atol=1e-6, rtol=1e-6)

    def test_shift(self, device, dtype):
        pts1 = torch.zeros(3, 2, device=device, dtype=dtype)[None]
        pts2 = torch.tensor([[2, 0.0], [2, 1], [2, 2.0]], device=device, dtype=dtype)[None]
        Fm = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]], dtype=dtype, device=device)[None]
        expected = torch.tensor([0.0, 0.5, 2.0], device=device, dtype=dtype)[None]
        self.assert_close(epi.sampson_epipolar_distance(pts1, pts2, Fm), expected, atol=1e-4, rtol=1e-4)

    def test_gradcheck(self, device):
        # generate input data
        batch_size, num_points, num_dims = 2, 3, 2
        points1 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64, requires_grad=True)
        points2 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64)
        Fm = create_random_fundamental_matrix(batch_size, dtype=torch.float64, device=device)

        self.gradcheck(epi.sampson_epipolar_distance, (points1, points2, Fm), requires_grad=(True, False, False))


class TestLeftToRightEpipolarDistance(BaseTester):
    def test_smoke(self, device, dtype):
        pts1 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)

        assert epi.left_to_right_epipolar_distance(pts1, pts2, Fm).shape == (1, 4)

    def test_batch(self, device, dtype):
        batch_size = 5
        pts1 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)
        assert epi.left_to_right_epipolar_distance(pts1, pts2, Fm).shape == (5, 4)

    def test_frames(self, device, dtype):
        batch_size, num_frames, num_points = 5, 3, 4
        pts1 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        Fm = torch.stack(
            [create_random_fundamental_matrix(1, dtype=dtype, device=device) for _ in range(num_frames)], dim=1
        )
        dist_frame_by_frame = torch.stack(
            [
                epi.left_to_right_epipolar_distance(pts1[:, t, ...], pts2[:, t, ...], Fm[:, t, ...])
                for t in range(num_frames)
            ],
            dim=1,
        )
        dist_all_frames = epi.left_to_right_epipolar_distance(pts1, pts2, Fm)
        self.assert_close(dist_frame_by_frame, dist_all_frames, atol=1e-6, rtol=1e-6)

    def test_shift(self, device, dtype):
        pts1 = torch.zeros(3, 2, device=device, dtype=dtype)[None]
        pts2 = torch.tensor([[2, 0.0], [2, 1], [2, 2.0]], device=device, dtype=dtype)[None]
        Fm = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]], dtype=dtype, device=device)[None]
        expected = torch.tensor([0.0, 1.0, 2.0], device=device, dtype=dtype)[None]
        self.assert_close(epi.left_to_right_epipolar_distance(pts1, pts2, Fm), expected, atol=1e-4, rtol=1e-4)

    def test_gradcheck(self, device):
        # generate input data
        batch_size, num_points, num_dims = 2, 3, 2
        points1 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64, requires_grad=True)
        points2 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64)
        Fm = create_random_fundamental_matrix(batch_size, dtype=torch.float64, device=device)

        self.gradcheck(epi.left_to_right_epipolar_distance, (points1, points2, Fm), requires_grad=(True, False, False))

    def test_homogeneous_weight_4935(self, device, dtype):
        # #4935: the measured point went to point_line_distance with its weight ignored, so (10, 14, 2) scored 6.08
        # where the same point (5, 7) scored 13.72.
        Fm = torch.tensor([[[0.0, -0.02, 0.3], [0.02, 0.0, -0.9], [-0.3, 0.9, 0.1]]], device=device, dtype=dtype)
        pts1 = torch.tensor([[[10.0, 20.0]]], device=device, dtype=dtype)
        pts2 = torch.tensor([[[5.0, 7.0]]], device=device, dtype=dtype)
        pts2_weighted = torch.tensor([[[10.0, 14.0, 2.0]]], device=device, dtype=dtype)
        expected = epi.left_to_right_epipolar_distance(pts1, pts2, Fm)
        self.assert_close(expected, torch.tensor([[13.717871]], device=device, dtype=dtype))
        self.assert_close(epi.left_to_right_epipolar_distance(pts1, pts2_weighted, Fm), expected)


class TestRightToLeftEpipolarDistance(BaseTester):
    def test_smoke(self, device, dtype):
        pts1 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(1, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)

        assert epi.right_to_left_epipolar_distance(pts1, pts2, Fm).shape == (1, 4)

    def test_batch(self, device, dtype):
        batch_size = 5
        pts1 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, 4, 3, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)
        assert epi.right_to_left_epipolar_distance(pts1, pts2, Fm).shape == (5, 4)

    def test_frames(self, device, dtype):
        batch_size, num_frames, num_points = 5, 3, 4
        pts1 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        pts2 = torch.rand(batch_size, num_frames, num_points, 3, device=device, dtype=dtype)
        Fm = torch.stack(
            [create_random_fundamental_matrix(1, dtype=dtype, device=device) for _ in range(num_frames)], dim=1
        )
        dist_frame_by_frame = torch.stack(
            [
                epi.right_to_left_epipolar_distance(pts1[:, t, ...], pts2[:, t, ...], Fm[:, t, ...])
                for t in range(num_frames)
            ],
            dim=1,
        )
        dist_all_frames = epi.right_to_left_epipolar_distance(pts1, pts2, Fm)
        self.assert_close(dist_frame_by_frame, dist_all_frames, atol=1e-6, rtol=1e-6)

    def test_shift(self, device, dtype):
        pts1 = torch.zeros(3, 2, device=device, dtype=dtype)[None]
        pts2 = torch.tensor([[2, 0.0], [2, 1], [2, 2.0]], device=device, dtype=dtype)[None]
        Fm = torch.tensor([[0.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]], dtype=dtype, device=device)[None]
        expected = torch.tensor([0.0, 1.0, 2.0], device=device, dtype=dtype)[None]
        self.assert_close(epi.right_to_left_epipolar_distance(pts1, pts2, Fm), expected, atol=1e-4, rtol=1e-4)

    def test_gradcheck(self, device):
        # generate input data
        batch_size, num_points, num_dims = 2, 3, 2
        points1 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64, requires_grad=True)
        points2 = torch.rand(batch_size, num_points, num_dims, device=device, dtype=torch.float64)
        Fm = create_random_fundamental_matrix(batch_size, dtype=torch.float64, device=device)

        self.gradcheck(epi.right_to_left_epipolar_distance, (points1, points2, Fm), requires_grad=(True, False, False))

    def test_homogeneous_weight_4935(self, device, dtype):
        # #4935: the measured point went to point_line_distance with its weight ignored, so (20, 40, 2) scored 29.54
        # where the same point (10, 20) scored 11.89.
        Fm = torch.tensor([[[0.0, -0.02, 0.3], [0.02, 0.0, -0.9], [-0.3, 0.9, 0.1]]], device=device, dtype=dtype)
        pts1 = torch.tensor([[[10.0, 20.0]]], device=device, dtype=dtype)
        pts1_weighted = torch.tensor([[[20.0, 40.0, 2.0]]], device=device, dtype=dtype)
        pts2 = torch.tensor([[[5.0, 7.0]]], device=device, dtype=dtype)
        expected = epi.right_to_left_epipolar_distance(pts1, pts2, Fm)
        self.assert_close(epi.right_to_left_epipolar_distance(pts1_weighted, pts2, Fm), expected)


_HALF_PIXEL_F = (
    "a pixel-unit F spans eight decades (entries down to ~1e-8): float16 flushes the small entries to zero and "
    "bfloat16's 8-bit mantissa cannot resolve the epipolar residual, which kornia evaluates in the input dtype"
)
# Fixed pixel offsets added to x2 so that the distances are non-zero.
_NOISE = [
    [1.5, -2.0], [-2.5, 1.0], [0.5, 3.0], [-1.0, -1.5], [2.0, 0.5], [-3.0, 2.5],
    [1.0, -0.5], [-0.5, -3.0], [2.5, 1.5], [-2.0, -2.5], [3.0, -1.0], [-1.5, 2.0],
]  # fmt: skip


def _hom(p: torch.Tensor) -> torch.Tensor:
    return torch.cat([p, torch.ones_like(p[..., :1])], -1)


def _pixel_F_unit_norm(scene) -> torch.Tensor:
    """Ground-truth F of the two-view fixture in closed form, scaled to unit Frobenius norm."""
    eye = torch.eye(3, device=scene["R"].device, dtype=scene["R"].dtype)[None]
    E = epi.essential_from_Rt(eye, torch.zeros_like(scene["t"]), scene["R"], scene["t"])
    F = epi.fundamental_from_essential(E, scene["K1"], scene["K2"])
    return F / F.norm(dim=(-2, -1), keepdim=True)


def _cpu64(x: torch.Tensor) -> torch.Tensor:
    """Reference arithmetic in float64 on the CPU (MPS has no float64)."""
    return x.detach().cpu().double()


class TestConventionEpipolarMetrics(BaseTester):
    @pytest.mark.parametrize("metric", ["sampson", "symmetrical", "left_to_right", "right_to_left"])
    def test_convention_metrics_argument_order_and_squared(self, metric, device, dtype):
        two_view = two_view_scene(device, dtype)
        if dtype in (torch.float16, torch.bfloat16):
            pytest.skip(_HALF_PIXEL_F)
        x1 = two_view["x1"]
        x2 = two_view["x2"] + torch.tensor([_NOISE], device=device, dtype=dtype)
        F = _pixel_F_unit_norm(two_view)
        # Reference in float64 from the point-to-epiline geometry, in pixels: pts1 are first-image points, pts2
        # second-image points, and Fm follows x2^T F x1 = 0.
        F64, p1, p2 = _cpu64(F), _cpu64(x1), _cpu64(x2)
        lines_in_2 = _hom(p1) @ F64.transpose(-2, -1)  # epilines of pts1, in the second image
        lines_in_1 = _hom(p2) @ F64  # epilines of pts2, in the first image
        algebraic = (_hom(p2) * lines_in_2).sum(-1)
        d2 = algebraic.abs() / lines_in_2[..., :2].norm(dim=-1)  # pts2 to the epiline of pts1
        d1 = algebraic.abs() / lines_in_1[..., :2].norm(dim=-1)  # pts1 to the epiline of pts2
        expected = {
            "sampson": algebraic**2 / (lines_in_2[..., :2].pow(2).sum(-1) + lines_in_1[..., :2].pow(2).sum(-1)),
            "symmetrical": d1**2 + d2**2,  # the sum of the two squared distances, not their mean
            "left_to_right": d2,
            "right_to_left": d1,
        }[metric]
        fn = getattr(epi, f"{metric}_epipolar_distance")
        self.assert_close(_cpu64(fn(x1, x2, F)), expected, rtol=1e-3, atol=0.0)
        # Control: the points swapped against the same F give a different value on every correspondence.
        assert ((_cpu64(fn(x2, x1, F)) - expected).abs() / expected).min() > 0.5
        # Relabelling the images (swap the points, transpose F) keeps Sampson and the symmetric distance and swaps
        # the two one-way distances.
        relabelled = {"left_to_right": "right_to_left", "right_to_left": "left_to_right"}.get(metric, metric)
        relabelled_fn = getattr(epi, f"{relabelled}_epipolar_distance")
        self.assert_close(_cpu64(relabelled_fn(x2, x1, F.transpose(-2, -1))), expected, rtol=1e-3, atol=0.0)
        if metric in ("sampson", "symmetrical"):
            # squared=True is the default; squared=False returns the root, in pixels. The one-way distances are
            # unsquared and have no squared argument.
            self.assert_close(_cpu64(fn(x1, x2, F, squared=False)), expected.sqrt(), rtol=1e-3, atol=0.0)
        if metric == "sampson":
            # A 3-vector point is used as given: w = 1 matches the 2-vector input, w = 2 is not dehomogenised and
            # changes every value.
            self.assert_close(fn(_hom(x1), _hom(x2), F), fn(x1, x2, F))
            assert ((fn(2.0 * _hom(x1), _hom(x2), F) - fn(x1, x2, F)).abs() / fn(x1, x2, F)).min() > 0.25

    def test_metrics_do_not_depend_on_the_scale_of_F_4881(self, device, dtype):
        two_view = two_view_scene(device, dtype)
        if dtype in (torch.float16, torch.bfloat16):
            pytest.skip(_HALF_PIXEL_F)
        x1 = two_view["x1"]
        x2 = two_view["x2"] + torch.tensor([_NOISE], device=device, dtype=dtype)
        F = _pixel_F_unit_norm(two_view)
        # #4881: eps was added inside the denominators, so the same F at ||F|| = 1e-3 scored 90% lower than at
        # ||F|| = 1, and 1e-6 F moved the one-way distances by a few percent. Every path on every device: the manual,
        # matmul and shared-point (two models) Sampson implementations, both symmetrical ones and the one-way
        # distances. The tolerance is float32 roundoff of the cancelling residual, as in the convention test above.
        paths = (
            partial(epi.sampson_epipolar_distance, use_matmul_at_less_than_points=0),
            _sampson_epipolar_distance_matmul_impl_,
            lambda a, b, F: _sampson_epipolar_distance_shared_impl_(a, b, F.expand(2, 3, 3), True)[:1],
            _symmetrical_epipolar_distance_manual_impl_,
            _symmetrical_epipolar_distance_matmul_impl_,
            epi.left_to_right_epipolar_distance,
            epi.right_to_left_epipolar_distance,
        )
        for fn in paths:
            unit = fn(x1, x2, F)
            for scale in (1e-6, 1e-3, 1e3):
                self.assert_close(fn(x1, x2, scale * F), unit, rtol=1e-3, atol=0.0)

    def test_exact_match_is_zero_with_a_zero_gradient_4881(self, device, dtype):
        # #4881: squared=False returned sqrt(d^2 + eps), 1e-4 for an exact match, and eps=0 returned 0 with a NaN
        # gradient. The first pts2 lies on the epiline of its pts1 (the fixture of test_shift), exact in every dtype.
        Fm = torch.tensor([[[0.0, 0.0, 0.0], [0.0, 0.0, -1.0], [0.0, 1.0, 0.0]]], device=device, dtype=dtype)
        pts1 = torch.zeros(1, 3, 2, device=device, dtype=dtype)
        pts2 = torch.tensor([[[2.0, 0.0], [2.0, 1.0], [2.0, 2.0]]], device=device, dtype=dtype, requires_grad=True)
        sampson, symmetrical = [0.0, 0.5, 2.0], [0.0, 2.0, 8.0]
        for fn, expected in (
            (partial(epi.sampson_epipolar_distance, use_matmul_at_less_than_points=0), sampson),
            (_sampson_epipolar_distance_matmul_impl_, sampson),
            (
                lambda a, b, F, squared: _sampson_epipolar_distance_shared_impl_(a, b, F.expand(2, 3, 3), squared)[:1],
                sampson,
            ),
            (_symmetrical_epipolar_distance_manual_impl_, symmetrical),
            (_symmetrical_epipolar_distance_matmul_impl_, symmetrical),
        ):
            out = fn(pts1, pts2, Fm, squared=False)
            self.assert_close(out, torch.tensor([expected], device=device, dtype=dtype).sqrt())
            assert out[0, 0] == 0
            (grad,) = torch.autograd.grad(out.sum(), pts2)
            assert torch.isfinite(grad).all()
            assert torch.equal(grad[0, 0], torch.zeros_like(grad[0, 0]))

    def test_degenerate_epilines_are_inf_4881(self, device, dtype):
        # F = diag(0, 0, 1): every epiline has a = b = 0. The manual Sampson path returned residual^2 / eps = 1e8 there
        # and the matmul path inf (#4881). Every path returns inf, with a finite gradient.
        pts1 = torch.rand(1, 5, 2, device=device, dtype=dtype, requires_grad=True)
        pts2 = torch.rand(1, 5, 2, device=device, dtype=dtype)
        Fm = torch.zeros(1, 3, 3, device=device, dtype=dtype)
        Fm[:, 2, 2] = 1.0
        paths = (
            partial(epi.sampson_epipolar_distance, use_matmul_at_less_than_points=0),
            _sampson_epipolar_distance_matmul_impl_,
            lambda a, b, F, squared: _sampson_epipolar_distance_shared_impl_(a, b, F.expand(2, 3, 3), squared)[:1],
            _symmetrical_epipolar_distance_manual_impl_,
            _symmetrical_epipolar_distance_matmul_impl_,
        )
        outputs = [fn(pts1, pts2, Fm, squared=squared) for fn in paths for squared in (True, False)]
        outputs += [
            epi.left_to_right_epipolar_distance(pts1, pts2, Fm),
            epi.right_to_left_epipolar_distance(pts1, pts2, Fm),
        ]
        for out in outputs:
            assert out.isposinf().all(), out
            (grad,) = torch.autograd.grad(out.sum(), pts1)
            assert torch.isfinite(grad).all(), grad

    def test_one_singular_epiline_4881(self, device, dtype):
        # F = [[1, 0, 0], [0, 0, 0], [0, 0, 1]] (rank 2) maps (0, 5) to the line at infinity (0, 0, 1): row 0's
        # epiline F x in image 2 and row 1's F^T x' in image 1; the other epiline of both is (2, 0, 1) and the residual
        # 1. Row 2 is regular: F x = (2, 0, 1), F^T x' = (1, 0, 1), residual 3. The symmetrical distance is inf when
        # either epiline is singular, the Sampson distance only when both are (#4881), so it stays 1 / (0 + 4) there.
        Fm = torch.tensor([[[1.0, 0.0, 0.0], [0.0, 0.0, 0.0], [0.0, 0.0, 1.0]]], device=device, dtype=dtype)
        pts1 = torch.tensor([[[0.0, 5.0], [2.0, 3.0], [2.0, 3.0]]], device=device, dtype=dtype, requires_grad=True)
        pts2 = torch.tensor([[[2.0, 3.0], [0.0, 5.0], [1.0, 1.0]]], device=device, dtype=dtype)
        inf = float("inf")
        sampson, symmetrical = [0.25, 0.25, 1.8], [inf, inf, 11.25]
        for fn, expected in (
            (partial(epi.sampson_epipolar_distance, use_matmul_at_less_than_points=0), sampson),
            (_sampson_epipolar_distance_matmul_impl_, sampson),
            (
                lambda a, b, F, squared: _sampson_epipolar_distance_shared_impl_(a, b, F.expand(2, 3, 3), squared)[:1],
                sampson,
            ),
            (_symmetrical_epipolar_distance_manual_impl_, symmetrical),
            (_symmetrical_epipolar_distance_matmul_impl_, symmetrical),
        ):
            for squared in (True, False):
                out = fn(pts1, pts2, Fm, squared=squared)
                target = torch.tensor([expected], device=device, dtype=dtype)
                self.assert_close(out, target if squared else target.sqrt())
                (grad,) = torch.autograd.grad(out.sum(), pts1)
                assert torch.isfinite(grad).all(), grad
        # The one-way distances: |ax + by + c| / ||(a, b)|| to the epiline of the other point.
        expected = torch.tensor([[inf, 0.5, 1.5]], device=device, dtype=dtype)
        self.assert_close(epi.left_to_right_epipolar_distance(pts1, pts2, Fm), expected)
        expected = torch.tensor([[0.5, inf, 3.0]], device=device, dtype=dtype)
        self.assert_close(epi.right_to_left_epipolar_distance(pts1, pts2, Fm), expected)


def _near_epipole_scene(n: int, seed: int):
    """Pixel correspondences 0.01-1000 px from both epipoles, which lie inside a 1920x1080 image, and their F."""
    generator = torch.Generator().manual_seed(seed)
    f64 = torch.float64
    size = torch.tensor([1920.0, 1080.0], dtype=f64)
    e1 = torch.cat([torch.rand(2, generator=generator, dtype=f64) * size, torch.ones(1, dtype=f64)])
    e2 = torch.cat([torch.rand(2, generator=generator, dtype=f64) * size, torch.ones(1, dtype=f64)])
    eye = torch.eye(3, dtype=f64)
    A = torch.randn(3, 3, generator=generator, dtype=f64)
    F = (eye - e2[:, None] * e2[None] / (e2 @ e2)) @ A @ (eye - e1[:, None] * e1[None] / (e1 @ e1))
    offsets = torch.logspace(-2, 3, n, dtype=f64)[:, None]
    x1 = e1[:2] + torch.randn(n, 2, generator=generator, dtype=f64) * offsets
    x2 = e2[:2] + torch.randn(n, 2, generator=generator, dtype=f64) * offsets
    return x1, x2, F / F[2, 2]


class TestSampsonSharedPoints(BaseTester):
    """One point set scored against many fundamental matrices: two matrix products instead of per-model broadcasting."""

    @pytest.mark.parametrize("squared", [True, False])
    def test_public_dispatch_and_paths_agree_4881(self, device, dtype, squared):
        if dtype in (torch.float16, torch.bfloat16):
            pytest.skip("compared at float32 and float64 accuracy")
        # 256 matrices x 300 points reach the shared path on every device. At 1e-4 of unit scale the eps that the
        # manual path added to the denominator moved the distances by tens of percent against the CUDA matmul path
        # (#4881); the three implementations agree to roundoff.
        generator = torch.Generator().manual_seed(5)
        pts1 = torch.rand(1, 300, 2, generator=generator).to(device, dtype)
        pts2 = torch.rand(1, 300, 2, generator=generator).to(device, dtype)
        Fm = 1e-4 * create_random_fundamental_matrix(256, dtype=dtype, device=device)
        out = epi.sampson_epipolar_distance(pts1, pts2, Fm, squared=squared)
        assert torch.equal(out, _sampson_epipolar_distance_shared_impl_(pts1, pts2, Fm, squared))
        for reference in (_sampson_epipolar_distance_manual_impl_, _sampson_epipolar_distance_matmul_impl_):
            expected = reference(pts1, pts2, Fm, squared)
            # Roundoff on the near-zero distances aside.
            self.assert_close(out, expected, rtol=1e-3, atol=1e-3 * float(expected.abs().median()))

    def test_single_matrix_without_batch_dimension(self, device, dtype):
        # Every implementation broadcasts a single (3, 3) matrix, and the shared-point dispatch must not reject it.
        # The CPU manual implementation used to require a batch dimension on Fm (#6108).
        pts1 = torch.rand(1, 9, 2, device=device, dtype=dtype)
        pts2 = torch.rand(1, 9, 2, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)
        out = epi.sampson_epipolar_distance(pts1, pts2, Fm[0])
        assert out.shape == (1, 9)
        self.assert_close(out, epi.sampson_epipolar_distance(pts1, pts2, Fm))

    @pytest.mark.parametrize("squared", [True, False])
    def test_matches_the_other_paths(self, device, dtype, squared):
        pts1 = torch.rand(1, 20, 2, device=device, dtype=dtype)
        pts2 = torch.rand(1, 20, 2, device=device, dtype=dtype)
        # 4000 models x 20 points: the GEMM path on every device.
        Fm = create_random_fundamental_matrix(4000, dtype=dtype, device=device)
        out = epi.sampson_epipolar_distance(pts1, pts2, Fm, squared=squared)
        other = (
            _sampson_epipolar_distance_matmul_impl_
            if device.type == "cuda"
            else _sampson_epipolar_distance_manual_impl_
        )
        work = torch.promote_types(dtype, torch.float32)
        expected = other(pts1.to(work), pts2.to(work), Fm.to(work), squared).to(dtype)
        assert out.dtype == dtype
        self.assert_close(out, expected)

    def test_shapes(self, device, dtype):
        pts1 = torch.rand(1, 1, 7, 2, device=device, dtype=dtype)
        pts2 = torch.cat(
            [torch.rand(1, 7, 2, device=device, dtype=dtype), torch.ones(1, 7, 1, device=device, dtype=dtype)], -1
        )
        Fm = create_random_fundamental_matrix(6, dtype=dtype, device=device).reshape(2, 3, 3, 3)
        assert _sampson_epipolar_distance_shared_impl_(pts1, pts2, Fm, True).shape == (2, 3, 7)
        assert _sampson_epipolar_distance_shared_impl_(pts1[0, 0], pts2[0], Fm[0], True).shape == (3, 7)
        empty = torch.zeros(1, 0, 2, device=device, dtype=dtype)
        assert _sampson_epipolar_distance_shared_impl_(empty, empty, Fm[0], True).shape == (3, 0)

    def test_homogeneous_points_are_used_as_given(self, device, dtype):
        pts1 = torch.rand(1, 9, 2, device=device, dtype=dtype)
        pts2 = torch.rand(1, 9, 2, device=device, dtype=dtype)
        Fm = create_random_fundamental_matrix(3, dtype=dtype, device=device)
        scaled = 2.0 * _hom(pts1)
        out = _sampson_epipolar_distance_shared_impl_(scaled, pts2, Fm, True)
        work = torch.promote_types(dtype, torch.float32)
        expected = _sampson_epipolar_distance_manual_impl_(scaled.to(work), pts2.to(work), Fm.to(work), True)
        self.assert_close(out, expected.to(dtype))

    def test_non_contiguous_and_mixed_dtype_models(self, device, dtype):
        if device.type == "mps":
            pytest.skip("MPS does not support float64")
        pts1 = torch.rand(1, 11, 2, device=device, dtype=torch.float64)
        pts2 = torch.rand(1, 11, 2, device=device, dtype=torch.float64)
        Fm = create_random_fundamental_matrix(4, dtype=dtype, device=device)
        out = _sampson_epipolar_distance_shared_impl_(pts1, pts2, Fm.mT, True)
        assert out.dtype == torch.promote_types(torch.float64, dtype)
        self.assert_close(out, _sampson_epipolar_distance_shared_impl_(pts1, pts2, Fm.mT.contiguous(), True))
        one = Fm[:1].expand(5, 3, 3)
        expanded = _sampson_epipolar_distance_shared_impl_(pts1, pts2, one, True)
        self.assert_close(expanded, expanded[:1].expand_as(expanded))

    def test_residual_order_regression(self, device, dtype):
        if dtype != torch.float32:
            pytest.skip("a float32 cancellation case (half cannot hold 1000.1 or 2e6; float64 is the reference)")
        # PR #5031 review: a numerator expanded into monomials of the coordinates cancels here (0.0521 against a
        # float64 value of 0.0403); forming x2 . (F x1) keeps the manual path's 0.0319.
        F = torch.tensor(
            [[1.0, 0.0, -1000.0], [0.0, 1.0, -1000.0], [-1000.0, -1000.0, 2e6]], device=device, dtype=dtype
        )
        x1 = torch.tensor([[[1000.1, 1000.2]]], device=device, dtype=dtype)
        x2 = torch.tensor([[[1000.3, 1000.4]]], device=device, dtype=dtype)
        models = F.expand(64, 3, 3)
        shared = _sampson_epipolar_distance_shared_impl_(x1, x2, models, True)[:, 0].cpu().double()
        manual = _sampson_epipolar_distance_manual_impl_(x1, x2, F[None], True)[0, 0].cpu().double()
        reference = _sampson_epipolar_distance_manual_impl_(
            x1.cpu().double(), x2.cpu().double(), F[None].cpu().double(), True
        )
        reference = reference[0, 0].cpu()
        self.assert_close(shared, manual.expand_as(shared), rtol=1e-4, atol=0.0)
        assert (shared - reference).abs().max() <= 2 * (manual - reference).abs()

    @pytest.mark.parametrize("seed", [0, 1, 2, 3])
    def test_near_epipoles_in_pixels(self, device, dtype, seed):
        if dtype != torch.float32:
            pytest.skip("the float32 cancellation case")
        x1, x2, F = _near_epipole_scene(2000, seed=seed)
        perturb = 1 + 1e-4 * torch.randn(7, 3, 3, generator=torch.Generator().manual_seed(1), dtype=torch.float64)
        models = torch.cat([F[None], F[None] * perturb])
        reference = _sampson_epipolar_distance_manual_impl_(x1[None], x2[None], models, True)
        cast = lambda t: t.to(device, dtype)  # noqa: E731
        shared = _sampson_epipolar_distance_shared_impl_(cast(x1[None]), cast(x2[None]), cast(models), True)
        manual = _sampson_epipolar_distance_manual_impl_(cast(x1[None]), cast(x2[None]), cast(models), True)
        shared, manual = shared.cpu().double(), manual.cpu().double()
        assert torch.isfinite(shared).all()
        assert (shared >= 0).all()
        # Within 0.1 px of an epipole both float32 computations are rounding noise (the maximum error of either is
        # an accident of summation order), so compare the error distributions: the tail and the number of
        # distances off by more than 1 px^2. An expanded monomial residual fails this on seeds 1 and 3.
        small = reference < 100
        error_shared = (shared - reference).abs()[small]
        error_manual = (manual - reference).abs()[small]
        assert error_shared.quantile(0.99) <= 1.25 * error_manual.quantile(0.99)
        assert (error_shared > 1).sum() <= 1.25 * (error_manual > 1).sum() + 2

    def test_ransac_quadratic_form_near_epipoles(self, device, dtype):
        if dtype != torch.float32:
            pytest.skip("RANSAC scores in float32 on the device")
        x1, x2, F = _near_epipole_scene(2000, seed=0)
        n1, t1 = epi.normalize_points(x1[None])
        n2, t2 = epi.normalize_points(x2[None])
        h1, h2 = _hom(n1[0]), _hom(n2[0])
        Fn = torch.linalg.inv(t2[0]).mT @ F @ torch.linalg.inv(t1[0])
        Fn = Fn / Fn.norm()
        generator = torch.Generator().manual_seed(2)
        perturbed = Fn + 1e-3 * torch.randn(7, 3, 3, generator=generator, dtype=torch.float64)
        models = torch.cat([Fn[None], _rank2_projection(perturbed)])
        models = models / models.flatten(1).norm(dim=1)[:, None, None]
        reference = _sampson_errors(models, h1, h2)
        cast = lambda t: t.to(device, torch.float32)  # noqa: E731
        errors = _sampson_from_quadratic_basis(cast(models), _sampson_quadratic_basis(cast(h1), cast(h2)))
        errors = errors.cpu().double()
        assert not errors.isnan().any()
        assert (errors >= 0).all()
        scale = float(t2[0, 0, 0])
        decisions = 0
        flips = 0
        for px in (0.5, 1.0, 2.0, 4.0):
            threshold = (px * scale) ** 2
            flips += int(((errors <= threshold) != (reference <= threshold)).sum())
            decisions += errors.numel()
        # Measured 1.2e-5 of the decisions over 40 such scenes (PR #5031 review).
        assert flips <= 1e-4 * decisions

    def test_gradcheck(self, device):
        pts1 = torch.rand(1, 5, 2, device=device, dtype=torch.float64)
        pts2 = torch.rand(1, 5, 2, device=device, dtype=torch.float64)
        Fm = create_random_fundamental_matrix(3, dtype=torch.float64, device=device)
        self.gradcheck(lambda a, b, F: _sampson_epipolar_distance_shared_impl_(a, b, F, True), (pts1, pts2, Fm))


class TestUnbatchedFundamentalMatrix(BaseTester):
    """A single (3, 3) ``Fm`` with unbatched points, as documented by the ``(*, 3, 3)`` shapes (#6108)."""

    METRICS = (
        epi.sampson_epipolar_distance,
        epi.symmetrical_epipolar_distance,
        epi.left_to_right_epipolar_distance,
        epi.right_to_left_epipolar_distance,
    )

    @pytest.mark.parametrize("metric", METRICS, ids=lambda fn: fn.__name__)
    @pytest.mark.parametrize("homogeneous", [False, True])
    def test_unbatched_fm_matches_the_batched_call_6108(self, metric, homogeneous, device, dtype):
        # A (3, 3) Fm raised "Fm must be a (*, 3, 3) tensor" on the CPU, although (3, 3) is such a shape.
        generator = torch.Generator().manual_seed(6108)
        pts1 = torch.rand(7, 2, generator=generator).to(device, dtype)
        pts2 = torch.rand(7, 2, generator=generator).to(device, dtype)
        if homogeneous:
            pts1 = torch.cat([pts1, torch.ones_like(pts1[:, :1])], dim=-1)
            pts2 = torch.cat([pts2, torch.ones_like(pts2[:, :1])], dim=-1)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)

        out = metric(pts1, pts2, Fm[0])

        assert out.shape == (7,)
        # The unbatched matmul may round differently from the batched one, so compare to rounding, not bitwise.
        self.assert_close(out, metric(pts1[None], pts2[None], Fm)[0])

    @pytest.mark.parametrize("metric", METRICS, ids=lambda fn: fn.__name__)
    def test_unbatched_fm_with_batched_points_6108(self, metric, device, dtype):
        # One matrix shared by several point sets broadcasts like any other (*, 3, 3) Fm.
        generator = torch.Generator().manual_seed(6108)
        pts1 = torch.rand(3, 7, 2, generator=generator).to(device, dtype)
        pts2 = torch.rand(3, 7, 2, generator=generator).to(device, dtype)
        Fm = create_random_fundamental_matrix(1, dtype=dtype, device=device)

        out = metric(pts1, pts2, Fm[0])

        assert out.shape == (3, 7)
        # A (3, 3) Fm folds the product into one matrix multiplication, and the expanded Fm runs a batched one. The
        # two kernels can round differently (float64 on an AVX-512 CPU and on CUDA), so compare to rounding.
        self.assert_close(out, metric(pts1, pts2, Fm.expand(3, 3, 3)))

    @pytest.mark.parametrize("metric", METRICS, ids=lambda fn: fn.__name__)
    @pytest.mark.parametrize("shape", [(3,), (4, 4), (2, 3), (2, 4, 4)])
    def test_fm_without_a_3x3_matrix_still_raises_6108(self, metric, shape, device, dtype):
        # The CUDA matmul paths of the first two metrics, and the shared-point Sampson path that (2, 4, 4) reaches on
        # the CPU, did not check Fm and raised a RuntimeError or an IndexError instead.
        pts = torch.rand(7, 2, device=device, dtype=dtype)
        with pytest.raises(ValueError, match=r"Fm must be a \(\*, 3, 3\) tensor"):
            metric(pts, pts, torch.rand(*shape, device=device, dtype=dtype))

    @pytest.mark.parametrize("metric", METRICS, ids=lambda fn: fn.__name__)
    def test_unbatched_fm_gradcheck_6108(self, metric, device):
        generator = torch.Generator().manual_seed(6108)
        pts1 = torch.rand(5, 2, generator=generator, dtype=torch.float64).to(device)
        pts2 = torch.rand(5, 2, generator=generator, dtype=torch.float64).to(device)
        Fm = create_random_fundamental_matrix(1, dtype=torch.float64, device=device)[0]
        self.gradcheck(metric, (pts1, pts2, Fm))