v2 KeyPoints: flip and resize assume different pixel conventions
Describe the bug
Describe the bug
Hi — the v2 KeyPoints kernels look like they disagree with each other about where a pixel's coordinate sits, and I think one of them ends up half a pixel off after a resize.
horizontal_flip_keypoints (_geometry.py:81-85) does sub_(canvas_size[1] - 1).neg_(), i.e. W - 1 - x, which puts keypoints at integer pixel centers. resize_keypoints (same file, keypoints.mul(ratios)) scales from the origin, which assumes continuous coordinates. Those two can't both be right.
The bounding box kernels in the same file are self-consistent the other way: horizontal_flip_bounding_boxes at line 109 uses sub_(canvas_size[1]).neg_() — W - x, without the - 1 — and resize_bounding_boxes also multiplies by the ratio. So boxes agree with each other and keypoints don't. PR #8817 notes the keypoint kernels were adapted from the bounding box code, which may be how the flip and resize ended up in different frames.
To check which one matches the image, I rendered a Gaussian blob at a known position, passed the image and its keypoint through v2.Resize, and measured where the blob's center actually landed in the output pixels. Flips agree with the image to under 0.00001 px. Resize is off by up to 0.38 px, and the error is always 0.5 * (1 - scale) — exactly what origin-anchored scaling gives when the raster uses half-pixel centers.
If KeyPoints are integer-centered, the resize mapping would be (p + 0.5) * scale - 0.5. If they're meant to be continuous like boxes, then the flip is the odd one out. Upscaling, downscaling, anisotropic resizes and different image sizes all show the same pattern, so I think it's one issue rather than several.
The same mismatch in AlbumentationsX was confirmed and fixed by its maintainer (albumentations-team/AlbumentationsX#529, fixed in #531), in case that's a useful cross-reference.
Happy to open a PR or send more measurements.
Repro:
import torch
import torchvision
from torchvision import tv_tensors
from torchvision.transforms import v2
H, W = 65, 97
XC, YC, SIGMA = 20.25, 24.5, 3.0
y = torch.arange(H)[:, None] + 0.5
x = torch.arange(W)[None, :] + 0.5
image = torch.exp(-((x - XC) ** 2 + (y - YC) ** 2) / (2 * SIGMA**2))[None].double()
def centroid(channel):
h, w = channel.shape
yy = torch.arange(h, dtype=torch.float64) + 0.5
xx = torch.arange(w, dtype=torch.float64) + 0.5
mass = channel.double().sum()
return torch.tensor([(channel.double().sum(0) * xx).sum() / mass, (channel.double().sum(1) * yy).sum() / mass])
print("torchvision", torchvision.__version__)
for output_size in [(94, 155), (47, 65), (85, 77)]:
point = tv_tensors.KeyPoints([[XC - 0.5, YC - 0.5]], canvas_size=(H, W))
output_image, output_point = v2.Resize(output_size, antialias=True)(image, point)
image_xy = centroid(output_image[0])
annotation_xy = output_point[0].double() + 0.5
print(output_size, "image", image_xy.tolist(), "annotation", annotation_xy.tolist(), "difference", (annotation_xy - image_xy).tolist())Output:
torchvision 0.29.0
(94, 155) image [32.35810392490065, 35.429671540877784] annotation [32.05927848815918, 35.20769500732422] difference [-0.2988254367414669, -0.2219765335535655]
(47, 65) image [13.568886592079116, 17.716229477718894] annotation [13.734535217285156, 17.85384750366211] difference [0.16564862520604073, 0.13761802594321537]
(85, 77) image [16.075099906618345, 32.03859468158568] annotation [16.177834510803223, 31.884614944458008] difference [0.10273460418487801, -0.15397973712767055]The - 0.5 on input and + 0.5 on output convert between the library's integer-center coordinates and the continuous frame the centroid is measured in.
Versions
torchvision 0.29.0, torch 2.14.0, Python 3.13.5, numpy 2.5.3, macOS 26.3.1 arm64.
Versions
PyTorch version: 2.14.0
Is debug build: False
CUDA used to build PyTorch: None
ROCm SDK used to build PyTorch: N/A
HIP used to build PyTorch: N/A
OS: macOS 26.3.1 (arm64)
GCC version: Could not collect
Clang version: 21.0.0 (clang-2100.0.123.102)
CMake version: version 4.3.0
Libc version: N/A
Python version: 3.13.5 (main, Sep 4 2025, 12:51:39) [Clang 16.0.0 (clang-1600.0.26.6)] (64-bit runtime)
Python platform: macOS-26.3.1-arm64-arm-64bit-Mach-O
Is CUDA available: False
CUDA runtime version: No CUDA
CUDA_MODULE_LOADING set to: N/A
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA
Is XPU available: False
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: False
Caching allocator config: N/A
CPU:
Apple M3 Pro
Versions of relevant libraries:
[pip3] numpy==2.5.3
[pip3] torch==2.14.0
[pip3] torchvision==0.29.0
[conda] Could not collectSource: pytorch/vision