实际目标检测回归任务中的Loss

● Smooth L1 Loss:


● L1、L2、Smooth L1作为目标检测回归Loss的缺点:

1)坐标分别计算:x、y、w、h分别回归,当成4个不同的对象处理。bbox的4个部分应该是作为一个整体讨论,但是被独立看待了。
2)不同的预测bbox具有相同的损失:把x、y、w、h独立看待,4个部分产生不同的loss会回归出不同的预测框,但是如果4个部分的总体loss相同,预测框该如何选取。

针对这些问题,引出了下面的各种IoU Loss。

(关于L1、L2、Smooth L1 Loss不太了解的可以看我的这篇文章)

1. IoU Loss


       L1 、 L2以及Smooth L1 Loss 是将 bbox 四个点分别求 loss 然后相加,并没有考虑坐标之间的相关性, 而评价指标 IoU 具备相关性。上图中的第一行,所有目标的 L1 loss 都一样,但是第三个的 IoU 显然是要大于第一个,并且第三个的检测结果似乎也是好于第一个的,第二行类似,所 有目标的 L1 loss 也都一样,但 IoU 却存在差异。
       基于此 IoU Loss 将 4 个点构成的 bbox 看成一个整体进行回归。

● 计算过程如下图:


● 算法流程如下:

● IoU Loss的优点:

1)它可以反映预测光与真实框的检测效果。
2)具有尺度不变性,也就是对尺度不敏感(scale invariant),满足非负性、同一性、对称性、三角不变性。

● IoU Loss存在的问题:

代码实现:

# IoU Loss
def iou(bboxes1, bboxes2):### rows:rows = bboxes1.shape[0]cols = bboxes2.shape[0]ious = torch.zeros((rows, cols))if rows * cols == 0:return iousexchange = Falseif bboxes1.shape[0] > bboxes2.shape[0]:bboxes1, bboxes2 = bboxes2, bboxes1ious = torch.zeros((cols, rows))exchange = Truearea1 = (bboxes1[:, 2] - bboxes1[:, 0]) * (bboxes1[:, 3] - bboxes1[:, 1])area2 = (bboxes2[:, 2] - bboxes2[:, 0]) * (bboxes2[:, 3] - bboxes2[:, 1])inter_max_xy = torch.min(bboxes1[:, 2:],bboxes2[:, 2:])inter_min_xy = torch.max(bboxes1[:, :2],bboxes2[:, :2])inter = torch.clamp((inter_max_xy - inter_min_xy), min=0)inter_area = inter[:, 0] * inter[:, 1]union = area1+area2-inter_areaious = inter_area / unionious = torch.clamp(ious,min=0,max = 1.0)if exchange:ious = ious.Treturn torch.sum(1-ious)

2. GIoU Loss(G:Generalized)

● GIoU公式:

● GIoU Loss公式:


● 图示及算法流程:



其中:

算法解释:


代码实现:

# GIoU Loss
def giou(bboxes1, bboxes2):rows = bboxes1.shape[0]cols = bboxes2.shape[0]ious = torch.zeros((rows, cols))if rows * cols == 0:return iousexchange = Falseif bboxes1.shape[0] > bboxes2.shape[0]:bboxes1, bboxes2 = bboxes2, bboxes1ious = torch.zeros((cols, rows))exchange = Truearea1 = (bboxes1[:, 2] - bboxes1[:, 0]) * (bboxes1[:, 3] - bboxes1[:, 1])area2 = (bboxes2[:, 2] - bboxes2[:, 0]) * (bboxes2[:, 3] - bboxes2[:, 1])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]outer = torch.clamp((out_max_xy - out_min_xy), min=0)outer_area = outer[:, 0] * outer[:, 1]union = area1+area2-inter_areaclosure = outer_areaious = inter_area / union - (closure - union) / closureious = torch.clamp(ious,min=-1.0,max = 1.0)if exchange:ious = ious.Treturn torch.sum(1-ious)

3. DIoU Loss(D:Distance)

● DIoU公式:


● DIoU Loss公式:


公式解释:
       其中,ܾ、ܾ݃b、b_gt分别代表了预测框和真实框的中心点,且ρ代表的是计算两个中心点间的 欧式距离,c 代表的是能够同时包含预测框和真实框的最小闭包区域的对角线的距离。

代码实现:

