53
53
from torchvision .transforms .v2 .functional ._utils import _get_kernel , _register_kernel_internal
54
54
55
55
56
- # While we are working on adjusting transform functions
57
- # for rotated and oriented bounding boxes formats,
58
- # we limit the perimeter of tests to formats
59
- # for which transform functions are already implemented.
60
- # In the future, this global variable will be replaced with `list(tv_tensors.BoundingBoxFormat)`
61
- # to support all available formats.
62
- SUPPORTED_BOX_FORMATS = [tv_tensors .BoundingBoxFormat [x ] for x in ["XYXY" , "XYWH" , "CXCYWH" ]]
63
- NEW_BOX_FORMATS = [tv_tensors .BoundingBoxFormat [x ] for x in ["XYWHR" , "CXCYWHR" , "XYXYXYXY" ]]
64
-
65
56
# turns all warnings into errors for this module
66
57
pytestmark = [pytest .mark .filterwarnings ("error" )]
67
58
@@ -635,7 +626,7 @@ def test_kernel_image(self, size, interpolation, use_max_size, antialias, dtype,
635
626
check_scripted_vs_eager = not isinstance (size , int ),
636
627
)
637
628
638
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
629
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
639
630
@pytest .mark .parametrize ("size" , OUTPUT_SIZES )
640
631
@pytest .mark .parametrize ("use_max_size" , [True , False ])
641
632
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
@@ -766,7 +757,7 @@ def _reference_resize_bounding_boxes(self, bounding_boxes, *, size, max_size=Non
766
757
new_canvas_size = (new_height , new_width ),
767
758
)
768
759
769
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
760
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
770
761
@pytest .mark .parametrize ("size" , OUTPUT_SIZES )
771
762
@pytest .mark .parametrize ("use_max_size" , [True , False ])
772
763
@pytest .mark .parametrize ("fn" , [F .resize , transform_cls_to_functional (transforms .Resize )])
@@ -1012,7 +1003,7 @@ class TestHorizontalFlip:
1012
1003
def test_kernel_image (self , dtype , device ):
1013
1004
check_kernel (F .horizontal_flip_image , make_image (dtype = dtype , device = device ))
1014
1005
1015
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1006
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1016
1007
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
1017
1008
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
1018
1009
def test_kernel_bounding_boxes (self , format , dtype , device ):
@@ -1081,7 +1072,7 @@ def _reference_horizontal_flip_bounding_boxes(self, bounding_boxes):
1081
1072
1082
1073
return reference_affine_bounding_boxes_helper (bounding_boxes , affine_matrix = affine_matrix )
1083
1074
1084
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1075
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1085
1076
@pytest .mark .parametrize (
1086
1077
"fn" , [F .horizontal_flip , transform_cls_to_functional (transforms .RandomHorizontalFlip , p = 1 )]
1087
1078
)
@@ -1178,7 +1169,7 @@ def test_kernel_image(self, param, value, dtype, device):
1178
1169
shear = _EXHAUSTIVE_TYPE_AFFINE_KWARGS ["shear" ],
1179
1170
center = _EXHAUSTIVE_TYPE_AFFINE_KWARGS ["center" ],
1180
1171
)
1181
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1172
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1182
1173
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
1183
1174
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
1184
1175
def test_kernel_bounding_boxes (self , param , value , format , dtype , device ):
@@ -1327,7 +1318,7 @@ def _reference_affine_bounding_boxes(self, bounding_boxes, *, angle, translate,
1327
1318
),
1328
1319
)
1329
1320
1330
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1321
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1331
1322
@pytest .mark .parametrize ("angle" , _CORRECTNESS_AFFINE_KWARGS ["angle" ])
1332
1323
@pytest .mark .parametrize ("translate" , _CORRECTNESS_AFFINE_KWARGS ["translate" ])
1333
1324
@pytest .mark .parametrize ("scale" , _CORRECTNESS_AFFINE_KWARGS ["scale" ])
@@ -1355,7 +1346,7 @@ def test_functional_bounding_boxes_correctness(self, format, angle, translate, s
1355
1346
1356
1347
torch .testing .assert_close (actual , expected )
1357
1348
1358
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1349
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1359
1350
@pytest .mark .parametrize ("center" , _CORRECTNESS_AFFINE_KWARGS ["center" ])
1360
1351
@pytest .mark .parametrize ("seed" , list (range (5 )))
1361
1352
def test_transform_bounding_boxes_correctness (self , format , center , seed ):
@@ -1462,7 +1453,7 @@ class TestVerticalFlip:
1462
1453
def test_kernel_image (self , dtype , device ):
1463
1454
check_kernel (F .vertical_flip_image , make_image (dtype = dtype , device = device ))
1464
1455
1465
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1456
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1466
1457
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
1467
1458
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
1468
1459
def test_kernel_bounding_boxes (self , format , dtype , device ):
@@ -1529,7 +1520,7 @@ def _reference_vertical_flip_bounding_boxes(self, bounding_boxes):
1529
1520
1530
1521
return reference_affine_bounding_boxes_helper (bounding_boxes , affine_matrix = affine_matrix )
1531
1522
1532
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1523
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1533
1524
@pytest .mark .parametrize ("fn" , [F .vertical_flip , transform_cls_to_functional (transforms .RandomVerticalFlip , p = 1 )])
1534
1525
def test_bounding_boxes_correctness (self , format , fn ):
1535
1526
bounding_boxes = make_bounding_boxes (format = format )
@@ -1598,7 +1589,7 @@ def test_kernel_image(self, param, value, dtype, device):
1598
1589
expand = [False , True ],
1599
1590
center = _EXHAUSTIVE_TYPE_AFFINE_KWARGS ["center" ],
1600
1591
)
1601
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1592
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1602
1593
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .uint8 ])
1603
1594
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
1604
1595
def test_kernel_bounding_boxes (self , param , value , format , dtype , device ):
@@ -1769,7 +1760,7 @@ def _reference_rotate_bounding_boxes(self, bounding_boxes, *, angle, expand, cen
1769
1760
bounding_boxes
1770
1761
)
1771
1762
1772
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1763
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1773
1764
@pytest .mark .parametrize ("angle" , _CORRECTNESS_AFFINE_KWARGS ["angle" ])
1774
1765
@pytest .mark .parametrize ("expand" , [False , True ])
1775
1766
@pytest .mark .parametrize ("center" , _CORRECTNESS_AFFINE_KWARGS ["center" ])
@@ -1782,7 +1773,7 @@ def test_functional_bounding_boxes_correctness(self, format, angle, expand, cent
1782
1773
torch .testing .assert_close (actual , expected )
1783
1774
torch .testing .assert_close (F .get_size (actual ), F .get_size (expected ), atol = 2 if expand else 0 , rtol = 0 )
1784
1775
1785
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1776
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
1786
1777
@pytest .mark .parametrize ("expand" , [False , True ])
1787
1778
@pytest .mark .parametrize ("center" , _CORRECTNESS_AFFINE_KWARGS ["center" ])
1788
1779
@pytest .mark .parametrize ("seed" , list (range (5 )))
@@ -2703,7 +2694,7 @@ def test_kernel_image(self, param, value, dtype, device):
2703
2694
check_cuda_vs_cpu = dtype is not torch .float16 ,
2704
2695
)
2705
2696
2706
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
2697
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
2707
2698
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
2708
2699
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
2709
2700
def test_kernel_bounding_boxes (self , format , dtype , device ):
@@ -2830,7 +2821,7 @@ def test_kernel_image(self, kwargs, dtype, device):
2830
2821
check_kernel (F .crop_image , make_image (self .INPUT_SIZE , dtype = dtype , device = device ), ** kwargs )
2831
2822
2832
2823
@pytest .mark .parametrize ("kwargs" , CORRECTNESS_CROP_KWARGS )
2833
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
2824
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
2834
2825
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
2835
2826
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
2836
2827
def test_kernel_bounding_box (self , kwargs , format , dtype , device ):
@@ -2980,7 +2971,7 @@ def _reference_crop_bounding_boxes(self, bounding_boxes, *, top, left, height, w
2980
2971
)
2981
2972
2982
2973
@pytest .mark .parametrize ("kwargs" , CORRECTNESS_CROP_KWARGS )
2983
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
2974
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
2984
2975
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
2985
2976
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
2986
2977
def test_functional_bounding_box_correctness (self , kwargs , format , dtype , device ):
@@ -2993,7 +2984,7 @@ def test_functional_bounding_box_correctness(self, kwargs, format, dtype, device
2993
2984
assert_equal (F .get_size (actual ), F .get_size (expected ))
2994
2985
2995
2986
@pytest .mark .parametrize ("output_size" , [(17 , 11 ), (11 , 17 ), (11 , 11 )])
2996
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
2987
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
2997
2988
@pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
2998
2989
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
2999
2990
@pytest .mark .parametrize ("seed" , list (range (5 )))
@@ -3516,8 +3507,7 @@ def test_aug_mix_severity_error(self, severity):
3516
3507
3517
3508
3518
3509
class TestConvertBoundingBoxFormat :
3519
- old_new_formats = list (itertools .permutations (SUPPORTED_BOX_FORMATS , 2 ))
3520
- old_new_formats += list (itertools .permutations (NEW_BOX_FORMATS , 2 ))
3510
+ old_new_formats = list (itertools .permutations (iter (tv_tensors .BoundingBoxFormat ), 2 ))
3521
3511
3522
3512
@pytest .mark .parametrize (("old_format" , "new_format" ), old_new_formats )
3523
3513
def test_kernel (self , old_format , new_format ):
@@ -3528,7 +3518,7 @@ def test_kernel(self, old_format, new_format):
3528
3518
old_format = old_format ,
3529
3519
)
3530
3520
3531
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3521
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
3532
3522
@pytest .mark .parametrize ("inplace" , [False , True ])
3533
3523
def test_kernel_noop (self , format , inplace ):
3534
3524
input = make_bounding_boxes (format = format ).as_subclass (torch .Tensor )
@@ -3552,13 +3542,9 @@ def test_kernel_inplace(self, old_format, new_format):
3552
3542
output_inplace = F .convert_bounding_box_format (
3553
3543
input , old_format = old_format , new_format = new_format , inplace = True
3554
3544
)
3555
- if old_format != tv_tensors .BoundingBoxFormat .XYXYXYXY and new_format != tv_tensors .BoundingBoxFormat .XYXYXYXY :
3556
- # NOTE: BoundingBox format conversion from and to XYXYXYXY format
3557
- # cannot modify the input tensor inplace as it requires a dimension
3558
- # change.
3559
- assert output_inplace .data_ptr () == input .data_ptr ()
3560
- assert output_inplace ._version > input_version
3561
- assert output_inplace is input
3545
+ assert output_inplace .data_ptr () == input .data_ptr ()
3546
+ assert output_inplace ._version > input_version
3547
+ assert output_inplace is input
3562
3548
3563
3549
assert_equal (output_inplace , output_out_of_place )
3564
3550
@@ -3577,7 +3563,7 @@ def test_transform(self, old_format, new_format, format_type):
3577
3563
@pytest .mark .parametrize (("old_format" , "new_format" ), old_new_formats )
3578
3564
def test_strings (self , old_format , new_format ):
3579
3565
# Non-regression test for https://github.com/pytorch/vision/issues/8258
3580
- input = make_bounding_boxes ( format = old_format , canvas_size = (50 , 50 ))
3566
+ input = tv_tensors . BoundingBoxes ( torch . tensor ([[ 10 , 10 , 20 , 20 ]]), format = old_format , canvas_size = (50 , 50 ))
3581
3567
expected = self ._reference_convert_bounding_box_format (input , new_format )
3582
3568
3583
3569
old_format = old_format .name
@@ -3742,7 +3728,7 @@ def _reference_resized_crop_bounding_boxes(self, bounding_boxes, *, top, left, h
3742
3728
new_canvas_size = size ,
3743
3729
)
3744
3730
3745
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3731
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
3746
3732
def test_functional_bounding_boxes_correctness (self , format ):
3747
3733
bounding_boxes = make_bounding_boxes (self .INPUT_SIZE , format = format )
3748
3734
@@ -3816,7 +3802,7 @@ def test_kernel_image(self, param, value, dtype, device):
3816
3802
),
3817
3803
)
3818
3804
3819
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3805
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
3820
3806
def test_kernel_bounding_boxes (self , format ):
3821
3807
bounding_boxes = make_bounding_boxes (format = format )
3822
3808
check_kernel (
@@ -3935,7 +3921,7 @@ def _reference_pad_bounding_boxes(self, bounding_boxes, *, padding):
3935
3921
)
3936
3922
3937
3923
@pytest .mark .parametrize ("padding" , CORRECTNESS_PADDINGS )
3938
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3924
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
3939
3925
@pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
3940
3926
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
3941
3927
@pytest .mark .parametrize ("fn" , [F .pad , transform_cls_to_functional (transforms .Pad )])
@@ -3964,7 +3950,7 @@ def test_kernel_image(self, output_size, dtype, device):
3964
3950
)
3965
3951
3966
3952
@pytest .mark .parametrize ("output_size" , OUTPUT_SIZES )
3967
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3953
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
3968
3954
def test_kernel_bounding_boxes (self , output_size , format ):
3969
3955
bounding_boxes = make_bounding_boxes (self .INPUT_SIZE , format = format )
3970
3956
check_kernel (
@@ -4043,7 +4029,7 @@ def _reference_center_crop_bounding_boxes(self, bounding_boxes, output_size):
4043
4029
)
4044
4030
4045
4031
@pytest .mark .parametrize ("output_size" , OUTPUT_SIZES )
4046
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4032
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
4047
4033
@pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
4048
4034
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
4049
4035
@pytest .mark .parametrize ("fn" , [F .center_crop , transform_cls_to_functional (transforms .CenterCrop )])
@@ -4110,7 +4096,7 @@ def test_kernel_image_error(self):
4110
4096
coefficients = COEFFICIENTS ,
4111
4097
start_end_points = START_END_POINTS ,
4112
4098
)
4113
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4099
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
4114
4100
def test_kernel_bounding_boxes (self , param , value , format ):
4115
4101
if param == "start_end_points" :
4116
4102
kwargs = dict (zip (["startpoints" , "endpoints" ], value ))
@@ -4286,7 +4272,7 @@ def perspective_bounding_boxes(bounding_boxes):
4286
4272
)
4287
4273
4288
4274
@pytest .mark .parametrize (("startpoints" , "endpoints" ), START_END_POINTS )
4289
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4275
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
4290
4276
@pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
4291
4277
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
4292
4278
def test_correctness_perspective_bounding_boxes (self , startpoints , endpoints , format , dtype , device ):
@@ -4493,7 +4479,7 @@ def test_correctness_image(self, mean, std, dtype, fn):
4493
4479
4494
4480
4495
4481
class TestClampBoundingBoxes :
4496
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4482
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
4497
4483
@pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
4498
4484
@pytest .mark .parametrize ("device" , cpu_and_cuda ())
4499
4485
def test_kernel (self , format , dtype , device ):
@@ -4505,7 +4491,7 @@ def test_kernel(self, format, dtype, device):
4505
4491
canvas_size = bounding_boxes .canvas_size ,
4506
4492
)
4507
4493
4508
- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4494
+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
4509
4495
def test_functional (self , format ):
4510
4496
check_functional (F .clamp_bounding_boxes , make_bounding_boxes (format = format ))
4511
4497
0 commit comments