YOLOv5|YOLOv7|YOLOv8改各种IoU损失函数:YOLOv8涨点Trick,改进添加SIoU损失函数、EIoU损失函数、GIoU损失函数、α-IoU损失函数
函数 添加 各种 改进 YOLOv5 损失 YOLOv7 yoloV8
2023-09-14 09:14:46 时间
-
💡该教程为改进入门指南,属于
《芒果书》
📚系列,包含大量的原创首发改进方式, 所有文章都是全网首发原创改进内容🚀 -
💡本篇文章 基于 YOLOv5、YOLOv7
芒果
改进YOLO系列:YOLOv7改进IoU损失函数:YOLOv7涨点Trick,改进添加SIoU损失函数、EIoU损失函数、GIoU损失函数、α-IoU损失函数、打造全新YOLOv7检测器
。 -
重点
:🔥🔥🔥有不少同学已经反应有效涨点!!!
🌟其他改进内容:CSDN原创YOLO进阶目录 | 《芒果改进YOLO进阶指南》推荐! -
最全《芒果书📚》改进目录:YOLOv5改进、YOLOv7改进(芒果书系列)目录一览|原创YOLO改进模型全系列目录 | 人工智能专家老师联袂推荐
解析|YOLOv7网络模型源代码训练推理教程解析
总结|YOLO系列期刊创新点总结
核心代码改进
以下SIoU、EIoU、GIoU、α-IoU改进,代码均在博主开源的YOLOAir
中有写
改进核心代码
在YOLOv5中,使用以下函数替换原有的utils/metrics.py
文件中的bbox_iou
函数
如果在YOLOv7中,使用以下函数替换原有的utils/general.py
文件中的bbox_iou
函数
def bbox_iou(box1, box2, x1y1x2y2=True, GIoU=False, DIoU=False, CIoU=False, EIoU=False, SIoU=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
if CIoU or DIoU or GIoU or EIoU or SIoU:
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
if CIoU or DIoU or EIoU or SIoU: # 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: #DIoU
return iou - rho2 / c2 # DIoU
elif CIoU: #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) # CIoU
elif SIoU:# SIoU
s_cw = (b2_x1 + b2_x2 - b1_x1 - b1_x2) * 0.5
s_ch = (b2_y1 + b2_y2 - b1_y1 - b1_y2) * 0.5
sigma = torch.pow(s_cw ** 2 + s_ch ** 2, 0.5)
sin_alpha_1 = torch.abs(s_cw) / sigma
sin_alpha_2 = torch.abs(s_ch) / sigma
threshold = pow(2, 0.5) / 2
sin_alpha = torch.where(sin_alpha_1 > threshold, sin_alpha_2, sin_alpha_1)
angle_cost = torch.cos(torch.arcsin(sin_alpha) * 2 - math.pi / 2)
rho_x = (s_cw / cw) ** 2
rho_y = (s_ch / ch) ** 2
gamma = angle_cost - 2
distance_cost = 2 - torch.exp(gamma * rho_x) - torch.exp(gamma * rho_y)
omiga_w = torch.abs(w1 - w2) / torch.max(w1, w2)
omiga_h = torch.abs(h1 - h2) / torch.max(h1, h2)
shape_cost = torch.pow(1 - torch.exp(-1 * omiga_w), 4) + torch.pow(1 - torch.exp(-1 * omiga_h), 4)
return iou - 0.5 * (distance_cost + shape_cost)
else:# EIoU
w_dis=torch.pow(b1_x2-b1_x1-b2_x2+b2_x1, 2)
h_dis=torch.pow(b1_y2-b1_y1-b2_y2+b2_y1, 2)
cw2=torch.pow(cw , 2)+eps
ch2=torch.pow(ch , 2)+eps
return iou-(rho2/c2+w_dis/cw2+h_dis/ch2)
else:
c_area = cw * ch + eps # convex area
return iou - (c_area - union) / c_area # GIoU https://arxiv.org/pdf/1902.09630.pdf
return iou # IoU
改进α-IoU核心代码
def bbox_alpha_iou(box1, box2, x1y1x2y2=False, GIoU=False, DIoU=False, CIoU=False, alpha=2, eps=1e-9):
# Returns tsqrt_he 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
# change iou into pow(iou+eps)
# iou = inter / union
iou = torch.pow(inter/union + eps, alpha)
# beta = 2 * alpha
if GIoU or DIoU or CIoU:
cw = torch.max(b1_x2, b2_x2) - torch.min(b1_x1, b2_x1) # convex (smallest enclosing box) width
ch = torch.max(b1_y2, b2_y2) - torch.min(b1_y1, b2_y1) # convex height
if CIoU or DIoU: # Distance or Complete IoU https://arxiv.org/abs/1911.08287v1
c2 = (cw ** 2 + ch ** 2) ** alpha + eps # convex diagonal
rho_x = torch.abs(b2_x1 + b2_x2 - b1_x1 - b1_x2)
rho_y = torch.abs(b2_y1 + b2_y2 - b1_y1 - b1_y2)
rho2 = ((rho_x ** 2 + rho_y ** 2) / 4) ** alpha # center distance
if DIoU:
return iou - rho2 / c2 # DIoU
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_ciou = v / ((1 + eps) - inter / union + v)
# return iou - (rho2 / c2 + v * alpha_ciou) # CIoU
return iou - (rho2 / c2 + torch.pow(v * alpha_ciou + eps, alpha)) # CIoU
else: # GIoU https://arxiv.org/pdf/1902.09630.pdf
# c_area = cw * ch + eps # convex area
# return iou - (c_area - union) / c_area # GIoU
c_area = torch.max(cw * ch + eps, union) # convex area
return iou - torch.pow((c_area - union) / c_area + eps, alpha) # GIoU
else:
return iou # torch.log(iou+eps) or iou
SIoU改进
参考上面的核心代码
将
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)
替换为
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, SIoU=True)
EIoU改进
参考上面的核心代码
将
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)
替换为
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, EIoU=True)
GIoU改进
参考上面的核心代码
将
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)
替换为
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, GIoU=True)
α-IoU改进
参考上面的核心代码
bbox_alpha_iou
将
iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)
替换为
iou = bbox_alpha_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)
以上是yolov5的改进
yolov7 将 tbox[i] 改为 selected_tbox
比如 iou = bbox_iou(pbox.T, tbox[i], x1y1x2y2=False, CIoU=True)
改为iou = bbox_iou(pbox.T, selected_tbox, x1y1x2y2=False, CIoU=True)
代码直接运行
python train.py cfg yolov7.yaml即可
相关文章
- layer执行父窗口ajax方法,layer 弹出层 回调函数调用 弹出层页面 函数
- R自定义构建函数绘制相关性条形图
- 《R构建函数基础篇》计算多样性指数及绘图
- golang实现类似PHP json_encode和json_decode 函数
- Casbin如何添加自定义函数
- 【Flutter】Dart 数据类型 List 集合类型 ( 定义集合 | 初始化 | 泛型用法 | 初始化后添加元素 | 集合生成函数 | 集合遍历 )
- 【Kotlin】apply 内联扩展函数 ( apply 函数原型 | apply 函数示例 | Kotlin 调用 Java API )
- 【Android 逆向】IDA 工具使用 ( 重命名函数 | 添加注释 | 添加标签 / 跳转标签 | 代码跳转前进 / 后退 )
- 【Linux 内核 内存管理】RCU 机制 ③ ( RCU 模式下添加链表项 list_add_rcu 函数 | RCU 模式下删除链表项 list_del_rcu 函数 )
- ORACLE学习笔记-添加更新数据函数篇
- Linux下实现计时功能技巧精选(linux计时函数)
- MySQL中SUM函数的应用(mysqlsumas)
- MySQL中使用Cast函数进行类型转换(mysql中使用cast)
- 使用Oracle中的NVL函数替换NULL值(oracle中nvl函数用法)
- aspform表单验证函数
- oracle存储过程和函数例子
- sqlconvert函数使用小结
- javascript添加和移除函数的通用方法
- 如何写JS数组sort的比较函数
- 为JavaScript添加重载函数的辅助方法
- JS函数实现动态添加CSS样式表文件
- JS自定义功能函数实现动态添加网址参数修改网址参数值