# DIoU Loss
def diou(bboxes1, bboxes2):# this is from official website:# https://github.com/Zzh-tju/CIoU/blob/master/layers/modules/multibox_loss.pybboxes1 = torch.sigmoid(bboxes1)        # make sure the input belongs to [0, 1]bboxes2 = torch.sigmoid(bboxes2)rows = bboxes1.shape[0]cols = bboxes2.shape[0]cious = torch.zeros((rows, cols))if rows * cols == 0:return ciousexchange = Falseif bboxes1.shape[0] > bboxes2.shape[0]:bboxes1, bboxes2 = bboxes2, bboxes1cious = torch.zeros((cols, rows))exchange = Truew1 = torch.exp(bboxes1[:, 2])       # this means this bbox has been encoded by logh1 = torch.exp(bboxes1[:, 3])       # you needn't do this if your bboxes are not encodedw2 = torch.exp(bboxes2[:, 2])h2 = torch.exp(bboxes2[:, 3])area1 = w1 * h1area2 = w2 * h2center_x1 = bboxes1[:, 0]center_y1 = bboxes1[:, 1]center_x2 = bboxes2[:, 0]center_y2 = bboxes2[:, 1]inter_l = torch.max(center_x1 - w1 / 2, center_x2 - w2 / 2)inter_r = torch.min(center_x1 + w1 / 2, center_x2 + w2 / 2)inter_t = torch.max(center_y1 - h1 / 2, center_y2 - h2 / 2)inter_b = torch.min(center_y1 + h1 / 2, center_y2 + h2 / 2)inter_area = torch.clamp((inter_r - inter_l),min=0) * torch.clamp((inter_b - inter_t),min=0)c_l = torch.min(center_x1 - w1 / 2, center_x2 - w2 / 2)c_r = torch.max(center_x1 + w1 / 2, center_x2 + w2 / 2)c_t = torch.min(center_y1 - h1 / 2, center_y2 - h2 / 2)c_b = torch.max(center_y1 + h1 / 2, center_y2 + h2 / 2)inter_diag = (center_x2 - center_x1)**2 + (center_y2 - center_y1)**2c_diag = torch.clamp((c_r - c_l), min=0)**2 + torch.clamp((c_b - c_t), min=0)**2union = area1+area2-inter_areau = (inter_diag) / c_diagiou = inter_area / uniondious = iou - udious = torch.clamp(dious, min=-1.0, max=1.0)if exchange:dious = dious.Treturn torch.sum(1 - dious)

4. CIoU Loss

代码实现:

# DIoU Loss
def diou(bboxes1, bboxes2):# this is from official website:# https://github.com/Zzh-tju/CIoU/blob/master/layers/modules/multibox_loss.pybboxes1 = torch.sigmoid(bboxes1)        # make sure the input belongs to [0, 1]bboxes2 = torch.sigmoid(bboxes2)rows = bboxes1.shape[0]cols = bboxes2.shape[0]cious = torch.zeros((rows, cols))if rows * cols == 0:return ciousexchange = Falseif bboxes1.shape[0] > bboxes2.shape[0]:bboxes1, bboxes2 = bboxes2, bboxes1cious = torch.zeros((cols, rows))exchange = Truew1 = torch.exp(bboxes1[:, 2])       # this means this bbox has been encoded by logh1 = torch.exp(bboxes1[:, 3])       # you needn't do this if your bboxes are not encodedw2 = torch.exp(bboxes2[:, 2])h2 = torch.exp(bboxes2[:, 3])area1 = w1 * h1area2 = w2 * h2center_x1 = bboxes1[:, 0]center_y1 = bboxes1[:, 1]center_x2 = bboxes2[:, 0]center_y2 = bboxes2[:, 1]inter_l = torch.max(center_x1 - w1 / 2, center_x2 - w2 / 2)inter_r = torch.min(center_x1 + w1 / 2, center_x2 + w2 / 2)inter_t = torch.max(center_y1 - h1 / 2, center_y2 - h2 / 2)inter_b = torch.min(center_y1 + h1 / 2, center_y2 + h2 / 2)inter_area = torch.clamp((inter_r - inter_l),min=0) * torch.clamp((inter_b - inter_t),min=0)c_l = torch.min(center_x1 - w1 / 2, center_x2 - w2 / 2)c_r = torch.max(center_x1 + w1 / 2, center_x2 + w2 / 2)c_t = torch.min(center_y1 - h1 / 2, center_y2 - h2 / 2)c_b = torch.max(center_y1 + h1 / 2, center_y2 + h2 / 2)inter_diag = (center_x2 - center_x1)**2 + (center_y2 - center_y1)**2c_diag = torch.clamp((c_r - c_l), min=0)**2 + torch.clamp((c_b - c_t), min=0)**2union = area1+area2-inter_areau = (inter_diag) / c_diagiou = inter_area / uniondious = iou - udious = torch.clamp(dious, min=-1.0, max=1.0)if exchange:dious = dious.Treturn torch.sum(1 - dious)

