三分钟学会使用系列|Inner-IoU损失:通过辅助边界框计算IoU提升检测效果!

三分钟学会使用系列(YOLOv5)|Inner-IoU损失:通过辅助边界框计算IoU提升检测效果。


本文旨在让读者快速了解相关技术并运用,欢迎关注。
仅看前三节即可学会使用!!


1. Inner-IoU导读

​Inner-IoU的作者在IoU的基础上提出了一种通过使用辅助边框计算损失的方法。其通过尺度因子ratio控制辅助边框尺寸以计算损失加速收敛,并能够集成至现有基于IoU的损失函数GIoU、DIoU、CIoU、EIoU、SIoU中。


2. 代码

​ 以下代码出自官方代码地址: 官方GitHub代码

def inner_ciou(box1, box2, ratio = 1.0, xywh=True, 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_
    #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU      #IoU    
    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 = w1 * h1 + w2 * h2 - inter + eps
    iou = inter / union
   #Inner-IoU      #Inner-IoU        #Inner-IoU        #Inner-IoU        #Inner-IoU        #Inner-IoU        #Inner-IoU       
    inner_b1_x1, inner_b1_x2, inner_b1_y1, inner_b1_y2 = x1 - w1_*ratio, x1 + w1_*ratio,\
                                                             y1 - h1_*ratio, y1 + h1_*ratio
    inner_b2_x1,inner_b2_x2, inner_b2_y1, inner_b2_y2 = x2 - w2_*ratio, x2 + w2_*ratio,\
                                                             y2 - h2_*ratio, y2 + h2_*ratio
    inner_inter = (torch.min(inner_b1_x2, inner_b2_x2) - torch.max(inner_b1_x1, inner_b2_x1)).clamp(0) * \
                   (torch.min(inner_b1_y2, inner_b2_y2) - torch.max(inner_b1_y1, inner_b2_y1)).clamp(0)
    inner_union = w1*ratio * h1*ratio + w2*ratio * h2*ratio - inner_inter + eps
    inner_iou = inner_inter/inner_union

    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
    rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4  # center dist ** 2
    v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / h2) - torch.atan(w1 / h1), 2)
    with torch.no_grad():
        alpha = v / (v - iou + (1 + eps))
    return inner_iou - (rho2 / c2 + v * alpha)

def inner_siou(box1, box2, ratio = 1.0, xywh=True, 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_
    #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU       #IoU        #IoU  
    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 = w1 * h1 + w2 * h2 - inter + eps
    iou = inter / union
   #Inner-IoU      #Inner-IoU        #Inner-IoU        #Inner-IoU        #Inner-IoU        #Inner-IoU        #Inner-IoU       
    inner_b1_x1, inner_b1_x2, inner_b1_y1, inner_b1_y2 = x1 - w1_*ratio, x1 + w1_*ratio,\
                                                             y1 - h1_*ratio, y1 + h1_*ratio
    inner_b2_x1,inner_b2_x2, inner_b2_y1, inner_b2_y2 = x2 - w2_*ratio, x2 + w2_*ratio,\
                                                             y2 - h2_*ratio, y2 + h2_*ratio
    inner_inter = (torch.min(inner_b1_x2, inner_b2_x2) - torch.max(inner_b1_x1, inner_b2_x1)).clamp(0) * \
                   (torch.min(inner_b1_y2, inner_b2_y2) - torch.max(inner_b1_y1, inner_b2_y1)).clamp(0)
    inner_union = w1*ratio * h1*ratio + w2*ratio * h2*ratio - inner_inter + eps
    inner_iou = inner_inter/inner_union

    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
    s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5 + eps
    s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5 + eps
    sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5)
    sin_alpha_1 = torch.abs(s_cw) / sigma
    sin_alpha_2 = torch.abs(s_ch) / sigma
    threshold = pow(2, 0.5) / 2
    sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
    angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2)
    rho_x = (s_cw / cw) ** 2
    rho_y = (s_ch / ch) ** 2
    gamma = angle_cost - 2
    distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
    omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
    omiga_h = 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)
    return inner_iou - 0.5 * (distance_cost + shape_cost)

3. 使用教程

​ 以YOLOv5为例,更换Inner-IoU损失。

  • [1]. 下载YOLOv5代码。
  • [2] 依次打开yolov5-master工程下的util->loss.py,将上文提到的inner_ciou代码放置于loss.py文件中。
  • [3]. 将loss.py中ComputeLoss类的__call__方法中的iou替换为iou = inner_ciou(pbox, tbox[i], ratio=0.7,)[0],如图1。
    iou = inner_ciou(pbox, tbox[i], ratio=0.7,)[0]
    

在这里插入图片描述

图1 Inner-IoU替换位置图

4. 文章详解

​ 文章作者认为,现有的IoU损失在实际应用中无法根据不同检测器与检测任务进行自我调整,泛化性弱。基于这个观点,作者分析了BBR(Bounding Box Regression: 边界框回归)模式的过程,并得出结论,在回归过程区分不同回归样本并且使用不用尺度的辅助边框计算损失能够有效加速边框回归的过程。然后,作者提出了Inner-IoU Loss,即通过使用辅助边框计算IoU损失,辅助边框的大小可以通过尺度因子rato控制。

​ Inner-IoU计算方式:

图1 Inner-IoU示意图

​ 图1中, Bgt 和B分别为GT框和锚框,Xc 和Yc表示框的中心点x和y坐标值,w和h分别表示框的高宽值。变量 "ratio "对应的是尺度因子,通常取值范围为 [0.5,1.5]。Inner-IoU的定义如下:

​ rato为尺度缩放因子,当rato为1时辅助边框尺寸与实际边框尺寸相等。Inner-IoU损失的取值范围也为[0,1]。辅助边框与实际边框仅存在尺度上的差异,损失函数计算方式相同。与IoU损失相比,当ratio小于1,辅助边框尺寸小于实际边框,其回归的有效范围小于IoU损失,但其梯度绝对值大于IoU损失所得的梯度,能够加速高IoU样本的收敛;当ratio大于1,较大尺度的辅助边框扩大了回归的有效范围,对于低Iou的回归有所增益。

​ Inner-IoU可以集成于现有的其他Iou边框损失GIoU、DIoU、CIoU、EIoU、SIoU函数中,公式如下:
在这里插入图片描述
​ 作者已开源文章视频代码,更多细节可点击文中相关蓝字超链接移步阅读。


5. 参考

  1. 《Inner-IoU: More Effective Intersection over Union Loss with Auxiliary Bounding Box》

  2. 官方GitHub代码


  • 24
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值