bbox_iou() stability and speed improvements (#2385)
Browse files- utils/general.py +2 -2
utils/general.py
CHANGED
@@ -312,7 +312,7 @@ def clip_coords(boxes, img_shape):
|
|
312 |
boxes[:, 3].clamp_(0, img_shape[0]) # y2
|
313 |
|
314 |
|
315 |
-
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-
|
316 |
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
|
317 |
box2 = box2.T
|
318 |
|
@@ -348,7 +348,7 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=
|
|
348 |
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
|
349 |
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
|
350 |
with torch.no_grad():
|
351 |
-
alpha = v / (
|
352 |
return iou - (rho2 / c2 + v * alpha) # CIoU
|
353 |
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
|
354 |
c_area = cw * ch + eps # convex area
|
|
|
312 |
boxes[:, 3].clamp_(0, img_shape[0]) # y2
|
313 |
|
314 |
|
315 |
+
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-7):
|
316 |
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
|
317 |
box2 = box2.T
|
318 |
|
|
|
348 |
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
|
349 |
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
|
350 |
with torch.no_grad():
|
351 |
+
alpha = v / (v - iou + (1 + eps))
|
352 |
return iou - (rho2 / c2 + v * alpha) # CIoU
|
353 |
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
|
354 |
c_area = cw * ch + eps # convex area
|