YOLOv5更改损失函数

不废话,上干货!


第一步:在metric.py里添加EIOU代码

#-------------------------------------------------Eiou start-------------------------------------------
# 计算两个框的特定IOU
# def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, EIoU=False, eps=1e-7):
#     # Returns the IoU of box1 to box2. box1 is 4, box2 is nx4
#     # 这里取转置,为了后续方便每个维度(坐标)之间的计算
#     box2 = box2.T
#
#     # Get the coordinates of bounding boxes
#     if x1y1x2y2:  # x1, y1, x2, y2 = box1
#         b1_x1, b1_y1, b1_x2, b1_y2 = box1[0], box1[1], box1[2], box1[3]
#         b2_x1, b2_y1, b2_x2, b2_y2 = box2[0], box2[1], box2[2], box2[3]
#     else:  # transform from xywh to xyxy 默认执行这里
#         b1_x1, b1_x2 = box1[0] - box1[2] / 2, box1[0] + box1[2] / 2
#         b1_y1, b1_y2 = box1[1] - box1[3] / 2, box1[1] + box1[3] / 2
#         b2_x1, b2_x2 = box2[0] - box2[2] / 2, box2[0] + box2[2] / 2
#         b2_y1, b2_y2 = box2[1] - box2[3] / 2, box2[1] + box2[3] / 2
#
#     # 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
#     w1, h1 = b1_x2 - b1_x1, b1_y2 - b1_y1 + eps
#     w2, h2 = b2_x2 - b2_x1, b2_y2 - b2_y1 + eps
#     union = w1 * h1 + w2 * h2 - inter + eps
#
#     iou = inter / union
#     # 目标框IOU损失函数的计算
#     if CIoU or DIoU or GIoU or EIoU:
#         # 两个框的最小闭包区域的width
#         cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1)  # convex (smallest enclosing box) width
#         # 两个框的最小闭包区域的height
#         ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1)  # convex height
#
#         if CIoU or DIoU or EIoU:  # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
#             # 最小外接矩形 对角线的长度平方
#             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 distance squared
#             if DIoU:
#                 return iou - rho2 / c2  # DIoU
#
#             # CIoU 比DIoU多了限制长宽比的因素:v * alpha
#             elif CIoU:  # https://github.com/Zzh-tju/DIoU-SSD-pytorch/blob/master/utils/box/box_utils.py#L47
#                 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 iou - (rho2 / c2 + v * alpha)
#
#             # EIoU 在CIoU的基础上将纵横比的损失项拆分成预测的宽高分别与最小外接框宽高的差值 加速了收敛提高了回归精度
#             elif EIoU:
#                 rho_w2 = ((b2_x2 - b2_x1) - (b1_x2 - b1_x1)) ** 2
#                 rho_h2 = ((b2_y2 - b2_y1) - (b1_y2 - b1_y1)) ** 2
#                 cw2 = cw ** 2 + eps
#                 ch2 = ch ** 2 + eps
#                 return iou - (rho2 / c2 + rho_w2 / cw2 + rho_h2 / ch2)
#
#         # GIoU https://arxiv.org/pdf/1902.09630.pdf
#         c_area = cw * ch + eps  # convex area
#         return iou - (c_area - union) / c_area
#     return iou  # IoU
#-------------------------------------------------Eiou end-------------------------------------------

第二步:在loss.py里将这段代码改成那段代码

#--------------------------原来的计算iou代码--------------------------------------------------
                iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)  # iou(prediction, target)
                lbox += (1.0 - iou).mean()  # iou loss

改成

iou = bbox_iou(pbox, tbox[i], EIoU=True)                 #EOIOU
if type(iou) is tuple:
   lbox += (iou[1].detach().squeeze() * (1 - iou[0].squeeze())).mean()
   iou = iou[0].squeeze()
else:
   lbox += (1.0 - iou.squeeze()).mean()  # iou loss
   iou = iou.squeeze()

第二步:运行train.py,比较算法更改前后的检测精度。
over!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
根据引用和引用的内容,可以了解到在YOLOv5目标检测算法中,可以通过修改损失函数来进行改进。其中,使用OTA(Online Target-aware)损失函数来替换原有的损失函数。 OTA损失函数的改进主要包括以下几个步骤: 1. 修改loss.py文件:需要在该文件中对损失函数进行修改,将原有的损失函数替换为OTA损失函数。 2. 修改train.py和val.py文件:在这两个文件中,需要对compute_loss函数进行修改,以适应新的OTA损失函数的计算。 通过这样的方式,可以将YOLOv5目标检测算法中的损失函数改为OTA损失函数,从而实现更好的检测效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* *3* [[YOLOv7/YOLOv5系列算法改进NO.7]损失函数改进](https://blog.csdn.net/m0_70388905/article/details/125419887)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* [目标检测改进系列1:yolo v5网络中OTA损失函数替换](https://blog.csdn.net/qq_45919032/article/details/129500750)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

不说废话的楼主

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值