Skip to content

Commit 3b4100c

Browse files
Fix vertical flip orientation
1 parent 3996daa commit 3b4100c

File tree

2 files changed

+23
-11
lines changed

2 files changed

+23
-11
lines changed

test/test_transforms_v2.py

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -588,14 +588,14 @@ def affine_rotated_bounding_boxes(bounding_boxes):
588588
transformed_points = np.matmul(points, affine_matrix.astype(points.dtype).T)
589589
output = torch.Tensor(
590590
[
591+
float(transformed_points[1, 0]),
592+
float(transformed_points[1, 1]),
591593
float(transformed_points[0, 0]),
592594
float(transformed_points[0, 1]),
593595
float(transformed_points[3, 0]),
594596
float(transformed_points[3, 1]),
595597
float(transformed_points[2, 0]),
596598
float(transformed_points[2, 1]),
597-
float(transformed_points[1, 0]),
598-
float(transformed_points[1, 1]),
599599
]
600600
)
601601

@@ -618,13 +618,20 @@ def affine_rotated_bounding_boxes(bounding_boxes):
618618
return output.to(dtype=dtype, device=device)
619619

620620
return tv_tensors.BoundingBoxes(
621-
torch.cat([affine_rotated_bounding_boxes(b) for b in bounding_boxes.reshape(-1, 5 if format != tv_tensors.BoundingBoxFormat.XYXYXYXY else 8).unbind()], dim=0).reshape(
622-
bounding_boxes.shape
623-
),
621+
torch.cat(
622+
[
623+
affine_rotated_bounding_boxes(b)
624+
for b in bounding_boxes.reshape(
625+
-1, 5 if format != tv_tensors.BoundingBoxFormat.XYXYXYXY else 8
626+
).unbind()
627+
],
628+
dim=0,
629+
).reshape(bounding_boxes.shape),
624630
format=format,
625631
canvas_size=canvas_size,
626632
)
627633

634+
628635
class TestResize:
629636
INPUT_SIZE = (17, 11)
630637
OUTPUT_SIZES = [17, [17], (17,), None, [12, 13], (12, 13)]

torchvision/transforms/v2/functional/_geometry.py

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -163,15 +163,20 @@ def vertical_flip_bounding_boxes(
163163
bounding_boxes[:, 1].sub_(canvas_size[0]).neg_()
164164
elif format == tv_tensors.BoundingBoxFormat.XYXYXYXY:
165165
bounding_boxes[:, 1::2].sub_(canvas_size[0]).neg_()
166-
bounding_boxes = bounding_boxes[:, [0, 1, 6, 7, 4, 5, 2, 3]]
166+
bounding_boxes = bounding_boxes[:, [2, 3, 0, 1, 6, 7, 4, 5]]
167167
elif format == tv_tensors.BoundingBoxFormat.XYWHR:
168-
bounding_boxes[:, 1].sub_(canvas_size[0]).neg_()
169-
bounding_boxes = bounding_boxes[:, [0, 1, 3, 2, 4]]
170-
bounding_boxes[:, -1].sub_(90).neg_()
168+
dtype = bounding_boxes.dtype
169+
if not torch.is_floating_point(bounding_boxes):
170+
# Casting to float to support cos and sin computations.
171+
bounding_boxes = bounding_boxes.to(torch.float64)
172+
angle_rad = bounding_boxes[:, 4].mul(torch.pi).div(180)
173+
bounding_boxes[:, 1].sub_(bounding_boxes[:, 2].mul(angle_rad.sin())).sub_(canvas_size[0]).neg_()
174+
bounding_boxes[:, 0].add_(bounding_boxes[:, 2].mul(angle_rad.cos()))
175+
bounding_boxes[:, 4].neg_().add_(180)
176+
bounding_boxes = bounding_boxes.to(dtype)
171177
else: # format == tv_tensors.BoundingBoxFormat.CXCYWHR:
172178
bounding_boxes[:, 1].sub_(canvas_size[0]).neg_()
173-
bounding_boxes = bounding_boxes[:, [0, 1, 3, 2, 4]]
174-
bounding_boxes[:, -1].sub_(90).neg_()
179+
bounding_boxes[:, 4].neg_().add_(180)
175180

176181
return bounding_boxes.reshape(shape)
177182

0 commit comments

Comments
 (0)