@@ -36,14 +36,18 @@ def horizontal_flip_bounding_box(
3636) -> torch .Tensor :
3737 shape = bounding_box .shape
3838
39- bounding_box = convert_format_bounding_box (
40- bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY
39+ # TODO: Investigate if it makes sense from a performance perspective to have an implementation for every
40+ # BoundingBoxFormat instead of converting back and forth
41+ bounding_box = (
42+ bounding_box .clone ()
43+ if format == features .BoundingBoxFormat .XYXY
44+ else convert_format_bounding_box (bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY )
4145 ).reshape (- 1 , 4 )
4246
4347 bounding_box [:, [0 , 2 ]] = spatial_size [1 ] - bounding_box [:, [2 , 0 ]]
4448
4549 return convert_format_bounding_box (
46- bounding_box , old_format = features .BoundingBoxFormat .XYXY , new_format = format , copy = False
50+ bounding_box , old_format = features .BoundingBoxFormat .XYXY , new_format = format
4751 ).reshape (shape )
4852
4953
@@ -73,14 +77,18 @@ def vertical_flip_bounding_box(
7377) -> torch .Tensor :
7478 shape = bounding_box .shape
7579
76- bounding_box = convert_format_bounding_box (
77- bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY
80+ # TODO: Investigate if it makes sense from a performance perspective to have an implementation for every
81+ # BoundingBoxFormat instead of converting back and forth
82+ bounding_box = (
83+ bounding_box .clone ()
84+ if format == features .BoundingBoxFormat .XYXY
85+ else convert_format_bounding_box (bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY )
7886 ).reshape (- 1 , 4 )
7987
8088 bounding_box [:, [1 , 3 ]] = spatial_size [0 ] - bounding_box [:, [3 , 1 ]]
8189
8290 return convert_format_bounding_box (
83- bounding_box , old_format = features .BoundingBoxFormat .XYXY , new_format = format , copy = False
91+ bounding_box , old_format = features .BoundingBoxFormat .XYXY , new_format = format
8492 ).reshape (shape )
8593
8694
@@ -394,16 +402,17 @@ def affine_bounding_box(
394402 center : Optional [List [float ]] = None ,
395403) -> torch .Tensor :
396404 original_shape = bounding_box .shape
397- bounding_box = convert_format_bounding_box (
398- bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY
405+
406+ bounding_box = (
407+ convert_format_bounding_box (bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY )
399408 ).reshape (- 1 , 4 )
400409
401410 out_bboxes , _ = _affine_bounding_box_xyxy (bounding_box , spatial_size , angle , translate , scale , shear , center )
402411
403412 # out_bboxes should be of shape [N boxes, 4]
404413
405414 return convert_format_bounding_box (
406- out_bboxes , old_format = features .BoundingBoxFormat .XYXY , new_format = format , copy = False
415+ out_bboxes , old_format = features .BoundingBoxFormat .XYXY , new_format = format
407416 ).reshape (original_shape )
408417
409418
@@ -583,8 +592,8 @@ def rotate_bounding_box(
583592 center = None
584593
585594 original_shape = bounding_box .shape
586- bounding_box = convert_format_bounding_box (
587- bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY
595+ bounding_box = (
596+ convert_format_bounding_box ( bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY )
588597 ).reshape (- 1 , 4 )
589598
590599 out_bboxes , spatial_size = _affine_bounding_box_xyxy (
@@ -599,9 +608,9 @@ def rotate_bounding_box(
599608 )
600609
601610 return (
602- convert_format_bounding_box (
603- out_bboxes , old_format = features . BoundingBoxFormat . XYXY , new_format = format , copy = False
604- ). reshape ( original_shape ) ,
611+ convert_format_bounding_box (out_bboxes , old_format = features . BoundingBoxFormat . XYXY , new_format = format ). reshape (
612+ original_shape
613+ ),
605614 spatial_size ,
606615 )
607616
@@ -818,18 +827,20 @@ def crop_bounding_box(
818827 height : int ,
819828 width : int ,
820829) -> Tuple [torch .Tensor , Tuple [int , int ]]:
821- bounding_box = convert_format_bounding_box (
822- bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY
830+ # TODO: Investigate if it makes sense from a performance perspective to have an implementation for every
831+ # BoundingBoxFormat instead of converting back and forth
832+ bounding_box = (
833+ bounding_box .clone ()
834+ if format == features .BoundingBoxFormat .XYXY
835+ else convert_format_bounding_box (bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY )
823836 )
824837
825838 # Crop or implicit pad if left and/or top have negative values:
826839 bounding_box [..., 0 ::2 ] -= left
827840 bounding_box [..., 1 ::2 ] -= top
828841
829842 return (
830- convert_format_bounding_box (
831- bounding_box , old_format = features .BoundingBoxFormat .XYXY , new_format = format , copy = False
832- ),
843+ convert_format_bounding_box (bounding_box , old_format = features .BoundingBoxFormat .XYXY , new_format = format ),
833844 (height , width ),
834845 )
835846
@@ -896,8 +907,8 @@ def perspective_bounding_box(
896907 raise ValueError ("Argument perspective_coeffs should have 8 float values" )
897908
898909 original_shape = bounding_box .shape
899- bounding_box = convert_format_bounding_box (
900- bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY
910+ bounding_box = (
911+ convert_format_bounding_box ( bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY )
901912 ).reshape (- 1 , 4 )
902913
903914 dtype = bounding_box .dtype if torch .is_floating_point (bounding_box ) else torch .float32
@@ -967,7 +978,7 @@ def perspective_bounding_box(
967978 # out_bboxes should be of shape [N boxes, 4]
968979
969980 return convert_format_bounding_box (
970- out_bboxes , old_format = features .BoundingBoxFormat .XYXY , new_format = format , copy = False
981+ out_bboxes , old_format = features .BoundingBoxFormat .XYXY , new_format = format
971982 ).reshape (original_shape )
972983
973984
@@ -1061,8 +1072,8 @@ def elastic_bounding_box(
10611072 displacement = displacement .to (bounding_box .device )
10621073
10631074 original_shape = bounding_box .shape
1064- bounding_box = convert_format_bounding_box (
1065- bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY
1075+ bounding_box = (
1076+ convert_format_bounding_box ( bounding_box , old_format = format , new_format = features .BoundingBoxFormat .XYXY )
10661077 ).reshape (- 1 , 4 )
10671078
10681079 # Question (vfdev-5): should we rely on good displacement shape and fetch image size from it
@@ -1088,7 +1099,7 @@ def elastic_bounding_box(
10881099 out_bboxes = torch .cat ([out_bbox_mins , out_bbox_maxs ], dim = 1 ).to (bounding_box .dtype )
10891100
10901101 return convert_format_bounding_box (
1091- out_bboxes , old_format = features .BoundingBoxFormat .XYXY , new_format = format , copy = False
1102+ out_bboxes , old_format = features .BoundingBoxFormat .XYXY , new_format = format
10921103 ).reshape (original_shape )
10931104
10941105
0 commit comments