今天看到一篇文章主要是提出了一种更有效的IOU度量方法,论文地址在这里,如下所示:
摘要
边界盒回归损失作为检测器定位分支的重要组成部分,在目标检测任务中起着重要作用。现有的边界框回归方法通常考虑GT框和预测框之间的几何关系,并使用边界框的相对位置和形状来计算损失,而忽略了边界框的形状和比例等固有特性对边界框回归的影响。为了弥补现有研究的不足,本文提出了一种以包围盒本身的形状和尺度为重点的包围盒回归方法。首先,我们分析了边界框的回归特性,发现边界框本身的形状和比例因素会对回归结果产生影响。基于上述结论,我们提出了Shape-IoU方法,该方法可以通过关注边界框本身的形状和比例来计算损失,从而使边界框回归更加准确。最后,我们通过大量的对比实验验证了我们的方法,结果表明我们的方法可以有效地提高检测性能,并优于现有方法,在不同的检测任务中达到最先进的性能。
作者也开源了对应的代码,如下所示:
def shape_iou(box1, box2, xywh=True, scale=0, eps=1e-7):
(x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, -1), box2.chunk(4, -1)
w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
# 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
union = w1 * h1 + w2 * h2 - inter + eps
# IoU
iou = inter / union
#Shape-Distance #Shape-Distance #Shape-Distance #Shape-Distance #Shape-Distance #Shape-Distance #Shape-Distance
ww = 2 * torch.pow(w2, scale) / (torch.pow(w2, scale) + torch.pow(h2, scale))
hh = 2 * torch.pow(h2, scale) / (torch.pow(w2, scale) + torch.pow(h2, scale))
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
center_distance_x = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2) / 4
center_distance_y = ((b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4
center_distance = hh * center_distance_x + ww * center_distance_y
distance = center_distance / c2
#Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape #Shape-Shape
omiga_w = hh * torch.abs(w1 - w2) / torch.max(w1, w2)
omiga_h = ww * torch.abs(h1 - h2) / torch.max(h1, h2)
shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
#Shape-IoU #Shape-IoU #Shape-IoU #Shape-IoU #Shape-IoU #Shape-IoU #Shape-IoU #Shape-IoU #Shape-IoU
iou = iou - distance - 0.5 * ( shape_cost)
return iou # IoU
关于Shape-IoU的详细内容可以自行移步阅读原论文,这里我还没有详细去读。
后续找时间可以拿来这个Shape-IoU移植到yolo各个系列模型中来进行实验对比分析,看看是否能在实际业务场景中带来有效地提升。