Skip to content

Commit 982d21d

Browse files
committed
correct affine behavior, cvcuda center on top left
1 parent 23816f4 commit 982d21d

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
@@ -1601,11 +1601,8 @@ def test_functional_image_correctness(
16011601
)
16021602

16031603
if make_input is make_image_cvcuda:
1604-
actual = F.cvcuda_to_tensor(actual).to(device="cpu")
1605-
actual = actual.squeeze(0)
1606-
# drop the batch dimensions for image now
1607-
image = F.cvcuda_to_tensor(image)
1608-
image = image.squeeze(0)
1604+
actual = cvcuda_to_pil_compatible_tensor(actual)
1605+
image = cvcuda_to_pil_compatible_tensor(image)
16091606

16101607
expected = F.to_image(
16111608
F.affine(
@@ -1655,11 +1652,8 @@ def test_transform_image_correctness(self, center, interpolation, fill, seed, ma
16551652
actual = transform(image)
16561653

16571654
if make_input is make_image_cvcuda:
1658-
actual = F.cvcuda_to_tensor(actual).to(device="cpu")
1659-
actual = actual.squeeze(0)
1660-
# drop the batch dimensions for image now
1661-
image = F.cvcuda_to_tensor(image)
1662-
image = image.squeeze(0)
1655+
actual = cvcuda_to_pil_compatible_tensor(actual)
1656+
image = cvcuda_to_pil_compatible_tensor(image)
16631657

16641658
torch.manual_seed(seed)
16651659
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
@@ -1373,12 +1373,17 @@ def _affine_cvcuda(
13731373

13741374
height, width, num_channels = image.shape[1:]
13751375

1376-
center_f = [0.0, 0.0]
1377-
if center is not None:
1378-
center_f = [(c - s * 0.5) for c, s in zip(center, [width, height])]
1376+
# Determine the actual center point (cx, cy)
1377+
# torchvision uses image center by default, cvcuda transforms around upper-left (0,0)
1378+
# Unlike the tensor version which uses normalized coordinates centered at image center,
1379+
# CV-CUDA uses absolute pixel coordinates, so we pass actual center to _get_inverse_affine_matrix
1380+
if center is None:
1381+
cx, cy = width / 2.0, height / 2.0
1382+
else:
1383+
cx, cy = float(center[0]), float(center[1])
13791384

13801385
translate_f = [float(t) for t in translate]
1381-
matrix = _get_inverse_affine_matrix(center_f, angle, translate_f, scale, shear)
1386+
matrix = _get_inverse_affine_matrix([cx, cy], angle, translate_f, scale, shear)
13821387

13831388
interp = _cvcuda_interp.get(interpolation)
13841389
if interp is None:

0 commit comments

Comments
 (0)