Skip to content

Commit 84d204b

Browse files
committed
cvcuda rotate verified correct visualizly and passing all tests
1 parent 06a0fe7 commit 84d204b

File tree

2 files changed

+12
-9
lines changed

2 files changed

+12
-9
lines changed

test/test_transforms_v2.py

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2152,7 +2152,8 @@ def test_functional_image_correctness(self, angle, center, interpolation, expand
21522152

21532153
actual = F.rotate(image, angle=angle, center=center, interpolation=interpolation, expand=expand, fill=fill)
21542154

2155-
if make_input == make_image_cvcuda:
2155+
if make_input is make_image_cvcuda:
2156+
actual = cvcuda_to_pil_compatible_tensor(actual)
21562157
image = cvcuda_to_pil_compatible_tensor(image)
21572158

21582159
expected = F.to_image(
@@ -2162,7 +2163,7 @@ def test_functional_image_correctness(self, angle, center, interpolation, expand
21622163
)
21632164

21642165
mae = (actual.float() - expected.float()).abs().mean()
2165-
if make_input == make_image_cvcuda:
2166+
if make_input is make_image_cvcuda:
21662167
# CV-CUDA nearest interpolation differs significantly from PIL, set much higher bound
21672168
assert mae < (122.5) if interpolation is transforms.InterpolationMode.NEAREST else 6, f"MAE: {mae}"
21682169
else:
@@ -2202,16 +2203,14 @@ def test_transform_image_correctness(self, center, interpolation, expand, fill,
22022203

22032204
torch.manual_seed(seed)
22042205

2205-
if make_input == make_image_cvcuda:
2206-
actual = F.cvcuda_to_tensor(actual).to(device="cpu")
2207-
image = F.cvcuda_to_tensor(image)
2208-
# drop the batch dimensions
2209-
image = image.squeeze(0)
2206+
if make_input is make_image_cvcuda:
2207+
actual = cvcuda_to_pil_compatible_tensor(actual)
2208+
image = cvcuda_to_pil_compatible_tensor(image)
22102209

22112210
expected = F.to_image(transform(F.to_pil_image(image)))
22122211

22132212
mae = (actual.float() - expected.float()).abs().mean()
2214-
if make_input == make_image_cvcuda:
2213+
if make_input is make_image_cvcuda:
22152214
# CV-CUDA nearest interpolation differs significantly from PIL, set much higher bound
22162215
assert mae < (122.5) if interpolation is transforms.InterpolationMode.NEAREST else 6, f"MAE: {mae}"
22172216
else:

torchvision/transforms/v2/functional/_geometry.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1584,8 +1584,11 @@ def _rotate_cvcuda(
15841584
# We need to calculate a shift to effectively rotate around the desired center
15851585
if center is None:
15861586
cx, cy = input_width / 2.0, input_height / 2.0
1587+
center_f = [0.0, 0.0]
15871588
else:
15881589
cx, cy = float(center[0]), float(center[1])
1590+
# Convert to image-center-relative coordinates (same as torchvision)
1591+
center_f = [cx - input_width * 0.5, cy - input_height * 0.5]
15891592

15901593
angle_rad = math.radians(angle)
15911594
cos_angle = math.cos(angle_rad)
@@ -1599,7 +1602,8 @@ def _rotate_cvcuda(
15991602
return cvcuda.rotate(inpt, angle_deg=angle, shift=(shift_x, shift_y), interpolation=interp)
16001603

16011604
# if we need to expand, use much of the same logic as torchvision, for output size/pad
1602-
matrix = _get_inverse_affine_matrix([0.0, 0.0], -angle, [0.0, 0.0], 1.0, [0.0, 0.0])
1605+
# Use center_f (image-center-relative coords) to match torchvision's output size calculation
1606+
matrix = _get_inverse_affine_matrix(center_f, -angle, [0.0, 0.0], 1.0, [0.0, 0.0])
16031607
output_width, output_height = _compute_affine_output_size(matrix, input_width, input_height)
16041608

16051609
pad_left = (output_width - input_width) // 2

0 commit comments

Comments
 (0)