Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

docs(geometry): retire the #3960 shape-guard warnings (#4028)

Dmytro Mishkin9e45dddcfae8
diff --git a/AGENTS.md b/AGENTS.md
index d2112778b..b0f2a21d4 100644
--- a/AGENTS.md
+++ b/AGENTS.md
@@ -78,6 +78,7 @@ Use `KORNIA_TEST_RUNSLOW=true` to include slow tests. `KORNIA_TEST_OPTIMIZER=ind
## Documentation and generated examples
- Add every new public class or function to the corresponding `docs/source/*.rst` page so it appears in the rendered API reference.
+- Some modules document a known defect inline and link its tracking issue; `grep -rn "github.com/kornia/kornia/issues/" kornia/` finds them. That prose is part of the fix's blast radius: **before merging a change that closes one of those issues, run `grep -rnE "#NNNN|issues/NNNN" kornia/ tests/`.** Match the issue number rather than a name or a phrase β€” the wording varies across `Tracked in #NNNN`, a lowercase `tracked in` clause, a `Note:` block, a runtime error message, an `xfail` `reason=`, a plain comment and a test-name suffix (`test_wart_*_<issue>` and `test_convention_*_<issue>`, where that convention was followed), and a rendered link label can disagree with the URL beneath it. The `#`/`issues/` anchors keep the pattern off float literals. A surviving hit in `kornia/` means the change is incomplete. Hits in `tests/` are the pins that record the documented behavior; each has to be re-checked in the same change β€” a now-XPASSing strict `xfail` dropped, a wart assertion inverted β€” but a pin goes on naming its issue afterwards, so a hit there is not by itself a defect. Not every documented issue has a pin, so an empty `tests/` result is an answer rather than a failed search.
- When adding a feature detector or descriptor, update the `responses` list in `docs/generate_examples.py` and add the matching branch that renders its heatmap or score visualization. Follow the existing `DISK`, `ALIKED`, and `XFeat` examples and preserve the expected `(B, 3, H, W)` image shapes.
## Benchmarks
diff --git a/CHANGELOG.md b/CHANGELOG.md
index 61f8c1cd4..4a3c4354a 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -8,6 +8,21 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Unreleased
+### Breaking changes
+
+* The shape guards of `normalize_homography` and `denormalize_homography` now reject everything but a
+ `(3, 3)`/`(B, 3, 3)` matrix, and `normalize_homography3d`'s everything but a `(4, 4)`/`(B, 4, 4)` one (#3999).
+ A rank-4 input used to pass the guard and come back with its rank unchanged β€”
+ `normalize_homography(eye(3).expand(2, 4, 3, 3), (4, 5), (8, 9))` returned a `(2, 4, 3, 3)` matrix and now raises
+ `ValueError`. A wrong-sized input, such as a `(B, 4, 4)` to the 2-D functions, used to pass the guard and fail
+ later inside `matmul`; it now raises at the guard, and `normalize_homography3d`'s message names `Bx4x4` instead of
+ `Bx3x3`. Unbatched `(3, 3)`/`(4, 4)` matrices are still accepted and still promoted to a leading batch of 1.
+
+ One side effect reaches tracing callers: the rewritten guard evaluates its shape comparison unconditionally, where
+ the old `or`-form short-circuited past it for a rank-3 input, so `torch.jit.trace` of any of the three functions
+ now emits a `TracerWarning` about converting a tensor to a Python boolean that it did not emit before. The guard
+ is a static check and the traced graph is unchanged β€” the warning is noise, not a correctness signal.
+
### Bug fixes
* Define singleton pixel axes at normalized center, reject non-positive normalization sizes, preserve empty warp
diff --git a/kornia/geometry/conversions.py b/kornia/geometry/conversions.py
index fba730675..0974b98a2 100644
--- a/kornia/geometry/conversions.py
+++ b/kornia/geometry/conversions.py
@@ -1867,16 +1867,15 @@ def normalize_homography(
:func:`~kornia.geometry.conversions.normal_transform_pixel`'s
**corner-aligned** ones, inherited unconditionally β€” see the
convention warning below
-
- .. warning::
- The documented :math:`(B, 3, 3)` is not what the guard enforces. An
- unbatched ``(3, 3)`` is accepted and promoted to ``(1, 3, 3)``, and any
- number of leading batch dimensions is accepted and preserved
- (``(2, 4, 3, 3)`` returns ``(2, 4, 3, 3)``). Worse, a ``(B, 4, 4)``
- input passes the guard entirely and fails later inside ``matmul`` with
- a message naming neither the argument nor the expected shape. Which of
- those ranks the contract will ratify is undecided. Tracked in
- `#3960 <https://github.com/kornia/kornia/issues/3960>`_.
+ - the shape guard accepts a :math:`(3, 3)` or a :math:`(B, 3, 3)`
+ matrix and nothing else. An unbatched ``(3, 3)`` is promoted to
+ ``(1, 3, 3)`` β€” the returned matrix is batched even though the input
+ was not β€” while a rank-4 input such as ``(2, 4, 3, 3)`` and a
+ wrong-sized one such as ``(B, 4, 4)`` both raise ``ValueError``
+ naming the argument and the expected shape. Rank-4 inputs and
+ ``(B, 4, 4)`` inputs used to pass this guard, the first returned
+ unchanged in rank and the second dying later inside ``matmul``; fixed
+ in `#3999 <https://github.com/kornia/kornia/pull/3999>`_
.. warning::
In **eager mode** the inverse this function takes is a closed-form 3x3
@@ -1976,7 +1975,8 @@ def normalize_homography(
Args:
dst_pix_trans_src_pix: homography/ies from source to destination to be
- normalized. :math:`(B, 3, 3)`
+ normalized. :math:`(B, 3, 3)`, or an unbatched :math:`(3, 3)`, which
+ is promoted to :math:`(1, 3, 3)`
dsize_src: size of the source image (height, width).
dsize_dst: size of the destination image (height, width).
@@ -2301,18 +2301,18 @@ def denormalize_homography(
``(height, width)`` ``dsize`` tuples, ``dsize_src`` on the right and
``dsize_dst`` on the left, ``(x, y, 1)`` column vectors, per-sample
batching, the corner-aligned frames β€” is as documented there, and so
- are that function's shape-guard (`#3960
- <https://github.com/kornia/kornia/issues/3960>`_), dtype-pass-through
+ is the shape guard: this function carries the same one and rejects
+ the same shapes. That function's dtype-pass-through
(`#3958 <https://github.com/kornia/kornia/issues/3958>`_), int64-handling
(`#3959 <https://github.com/kornia/kornia/issues/3959>`_ β€” this
function's own clause there) and corner-alignment (`#3904
- <https://github.com/kornia/kornia/issues/3904>`_) warnings. The
- exception is the closed-form-inverse warning: in eager mode this
- function inverts through ``torch.linalg.inv`` rather than through the
- ``torch.linalg.cross`` adjugate, and under ``torch.jit.trace`` through
- a cofactor expansion that calls neither, so it does not have that gap
- in either mode β€” where it does reach ``linalg.inv`` it carries the
- ``cusolver`` dependency instead
+ <https://github.com/kornia/kornia/issues/3904>`_) warnings apply
+ here too. The exception is the closed-form-inverse warning: in eager
+ mode this function inverts through ``torch.linalg.inv`` rather than
+ through the ``torch.linalg.cross`` adjugate, and under
+ ``torch.jit.trace`` through a cofactor expansion that calls neither,
+ so it does not have that gap in either mode β€” where it does reach
+ ``linalg.inv`` it carries the ``cusolver`` dependency instead
- both round trips hold, but neither is an exact identity in general.
Each leg runs four matrix products and **two** inverses β€” one per
function, and by different routines (see the bullet below) β€” so the
@@ -2345,7 +2345,8 @@ def denormalize_homography(
Args:
dst_pix_trans_src_pix: homography/ies from source to destination to be
- denormalized. :math:`(B, 3, 3)`
+ denormalized. :math:`(B, 3, 3)`, or an unbatched :math:`(3, 3)`,
+ which is promoted to :math:`(1, 3, 3)`
dsize_src: size of the source image (height, width).
dsize_dst: size of the destination image (height, width).
@@ -2415,20 +2416,20 @@ def normalize_homography3d(
complete, this one is not, so an inverse has to be composed by hand
from :func:`~kornia.geometry.conversions.normal_transform_pixel3d`.
Tracked in `#3962 <https://github.com/kornia/kornia/issues/3962>`_
-
- .. warning::
- The shape guard has the 2-D function's ``or``-structure and the 2-D
- function's message, with only the size adapted: it accepts an unbatched
- ``(4, 4)`` (promoted to ``(1, 4, 4)``) and any number of leading batch
- dimensions, it lets a ``(B, 3, 3)`` through to die inside ``matmul``,
- and when it does fire its message names the wrong shape β€”
- ``Input dst_pix_trans_src_pix must be a Bx3x3 tensor`` from a function
- that takes 4x4 matrices. Tracked in
- `#3960 <https://github.com/kornia/kornia/issues/3960>`_.
+ - the shape guard is the 2-D function's, with the size adapted: a
+ :math:`(4, 4)` or :math:`(B, 4, 4)` matrix and nothing else, an
+ unbatched ``(4, 4)`` promoted to ``(1, 4, 4)``, and anything else β€”
+ a rank-4 tensor such as ``(2, 4, 4, 4)``, or a ``(B, 3, 3)`` β€”
+ rejected with
+ ``Input dst_pix_trans_src_pix must be a Bx4x4 tensor``. Both the
+ guard's old ``or``-structure and its message, which used to name
+ ``Bx3x3`` from a function that takes 4x4 matrices, were fixed in
+ `#3999 <https://github.com/kornia/kornia/pull/3999>`_
Args:
dst_pix_trans_src_pix: homography/ies from source to destination to be
- normalized. :math:`(B, 4, 4)`
+ normalized. :math:`(B, 4, 4)`, or an unbatched :math:`(4, 4)`, which
+ is promoted to :math:`(1, 4, 4)`
dsize_src: size of the source image (depth, height, width).
dsize_dst: size of the destination image (depth, height, width).