@@ -928,16 +928,16 @@ def perspective_bounding_box(
928
928
(- perspective_coeffs [0 ] * perspective_coeffs [7 ] + perspective_coeffs [1 ] * perspective_coeffs [6 ]) / denom ,
929
929
]
930
930
931
- theta1 = torch .tensor (
932
- [[inv_coeffs [0 ], inv_coeffs [1 ], inv_coeffs [2 ]], [inv_coeffs [3 ], inv_coeffs [4 ], inv_coeffs [5 ]]],
931
+ theta12_T = torch .tensor (
932
+ [
933
+ [inv_coeffs [0 ], inv_coeffs [3 ], inv_coeffs [6 ], inv_coeffs [6 ]],
934
+ [inv_coeffs [1 ], inv_coeffs [4 ], inv_coeffs [7 ], inv_coeffs [7 ]],
935
+ [inv_coeffs [2 ], inv_coeffs [5 ], 1.0 , 1.0 ],
936
+ ],
933
937
dtype = dtype ,
934
938
device = device ,
935
939
)
936
940
937
- theta2 = torch .tensor (
938
- [[inv_coeffs [6 ], inv_coeffs [7 ], 1.0 ], [inv_coeffs [6 ], inv_coeffs [7 ], 1.0 ]], dtype = dtype , device = device
939
- )
940
-
941
941
# 1) Let's transform bboxes into a tensor of 4 points (top-left, top-right, bottom-left, bottom-right corners).
942
942
# Tensor of points has shape (N * 4, 3), where N is the number of bboxes
943
943
# Single point structure is similar to
@@ -948,15 +948,16 @@ def perspective_bounding_box(
948
948
# x_out = (coeffs[0] * x + coeffs[1] * y + coeffs[2]) / (coeffs[6] * x + coeffs[7] * y + 1)
949
949
# y_out = (coeffs[3] * x + coeffs[4] * y + coeffs[5]) / (coeffs[6] * x + coeffs[7] * y + 1)
950
950
951
- numer_points = torch .matmul (points , theta1 .T )
952
- denom_points = torch .matmul (points , theta2 .T )
953
- transformed_points = numer_points / denom_points
951
+ numer_denom_points = torch .matmul (points , theta12_T )
952
+ numer_points = numer_denom_points [:, :2 ]
953
+ denom_points = numer_denom_points [:, 2 :]
954
+ transformed_points = numer_points .div_ (denom_points )
954
955
955
956
# 3) Reshape transformed points to [N boxes, 4 points, x/y coords]
956
957
# and compute bounding box from 4 transformed points:
957
958
transformed_points = transformed_points .reshape (- 1 , 4 , 2 )
958
- out_bbox_mins , _ = torch .min (transformed_points , dim = 1 )
959
- out_bbox_maxs , _ = torch . max ( transformed_points , dim = 1 )
959
+ out_bbox_mins , out_bbox_maxs = torch .aminmax (transformed_points , dim = 1 )
960
+
960
961
out_bboxes = torch .cat ([out_bbox_mins , out_bbox_maxs ], dim = 1 ).to (bounding_box .dtype )
961
962
962
963
# out_bboxes should be of shape [N boxes, 4]
0 commit comments