|
|
|
|
|
|
|
|
union = w1 * h1 + w2 * h2 - inter + eps |
|
|
union = w1 * h1 + w2 * h2 - inter + eps |
|
|
|
|
|
|
|
|
iou = inter / union |
|
|
iou = inter / union |
|
|
if GIoU or DIoU or CIoU: |
|
|
|
|
|
|
|
|
if CIoU or DIoU or GIoU: |
|
|
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 CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 |
|
|
if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1 |
|
|
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared |
|
|
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared |
|
|
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + |
|
|
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + |
|
|
(b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared |
|
|
(b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared |
|
|
if DIoU: |
|
|
|
|
|
return iou - rho2 / c2 # DIoU |
|
|
|
|
|
elif CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 |
|
|
|
|
|
|
|
|
if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47 |
|
|
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2) |
|
|
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2) |
|
|
with torch.no_grad(): |
|
|
with torch.no_grad(): |
|
|
alpha = v / (v - iou + (1 + eps)) |
|
|
alpha = v / (v - iou + (1 + eps)) |
|
|
return iou - (rho2 / c2 + v * alpha) # CIoU |
|
|
return iou - (rho2 / c2 + v * alpha) # CIoU |
|
|
|
|
|
else: |
|
|
|
|
|
return iou - rho2 / c2 # DIoU |
|
|
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf |
|
|
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf |
|
|
c_area = cw * ch + eps # convex area |
|
|
c_area = cw * ch + eps # convex area |
|
|
return iou - (c_area - union) / c_area # GIoU |
|
|
return iou - (c_area - union) / c_area # GIoU |