交并比(Intersection over Union),表示两个bbox交集部分面积和并集部分面积的比值。
I o U = A ∩ B A ∪ B IoU = \frac{A \cap B }{A \cup B} IoU=A∪BA∩B
def cal_IOU(bbox_a, bbox_b):
# bbox_a : x1,y1,x2,y2(ltrb)
# bbox_b : x3,y3,x4,y4(ltrb)
x1,y1,x2,y2 = bbox_a
x3,y3,x4,y4 = bbox_b
# 计算交集
intersection_h = min(x2, x4) - max(x1, x3)
intersection_w = min(y2, y4) - max(y1, y3)
# 考虑两个bbox不相交的情况
intersection_h = max(intersection_h, 0)
intersection_w = max(intersection_w, 0)
intersection = intersection_h * intersection_w
# 计算并集
union = (x2-x1)*(y2-y1) + (x4-x3)*(y4-y3) - intersection
return intersection / union
G I O U = I O U − C − A ∪ B C GIOU = IOU - \frac{C-A \cup B}{C} GIOU=IOU−CC−A∪B,其中 C C C表示包含A,B的最小框面积
def cal_GIOU(bbox_a, bbox_b):
# bbox_a : x1,y1,x2,y2 (ltbr)
# bbox_b : x3,y3,x4,y4
x1,y1,x2,y2 = bbox_a
x3,y3,x4,y4 = bbox_b
# intersection
inter_h = min(y2,y4) - max(y1, y3)
inter_w = min(x4,x2) - max(x1, x3)
inter_h = max(0, inter_h)
inter_w = max(0, inter_w)
inter = inter_h * inter_w
# union
union = (y4-y3)*(x4-x3) + (y2-y1)*(x2-x1) - inter
# smallest enclosing convex
convex_h = max(y2,y4) - min(y1,y3)
convex_w = max(x2,x4) - min(x1,x3)
convex = convex_h * convex_w
# GIOU
return inter/union - (convex-union)/convex
当一个bbox包含另一个bbox的时候,GIOU退化为IOU,无法进一步梯度下降,为此DIOU引入了新的惩罚项
D I O U = I O U − ρ 2 ( b , b g t ) c 2 DIOU = IOU - \frac{\rho^2(b,b^{gt})}{c^2} DIOU=IOU−c2ρ2(b,bgt),其中 ρ \rho ρ代表了二者中心点的欧式距离, c c c代表了包含两bbox的最小矩形的对角线的欧式距离
def cal_DIOU(bbox_a, bbox_b):
# bbox_a : x1,y1,x2,y2 (ltbr)
# bbox_b : x3,y3,x4,y4
x1,y1,x2,y2 = bbox_a
x3,y3,x4,y4 = bbox_b
# intersection
inter_h = min(y2,y4) - max(y1,y3)
inter_w = min(x2,x4) - max(x1,x3)
inter_h = max(0, inter_h)
inter_w = max(0, inter_w)
inter = inter_h * inter_w
# union
union = (y4-y3)*(x4-x3) + (y2-y1)*(x2-x1) - inter
# center dist
center_ax = (x1+x2) / 2
center_ay = (y1+y2) / 2
center_bx = (x3+x4) / 2
center_by = (y3+y4) / 2
dist_ab = (center_ax - center_bx)**2 + (center_ay-center_by)**2
dist_c = (min(x1,x3)-max(x2,x4))**2 + (min(y1,y3) - max(y2,y4))**2
#DIOU
return inter/union - dist_ab/dist_c
在DIOU的基础上,添加了bbox长宽比的惩罚项,要求两bbox的形状也趋向一致
D I O U = I O U − ρ 2 ( b , b g t ) c 2 − α υ DIOU = IOU - \frac{\rho^2(b,b^{gt})}{c^2}-\alpha\upsilon DIOU=IOU−c2ρ2(b,bgt)−αυ,其中
α = υ ( 1 − I O U ) + υ \alpha=\frac{\upsilon}{(1-IOU)+\upsilon} α=(1−IOU)+υυ 表示比例系数
υ = 4 π 2 ( a r c t a n w g t h g t − a r c t a n w h ) 2 \upsilon=\frac{4}{\pi^2}(arctan\frac{w^{gt}}{h^{gt}}-arctan\frac{w}{h})^2 υ=π24(arctanhgtwgt−arctanhw)2表示两bbox之间的长宽比一致性