Commit
•
5e0b90d
1
Parent(s):
fb4fc8c
CIoU nan bug fix (#736)
Browse files- utils/general.py +13 -13
utils/general.py
CHANGED
@@ -1,15 +1,15 @@
|
|
1 |
import glob
|
|
|
2 |
import math
|
3 |
import os
|
|
|
4 |
import random
|
5 |
import shutil
|
6 |
import subprocess
|
7 |
import time
|
8 |
-
import logging
|
9 |
from contextlib import contextmanager
|
10 |
from copy import copy
|
11 |
from pathlib import Path
|
12 |
-
import platform
|
13 |
|
14 |
import cv2
|
15 |
import matplotlib
|
@@ -339,19 +339,19 @@ def compute_ap(recall, precision):
|
|
339 |
return ap
|
340 |
|
341 |
|
342 |
-
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
|
343 |
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
|
344 |
box2 = box2.T
|
345 |
|
346 |
# Get the coordinates of bounding boxes
|
347 |
if x1y1x2y2: # x1, y1, x2, y2 = box1
|
348 |
-
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
|
349 |
-
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
|
350 |
else: # transform from xywh to xyxy
|
351 |
-
b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
|
352 |
-
b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
|
353 |
-
b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
|
354 |
-
b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
|
355 |
|
356 |
# Intersection area
|
357 |
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
|
@@ -360,18 +360,18 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
|
|
360 |
# Union Area
|
361 |
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1
|
362 |
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1
|
363 |
-
union =
|
364 |
|
365 |
iou = inter / union # iou
|
366 |
if GIoU or DIoU or CIoU:
|
367 |
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
|
368 |
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
|
369 |
if GIoU: # Generalized IoU https://arxiv.org/pdf/1902.09630.pdf
|
370 |
-
c_area = cw * ch
|
371 |
return iou - (c_area - union) / c_area # GIoU
|
372 |
if DIoU or CIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
|
373 |
# convex diagonal squared
|
374 |
-
c2 = cw ** 2 + ch ** 2
|
375 |
# centerpoint distance squared
|
376 |
rho2 = ((b2_x1 + b2_x2) - (b1_x1 + b1_x2)) ** 2 / 4 + ((b2_y1 + b2_y2) - (b1_y1 + b1_y2)) ** 2 / 4
|
377 |
if DIoU:
|
@@ -379,7 +379,7 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False):
|
|
379 |
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
|
380 |
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
|
381 |
with torch.no_grad():
|
382 |
-
alpha = v / (1 - iou + v
|
383 |
return iou - (rho2 / c2 + v * alpha) # CIoU
|
384 |
|
385 |
return iou
|
|
|
1 |
import glob
|
2 |
+
import logging
|
3 |
import math
|
4 |
import os
|
5 |
+
import platform
|
6 |
import random
|
7 |
import shutil
|
8 |
import subprocess
|
9 |
import time
|
|
|
10 |
from contextlib import contextmanager
|
11 |
from copy import copy
|
12 |
from pathlib import Path
|
|
|
13 |
|
14 |
import cv2
|
15 |
import matplotlib
|
|
|
339 |
return ap
|
340 |
|
341 |
|
342 |
+
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-12):
|
343 |
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
|
344 |
box2 = box2.T
|
345 |
|
346 |
# Get the coordinates of bounding boxes
|
347 |
if x1y1x2y2: # x1, y1, x2, y2 = box1
|
348 |
+
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2] + eps, box1[3] + eps
|
349 |
+
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2] + eps, box2[3] + eps
|
350 |
else: # transform from xywh to xyxy
|
351 |
+
b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 + eps
|
352 |
+
b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 + eps
|
353 |
+
b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 + eps
|
354 |
+
b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 + eps
|
355 |
|
356 |
# Intersection area
|
357 |
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
|
|
|
360 |
# Union Area
|
361 |
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1
|
362 |
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1
|
363 |
+
union = w1 * h1 + w2 * h2 - inter
|
364 |
|
365 |
iou = inter / union # iou
|
366 |
if GIoU or DIoU or CIoU:
|
367 |
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
|
368 |
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
|
369 |
if GIoU: # Generalized IoU https://arxiv.org/pdf/1902.09630.pdf
|
370 |
+
c_area = cw * ch # convex area
|
371 |
return iou - (c_area - union) / c_area # GIoU
|
372 |
if DIoU or CIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
|
373 |
# convex diagonal squared
|
374 |
+
c2 = cw ** 2 + ch ** 2
|
375 |
# centerpoint distance squared
|
376 |
rho2 = ((b2_x1 + b2_x2) - (b1_x1 + b1_x2)) ** 2 / 4 + ((b2_y1 + b2_y2) - (b1_y1 + b1_y2)) ** 2 / 4
|
377 |
if DIoU:
|
|
|
379 |
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
|
380 |
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
|
381 |
with torch.no_grad():
|
382 |
+
alpha = v / ((1 + eps) - iou + v)
|
383 |
return iou - (rho2 / c2 + v * alpha) # CIoU
|
384 |
|
385 |
return iou
|