JAVA损失函数:IoU-GIoU-DIoU-CIoU计算复现
前言
损失函数常见于目标检测任务中,其解决问题的效率十分依赖定义的损失函数。关于目标检测损失函数的定义基本可以认定为依赖于边界框回归指标的聚合,例如预测框和真实框(即 GIoU、CIoU、DIoU 等)的距离、重叠区域和纵横比。
在本文中我们着重点将放在代码实现以及结果展示,关于原理部分大家可自行查阅论文以及相关的论文讲解,我在这里就不过多的炒冷饭了,望海涵!
计算实现
IoU家族代码实现
关于IoU、GIoU、DIoU和CIoU的代码实现摘录于互联网。结合计算部分的代码采用如图(w=h=300,c=3)进行实验:我们在标注软件上标定企鹅的鼻子部分,标签文件存储为Voc格式,查阅xml文件得到企鹅鼻子(目标位置)的[xmin,ymin,xmax,yxmax]=[105, 100, 195, 133],易得标注框的 w= 90,h=33。
为了计算的便捷性以及iou贴合度,我们将设定预测框的大小等同于真实目标框,也即 W真 = W 预 & H真 = H预。采用滑动框的方式在图像上进行滑动计算(STEP=1),动图如下:绿色框为目标位置,紫色框为滑动框也就是预测框。
计算结果
计算每一步得到的“iou”值得到如下图,其中在计算iou时出现了负数,我分别将负数未置零以及负数置零绘制了一遍供大家参考。 图像排列顺序为:左上=IOU 右上=GIOU 左下=DIOU 右下=CIOU
未置零图
置零图
IOU代码
import math
from mpmath import eps
from numpy import where, arcsin
def euclidean_distance(p1, p2):
"""计算两个点的欧式距离"""
x1, y1 = p1
x2, y2 = p2
return math.sqrt((x2 - x1) ** 2 (y2 - y1) ** 2)
class BBox(object):
def __init__(self, x, y, r, b):
self.x, self.y, self.r, self.b = x, y, r, b
def __xor__(self, other):
"""计算box和other的IoU"""
cross = self & other
union = self | other
return cross / (union 1e-6)
def __or__(self, other):
""" 计算box和other的并集"""
cross = self & other
return self.area other.area - cross
def __and__(self, other):
"""计算box和other的交集"""
xmax = min(self.r, other.r)
ymax = min(self.b, other.b)
xmin = max(self.x, other.x)
ymin = max(self.y, other.y)
return BBox(xmin, ymin, xmax, ymax).area
def boundof(self, other):
"""计算box和other的边缘外包框,使得2个box都在框内的最小矩形"""
xmin = min(self.x, other.x)
ymin = min(self.y, other.y)
xmax = max(self.r, other.r)
ymax = max(self.b, other.b)
return BBox(xmin, ymin, xmax, ymax)
def center_distance(self, other):
"""计算两个box的中心点距离"""
return euclidean_distance(self.center, other.center)
def bound_diagonal_distance(self, other):
"""计算两个box的bound的对角线距离"""
bound = self.boundof(other)
return euclidean_distance((bound.x, bound.y), (bound.r, bound.b))
@property
def center(self):
return (self.x self.r) / 2, (self.y self.b) / 2
@property
def area(self):
return self.width * self.height
@property
def width(self):
# todo 如果不考虑右侧的一个像素 返回 self.r - self.x
return self.r - self.x 1
@property
def height(self):
# todo 如果不考虑下侧的一个像素 返回 self.b - self.y
return self.b - self.y 1
def __repr__(self):
return f"{self.x}, {self.y}, {self.r}, {self.b}"
def IoU(box1: BBox, box2: BBox):
return box1 ^ box2
def GIoU(box1: BBox, box2: BBox):
bound_area = box1.boundof(box2).area
union_area = box1 | box2
return IoU(box1, box2) - (bound_area - union_area) / bound_area
def DIoU(box1: BBox, box2: BBox):
d = box1.center_distance(box2)
c = box1.bound_diagonal_distance(box2)
return IoU(box1, box2) - d ** 2 / c ** 2
def CIoU(box1: BBox, box2: BBox):
diou = DIoU(box1, box2)
v = 4 / (math.pi ** 2) * (math.atan(box1.width / box1.height) - math.atan(box2.width / box2.height)) ** 2
iou = IoU(box1, box2)
alpha = v / (1 - iou v)
return diou - alpha * v
拓展
大家可以通过本文的计算方法结合自己的项目中IOU计算方式进行替换更改或结合项目做适合自己项目的IOU。本人能力有限,路过的各位大神若发现纰漏的地方还望指教一二!感谢!希望本文能够帮助到大家。
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /boutique/detail/tanfbegg
系列文章
更多
同类精品
更多
-
photoshop保存的图片太大微信发不了怎么办
PHP中文网 06-15 -
Android 11 保存文件到外部存储,并分享文件
Luke 10-12 -
word里面弄一个表格后上面的标题会跑到下面怎么办
PHP中文网 06-20 -
《学习通》视频自动暂停处理方法
HelloWorld317 07-05 -
photoshop扩展功能面板显示灰色怎么办
PHP中文网 06-14 -
微信公众号没有声音提示怎么办
PHP中文网 03-31 -
怎样阻止微信小程序自动打开
PHP中文网 06-13 -
excel下划线不显示怎么办
PHP中文网 06-23 -
excel打印预览压线压字怎么办
PHP中文网 06-22 -
photoshop蒙版画笔没反应怎么办
PHP中文网 06-24