Update general.py bbox_iou() with eps improvements (#736)
eps reduced to 1e-9 for additional precaution.
This commit is contained in:
1 parent
82ed33a052
commit
5a7d79fbe6
1 file changed
+20
-21
+20
-21
@@ -339,41 +339,37 @@ def compute_ap(recall, precision):
|
|||||||
return ap
|
return ap
|
||||||
|
|
||||||
|
|
||||||
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-12):
|
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=1e-9):
|
||||||
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
|
# Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
|
||||||
box2 = box2.T
|
box2 = box2.T
|
||||||
|
|
||||||
# Get the coordinates of bounding boxes
|
# Get the coordinates of bounding boxes
|
||||||
if x1y1x2y2: # x1, y1, x2, y2 = box1
|
if x1y1x2y2: # x1, y1, x2, y2 = box1
|
||||||
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2] + eps, box1[3] + eps
|
b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
|
||||||
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2] + eps, box2[3] + eps
|
b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
|
||||||
else: # transform from xywh to xyxy
|
else: # transform from xywh to xyxy
|
||||||
b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2 + eps
|
b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
|
||||||
b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2 + eps
|
b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
|
||||||
b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2 + eps
|
b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
|
||||||
b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2 + eps
|
b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
|
||||||
|
|
||||||
# Intersection area
|
# Intersection area
|
||||||
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
|
inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
|
||||||
(torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
|
(torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
|
||||||
|
|
||||||
# Union Area
|
# Union Area
|
||||||
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1
|
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
|
||||||
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1
|
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
|
||||||
union = w1 * h1 + w2 * h2 - inter
|
union = w1 * h1 + w2 * h2 - inter + eps
|
||||||
|
|
||||||
iou = inter / union # iou
|
iou = inter / union
|
||||||
if GIoU or DIoU or CIoU:
|
if GIoU or DIoU or CIoU:
|
||||||
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
|
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
|
||||||
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
|
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
|
||||||
if GIoU: # Generalized IoU https://arxiv.org/pdf/1902.09630.pdf
|
if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
|
||||||
c_area = cw * ch # convex area
|
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
|
||||||
return iou - (c_area - union) / c_area # GIoU
|
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 +
|
||||||
if DIoU or CIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
|
(b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
|
||||||
# convex diagonal squared
|
|
||||||
c2 = cw ** 2 + ch ** 2
|
|
||||||
# centerpoint distance squared
|
|
||||||
rho2 = ((b2_x1 + b2_x2) - (b1_x1 + b1_x2)) ** 2 / 4 + ((b2_y1 + b2_y2) - (b1_y1 + b1_y2)) ** 2 / 4
|
|
||||||
if DIoU:
|
if DIoU:
|
||||||
return iou - rho2 / c2 # DIoU
|
return iou - rho2 / c2 # DIoU
|
||||||
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
|
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
|
||||||
@@ -381,8 +377,11 @@ def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, eps=
|
|||||||
with torch.no_grad():
|
with torch.no_grad():
|
||||||
alpha = v / ((1 + eps) - iou + v)
|
alpha = v / ((1 + eps) - iou + v)
|
||||||
return iou - (rho2 / c2 + v * alpha) # CIoU
|
return iou - (rho2 / c2 + v * alpha) # CIoU
|
||||||
|
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
|
||||||
return iou
|
c_area = cw * ch + eps # convex area
|
||||||
|
return iou - (c_area - union) / c_area # GIoU
|
||||||
|
else:
|
||||||
|
return iou # IoU
|
||||||
|
|
||||||
|
|
||||||
def box_iou(box1, box2):
|
def box_iou(box1, box2):
|
||||||
|
|||||||
Reference in new issue
Block a user