#5066·kornia

conv_soft_argmax2d/3d: with an even kernel_size and padding = k/2 the border windows average a zero-padded coordinate into their centre, so the last window of a row reports x = 3.5 for a peak at x = 8

Author: ducha-aikiCreated Sep 28, 2026Updated Sep 28, 2026

conv_soft_argmax2d and conv_soft_argmax3d compute each window's centre by convolving the pixel-coordinate grid with _get_center_kernel2d/3d, using the call's own padding (grid_global_pooled = F.conv2d(grid_global, center_kernel, stride=stride, padding=padding)). The convolution pads the coordinate grid with zeros. With an odd kernel_size, avg_pool allows at most k // 2 padding, so the centre pixel of every window is inside the image and the zeros are never read. With an even k, the centre is the mean of the two middle pixels, and at padding = k / 2 one of them lies in the padding for the first and the last window of each axis. That pixel contributes the coordinate 0 instead of -1 or W. The last window of a row then reports a coordinate near the middle of the image. The softmax weights and output_value are unaffected: padded positions have zero weight, as intended.

Reproduction

python
import torch

from kornia.geometry.subpix import conv_soft_argmax2d

# One hot pixel in the last column (x = 8) of a 5 x 9 map; every window that contains it should report x = 8.
y = torch.zeros(1, 1, 5, 9, dtype=torch.float64)
y[0, 0, 2, 8] = 50.0
for k, pad in ((3, 1), (4, 1), (4, 2), (2, 1)):
    coords = conv_soft_argmax2d(y, (k, k), (1, 1), (pad, pad), normalized_coordinates=False, eps=0.0)
    windows = [b for b in range(coords.shape[-1]) if b - pad <= 8 <= b - pad + k - 1]
    print(f"k={k} padding={pad}: x = {[round(coords[0, 0, 0, 2, b].item(), 3) for b in windows]}")
k=3 padding=1: x = [8.0, 8.0]
k=4 padding=1: x = [7.5, 7.833]
k=4 padding=2: x = [7.5, 7.833, 3.667]
k=2 padding=1: x = [8.5, 3.0]

With the #5059 head applied (in-window offsets in pixels):

k=3 padding=1: x = [8.0, 8.0]
k=4 padding=1: x = [8.0, 8.0]
k=4 padding=2: x = [8.0, 8.0, 3.5]
k=2 padding=1: x = [8.0, 3.5]

kornia main 78cd06c35, torch 2.14.0 and 2.5.1 (same output), CPU float64, macOS arm64. conv_soft_argmax3d with kernel_size=(3, 3, 4) and padding=(1, 1, 2) gives the same 3.5 on the #5059 head.

Expected

x = 8 in every window that contains the hot pixel. On main, the k = 4 rows also carry #5017 (offsets scaled by 2/(k−1)). After #5059 only the windows whose centre straddles the padding are still wrong: the right-border window reports (8 + 0)/2 − 0.5 = 3.5. On the left border the same windows report 0.5 instead of 0.

Classification

Silent wrong result, limited to an even kernel_size with padding = k / 2 (k = 2 with the default padding = 1): the outermost windows are displaced by up to half the image width. The default (3, 3) / (3, 3, 3) window is unaffected, and so are odd windows, since avg_pool caps padding at k // 2.

Disposition

The batch-10 Convention block of conv_soft_argmax2d limits its padding sentence to an odd kernel_size. Fix direction: build the centre grid without zero padding (for example, the centre of output index i is i · stride − padding + (k − 1)/2 per axis), or pad the coordinate grid with the extrapolated coordinates instead of zeros. This also covers conv_soft_argmax3d.

Related

  • #5017 / #5059 (in-window offsets scaled by 2/(k−1)): a separate defect in the offset kernel. Its fix direction assumes the centre kernel is right, which holds everywhere except at these padded borders.
  • #5020 (map-wide max plus eps).

Posted on behalf of @ducha-aiki by Claude (Opus 5.5).