Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): build homography normalization matrices in the caller's dtype (#3979)

Chandan P6c415a43bf29
diff --git a/changelog.d/3979.fixed.md b/changelog.d/3979.fixed.md
new file mode 100644
index 000000000..68b700f41
--- /dev/null
+++ b/changelog.d/3979.fixed.md
@@ -0,0 +1,2 @@
+Homography normalization now builds matrices with float64-native constants for float64 inputs, preserving full precision (#3958).
+`normal_transform_pixel` and `normal_transform_pixel3d` also keep float64 constants exact under TorchScript, where they were previously rounded through float32.
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index 761e32049..7179f4bd9 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -1792,32 +1792,6 @@ def normalize_homography(
``torch.linalg.cross``, so no ``cusolver`` is needed, but a backend
without a ``cross`` kernel for the dtype raises here
- .. warning::
- The two normalization matrices are built without a ``dtype=``
- pass-through and cast to the input afterwards, so their precision is
- decided by the **ambient default dtype** and not by the argument's. At
- the ``float32`` default β€” the usual case β€” a ``float64`` caller gets
- ``float32``-rounded constants: ``normalize_homography`` of the
- ``float64`` identity from ``(4, 4)`` to ``(6, 6)`` returns
- ``0.5999999910593036`` where the ``float64``-native composition gives
- ``0.6000000000000001`` β€” a deviation of ``8.94069651646845e-09``, about
- eight significant digits instead of sixteen. Under
- ``torch.set_default_dtype(torch.float64)`` that same ``float64`` call
- returns the native ``0.6000000000000001`` instead, which is what
- identifies the missing pass-through as the cause rather than an epsilon
- or a rounding choice: it is
- :func:`~kornia.geometry.conversions.normal_transform_pixel`'s
- documented ``dtype=None`` behaviour reaching through, so setting the
- ambient default is also the workaround. Same mechanism in
- :func:`~kornia.geometry.conversions.denormalize_homography`
- (``2.483526828633842e-08`` on the same input, at the ``float32``
- default) and in
- :func:`~kornia.geometry.conversions.normalize_homography3d`. These
- deviations run through ``matmul`` and an inverse, so their trailing
- digits are backend-dependent; the magnitude β€” half the mantissa gone β€”
- is the point, not the digits. Tracked in
- `#3958 <https://github.com/kornia/kornia/issues/3958>`_.
-
.. warning::
An integer matrix is not rejected: the normalization matrices are cast
to it and truncated, and the call then fails inside torch or returns
@@ -1869,16 +1843,29 @@ def normalize_homography(
dst_h, dst_w = dsize_dst
# compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w, align_corners=align_corners).to(
- dst_pix_trans_src_pix
+ normalization_dtype = (
+ dst_pix_trans_src_pix.dtype
+ if dst_pix_trans_src_pix.is_floating_point() or dst_pix_trans_src_pix.is_complex()
+ else None
)
+ src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(
+ src_h,
+ src_w,
+ device=dst_pix_trans_src_pix.device,
+ dtype=normalization_dtype,
+ align_corners=align_corners,
+ ).to(dst_pix_trans_src_pix)
# Closed-form 3x3 inverse of the (well-conditioned) pixel-normalization matrix: cusolver-free,
# so homography normalization runs on the Jetson wheel where ``torch.linalg.inv`` dlopen-fails.
src_pix_trans_src_norm = _inverse_3x3_closed_form(src_norm_trans_src_pix)
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w, align_corners=align_corners).to(
- dst_pix_trans_src_pix
- )
+ dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(
+ dst_h,
+ dst_w,
+ device=dst_pix_trans_src_pix.device,
+ dtype=normalization_dtype,
+ align_corners=align_corners,
+ ).to(dst_pix_trans_src_pix)
# compute chain transformations
dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
@@ -2018,7 +2005,20 @@ def normal_transform_pixel(
sy = 2.0 / height
tx = (1.0 - width) / width
ty = (1.0 - height) / height
- tr_mat = torch.tensor([[sx, 0.0, tx], [0.0, sy, ty], [0.0, 0.0, 1.0]], device=device, dtype=dtype)
+ if torch.jit.is_scripting():
+ # TorchScript builds ``torch.tensor`` from a float list in float32 even when ``dtype`` is
+ # float64, so scale a typed anchor instead to keep the constants in the output dtype.
+ anchor = torch.ones((), device=device, dtype=dtype)
+ zero = anchor * 0.0
+ tr_mat = torch.stack(
+ [
+ torch.stack([anchor * sx, zero, anchor * tx]),
+ torch.stack([zero, anchor * sy, anchor * ty]),
+ torch.stack([zero, zero, anchor]),
+ ]
+ )
+ else:
+ tr_mat = torch.tensor([[sx, 0.0, tx], [0.0, sy, ty], [0.0, 0.0, 1.0]], device=device, dtype=dtype)
else:
# Low-precision floating types cannot represent every practical image size exactly
# (e.g. bfloat16 rounds 257 to 256). Keep symbolic size arithmetic in at least
@@ -2147,11 +2147,24 @@ def normal_transform_pixel3d(
tx = 0.0 if width == 1 else -1.0
ty = 0.0 if height == 1 else -1.0
tz = 0.0 if depth == 1 else -1.0
- tr_mat = torch.tensor(
- [[sx, 0.0, 0.0, tx], [0.0, sy, 0.0, ty], [0.0, 0.0, sz, tz], [0.0, 0.0, 0.0, 1.0]],
- device=device,
- dtype=dtype,
- )
+ if torch.jit.is_scripting():
+ # As in 2-D: a typed anchor keeps float64 constants exact under TorchScript.
+ anchor = torch.ones((), device=device, dtype=dtype)
+ zero = anchor * 0.0
+ tr_mat = torch.stack(
+ [
+ torch.stack([anchor * sx, zero, zero, anchor * tx]),
+ torch.stack([zero, anchor * sy, zero, anchor * ty]),
+ torch.stack([zero, zero, anchor * sz, anchor * tz]),
+ torch.stack([zero, zero, zero, anchor]),
+ ]
+ )
+ else:
+ tr_mat = torch.tensor(
+ [[sx, 0.0, 0.0, tx], [0.0, sy, 0.0, ty], [0.0, 0.0, sz, tz], [0.0, 0.0, 0.0, 1.0]],
+ device=device,
+ dtype=dtype,
+ )
else:
# As in 2-D: keep the symbolic size arithmetic in at least float32, and resolve the
# output dtype first so a ``dtype=None`` call under a half default dtype is promoted too.
@@ -2200,9 +2213,8 @@ def denormalize_homography(
source pixels to destination pixels. The ``dsize`` roles, the
``(x, y, 1)`` column vectors, per-sample batching, the
``align_corners``-selected frames and the shape guard are as
- documented there, and so are its dtype
- (`#3958 <https://github.com/kornia/kornia/issues/3958>`_) and integer
- (`#3959 <https://github.com/kornia/kornia/issues/3959>`_) warnings
+ documented there, including its integer-input
+ (`#3959 <https://github.com/kornia/kornia/issues/3959>`_) warning
- ``inv(N_dst)`` goes through ``torch.linalg.inv`` in eager mode (so
``cusolver`` on ``cuda``), not through the ``cross``-based adjugate
- the two functions invert each other to a small multiple of the
@@ -2234,13 +2246,26 @@ def denormalize_homography(
dst_h, dst_w = dsize_dst
# compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(src_h, src_w, align_corners=align_corners).to(
- dst_pix_trans_src_pix
- )
-
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(dst_h, dst_w, align_corners=align_corners).to(
- dst_pix_trans_src_pix
+ normalization_dtype = (
+ dst_pix_trans_src_pix.dtype
+ if dst_pix_trans_src_pix.is_floating_point() or dst_pix_trans_src_pix.is_complex()
+ else None
)
+ src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel(
+ src_h,
+ src_w,
+ device=dst_pix_trans_src_pix.device,
+ dtype=normalization_dtype,
+ align_corners=align_corners,
+ ).to(dst_pix_trans_src_pix)
+
+ dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel(
+ dst_h,
+ dst_w,
+ device=dst_pix_trans_src_pix.device,
+ dtype=normalization_dtype,
+ align_corners=align_corners,
+ ).to(dst_pix_trans_src_pix)
dst_denorm_trans_dst_pix = _torch_inverse_cast(dst_norm_trans_dst_pix)
# compute chain transformations
dst_norm_trans_src_norm: torch.Tensor = dst_denorm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_norm_trans_src_pix)
@@ -2302,10 +2327,19 @@ def normalize_homography3d(
src_d, src_h, src_w = dsize_src
dst_d, dst_h, dst_w = dsize_dst
# compute the transformation pixel/norm for src/dst
- src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel3d(src_d, src_h, src_w).to(dst_pix_trans_src_pix)
+ normalization_dtype = (
+ dst_pix_trans_src_pix.dtype
+ if dst_pix_trans_src_pix.is_floating_point() or dst_pix_trans_src_pix.is_complex()
+ else None
+ )
+ src_norm_trans_src_pix: torch.Tensor = normal_transform_pixel3d(
+ src_d, src_h, src_w, device=dst_pix_trans_src_pix.device, dtype=normalization_dtype
+ ).to(dst_pix_trans_src_pix)
src_pix_trans_src_norm = _torch_inverse_cast(src_norm_trans_src_pix)
- dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel3d(dst_d, dst_h, dst_w).to(dst_pix_trans_src_pix)
+ dst_norm_trans_dst_pix: torch.Tensor = normal_transform_pixel3d(
+ dst_d, dst_h, dst_w, device=dst_pix_trans_src_pix.device, dtype=normalization_dtype
+ ).to(dst_pix_trans_src_pix)
# compute chain transformations
dst_norm_trans_src_norm: torch.Tensor = dst_norm_trans_dst_pix @ (dst_pix_trans_src_pix @ src_pix_trans_src_norm)
return dst_norm_trans_src_norm
diff --git a/tests/geometry/test_conversions.py b/tests/geometry/test_conversions.py
index 970a8e80c..18363ea56 100644
--- a/tests/geometry/test_conversions.py
+++ b/tests/geometry/test_conversions.py
@@ -483,8 +483,7 @@ def _ambient_default_dtype(dtype: torch.dtype) -> Iterator[None]:
# Swap the PROCESS-WIDE torch default dtype for the duration of a with-block, restoring it even
# if the body raises. The finally-restore is the safety-critical part -- a leaked float64
# default would silently change every later test's tolerances across the whole suite -- so it
- # lives in one place instead of being hand-rolled at each ambient-default pin (the two #3958
- # pins today; any future one should use this too).
+ # lives in one place instead of being hand-rolled at each ambient-default pin.
previous = torch.get_default_dtype()
torch.set_default_dtype(dtype)
try:
@@ -3530,9 +3529,9 @@ class TestNormalTransformPixel(BaseTester):
# Python ints rather than tensors. With dtype=None the matrix is built by torch.tensor()
# from Python floats, so its dtype is torch's AMBIENT default rather than float32
# unconditionally: changing the process default changes the result. That is the mechanism
- # behind the float32 constants that leak into float64 homography pipelines (kornia#3958,
- # pinned in TestNormalizeHomography): normalize_homography calls these helpers without
- # passing dtype= through, so they materialise at the ambient default and are cast after.
+ # behind the former float32 constants in float64 homography pipelines: the public
+ # homography functions now pass a floating or complex input dtype through explicitly
+ # (an integer input still passes None and is cast afterwards, see kornia#3959).
# The dtype fixture is dropped because the claim is about the *absence* of a dtype
# argument, and the default is read back through torch.get_default_dtype() rather than
# hardcoded, so the pin says "follows the ambient default" and not "is always float32".
@@ -3555,10 +3554,7 @@ class TestNormalTransformPixel(BaseTester):
with _ambient_default_dtype(torch.float64):
ambient_dtype = normal_transform_pixel(4, 5).dtype
- assert ambient_dtype == torch.float64, (
- "normal_transform_pixel no longer follows the ambient default dtype, so the kornia#3958 "
- "mechanism pinned in TestNormalizeHomography has changed"
- )
+ assert ambient_dtype == torch.float64, "normal_transform_pixel no longer follows the ambient default dtype"
def test_convention_corner_aligned_scale_is_two_over_size_minus_one(self, device):
# Convention pin: by default the matrix is diag(2/(width - 1), 2/(height - 1), 1) with a -1
@@ -3822,9 +3818,7 @@ class TestNormalizeHomography(BaseTester):
# normal_transform_pixel, whose corner-aligned convention all three inherit.
# The CONVENTION pins below (composition, direction, round-trip, batching, 3-D) use sizes of
# the form 2**k + 1 (3, 5, 9, 17) so that every 2/(size - 1) is exact in every dtype and those
- # pins compare at atol=rtol=0. That also keeps them independent of kornia#3958 (the float32
- # constants leak, pinned separately below with non-dyadic sizes): a fix for #3958 must not flip
- # an ordering or direction pin. The exactness invariant is theirs alone -- the bug pins below
+ # pins compare at atol=rtol=0. The exactness invariant is theirs alone -- the numerical pins below
# deliberately step outside it (the round-trip pin's non-dyadic (4, 5)/(8, 9) legs at
# atol=32*eps, the #3960 shape-guard cells), so a new atol=0 pin belongs here
# only at these sizes AND with a literal whose intermediates are exact. The invariant also
@@ -3843,11 +3837,10 @@ class TestNormalizeHomography(BaseTester):
# NOTE: kornia#3904 landed and moved none of these. normalize_homography and
# denormalize_homography now take an align_corners argument and forward it to
# normal_transform_pixel, but it defaults to True, so the composition, direction, round-trip,
- # batching and 3-D pins here -- and the #3957 singleton and #3958 bug pins -- still see the
+ # batching and 3-D pins here -- and the #3957 singleton pin -- still see the
# corner-aligned 2/(size - 1) constants they were written against. normalize_homography3d has no
# such argument at all. A change to that default, not the parameter, is what would flip them.
- # They record current default behavior; none of them ratifies that choice as contract. (The
- # #3958 pins would also flip on a #3958 fix, which is their point and is separate from this.)
+ # They record current default behavior; none of them ratifies that choice as contract.
def test_gradcheck(self, device):
# The three functions are on the warp_perspective path and are differentiable in their
@@ -4102,28 +4095,14 @@ class TestNormalizeHomography(BaseTester):
rtol=0.0,
)
- @pytest.mark.xfail(
- raises=AssertionError,
- reason="the normalization matrices are built at the ambient default dtype and cast to the "
- "input afterwards, so a float64 caller gets float32-rounded constants β€” kornia#3958",
- strict=True,
- )
- def test_convention_float64_input_gets_float64_normalization_constants_3958(self, device):
- # Intended behavior: a float64 homography is normalized with float64 constants, so the
- # entries carry float64 accuracy. They do not: normalize_homography calls
- # normal_transform_pixel() without passing dtype= through, so the constants materialise at
- # the ambient default (float32) and are cast to float64 afterwards, leaving about eight
- # significant digits. For sizes (4, 4) -> (6, 6) the (0, 0) entry is mathematically
+ def test_convention_float64_input_gets_float64_normalization_constants(self, device):
+ # A float64 homography is normalized with float64 constants, so the entries carry float64
+ # accuracy. For sizes (4, 4) -> (6, 6) the (0, 0) entry is mathematically
# 2/(6 - 1) * (4 - 1)/2 = 0.6 exactly, and any float64-native evaluation lands within an ulp
- # of it; the tolerance 1e-12 sits four orders above float64 noise and three below the
- # deviation the current implementation produces.
- # Non-dyadic sizes are required here: with 2**k + 1 sizes the float32 constants are exact
- # and there is nothing to leak, which is why the ordering pins above use them and this pin
- # does not. float64 is hardcoded and the dtype fixture dropped because the claim is a
- # float64 claim, and the skip is visible so that on MPS, which has no float64, a raw
- # TypeError cannot satisfy the raises=AssertionError mark instead of the assertion.
- # Marked xfail(strict=True) so fixing #3958 makes this XPASS and forces the mark out.
- # Companion wart: test_wart_float32_constants_leak_into_float64_results_3958.
+ # of it; the tolerance 1e-12 sits four orders above float64 noise and nearly four below the
+ # 8.9e-9 deviation that float32-rounded constants gave. Non-dyadic sizes are required here: with 2**k + 1
+ # sizes the constants are exact. float64 is hardcoded and the dtype fixture dropped because
+ # the claim is a float64 claim.
_skip_if_dtype_unavailable(device, torch.float64)
identity = torch.eye(3, device=device, dtype=torch.float64)[None]
@@ -4131,76 +4110,27 @@ class TestNormalizeHomography(BaseTester):
normalized = kornia.geometry.conversions.normalize_homography(identity, (4, 4), (6, 6))
assert abs(normalized[0, 0, 0].item() - 0.6) < 1e-12, (
- "kornia#3958: normalize_homography did not use float64 normalization constants"
- )
-
- def test_wart_float32_constants_leak_into_float64_results_3958(self, device):
- # Wart pin for kornia#3958, companion to the strict xfail above: assert the CURRENT
- # float32-rounded entries in a float64 result. Four cells:
- # (1) normalize_homography, whose src factor is inverted by the closed-form 3x3 inverse;
- # (2) denormalize_homography, whose dst factor is inverted by _torch_inverse_cast instead
- # -- a separate code path that could be fixed on its own;
- # (3) normalize_homography3d, which calls the 3-D helper and is a third call site;
- # (4) the control that proves the cause is the missing dtype= pass-through and not an
- # epsilon or a rounding choice: with the ambient default dtype set to float64 the
- # same call returns the float64-native value 0.6000000000000001, because the helper
- # now materialises in float64 before the cast. Cell (4) also fails if the helpers stop
- # reading the ambient default, which is the other half of the same mechanism.
- # If any cell fails, #3958 was (partly) fixed -- flip/remove the strict xfail above. NOT a
- # contract that float64 callers must keep receiving float32-rounded constants.
- # atol 1e-10 pins the MAGNITUDE of the deviation, which is what the docstring warning
- # promises ("the magnitude -- half the mantissa gone -- is the point ... rather than the
- # digits"): it sits an order below the ~8.9e-09 deviation being discriminated (so a fix
- # still flips these cells red) and six above the ~1.1e-16 ulp of the entries, so no
- # backend's reassociation of the matmul-and-inverse chain can flip them. float64 is
- # hardcoded for the same reason as the xfail above.
- # Snippet used to generate expected (torch only, executed on cpu float64):
- # normalize_homography(eye(3, float64), (4, 4), (6, 6))[0, 0] -> 0.5999999910593036
- # denormalize_homography(eye(3, float64), (4, 4), (6, 6))[0, 0] -> 1.6666666915019348
- # normalize_homography3d(eye(4, float64), (4, 4, 4), (6, 6, 6))[0, 0] -> 0.5999999910593036
- # with torch.set_default_dtype(torch.float64):
- # normalize_homography(eye(3, float64), (4, 4), (6, 6))[0, 0] -> 0.6000000000000001
- _skip_if_dtype_unavailable(device, torch.float64)
-
- normalize_homography = kornia.geometry.conversions.normalize_homography
- identity = torch.eye(3, device=device, dtype=torch.float64)[None]
- identity3d = torch.eye(4, device=device, dtype=torch.float64)[None]
-
- normalized = normalize_homography(identity, (4, 4), (6, 6))[0, 0, 0]
- denormalized = kornia.geometry.conversions.denormalize_homography(identity, (4, 4), (6, 6))[0, 0, 0]
- normalized3d = kornia.geometry.conversions.normalize_homography3d(identity3d, (4, 4, 4), (6, 6, 6))[0, 0, 0]
+ "normalize_homography did not use float64 normalization constants"
+ )
- with _ambient_default_dtype(torch.float64):
- with_float64_default = normalize_homography(identity, (4, 4), (6, 6))[0, 0, 0]
+ @pytest.mark.parametrize(
+ ("op_name", "src", "dst"),
+ [
+ ("normalize_homography", (4, 4), (6, 6)),
+ ("denormalize_homography", (4, 4), (6, 6)),
+ ("normalize_homography3d", (4, 4, 7), (6, 6, 9)),
+ ],
+ )
+ def test_scripted_normalization_constants_match_eager(self, device, dtype, op_name, src, dst):
+ # TorchScript builds torch.tensor from a float list in float32 even for dtype=float64, so
+ # the scripted path must not use it: under float64 the scripted result drifted from eager
+ # by the same ~1e-8 as the old float32 constants. Non-dyadic sizes keep that drift visible.
+ _skip_if_dtype_unavailable(device, dtype)
+ op = getattr(kornia.geometry.conversions, op_name)
+ size = 4 if op_name.endswith("3d") else 3
+ homography = torch.eye(size, device=device, dtype=dtype)[None]
- assert_close(
- normalized,
- torch.tensor(0.5999999910593036, device=device, dtype=torch.float64),
- atol=1e-10,
- rtol=0.0,
- msg=_issue_msg("kornia#3958: normalize_homography no longer rounds its constants to float32"),
- )
- assert_close(
- denormalized,
- torch.tensor(1.6666666915019348, device=device, dtype=torch.float64),
- atol=1e-10,
- rtol=0.0,
- msg=_issue_msg("kornia#3958: denormalize_homography no longer rounds its constants to float32"),
- )
- assert_close(
- normalized3d,
- torch.tensor(0.5999999910593036, device=device, dtype=torch.float64),
- atol=1e-10,
- rtol=0.0,
- msg=_issue_msg("kornia#3958: normalize_homography3d no longer rounds its constants to float32"),
- )
- assert_close(
- with_float64_default,
- torch.tensor(0.6000000000000001, device=device, dtype=torch.float64),
- atol=1e-10,
- rtol=0.0,
- msg=_issue_msg("kornia#3958: the ambient default dtype no longer decides the constants' precision"),
- )
+ self.assert_close(torch.jit.script(op)(homography, src, dst), op(homography, src, dst), atol=0.0, rtol=0.0)
@pytest.mark.parametrize("op_name", ["normalize_homography", "denormalize_homography"])
def test_wart_integer_input_raises_or_nans_by_backend_3959(self, device, op_name):
diff --git a/tests/geometry/transform/test_homography_warper.py b/tests/geometry/transform/test_homography_warper.py
index a62377aa7..fac59068a 100644
--- a/tests/geometry/transform/test_homography_warper.py
+++ b/tests/geometry/transform/test_homography_warper.py
@@ -441,6 +441,72 @@ class TestHomographyNormalTransform(BaseTester):
self.assert_close(output, expected.to(device=device, dtype=dtype), atol=1e-4, rtol=1e-4)
+class TestHomographyNormalizePrecision(BaseTester):
+ """The normalization matrices must be built in the caller's dtype, not in float32 and then cast.
+
+ The oracle is the documented contract of each op, evaluated natively in float64 with the same
+ public helper: ``normalize_homography`` chains ``normal_transform_pixel(dst)`` with the inverse
+ of ``normal_transform_pixel(src)``. Every size is picked so that ``2 / (size - 1)`` is not
+ exactly representable in binary -- the 2-D pins use 2/3 and 2/5, the 3-D pin 2/3, 2/5, 2/7 and
+ 2/9 across all six axes -- which is what exposes the float32 rounding. A size whose
+ ``size - 1`` is a power of two (2/1, 2/2, 2/4, 2/8 ...) is exact in every dtype and pins
+ nothing here, so do not "simplify" these numbers.
+ """
+
+ @staticmethod
+ def _skip_if_no_float64(device):
+ if device.type == "mps":
+ pytest.skip("float64 is not supported on MPS")
+
+ def test_normalize_homography_float64(self, device):
+ self._skip_if_no_float64(device)
+ f64 = torch.float64
+ dst_pix_trans_src_pix = torch.eye(3, device=device, dtype=f64)[None]
+
+ out = kornia.geometry.conversions.normalize_homography(dst_pix_trans_src_pix, (4, 4), (6, 6))
+
+ src_norm = kornia.geometry.conversions.normal_transform_pixel(4, 4, device=device, dtype=f64)
+ dst_norm = kornia.geometry.conversions.normal_transform_pixel(6, 6, device=device, dtype=f64)
+ expected = dst_norm @ _torch_inverse_cast(src_norm)
+ self.assert_close(out, expected, rtol=0.0, atol=1e-15)
+
+ def test_denormalize_homography_float64(self, device):
+ self._skip_if_no_float64(device)
+ f64 = torch.float64
+ dst_pix_trans_src_pix = torch.eye(3, device=device, dtype=f64)[None]
+
+ out = kornia.geometry.conversions.denormalize_homography(dst_pix_trans_src_pix, (4, 4), (6, 6))
+
+ src_norm = kornia.geometry.conversions.normal_transform_pixel(4, 4, device=device, dtype=f64)
+ dst_norm = kornia.geometry.conversions.normal_transform_pixel(6, 6, device=device, dtype=f64)
+ expected = _torch_inverse_cast(dst_norm) @ src_norm
+ self.assert_close(out, expected, rtol=0.0, atol=1e-15)
+
+ def test_normalize_homography3d_float64(self, device):
+ self._skip_if_no_float64(device)
+ f64 = torch.float64
+ dst_pix_trans_src_pix = torch.eye(4, device=device, dtype=f64)[None]
+
+ out = kornia.geometry.conversions.normalize_homography3d(dst_pix_trans_src_pix, (4, 4, 6), (6, 8, 10))
+
+ src_norm = kornia.geometry.conversions.normal_transform_pixel3d(4, 4, 6, device=device, dtype=f64)
+ dst_norm = kornia.geometry.conversions.normal_transform_pixel3d(6, 8, 10, device=device, dtype=f64)
+ expected = dst_norm @ _torch_inverse_cast(src_norm)
+ self.assert_close(out, expected, rtol=0.0, atol=1e-15)
+
+ def test_dtype_device_preserved(self, device, dtype):
+ homo_2d = torch.eye(3, device=device, dtype=dtype)[None]
+ homo_3d = torch.eye(4, device=device, dtype=dtype)[None]
+
+ for out in (
+ kornia.geometry.conversions.normalize_homography(homo_2d, (4, 4), (6, 6)),
+ kornia.geometry.conversions.denormalize_homography(homo_2d, (4, 4), (6, 6)),
+ kornia.geometry.conversions.normalize_homography3d(homo_3d, (2, 4, 5), (3, 8, 9)),
+ ):
+ assert out.dtype == dtype
+ assert out.device.type == device.type
+
+
class TestHomographyWarper3D(BaseTester):
num_tests = 10
threshold = 0.1