Skip to content

🐍 Geometric Computer Vision Library for Spatial AI

fix(geometry): reject quad-interp keypoints that still move on the last iteration (#5060)

Mohammad Hijjawi26ac82a51995
diff --git a/changelog.d/5060.breaking.md b/changelog.d/5060.breaking.md
new file mode 100644
index 000000000..fa801f71f
--- /dev/null
+++ b/changelog.d/5060.breaking.md
@@ -0,0 +1,9 @@
+`conv_quad_interp3d`, `iterative_quad_interp3d`, `ConvQuadInterp3d`, `IterativeQuadInterp3d` and `AdaptiveQuadInterp3d`
+now reject a maximum whose last iteration still moves its integer centre: it keeps its grid coordinates and its input
+value. Previously such a point was reported at the new centre plus the shift solved at the old one, one voxel beyond
+that fit on every axis that moved, with its value read at the moved voxel, and a point alternating between two cells
+landed one voxel off on either side depending on the parity of `n_iters`. A rejected maximum, whatever rejected it, now
+reads its value at the voxel it reports instead of the voxel it last moved to. With the default `n_iters=5` this
+changes a few percent of the refined maxima (1.4% to 8.8% on blurred-noise inputs), so `ScaleSpaceDetector` and the
+detectors built on it, such as `SIFTFeatureScaleSpace` with its default `descriptor_backend="patch"`, return different
+keypoint positions, scales and responses. (#5038)
diff --git a/kornia/geometry/subpix/spatial_soft_argmax.py b/kornia/geometry/subpix/spatial_soft_argmax.py
index be67433c7..67d563679 100644
--- a/kornia/geometry/subpix/spatial_soft_argmax.py
+++ b/kornia/geometry/subpix/spatial_soft_argmax.py
@@ -778,7 +778,8 @@ def conv_quad_interp3d(
Args:
input: response pyramid with shape :math:`(B, C, D, H, W)`.
- n_iters: maximum number of localization iterations per keypoint.
+ n_iters: maximum number of localization iterations per keypoint. A keypoint that still moves on the
+ last iteration has not converged and is rejected: it keeps its grid coordinates and input value.
strict_maxima_bonus: deprecated since kornia 0.9.0 and ignored; passing it emits a
:class:`DeprecationWarning`. Select strict maxima with ``precomputed_nms_mask`` or
:func:`~kornia.geometry.subpix.nms3d` instead.
@@ -943,6 +944,7 @@ def conv_quad_interp3d(
shift_y = torch.zeros(N, device=device, dtype=dtype)
shift_s = torch.zeros(N, device=device, dtype=dtype)
grad_dot_shift = torch.zeros(N, device=device, dtype=dtype)
+ moved = torch.zeros(N, dtype=torch.bool, device=device)
for _ in range(n_iters):
di = d_cur.clamp(1, D - 2)
@@ -973,12 +975,14 @@ def conv_quad_interp3d(
move_px = valid & (sx > max_subpixel_shift)
move_nx = valid & (sx < -max_subpixel_shift)
+ moved = move_px | move_nx
new_w = w_cur + move_px.long() - move_nx.long()
valid = valid & (new_w >= 1) & (new_w <= W - 2)
w_cur = new_w.clamp(0, W - 1)
move_py = valid & (sy > max_subpixel_shift)
move_ny = valid & (sy < -max_subpixel_shift)
+ moved = moved | move_py | move_ny
new_h = h_cur + move_py.long() - move_ny.long()
valid = valid & (new_h >= 1) & (new_h <= H - 2)
h_cur = new_h.clamp(0, H - 1)
@@ -986,11 +990,14 @@ def conv_quad_interp3d(
if allow_scale_steps:
move_ps = valid & (ss > max_subpixel_shift)
move_ns = valid & (ss < -max_subpixel_shift)
+ moved = moved | move_ps | move_ns
new_d = d_cur + move_ps.long() - move_ns.long()
valid = valid & (new_d >= 1) & (new_d <= D - 2)
d_cur = new_d.clamp(0, D - 1)
- valid = valid & (shift_x.abs() <= 1.5) & (shift_y.abs() <= 1.5) & (shift_s.abs() <= 1.5)
+ # A point the last iteration moved has not converged: its shift was solved at the previous centre, so
+ # reject it rather than report it one voxel beyond that fit.
+ valid = valid & ~moved & (shift_x.abs() <= 1.5) & (shift_y.abs() <= 1.5) & (shift_s.abs() <= 1.5)
# ── Write refined coordinates and corrected response ──────────────────────
b_idx = bc_idx // C
@@ -1001,7 +1008,10 @@ def conv_quad_interp3d(
coords_max[b_idx, c_idx, 2, d_idx, h_idx, w_idx] = torch.where(valid, h_cur.to(dtype) + shift_y, h_idx.to(dtype))
val_correction = 0.5 * torch.where(valid, grad_dot_shift, torch.zeros_like(grad_dot_shift))
- # Use the final recentered position for val_center (h_cur/w_cur may have moved during iteration)
+ # Read the value where the point is reported: the recentered voxel, or the NMS voxel of a rejected point
+ d_cur = torch.where(valid, d_cur, d_idx)
+ h_cur = torch.where(valid, h_cur, h_idx)
+ w_cur = torch.where(valid, w_cur, w_idx)
val_center = input.view(BC, D, H, W)[bc_idx, d_cur, h_cur, w_cur]
y_max[b_idx, c_idx, d_idx, h_idx, w_idx] = val_center + val_correction
@@ -1100,7 +1110,8 @@ def iterative_quad_interp3d(
Args:
input: response pyramid with shape :math:`(B, C, D, H, W)`.
- n_iters: maximum number of localization iterations per keypoint.
+ n_iters: maximum number of localization iterations per keypoint. A keypoint that still moves on the
+ last iteration has not converged and is rejected: it keeps its grid coordinates and input value.
strict_maxima_bonus: deprecated since kornia 0.9.0 and ignored; passing it emits a
:class:`DeprecationWarning`. Select strict maxima with ``precomputed_nms_mask`` or
:func:`~kornia.geometry.subpix.nms3d` instead.
@@ -1213,6 +1224,7 @@ def iterative_quad_interp3d(
shift_y = torch.zeros(N, device=device, dtype=dtype)
shift_s = torch.zeros(N, device=device, dtype=dtype)
grad_dot_shift = torch.zeros(N, device=device, dtype=dtype)
+ moved = torch.zeros(N, dtype=torch.bool, device=device)
inp_flat = inp.reshape(-1)
bc_base = bc_idx * DHW
@@ -1273,12 +1285,14 @@ def iterative_quad_interp3d(
move_pos_x = valid & (sx > max_subpixel_shift)
move_neg_x = valid & (sx < -max_subpixel_shift)
+ moved = move_pos_x | move_neg_x
new_w = w_cur + move_pos_x.long() - move_neg_x.long()
valid = valid & (new_w >= 1) & (new_w <= W - 2)
w_cur = new_w.clamp(0, W - 1)
move_pos_y = valid & (sy > max_subpixel_shift)
move_neg_y = valid & (sy < -max_subpixel_shift)
+ moved = moved | move_pos_y | move_neg_y
new_h = h_cur + move_pos_y.long() - move_neg_y.long()
valid = valid & (new_h >= 1) & (new_h <= H - 2)
h_cur = new_h.clamp(0, H - 1)
@@ -1286,11 +1300,14 @@ def iterative_quad_interp3d(
if allow_scale_steps:
move_pos_s = valid & (ss > max_subpixel_shift)
move_neg_s = valid & (ss < -max_subpixel_shift)
+ moved = moved | move_pos_s | move_neg_s
new_d = d_cur + move_pos_s.long() - move_neg_s.long()
valid = valid & (new_d >= 1) & (new_d <= D - 2)
d_cur = new_d.clamp(0, D - 1)
- valid = valid & (shift_x.abs() <= 1.5) & (shift_y.abs() <= 1.5) & (shift_s.abs() <= 1.5)
+ # A point the last iteration moved has not converged: its shift was solved at the previous centre, so
+ # reject it rather than report it one voxel beyond that fit.
+ valid = valid & ~moved & (shift_x.abs() <= 1.5) & (shift_y.abs() <= 1.5) & (shift_s.abs() <= 1.5)
b_idx = bc_idx // C
c_idx = bc_idx % C
@@ -1304,7 +1321,10 @@ def iterative_quad_interp3d(
coords_max[b_idx, c_idx, 2, d_idx, h_idx, w_idx] = final_y
val_correction = 0.5 * torch.where(valid, grad_dot_shift, torch.zeros_like(grad_dot_shift))
- # Use the final recentered position for val_center (h_cur/w_cur may have moved during iteration)
+ # Read the value where the point is reported: the recentered voxel, or the NMS voxel of a rejected point
+ d_cur = torch.where(valid, d_cur, d_idx)
+ h_cur = torch.where(valid, h_cur, h_idx)
+ w_cur = torch.where(valid, w_cur, w_idx)
val_center = inp[bc_idx, d_cur, h_cur, w_cur]
y_max[b_idx, c_idx, d_idx, h_idx, w_idx] = val_center + val_correction
diff --git a/tests/geometry/subpix/test_spatial_softargmax.py b/tests/geometry/subpix/test_spatial_softargmax.py
index 7e7d64a6a..d123c87ae 100644
--- a/tests/geometry/subpix/test_spatial_softargmax.py
+++ b/tests/geometry/subpix/test_spatial_softargmax.py
@@ -808,6 +808,39 @@ class TestConvQuadInterp3d(BaseTester):
coord5, _ = conv_quad_interp3d(sample, n_iters=5)
self.assert_close(coord1, coord5, atol=1e-5, rtol=1e-5)
+ @pytest.mark.parametrize("op", ["conv_quad_interp3d", "iterative_quad_interp3d"])
+ @pytest.mark.parametrize("n_iters", [1, 2])
+ @pytest.mark.parametrize("axis", [0, 1, 2], ids=["scale", "x", "y"])
+ @pytest.mark.parametrize("sign", [1.0, -1.0], ids=["pos", "neg"])
+ def test_last_iteration_move_rejects_5038(self, device, dtype, op, n_iters, axis, sign):
+ # Quadratic with its peak (value 0) 0.65 voxel from the candidate at (d, y, x) = (2, 2, 5) along one axis,
+ # either way. The solve at the candidate asks for a 0.65 > max_subpixel_shift move, so the centre steps one
+ # voxel toward the peak. With n_iters=1 no solve follows: the point has not converged and is rejected,
+ # keeping its grid coordinates and the value read at that voxel, -(0.65)**2. It used to be reported at the
+ # new centre plus the old shift, one voxel beyond the peak, with the value read at the moved voxel. With
+ # n_iters=2 the solve at the new centre converges to the peak.
+ D, H, W = 5, 6, 9
+ candidate = [2.0, 5.0, 2.0] # coords_max layout: dim2 = [scale, x(width), y(height)]
+ peak = list(candidate)
+ peak[axis] += sign * 0.65
+ zz, yy, xx = torch.meshgrid(
+ torch.arange(D, device=device, dtype=dtype),
+ torch.arange(H, device=device, dtype=dtype),
+ torch.arange(W, device=device, dtype=dtype),
+ indexing="ij",
+ )
+ sample = (-((zz - peak[0]) ** 2) - (xx - peak[1]) ** 2 - (yy - peak[2]) ** 2)[None, None]
+ mask = torch.zeros(1, 1, D, H, W, dtype=torch.bool, device=device)
+ mask[0, 0, 2, 2, 5] = True
+ kwargs = {"dilation_radius": 2} if op == "conv_quad_interp3d" else {}
+ coord, val = getattr(kornia.geometry.subpix, op)(sample, n_iters=n_iters, precomputed_nms_mask=mask, **kwargs)
+ converged = n_iters == 2
+ expected = torch.tensor(peak if converged else candidate, device=device, dtype=dtype)
+ self.assert_close(coord[0, 0, :, 2, 2, 5], expected, atol=1e-4, rtol=1e-4)
+ # a rejected point keeps the input value at its NMS voxel; the converged one reads the peak value 0
+ expected_val = torch.zeros((), device=device, dtype=dtype) if converged else sample[0, 0, 2, 2, 5]
+ self.assert_close(val[0, 0, 2, 2, 5], expected_val, atol=1e-4, rtol=1e-4)
+
class TestAdaptiveQuadInterp3d(BaseTester):
def test_smoke(self, device, dtype):