5353from torchvision .transforms .v2 .functional ._utils import _get_kernel , _register_kernel_internal
5454
5555
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-
6556# turns all warnings into errors for this module
6657pytestmark = [pytest .mark .filterwarnings ("error" )]
6758
@@ -635,7 +626,7 @@ def test_kernel_image(self, size, interpolation, use_max_size, antialias, dtype,
635626 check_scripted_vs_eager = not isinstance (size , int ),
636627 )
637628
638- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
629+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
639630 @pytest .mark .parametrize ("size" , OUTPUT_SIZES )
640631 @pytest .mark .parametrize ("use_max_size" , [True , False ])
641632 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
@@ -766,7 +757,7 @@ def _reference_resize_bounding_boxes(self, bounding_boxes, *, size, max_size=Non
766757 new_canvas_size = (new_height , new_width ),
767758 )
768759
769- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
760+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
770761 @pytest .mark .parametrize ("size" , OUTPUT_SIZES )
771762 @pytest .mark .parametrize ("use_max_size" , [True , False ])
772763 @pytest .mark .parametrize ("fn" , [F .resize , transform_cls_to_functional (transforms .Resize )])
@@ -1012,7 +1003,7 @@ class TestHorizontalFlip:
10121003 def test_kernel_image (self , dtype , device ):
10131004 check_kernel (F .horizontal_flip_image , make_image (dtype = dtype , device = device ))
10141005
1015- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1006+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
10161007 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
10171008 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
10181009 def test_kernel_bounding_boxes (self , format , dtype , device ):
@@ -1081,7 +1072,7 @@ def _reference_horizontal_flip_bounding_boxes(self, bounding_boxes):
10811072
10821073 return reference_affine_bounding_boxes_helper (bounding_boxes , affine_matrix = affine_matrix )
10831074
1084- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1075+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
10851076 @pytest .mark .parametrize (
10861077 "fn" , [F .horizontal_flip , transform_cls_to_functional (transforms .RandomHorizontalFlip , p = 1 )]
10871078 )
@@ -1178,7 +1169,7 @@ def test_kernel_image(self, param, value, dtype, device):
11781169 shear = _EXHAUSTIVE_TYPE_AFFINE_KWARGS ["shear" ],
11791170 center = _EXHAUSTIVE_TYPE_AFFINE_KWARGS ["center" ],
11801171 )
1181- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1172+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
11821173 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
11831174 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
11841175 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,
13271318 ),
13281319 )
13291320
1330- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1321+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
13311322 @pytest .mark .parametrize ("angle" , _CORRECTNESS_AFFINE_KWARGS ["angle" ])
13321323 @pytest .mark .parametrize ("translate" , _CORRECTNESS_AFFINE_KWARGS ["translate" ])
13331324 @pytest .mark .parametrize ("scale" , _CORRECTNESS_AFFINE_KWARGS ["scale" ])
@@ -1355,7 +1346,7 @@ def test_functional_bounding_boxes_correctness(self, format, angle, translate, s
13551346
13561347 torch .testing .assert_close (actual , expected )
13571348
1358- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1349+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
13591350 @pytest .mark .parametrize ("center" , _CORRECTNESS_AFFINE_KWARGS ["center" ])
13601351 @pytest .mark .parametrize ("seed" , list (range (5 )))
13611352 def test_transform_bounding_boxes_correctness (self , format , center , seed ):
@@ -1462,7 +1453,7 @@ class TestVerticalFlip:
14621453 def test_kernel_image (self , dtype , device ):
14631454 check_kernel (F .vertical_flip_image , make_image (dtype = dtype , device = device ))
14641455
1465- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1456+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
14661457 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
14671458 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
14681459 def test_kernel_bounding_boxes (self , format , dtype , device ):
@@ -1529,7 +1520,7 @@ def _reference_vertical_flip_bounding_boxes(self, bounding_boxes):
15291520
15301521 return reference_affine_bounding_boxes_helper (bounding_boxes , affine_matrix = affine_matrix )
15311522
1532- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1523+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
15331524 @pytest .mark .parametrize ("fn" , [F .vertical_flip , transform_cls_to_functional (transforms .RandomVerticalFlip , p = 1 )])
15341525 def test_bounding_boxes_correctness (self , format , fn ):
15351526 bounding_boxes = make_bounding_boxes (format = format )
@@ -1598,7 +1589,7 @@ def test_kernel_image(self, param, value, dtype, device):
15981589 expand = [False , True ],
15991590 center = _EXHAUSTIVE_TYPE_AFFINE_KWARGS ["center" ],
16001591 )
1601- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1592+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
16021593 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .uint8 ])
16031594 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
16041595 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
17691760 bounding_boxes
17701761 )
17711762
1772- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1763+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
17731764 @pytest .mark .parametrize ("angle" , _CORRECTNESS_AFFINE_KWARGS ["angle" ])
17741765 @pytest .mark .parametrize ("expand" , [False , True ])
17751766 @pytest .mark .parametrize ("center" , _CORRECTNESS_AFFINE_KWARGS ["center" ])
@@ -1782,7 +1773,7 @@ def test_functional_bounding_boxes_correctness(self, format, angle, expand, cent
17821773 torch .testing .assert_close (actual , expected )
17831774 torch .testing .assert_close (F .get_size (actual ), F .get_size (expected ), atol = 2 if expand else 0 , rtol = 0 )
17841775
1785- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
1776+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
17861777 @pytest .mark .parametrize ("expand" , [False , True ])
17871778 @pytest .mark .parametrize ("center" , _CORRECTNESS_AFFINE_KWARGS ["center" ])
17881779 @pytest .mark .parametrize ("seed" , list (range (5 )))
@@ -2703,7 +2694,7 @@ def test_kernel_image(self, param, value, dtype, device):
27032694 check_cuda_vs_cpu = dtype is not torch .float16 ,
27042695 )
27052696
2706- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
2697+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
27072698 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
27082699 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
27092700 def test_kernel_bounding_boxes (self , format , dtype , device ):
@@ -2830,7 +2821,7 @@ def test_kernel_image(self, kwargs, dtype, device):
28302821 check_kernel (F .crop_image , make_image (self .INPUT_SIZE , dtype = dtype , device = device ), ** kwargs )
28312822
28322823 @pytest .mark .parametrize ("kwargs" , CORRECTNESS_CROP_KWARGS )
2833- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
2824+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
28342825 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
28352826 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
28362827 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
29802971 )
29812972
29822973 @pytest .mark .parametrize ("kwargs" , CORRECTNESS_CROP_KWARGS )
2983- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
2974+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
29842975 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
29852976 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
29862977 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
29932984 assert_equal (F .get_size (actual ), F .get_size (expected ))
29942985
29952986 @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 ) )
29972988 @pytest .mark .parametrize ("dtype" , [torch .float32 , torch .int64 ])
29982989 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
29992990 @pytest .mark .parametrize ("seed" , list (range (5 )))
@@ -3516,8 +3507,7 @@ def test_aug_mix_severity_error(self, severity):
35163507
35173508
35183509class 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 ))
35213511
35223512 @pytest .mark .parametrize (("old_format" , "new_format" ), old_new_formats )
35233513 def test_kernel (self , old_format , new_format ):
@@ -3528,7 +3518,7 @@ def test_kernel(self, old_format, new_format):
35283518 old_format = old_format ,
35293519 )
35303520
3531- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3521+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
35323522 @pytest .mark .parametrize ("inplace" , [False , True ])
35333523 def test_kernel_noop (self , format , inplace ):
35343524 input = make_bounding_boxes (format = format ).as_subclass (torch .Tensor )
@@ -3552,13 +3542,9 @@ def test_kernel_inplace(self, old_format, new_format):
35523542 output_inplace = F .convert_bounding_box_format (
35533543 input , old_format = old_format , new_format = new_format , inplace = True
35543544 )
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
35623548
35633549 assert_equal (output_inplace , output_out_of_place )
35643550
@@ -3577,7 +3563,7 @@ def test_transform(self, old_format, new_format, format_type):
35773563 @pytest .mark .parametrize (("old_format" , "new_format" ), old_new_formats )
35783564 def test_strings (self , old_format , new_format ):
35793565 # 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 ))
35813567 expected = self ._reference_convert_bounding_box_format (input , new_format )
35823568
35833569 old_format = old_format .name
@@ -3742,7 +3728,7 @@ def _reference_resized_crop_bounding_boxes(self, bounding_boxes, *, top, left, h
37423728 new_canvas_size = size ,
37433729 )
37443730
3745- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3731+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
37463732 def test_functional_bounding_boxes_correctness (self , format ):
37473733 bounding_boxes = make_bounding_boxes (self .INPUT_SIZE , format = format )
37483734
@@ -3816,7 +3802,7 @@ def test_kernel_image(self, param, value, dtype, device):
38163802 ),
38173803 )
38183804
3819- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3805+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
38203806 def test_kernel_bounding_boxes (self , format ):
38213807 bounding_boxes = make_bounding_boxes (format = format )
38223808 check_kernel (
@@ -3935,7 +3921,7 @@ def _reference_pad_bounding_boxes(self, bounding_boxes, *, padding):
39353921 )
39363922
39373923 @pytest .mark .parametrize ("padding" , CORRECTNESS_PADDINGS )
3938- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3924+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
39393925 @pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
39403926 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
39413927 @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):
39643950 )
39653951
39663952 @pytest .mark .parametrize ("output_size" , OUTPUT_SIZES )
3967- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
3953+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
39683954 def test_kernel_bounding_boxes (self , output_size , format ):
39693955 bounding_boxes = make_bounding_boxes (self .INPUT_SIZE , format = format )
39703956 check_kernel (
@@ -4043,7 +4029,7 @@ def _reference_center_crop_bounding_boxes(self, bounding_boxes, output_size):
40434029 )
40444030
40454031 @pytest .mark .parametrize ("output_size" , OUTPUT_SIZES )
4046- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4032+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
40474033 @pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
40484034 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
40494035 @pytest .mark .parametrize ("fn" , [F .center_crop , transform_cls_to_functional (transforms .CenterCrop )])
@@ -4110,7 +4096,7 @@ def test_kernel_image_error(self):
41104096 coefficients = COEFFICIENTS ,
41114097 start_end_points = START_END_POINTS ,
41124098 )
4113- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4099+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
41144100 def test_kernel_bounding_boxes (self , param , value , format ):
41154101 if param == "start_end_points" :
41164102 kwargs = dict (zip (["startpoints" , "endpoints" ], value ))
@@ -4286,7 +4272,7 @@ def perspective_bounding_boxes(bounding_boxes):
42864272 )
42874273
42884274 @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 ) )
42904276 @pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
42914277 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
42924278 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):
44934479
44944480
44954481class TestClampBoundingBoxes :
4496- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4482+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
44974483 @pytest .mark .parametrize ("dtype" , [torch .int64 , torch .float32 ])
44984484 @pytest .mark .parametrize ("device" , cpu_and_cuda ())
44994485 def test_kernel (self , format , dtype , device ):
@@ -4505,7 +4491,7 @@ def test_kernel(self, format, dtype, device):
45054491 canvas_size = bounding_boxes .canvas_size ,
45064492 )
45074493
4508- @pytest .mark .parametrize ("format" , SUPPORTED_BOX_FORMATS )
4494+ @pytest .mark .parametrize ("format" , list ( tv_tensors . BoundingBoxFormat ) )
45094495 def test_functional (self , format ):
45104496 check_functional (F .clamp_bounding_boxes , make_bounding_boxes (format = format ))
45114497
0 commit comments