Keypoint resize scales from the origin while image resize uses half-pixel centers
Hi — I think the keypoint and image resize paths assume different pixel conventions, and keypoints end up about half a pixel off after a resize.
RandomHorizontalFlip (_torchvision.py:527-551) computes W - 1 - x, which implies keypoints sit at integer pixel centers. Resize (:394-432) multiplies keypoints by the output/input ratio, scaling from the origin. Those two can't both be right.
This matters here because the image side has an explicit convention: the merged export-parity change describes the canonical image resize as bilinear with half-pixel centres (PR #1269), and the 1.9.1 release repeats it (PR #1275). Origin-anchored keypoint scaling doesn't match that raster.
To check which path tracks the image, I rendered a Gaussian blob at a known position, passed the image and its keypoint through Resize, and measured where the blob's center actually landed in the output pixels. Flip agrees with the image feature to at most 0.001669 px across six tested sizes. Resize differs by up to 0.379520 px, and the error is always 0.5 * (1 - scale) — exactly what origin-anchored scaling gives against a half-pixel-centered raster.
If keypoints are integer-centered and meant to follow that raster, the mapping would be (p + 0.5) * scale - 0.5. If they're meant to be continuous, then the flip is the odd one out. Upscaling, downscaling, anisotropic sizes and different image shapes all show the same pattern, so I think it's one point-kernel rule rather than several separate bugs.
Related: the same mismatch in AlbumentationsX was confirmed and fixed by its maintainer (albumentations-team/AlbumentationsX#529, fixed in albumentations-team/AlbumentationsX#531), and I filed the equivalent for torchvision KeyPoints at https://github.com/pytorch/vision/issues/9675.
Environment: RF-DETR 1.10.1, torch 2.14.0, torchvision 0.29.0, Python 3.13.5, numpy 2.5.3, macOS 26.3.1 arm64.
Repro:
import numpy as np
import torch
from importlib.metadata import version
from rfdetr.datasets._torchvision import Resize
H, W = 65, 97
XC, YC, SIGMA = 20.25, 24.5, 3.0
y = np.arange(H)[:, None] + 0.5
x = np.arange(W)[None, :] + 0.5
blob = np.exp(-((x - XC) ** 2 + (y - YC) ** 2) / (2 * SIGMA**2))
image = torch.from_numpy(blob)[None].double()
def centroid(channel):
h, w = channel.shape
mass = channel.astype(np.float64).sum()
return np.array([(channel.sum(0) * (np.arange(w) + 0.5)).sum() / mass, (channel.sum(1) * (np.arange(h) + 0.5)).sum() / mass])
print("rfdetr", version("rfdetr"))
for output_size in [(94, 155), (47, 65), (85, 77)]:
target = {"keypoints": torch.tensor([[[XC - 0.5, YC - 0.5, 1.0]]], dtype=torch.float64)}
output_image, output_target = Resize(output_size)(image, target)
image_xy = centroid(output_image[0].numpy())
annotation_xy = output_target["keypoints"][0, 0, :2].numpy() + 0.5
print(output_size, "image", image_xy.tolist(), "annotation", annotation_xy.tolist(), "difference", (annotation_xy - image_xy).tolist())Output:
rfdetr 1.10.1
(94, 155) image [32.35810391886376, 35.4296715414695] annotation [32.05928039550781, 35.20769500732422] difference [-0.2988235233559493, -0.22197653414528418]
(47, 65) image [13.568886589324375, 17.716229478830137] annotation [13.734535217285156, 17.85384750366211] difference [0.16564862796078117, 0.13761802483197272]
(85, 77) image [16.075099902918865, 32.03859468127034] annotation [16.177833557128906, 31.884614944458008] difference [0.10273365421004144, -0.15397973681233168]The - 0.5 on input and + 0.5 on output convert between integer-center coordinates and the continuous frame the centroid is measured in.
Happy to open a PR or send more measurements.
Source: roboflow/rf-detr