• yolo增加MPDIoU loss


    边界框回归(Bounding Box Regression,BBR)在目标检测和实例分割中被广泛应用,是定位目标的重要步骤。然而,大多数现有的边界框回归损失函数在预测框与实际标注框具有相同的宽高比但宽度和高度值完全不同的情况下无法进行优化。为了解决上述问题,作者充分探索了水平矩形的几何特征,提出了一种基于最小点距离的边界框相似度比较度量——MPDIoU,其中包含了现有损失函数中考虑的所有相关因素,例如重叠或非重叠面积、中心点距离以及宽度和高度的偏差,同时简化了计算过程。在此基础上,作者提出了一种基于MPDIoU的边界框回归损失函数。

    关于MPDIoU:

    MPDIoU: A Loss for Efficient and Accurate Bounding BoxRegression--论文学习笔记_athrunsunny的博客-CSDN博客

    指标如下

    根据下图的公式,以及原理图进行复现,如有问题还请大佬不吝赐教,相互交流

    在metrics.py中修改bbox_iou函数

    1. def bbox_iou(box1, box2, xywh=True, GIoU=False, DIoU=False, CIoU=False, MDPIoU=False, feat_h=640, feat_w=640,
    2. eps=1e-7):
    3. # Returns Intersection over Union (IoU) of box1(1,4) to box2(n,4)
    4. # Get the coordinates of bounding boxes
    5. if xywh: # transform from xywh to xyxy
    6. (x1, y1, w1, h1), (x2, y2, w2, h2) = box1.chunk(4, 1), box2.chunk(4, 1)
    7. w1_, h1_, w2_, h2_ = w1 / 2, h1 / 2, w2 / 2, h2 / 2
    8. b1_x1, b1_x2, b1_y1, b1_y2 = x1 - w1_, x1 + w1_, y1 - h1_, y1 + h1_
    9. b2_x1, b2_x2, b2_y1, b2_y2 = x2 - w2_, x2 + w2_, y2 - h2_, y2 + h2_
    10. else: # x1, y1, x2, y2 = box1
    11. b1_x1, b1_y1, b1_x2, b1_y2 = box1.chunk(4, 1)
    12. b2_x1, b2_y1, b2_x2, b2_y2 = box2.chunk(4, 1)
    13. w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1
    14. w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1
    15. # Intersection area
    16. inter = (torch.min(b1_x2, b2_x2) - torch.max(b1_x1, b2_x1)).clamp(0) * \
    17. (torch.min(b1_y2, b2_y2) - torch.max(b1_y1, b2_y1)).clamp(0)
    18. # Union Area
    19. union = w1 * h1 + w2 * h2 - inter + eps
    20. # IoU
    21. iou = inter / union
    22. if CIoU or DIoU or GIoU:
    23. cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
    24. ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
    25. if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
    26. c2 = cw ** 2 + ch ** 2 + eps # convex diagonal squared
    27. rho2 = ((b2_x1 + b2_x2 - b1_x1 - b1_x2) ** 2 + (b2_y1 + b2_y2 - b1_y1 - b1_y2) ** 2) / 4 # center dist ** 2
    28. if CIoU: # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
    29. v = (4 / math.pi ** 2) * torch.pow(torch.atan(w2 / (h2 + eps)) - torch.atan(w1 / (h1 + eps)), 2)
    30. with torch.no_grad():
    31. alpha = v / (v - iou + (1 + eps))
    32. return iou - (rho2 / c2 + v * alpha) # CIoU
    33. return iou - rho2 / c2 # DIoU
    34. c_area = cw * ch + eps # convex area
    35. return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf
    36. elif MDPIoU:
    37. d1 = (b2_x1 - b1_x1) ** 2 + (b2_y1 - b1_y1) ** 2
    38. d2 = (b2_x2 - b1_x2) ** 2 + (b2_y2 - b1_y2) ** 2
    39. mpdiou_hw_pow = feat_h ** 2 + feat_w ** 2
    40. return iou - d1 / mpdiou_hw_pow - d2 / mpdiou_hw_pow # MPDIoU
    41. return iou # IoU

     在loss.py中修改iou的计算方式

    iou = bbox_iou(pbox, tbox[i], MDPIoU=True, feat_h=tobj.size()[2], feat_w=tobj.size()[3]).squeeze()
  • 相关阅读:
    【移植代码】matlab.engine报错、numpy+mkl安装、Qt platform plugin报错总结
    【Proteus仿真】【STM32单片机】水质监测报警系统设计
    数组 冒泡排序
    设计模式——3. 抽象工厂模式
    Scss 基础语法
    Robust Lane Detection from Continuous Driving
    雷达SAR成像仿真的应用(Matlab代码实现)
    PHP 给数组增加数据(array_unshift、array_push、array_splice、array_pad)
    【无标题】20221130 studynote1
    数据库分析与优化
  • 原文地址:https://blog.csdn.net/athrunsunny/article/details/132796892