5. 总结

【目标检测】IoU、GIoU、DIoU、CIoU Loss详解及代码实现相关推荐

  1. 目标检测回归损失函数简介:SmoothL1/IoU/GIoU/DIoU/CIoU Loss

    点击上方"小白学视觉",选择加"星标"或"置顶" 重磅干货,第一时间送达 来源:极市平台 目标检测任务的损失函数由Classificitio ...

  2. 目标检测回归损失函数:SmoothL1/IoU/GIoU/DIoU/CIoU Loss

    文章目录 1. Smooth L1 Loss 1.1 假设x为预测框和真实框之间的数值差异,常用的L1和L2 Loss定义为: 1.2 上述的3个损失函数对x的导数分别为: 1.3 实际目标检测框回归 ...

  3. 目标检测模型的评估指标mAP详解(附代码)

    https://zhuanlan.zhihu.com/p/37910324 对于使用机器学习解决的大多数常见问题,通常有多种可用的模型.每个模型都有自己的独特之处,并随因素变化而表现不同. 每个模型在 ...

  4. IoU GIoU DIoU CIoU分析比较

    IoU GIoU DIoU CIoU分析比较 IoU 1. IoU的简介及原理解析 2.IOU的应用有哪些? GIoU 1.Iou的缺陷 2.GIoU的简介及原理解析 3.GIoU的性质 DIoU & ...

  5. IOU .GIOU.DIOU.CIOU

    提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 IOU .GIOU.DIOU.CIOU 一.IOU(Intersection over Union) 二.GIOU(Generaliz ...

  6. 深度学习之目标检测(五)-- RetinaNet网络结构详解

    深度学习之目标检测(五)-- RetinaNet网络结构详解 深度学习之目标检测(五)RetinaNet网络结构详解 1. RetinaNet 1.1 backbone 部分 1.2 预测器部分 1. ...

  7. IoU系列(IoU, GIoU, DIoU, CIoU)

    ​​​​​ 写在前面 一.IoU (Intersection over Union) 1.1 IoU的优点 1.2 作为损失函数会出现的问题(缺点) 二.GIoU (Generalized) 三. D ...

  8. 基于IOU的损失函数合集, IoU, GIoU, DIoU,CIoU, EIoU

      目标检测任务的损失函数一般由 Classificition Loss(分类损失函数)和Bounding Box Regeression Loss(回归损失函数)两部分构成.   Bounding ...

  9. IOU GIOU DIOU CIOU 及代码实现

    总体发展过程: IOU IOU(交并比)顾名思义就是两个框的交集除以他们的并集. IOU Loss:IOU Loss = 1 -IOU(比较常用) IOU 的优点:1.能够很好的反应重合的程度    ...

最新文章

  1. dll的概念、dll导出类(转)
  2. 如何选择真正的万兆防火墙?
  3. css画三角形和提示框
  4. 开源netcore前后端分离,前端服务端渲染方案
  5. Javadoc的Html文件传输chm
  6. 【Clickhouse】Clickhouse 外部存储引擎之 hdfs
  7. W3C发布DRM作为推荐方案
  8. android怎么阿看手机是移动联通还是电信的网络,GSM是联通、移动还是电信的网络?...
  9. MATLAB电路仿真搭建教程
  10. 整理六百篇web前端知识混总
  11. oracle共享池使用率,oracle调优之--共享池使用问题(摘自文平书)
  12. 时序分析 29 - 时序预测 - 格兰杰因果关系(下) python实践2
  13. python画聚类树状图_如何在scipy/matplotlib中绘制和标注层次聚类树状图
  14. XMU 1614 刘备闯三国之三顾茅庐(二) 【逆向思维+二维并查集】
  15. 软件工程与计算:第3章需求分析 测试
  16. 操作系统P/V操作(V操作中的典型理解偏差)
  17. 新疆维吾尔自治区坡度数据
  18. 台式计算机摄像头怎么打开,电脑外接摄像头怎么打开怎么用
  19. Zbrush一些基本操作
  20. 什么软件去视频水印 拍抖音技巧视频教程

热门文章

  1. (8)UART发送verilog与Systemverilog编码
  2. python写入mysql乱码_python MYsql中文乱码
  3. linux dns resolver,如何解决Linux in-kernel dns_resolver问题
  4. Openflow协议规范
  5. STM32--MPU内存保护单元(一)
  6. 【STM32】HAL库 STM32CubeMX教程十四---SPI
  7. mysql多个外键删除设置_Mysql在删除集上使用多个外键创建表
  8. K2评分方法理解实例
  9. 进程通信方法的特点以及使用场景
  10. EmWin 接触---基础函数