@@ -325,13 +325,13 @@ def complete_box_iou(boxes1: Tensor, boxes2: Tensor, eps: float = 1e-7) -> Tenso
325325
326326 diou , iou = _box_diou_iou (boxes1 , boxes2 , eps )
327327
328- w_pred = boxes1 [:, 2 ] - boxes1 [:, 0 ]
329- h_pred = boxes1 [:, 3 ] - boxes1 [:, 1 ]
328+ w_pred = boxes1 [:, None , 2 ] - boxes1 [:, None , 0 ]
329+ h_pred = boxes1 [:, None , 3 ] - boxes1 [:, None , 1 ]
330330
331331 w_gt = boxes2 [:, 2 ] - boxes2 [:, 0 ]
332332 h_gt = boxes2 [:, 3 ] - boxes2 [:, 1 ]
333333
334- v = (4 / (torch .pi ** 2 )) * torch .pow (( torch .atan (w_gt / h_gt ) - torch .atan (w_pred / h_pred ) ), 2 )
334+ v = (4 / (torch .pi ** 2 )) * torch .pow (torch .atan (w_pred / h_pred ) - torch .atan (w_gt / h_gt ), 2 )
335335 with torch .no_grad ():
336336 alpha = v / (1 - iou + v + eps )
337337 return diou - alpha * v
@@ -358,7 +358,7 @@ def distance_box_iou(boxes1: Tensor, boxes2: Tensor, eps: float = 1e-7) -> Tenso
358358
359359 boxes1 = _upcast (boxes1 )
360360 boxes2 = _upcast (boxes2 )
361- diou , _ = _box_diou_iou (boxes1 , boxes2 )
361+ diou , _ = _box_diou_iou (boxes1 , boxes2 , eps = eps )
362362 return diou
363363
364364
@@ -375,7 +375,9 @@ def _box_diou_iou(boxes1: Tensor, boxes2: Tensor, eps: float = 1e-7) -> Tuple[Te
375375 x_g = (boxes2 [:, 0 ] + boxes2 [:, 2 ]) / 2
376376 y_g = (boxes2 [:, 1 ] + boxes2 [:, 3 ]) / 2
377377 # The distance between boxes' centers squared.
378- centers_distance_squared = (_upcast (x_p - x_g ) ** 2 ) + (_upcast (y_p - y_g ) ** 2 )
378+ centers_distance_squared = (_upcast ((x_p [:, None ] - x_g [None , :])) ** 2 ) + (
379+ _upcast ((y_p [:, None ] - y_g [None , :])) ** 2
380+ )
379381 # The distance IoU is the IoU penalized by a normalized
380382 # distance between boxes' centers squared.
381383 return iou - (centers_distance_squared / diagonal_distance_squared ), iou
0 commit comments