一、IOU
论文:UnitBox: An Advanced Object Detection Network
definition
The full name of IOU is Intersection over Union, which is what we often say. IOU can be used as the basis for the distribution of positive and negative samples in target detection, and can also be used to measure the difference between the prediction frame and the GT frame (AP (average precision) in target detection is obtained based on IOU). The calculation formula of IOU is also very simple, that is, the ratio of the intersection and union of two bounding boxes,
features
- Non-negativity, identity, symmetry, scale invariance
- Cannot accurately reflect the coincidence of two bounding boxes
- If the two bounding boxes do not intersect, the gap between the two bounding boxes cannot be reflected
code example
import torch
def cal_iou(box1, box2, eps=1e-7):
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], box2[3]
# Intersection area
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)
# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + eps
iou = inter / union
return iou
二、GIOU
论文:Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression
definition
In order to overcome the shortcomings of IOU, GIOU has been modified on the basis of IOU, the formula is as follows,
Among them, C: the smallest circumscribing rectangle containing the bounding boxes A and B (the green rectangle in the figure below)
features
- Based on the IOU, the minimum bounding rectangle is introduced to solve the problem that the IOU is 0 when there is no intersection between the prediction frame and the GT frame
- GIoU not only focuses on overlapping areas, but also on other non-overlapping areas, which can better reflect the overlap between the two
- The value range of GIOU is [-1, 1]. When the two overlap, take the maximum value of 1, and take the minimum value of -1 when the two have no intersection and are infinitely far away.
- When the prediction box and GT box contain phenomenon, GIOU degenerates into IOU
code example
import torch
def cal_giou(box1, box2, eps=1e-7):
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], box2[3]
# Intersection area
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)
# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + eps
iou = inter / union
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # smallest enclosing box width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # smallest enclosing box height
c_area = cw * ch + eps # convex area
giou = iou - (c_area - union) / c_area
return giou
三、DIOU
论文:Distance-IoULoss:FasterandBetterLearningforBoundingBoxRegression
definition
In order to overcome the shortcomings of GIOU, DIOU has been improved on the basis of IOU, the formula is as follows,
Among them, : the sum of the squares of the Euclidean distance between two points, , : the center point coordinates of the bounding boxes A and B, : the diagonal length of the smallest circumscribed rectangular box containing A and B
features
- DIOU can directly optimize the distance between two bounding boxes, which converges faster than GIOU Loss
- If the two bounding boxes coincide perfectly, d=0, IOU=1, DIOU=1-0=1. If the two bounding boxes are far apart, approaching 1, IOU=0, DIOU=0-1=-1. Therefore, the value range of DIOU is also [-1, 1]
- Even if the c value does not change when one box contains another, the d value can be a valid measure
- When the center points of the two bounding boxes coincide, there may be cases where both c and d remain unchanged
code example
import torch
def cal_diou(box1, box2, eps=1e-7):
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], box2[3]
# Intersection area
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)
# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + eps
iou = inter / union
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # smallest enclosing box width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # smallest enclosing box height
c2 = cw ** 2 + ch ** 2 + eps # the diagonal of smallest enclosing box
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
diou = iou - rho2 / c2
return diou
4. CIOU
Complete IOU
definition
CIOU is to increase the aspect ratio of the bounding box on the basis of DIOU, the formula is as follows,
Therefore, the three items of CIOU correspond exactly to IOU, the ratio of the center point distance to the diagonal distance of the smallest circumscribed rectangular frame, and the gap between the aspect ratios of the two bounding boxes
features
- CIOU is to increase the aspect ratio of the bounding box on the basis of DIOU, so that the prediction box is closer to the GT box
- The aspect ratio of the bounding box describes a relative value, and there is some ambiguity
code example
import torch
import math
def cal_ciou(box1, box2, eps=1e-7):
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], box2[3]
# Intersection area
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)
# Union Area
w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
union = w1 * h1 + w2 * h2 - inter + eps
iou = inter / union
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # smallest enclosing box width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # smallest enclosing box height
c2 = cw ** 2 + ch ** 2 + eps
rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center distance squared
v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / (h2 + eps)) - torch.atan(w1 / (h1 + eps)), 2)
with torch.no_grad():
alpha = v / (v - iou + (1 + eps))
ciou = iou - (rho2 / c2 + v * alpha)
return ciou
【Reference article】
https://www.cnblogs.com/wujianming-110117/p/13019343.html
Detailed explanation of IOU, GIOU, DIOU, CIOU loss function
Detailed explanation and code implementation of IOU, GIOU, DIOU, CIOU - Programmer Sought
IoU series (IoU, GIoU, DIoU, CIoU)_ciou formula_PoomHey's Blog-CSDN Blog