论文地址:
IoU: UnitBox: An Advanced Object Detection Network
GIoU:Generalized Intersection over Union: A Metric and A Loss for Bounding Box Regression
DIoU&CIoU: Distance-IoU Loss: Faster and Better Learning for Bounding Box Regression
SIoU: SIoU Loss: More Powerful Learning for Bounding Box Regression
def IoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
w = np.maximum(0.0, yy2 - yy1)
h = np.maximum(0.0, xx2 - xx1)
inter = w * h
IoU = inter/((b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter)
print("IoU: ", IoU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
IoU(box1, box2)
def GIoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
# IOU
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
inter_w = np.maximum(0.0, yy2 - yy1)
inter_h = np.maximum(0.0, xx2 - xx1)
inter = inter_w * inter_h
Union = (b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter
# GIOU
C_xx1 = np.minimum(b1_x1, b2_x1)
C_yy1 = np.minimum(b1_y1, b2_y1)
C_xx2 = np.maximum(b1_x2, b2_x2)
C_yy2 = np.maximum(b1_y2, b2_y2)
C_area = (C_xx2 - C_xx1) * (C_yy2 - C_yy1)
IOU = inter / Union
GIOU = IOU - abs((C_area-Union)/C_area)
print("GIOU:", GIOU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
GIoU(box1, box2)
def DIoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
# IOU
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
inter_w = np.maximum(0.0, xx2 - xx1)
inter_h = np.maximum(0.0, yy2 - yy1)
inter = inter_w * inter_h
Union = (b1_x2 - b1_x1)*(b1_y2 - b1_y1) + (b2_x2 - b2_x1)*(b2_y2 - b2_y1) - inter
# DISTANCE
C_xx1 = np.minimum(b1_x1, b2_x1)
C_yy1 = np.minimum(b1_y1, b2_y1)
C_xx2 = np.maximum(b1_x2, b2_x2)
C_yy2 = np.maximum(b1_y2, b2_y2)
C_area = (C_xx2 - C_xx1) * (C_yy2 - C_yy1)
center_b_x = (b1_x1+b1_x2)/2
center_b_y = (b1_y1+b1_y2)/2
center_gtb_x = (b2_x1+b2_x2)/2
center_gtb_y = (b2_y1+b2_y2)/2
Distance = (center_gtb_x-center_b_x)**2 + (center_gtb_y-center_b_y)**2
IOU = inter/Union
DIOU = IOU - Distance/(C_area**2)
print("DIOU:", DIOU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
DIoU(box1, box2)
def CIoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
# IOU
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
inter_w = np.maximum(0.0, xx2 - xx1)
inter_h = np.maximum(0.0, yy2 - yy1)
inter = inter_w*inter_h
Union = (b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter
IOU = inter/Union
C_xx1 = np.minimum(b1_x1, b2_x1)
C_yy1 = np.minimum(b1_y1, b2_y1)
C_xx2 = np.maximum(b1_x2, b2_x2)
C_yy2 = np.maximum(b1_y2, b2_y2)
# DISTANCE
center_b_x = (b1_x1 + b1_x2)/2
center_b_y = (b1_y1 + b1_y2)/2
center_gtb_x = (b2_x1 + b2_x2)/2
center_gtb_y = (b2_y1 + b2_y2)/2
C_area = (C_xx2-C_xx1)*(C_yy2-C_yy1)
Distance = (center_gtb_x-center_b_x)**2 + (center_gtb_y-center_b_y)**2
Distance_area = Distance/C_area**2
# aspect ratio
pred_w = b1_y2 - b1_y1
pred_h = b1_x2 - b1_x1
gt_w = b2_y2 - b2_y1
gt_h = b2_x2 - b2_x1
v = (4/(np.pi)**2)*(np.arctan(gt_w/gt_h) - np.arctan(pred_w/pred_h))**2
alpha = v/((1-IOU) + v)
CIOU = IOU - Distance_area - alpha*v
print("CIOU:", CIOU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
CIoU(box1, box2)
def SIoU(box1, box2):
b1_x1, b1_y1, b1_x2, b1_y2 = box1
b2_x1, b2_y1, b2_x2, b2_y2 = box2
# IOU
xx1 = np.maximum(b1_x1, b2_x1)
yy1 = np.maximum(b1_y1, b2_y1)
xx2 = np.minimum(b1_x2, b2_x2)
yy2 = np.minimum(b1_y2, b2_y2)
inter_w = np.maximum(0.0, xx2 - xx1)
inter_h = np.maximum(0.0, yy2 - yy1)
inter = inter_w*inter_h
Union = (b1_x2-b1_x1)*(b1_y2-b1_y1) + (b2_x2-b2_x1)*(b2_y2-b2_y1) - inter
IOU = inter/Union
center_b_x = (b1_x1 + b1_x2)/2
center_b_y = (b1_y1 + b1_y2)/2
center_gtb_x = (b2_x1 + b2_x2)/2
center_gtb_y = (b2_y1 + b2_y2)/2
# ANGLE
sigma = np.sqrt((center_gtb_x-center_b_x)**2 + (center_gtb_y-center_b_y)**2)
lambda_ch = max(center_gtb_y, center_b_y) - min(center_gtb_y, center_b_y)
lambda_x = lambda_ch/sigma
angle = 1 - 2*(np.sin(np.arctan(lambda_x)-np.pi/4)**2)
# DISTANCE
lambda_cw = max(center_gtb_x, center_b_x) - min(center_gtb_x, center_b_x)
Rho_x = ((center_gtb_x-center_b_x)/lambda_cw)**2
Rho_y = ((center_gtb_y-center_b_y)/lambda_ch)**2
gamma = 2-angle
Delat = (1-np.exp(-1*gamma*Rho_x)) + (1-np.exp(-1*gamma*Rho_y))
# SHAPE
Theta = 4
pred_w = b1_y2 - b1_y1
pred_h = b1_x2 - b1_x1
gt_w = b2_y2 - b2_y1
gt_h = b2_x2 - b2_x1
Omega_w = abs(pred_w-gt_w)/max(pred_w, gt_w)
Omega_h = abs(pred_h-gt_h)/max(pred_h, gt_h)
Omega = (1-np.exp(-1*Omega_w))**Theta + (1-np.exp(-1*Omega_h))**Theta
SIOU = 1 - IOU + (Delat + Omega)/2
print("SIOU:", SIOU)
if __name__ == "__main__":
box1 = np.array([100, 100, 210, 210])
box2 = np.array([150, 150, 230, 220])
SIoU(box1, box2)