目标检测中回归损失函数

1.Smooth L1 Loss

假设x为预测框和真实框之间的数值差异,常用的L1和L2 Loss定义为:

目标检测中回归损失函数_第1张图片

上述的3个损失函数对x的导数分别为:

目标检测中回归损失函数_第2张图片

从损失函数对x的导数可知: L^{_{1}}损失函数对x的导数为常数,在训练后期,x很小时,如果learning rate 不变,损失函数会在稳定值附近波动,很难收敛到更高的精度。 L^{_{2}}损失函数对x的导数在x值很大时,其导数也非常大,在训练初期不稳定。smooth完美的避开了 前两个损失的缺点。

实际目标检测框回归任务中的损失loss为:

目标检测中回归损失函数_第3张图片

2.IOU(Intersection over Union

优点:(1)可以反映预测检测框与真实检测框的检测效果,(2)尺度不变性

缺点:作为损失函数会出现问题:(1)如果两个框没有相交,IoU=0,loss=0,没有梯度回传,无法进行学习训练。

(2)IoU无法精确的反映两者的重合度大小。

#iou
import numpy as np

def iou(box1,box2,wh=False):
    #wh=False表示输入的box为(左上角点坐标和右下角点坐标四个值)
    if wh=False:
        xmin1,ymin1,xmax1,ymax1 = box1
        xmin2,ymin2,xmax2,ymax2 = box2
    
    #wh=True表示输入的box为(中心点和宽高四个值)   
    else:
        xmin1,ymin1 = int(box1[0]-box1[2]/2),int(box1[1]-box1[3]/2)
        xmax1,ymax1 = int(box1[0]+box1[2]/2),int(box1[1]+box1[3]/2)
        xmin2,ymin2 = int(box2[0]-box2[2]/2),int(box2[1]-box2[3]/2)
        xmax2,ymax2 = int(box2[0]+box2[2]/2),int(box2[1]+box2[3]/2)
    
    #获取矩形框交集对应的左上角和右下角坐标
    xmin = np.max([xmin1,xmin2])
    ymin = np.max([ymin1,ymin2])
    xmax = np.min([xmax1,xmax2])
    ymax = np.min([ymax1,ymax2])
    
    #计算两个矩形的面积
    area1 = (xmax1-xmin1)*(ymax1-ymin1)
    area2 = (xmax2-xmin2)*(ymax2-ymin2)
    #计算交集面积,注意无交集的情况
    inter_area = (np.max([0,xmax-xmin]))*(np.max([0,ymax-ymin]))
    #计算交并比
    iou = inter_area/(area1+area2-inter_area+1e-6)
    
    return iou

3.GIOU(Generalized Intersection over Union

优点:(1)与IoU只关注重叠区域不同,GIoU不仅关注重叠区域,还关注其他的非重合区域,能更好的反映两者的重合度。

(2)IoU取值[0,1],但GIoU有对称区间,取值范围[-1,1]。在两者重合的时候取最大值1,在两者无交集且无限远的时候取最小值-1,因此GIoU是一个非常好的距离度量指标。

def Giou(box_p,box_g):
    #预测框和真值框的左上角和右下角坐标
    xp1,yp1,xp2,yp2 = box_p
    xg1,yg1,xg2,yg2 = box_g
    #为了确保预测框的右下角坐标分别大于左上角坐标
    xp_1 = np.min([xp1,xp2])
    xp_2 = np.max([xp1,xp2])
    yp_1 = np.min([yp1,yp2])
    yp_2 = np.max([yp1,yp2])
    #获取矩形框交集对应的左上角和右下角坐标
    xmin = np.max([xp_1,xg1])
    ymin = np.max([yp_1,yg1])
    xmax = np.min([xp_2,xg2])
    ymax = np.min([yp_2,yg2])
    #计算两个矩形的面积
    area1 = (xp_2-xp_1)*(yp_2-yp_1)
    area2 = (xg2-xg1)*(yg2-yg1)
    #计算交集面积,注意无交集的情况
    inter_area = (np.max([0,xmax-xmin]))*(np.max([0,ymax-ymin]))
    #计算交并比
    u = area1+area2-inter_area
    iou = inter_area/u
    #闭包区域:同时包含了预测框和真实框的最小框的面积
    w = (np.max(yp_2,yg2)-np.min(yp_1,yg1))
    h = (np.max(yp_2,yg2)-np.min(yp_1,yg1))
    area = w*h
    #闭包区域中不属于两个框的区域占闭包区域的比重
    giou = iou-(area-u)/area
    
    return giou 

4.DIOU(Distance-IoU

                                                               目标检测中回归损失函数_第4张图片

其中, b,b^{gt} 分别代表了预测框和真实框的中心点,且 \rho代表的是计算两个中心点间的欧式距离。c 代表的是能够同时包含预测框和真实框的最小闭包区域的对角线距离。

优点:

DIoU要比GIou更加符合目标框回归的机制,将目标与anchor之间的距离,重叠率以及尺度都考虑进去,使得目标框回归变得更加稳定,不会像IoU和GIoU一样出现训练过程中发散等问题。

  • 与GIoU loss类似,DIoU loss在与目标框不重叠时,仍然可以为边界框提供移动方向。

  • DIoU loss可以直接最小化两个目标框的距离,因此比GIoU loss收敛快得多。

  • 对于包含两个框在水平方向和垂直方向上这种情况,DIoU损失可以使回归非常快,而GIoU损失几乎退化为IoU损失。

  • DIoU还可以替换普通的IoU评价策略,应用于NMS中,使得NMS得到的结果更加合理和有效。

def Diou(bboxes1, bboxes2):
    rows = bboxes1.shape[0]
    cols = bboxes2.shape[0]
    dious = torch.zeros((rows, cols))
    if rows * cols == 0:#
        return dious
    exchange = False
    if bboxes1.shape[0] > bboxes2.shape[0]:
        bboxes1, bboxes2 = bboxes2, bboxes1
        dious = torch.zeros((cols, rows))
        exchange = True
    # #xmin,ymin,xmax,ymax->[:,0],[:,1],[:,2],[:,3]
    w1 = bboxes1[:, 2] - bboxes1[:, 0]
    h1 = bboxes1[:, 3] - bboxes1[:, 1]
    w2 = bboxes2[:, 2] - bboxes2[:, 0]
    h2 = bboxes2[:, 3] - bboxes2[:, 1]

    area1 = w1 * h1
    area2 = w2 * h2

    center_x1 = (bboxes1[:, 2] + bboxes1[:, 0]) / 2 
    center_y1 = (bboxes1[:, 3] + bboxes1[:, 1]) / 2 
    center_x2 = (bboxes2[:, 2] + bboxes2[:, 0]) / 2
    center_y2 = (bboxes2[:, 3] + bboxes2[:, 1]) / 2

    inter_max_xy = torch.min(bboxes1[:, 2:],bboxes2[:, 2:])
    inter_min_xy = torch.max(bboxes1[:, :2],bboxes2[:, :2])
    out_max_xy = torch.max(bboxes1[:, 2:],bboxes2[:, 2:])
    out_min_xy = torch.min(bboxes1[:, :2],bboxes2[:, :2])

    inter = torch.clamp((inter_max_xy - inter_min_xy), min=0)
    inter_area = inter[:, 0] * inter[:, 1]
    inter_diag = (center_x2 - center_x1)**2 + (center_y2 - center_y1)**2
    outer = torch.clamp((out_max_xy - out_min_xy), min=0)
    outer_diag = (outer[:, 0] ** 2) + (outer[:, 1] ** 2)
    union = area1+area2-inter_area
    dious = inter_area / union - (inter_diag) / outer_diag
    dious = torch.clamp(dious,min=-1.0,max = 1.0)
    if exchange:
        dious = dious.T
    return dious

5.GIOU(Complete-IoU)

目标检测中回归损失函数_第5张图片

def bbox_overlaps_ciou(bboxes1, bboxes2):
    rows = bboxes1.shape[0]
    cols = bboxes2.shape[0]
    cious = torch.zeros((rows, cols))
    if rows * cols == 0:
        return cious
    exchange = False
    if bboxes1.shape[0] > bboxes2.shape[0]:
        bboxes1, bboxes2 = bboxes2, bboxes1
        cious = torch.zeros((cols, rows))
        exchange = True

    w1 = bboxes1[:, 2] - bboxes1[:, 0]
    h1 = bboxes1[:, 3] - bboxes1[:, 1]
    w2 = bboxes2[:, 2] - bboxes2[:, 0]
    h2 = bboxes2[:, 3] - bboxes2[:, 1]

    area1 = w1 * h1
    area2 = w2 * h2

    center_x1 = (bboxes1[:, 2] + bboxes1[:, 0]) / 2
    center_y1 = (bboxes1[:, 3] + bboxes1[:, 1]) / 2
    center_x2 = (bboxes2[:, 2] + bboxes2[:, 0]) / 2
    center_y2 = (bboxes2[:, 3] + bboxes2[:, 1]) / 2

    inter_max_xy = torch.min(bboxes1[:, 2:],bboxes2[:, 2:])
    inter_min_xy = torch.max(bboxes1[:, :2],bboxes2[:, :2])
    out_max_xy = torch.max(bboxes1[:, 2:],bboxes2[:, 2:])
    out_min_xy = torch.min(bboxes1[:, :2],bboxes2[:, :2])

    inter = torch.clamp((inter_max_xy - inter_min_xy), min=0)
    inter_area = inter[:, 0] * inter[:, 1]
    inter_diag = (center_x2 - center_x1)**2 + (center_y2 - center_y1)**2
    outer = torch.clamp((out_max_xy - out_min_xy), min=0)
    outer_diag = (outer[:, 0] ** 2) + (outer[:, 1] ** 2)
    union = area1+area2-inter_area
    u = (inter_diag) / outer_diag
    iou = inter_area / union
    with torch.no_grad():
        arctan = torch.atan(w2 / h2) - torch.atan(w1 / h1)
        v = (4 / (math.pi ** 2)) * torch.pow((torch.atan(w2 / h2) - torch.atan(w1 / h1)), 2)
        S = 1 - iou
        alpha = v / (S + v)
        w_temp = 2 * w1
    ar = (8 / (math.pi ** 2)) * arctan * ((w1 - w_temp) * h1)
    cious = iou - (u + alpha * ar)
    cious = torch.clamp(cious,min=-1.0,max = 1.0)
    if exchange:
        cious = cious.T
    return cious

我还参考了https://mp.weixin.qq.com/s/I74h-WGsgMGSBytFHLDAAQ,感谢大佬们的分享

你可能感兴趣的:(深度学习)