本篇总结Loss Function中的IoU系列代码。
1. IoU
交并集,两个框交集面积除以并集面积。(论写写画画的重要性)(找原文看看)
"""
box1[x1, y1, x2, y2]
box2[x1, y1, x2, y2]
return iou
"""
def iou(box1, box2):# Intersectionw = max(0, min(box1[2], box2[2])-max(box1[0], box2[0]))h = max(0, min(box1[3], box2[3])-max(box1[1], box2[1]))# Unions1 = (box1[2]-box1[0]) * (box1[3]-box1[1])s2 = (box2[2]-box2[0]) * (box2[3]-box2[1])return (w*h)/(s1+s2-w*h)
IoU的问题:
(1)在检测框和目标框完全不重叠时,IoU为0,无法衡量两个框的距离远近,损失函数不可导,无法优化;
(2)两个检测框相同,IoU也相同时,无法衡量两者和目标框相交情况的不同(这点一定要区分吗?)。
2. GIoU
公式:
其中C表示最小外接矩形面积,U表示并集面积。
(找原文看看)
"""
box1[x1, y1, x2, y2]
box2[x1, y1, x2, y2]
return giou
"""
def giou(box1, box2):# Intersectionw = max(0, min(box1[2], box2[2])-max(box1[0], box2[0]))h = max(0, min(box1[3], box2[3])-max(box1[1], box2[1]))# Unions1 = (box1[2]-box1[0]) * (box1[3]-box1[1])s2 = (box2[2]-box2[0]) * (box2[3]-box2[1])union = s1+s2-w*hiou = (w*h)/union# area of minimun rectangularx1 = min(box1[0], box2[0])x2 = max(box1[2], box2[2])y1 = min(box1[1], box2[1])y2 = max(box1[3], box2[3])c = (x2-x1) * (y2-y1)return iou-(c-union)/c
疑问:当两个检测框大小相同,和目标框的IoU相同时,GIoU更倾向于使检测框和目标框在x或y方向上平行,这样合理吗?