Skip to content

Commit d2f72db

Browse files
committed
correct affine behavior, cvcuda center on top left
1 parent 73db7f3 commit d2f72db

File tree

2 files changed

+13
-14
lines changed

2 files changed

+13
-14
lines changed

test/test_transforms_v2.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,11 +1575,8 @@ def test_functional_image_correctness(
15751575
)
15761576

15771577
if make_input is make_image_cvcuda:
1578-
actual = F.cvcuda_to_tensor(actual).to(device="cpu")
1579-
actual = actual.squeeze(0)
1580-
# drop the batch dimensions for image now
1581-
image = F.cvcuda_to_tensor(image)
1582-
image = image.squeeze(0)
1578+
actual = cvcuda_to_pil_compatible_tensor(actual)
1579+
image = cvcuda_to_pil_compatible_tensor(image)
15831580

15841581
expected = F.to_image(
15851582
F.affine(
@@ -1629,11 +1626,8 @@ def test_transform_image_correctness(self, center, interpolation, fill, seed, ma
16291626
actual = transform(image)
16301627

16311628
if make_input is make_image_cvcuda:
1632-
actual = F.cvcuda_to_tensor(actual).to(device="cpu")
1633-
actual = actual.squeeze(0)
1634-
# drop the batch dimensions for image now
1635-
image = F.cvcuda_to_tensor(image)
1636-
image = image.squeeze(0)
1629+
actual = cvcuda_to_pil_compatible_tensor(actual)
1630+
image = cvcuda_to_pil_compatible_tensor(image)
16371631

16381632
torch.manual_seed(seed)
16391633
expected = F.to_image(transform(F.to_pil_image(image)))

torchvision/transforms/v2/functional/_geometry.py

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1361,12 +1361,17 @@ def _affine_cvcuda(
13611361

13621362
height, width, num_channels = image.shape[1:]
13631363

1364-
center_f = [0.0, 0.0]
1365-
if center is not None:
1366-
center_f = [(c - s * 0.5) for c, s in zip(center, [width, height])]
1364+
# Determine the actual center point (cx, cy)
1365+
# torchvision uses image center by default, cvcuda transforms around upper-left (0,0)
1366+
# Unlike the tensor version which uses normalized coordinates centered at image center,
1367+
# CV-CUDA uses absolute pixel coordinates, so we pass actual center to _get_inverse_affine_matrix
1368+
if center is None:
1369+
cx, cy = width / 2.0, height / 2.0
1370+
else:
1371+
cx, cy = float(center[0]), float(center[1])
13671372

13681373
translate_f = [float(t) for t in translate]
1369-
matrix = _get_inverse_affine_matrix(center_f, angle, translate_f, scale, shear)
1374+
matrix = _get_inverse_affine_matrix([cx, cy], angle, translate_f, scale, shear)
13701375

13711376
interp = _cvcuda_interp.get(interpolation)
13721377
if interp is None:

0 commit comments

Comments
 (0)