开始讲解之前推荐一下我的专栏,本专栏的内容支持(分类、检测、分割、追踪、关键点检测),专栏目前为限时折扣,欢迎大家订阅本专栏,本专栏每周更新5-7篇最新机制,更有包含我所有改进的文件和交流群提供给大家,本人定期在群内分享发表论文方法和经验。
一、本文介绍
本文给大家带来的改进内容是将RT-DETR 中的 HGNet 主干网络融合到 YOLOv26 中,用来替换原有 Backbone,进一步提升模型的特征提取能力和检测效果。RT-DETR 是百度提出的一种实时端到端目标检测模型,主打的就是在保证检测精度的同时兼顾推理速度,在实时检测领域有很高的参考价值。其中HGNet是 RT-DETR 中非常重要的主干结构,相比普通卷积主干,它在特征提取时更加注重高效特征表达、多层信息融合和计算效率平衡,能够让模型在不明显增加推理压力的情况下获得更强的图像理解能力。将 HGNet 引入 YOLOv26 后,相当于给模型换了一个更强、更适合实时检测的“特征提取骨架”,可以帮助 YOLOv26 更好地提取目标的边缘、纹理、轮廓以及深层语义信息,尤其适合复杂背景、小目标、多尺度目标和特征不明显的检测场景。这个改进的卖点在于来源模型强、结构新颖、主干替换思路清晰、适合论文改进,而且目前关于 HGNet 的公开讲解相对较少,很多内容需要结合网络结构进行分析,所以本次改进也具备一定的二次整理和创新价值。实际测试中,该主干替换后在数据集上取得了一定的涨点效果,mAP 提高约 0.05,说明 HGNet 对 YOLOv26 的特征提取能力有一定增强作用。本文将从HGNet 的结构特点、主干替换思路、代码添加方法和实验效果几个方面进行讲解,方便大家快速将该网络接入自己的 YOLOv26 模型中,用来丰富论文中的 主干网络改进点、消融实验内容和性能提升分析。
专栏链接:YOLOv26有效涨点专栏包含:Conv、注意力机制、主干/Backbone、损失函数、优化器、后处理等改进机制
目录
一、本文介绍
二、HGNetV2原理讲解
三、HGNetV2的代码
四、手把手教你添加HGNetV2
4. 1 HGNetV2-l的yaml文件(此为对比试验版本)
4.2 HGNetV2-x的yaml文件
五、运行成功记录
5.1 运行记录
5.2 训练代码
六、本文总结
二、HGNetV2原理讲解
本文论文地址:RT-DETR论文地址
本文代码来源:HGNetV2的代码来源
PP-HGNet 骨干网络的整体结构如下:
其中,PP-HGNet是由多个HG-Block组成,HG-Block的细节如下:
上面的图表是PP-HGNet神经网络架构的概览,下面我会对其中的每一个模块进行分析:
1. Stem层:这是网络的初始预处理层,通常包含卷积层,开始从原始输入数据中提取特征。
2. HG(层次图)块:这些块是网络的核心组件,设计用于以层次化的方式处理数据。每个HG块可能处理数据的不同抽象层次,允许网络从低级和高级特征中学习。
3. LDS(可学习的下采样)层:位于HG块之间的这些层可能执行下采样操作,减少特征图的空间维度,减少计算负载并可能增加后续层的感受野。
4. GAP(全局平均池化):在最终分类之前,使用GAP层将特征图的空间维度减少到每个特征图一个向量,有助于提高网络对输入数据空间变换的鲁棒性。
5. 最终的卷积和全连接(FC)层:网络以一系列执行最终分类任务的层结束。这通常涉及一个卷积层(有时称为1x1卷积)来组合特征,然后是将这些特征映射到所需输出类别数量的全连接层。
这种架构的主要思想是利用层次化的方法来提取特征,其中复杂的模式可以在不同的规模和抽象层次上学习,提高网络处理复杂图像数据的能力。
这种分层和高效的处理对于图像分类等复杂任务非常有利,在这些任务中,精确预测至关重要的是在不同规模上识别复杂的模式和特征。图表还显示了HG块的扩展视图,包括多个不同滤波器大小的卷积层,以捕获多样化的特征,然后通过一个元素级相加或连接的操作(由+符号表示)在数据传递到下一层之前。
三、HGNetV2的代码
需要注意的是HGNetV2这个版本的所需组件已经集成在YOLOv8的仓库了,所以我们无需做任何的代码层面的改动,只需要设计yaml文件来配合Neck部分融合特征即可了,但是我还是把代码放在这里,供有兴趣的读者看一下,也和上面的结构进行一个对照。主要的三个结构HGStem,HGBlock,DWConv。
class HGStem(nn.Module): """ StemBlock of PPHGNetV2 with 5 convolutions and one maxpool2d. https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py """ def __init__(self, c1, cm, c2): """Initialize the SPP layer with input/output channels and specified kernel sizes for max pooling.""" super().__init__() self.stem1 = Conv(c1, cm, 3, 2) self.stem2a = Conv(cm, cm // 2, 2, 1, 0) self.stem2b = Conv(cm // 2, cm, 2, 1, 0) self.stem3 = Conv(cm * 2, cm, 3, 2) self.stem4 = Conv(cm, c2, 1, 1) self.pool = nn.MaxPool2d(kernel_size=2, stride=1, padding=0, ceil_mode=True) def forward(self, x): """Forward pass of a PPHGNetV2 backbone layer.""" x = self.stem1(x) x = F.pad(x, [0, 1, 0, 1]) x2 = self.stem2a(x) x2 = F.pad(x2, [0, 1, 0, 1]) x2 = self.stem2b(x2) x1 = self.pool(x) x = torch.cat([x1, x2], dim=1) x = self.stem3(x) x = self.stem4(x) return x class HGBlock(nn.Module): """ HG_Block of PPHGNetV2 with 2 convolutions and LightConv. https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py """ def __init__(self, c1, cm, c2, k=3, n=6, lightconv=False, shortcut=False, act=True): """Initializes a CSP Bottleneck with 1 convolution using specified input and output channels.""" super().__init__() block = LightConv if lightconv else Conv self.m = nn.ModuleList(block(c1 if i == 0 else cm, cm, k=k, act=act) for i in range(n)) self.sc = Conv(c1 + n * cm, c2 // 2, 1, 1, act=act) # squeeze conv self.ec = Conv(c2 // 2, c2, 1, 1, act=act) # excitation conv self.add = shortcut and c1 == c2 def forward(self, x): """Forward pass of a PPHGNetV2 backbone layer.""" y = [x] y.extend(m(y[-1]) for m in self.m) y = self.ec(self.sc(torch.cat(y, 1))) return y + x if self.add else y def autopad(k, p=None, d=1): # kernel, padding, dilation """Pad to 'same' shape outputs.""" if d > 1: k = d * (k - 1) + 1 if isinstance(k, int) else [d * (x - 1) + 1 for x in k] # actual kernel-size if p is None: p = k // 2 if isinstance(k, int) else [x // 2 for x in k] # auto-pad return p class Conv(nn.Module): """Standard convolution with args(ch_in, ch_out, kernel, stride, padding, groups, dilation, activation).""" default_act = nn.SiLU() # default activation def __init__(self, c1, c2, k=1, s=1, p=None, g=1, d=1, act=True): """Initialize Conv layer with given arguments including activation.""" super().__init__() self.conv = nn.Conv2d(c1, c2, k, s, autopad(k, p, d), groups=g, dilation=d, bias=False) self.bn = nn.BatchNorm2d(c2) self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity() def forward(self, x): """Apply convolution, batch normalization and activation to input tensor.""" return self.act(self.bn(self.conv(x))) def forward_fuse(self, x): """Perform transposed convolution of 2D data.""" return self.act(self.conv(x)) class DWConv(Conv): """Depth-wise convolution.""" def __init__(self, c1, c2, k=1, s=1, d=1, act=True): # ch_in, ch_out, kernel, stride, dilation, activation """Initialize Depth-wise convolution with given parameters.""" super().__init__(c1, c2, k, s, g=math.gcd(c1, c2), d=d, act=act)四、手把手教你添加HGNetV2
我们首先需要找到'ultralytics/nn/tasks.py'文件然后找到'def parse_model(d, ch, verbose=True): # model_dict, input_channels(3)'
我们按照下面第二张图片进行修改,图1为初始样子。
复制此处的代码按照下面的图片进行添加即可,不要自己打!
cm = make_divisible(min(cm, max_channels) * width, 8) c2 = make_divisible(min(c2, max_channels) * width, 8) n = n_ = max(round(n * depth), 1) if n > 1 else n # depth gain五、yaml文件
5.1 HGNetV2-l的yaml文件(此为对比试验版本)
此版本的信息为:YOLO26-Backbone-HGNetV2-l summary: 296 layers, 2,005,356 parameters, 2,005,356 gradients, 4.9 GFLOPs
# 需要注意模型轻量化了往往代表学习能力变弱相同的数据集需要训练的轮次(拟合)的次数需要变多.
# Ultralytics YOLO 🚀, AGPL-3.0 license # YOLO11 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect # Parameters nc: 80 # number of classes end2end: True # whether to use end-to-end mode reg_max: 1 # DFL bins scales: # model compound scaling constants, i.e. 'model=yolo26n.yaml' will call yolo26.yaml with scale 'n' # [depth, width, max_channels] n: [0.50, 0.25, 1024] # summary: 260 layers, 2,572,280 parameters, 2,572,280 gradients, 6.1 GFLOPs s: [0.50, 0.50, 1024] # summary: 260 layers, 10,009,784 parameters, 10,009,784 gradients, 22.8 GFLOPs m: [0.50, 1.00, 512] # summary: 280 layers, 21,896,248 parameters, 21,896,248 gradients, 75.4 GFLOPs l: [1.00, 1.00, 512] # summary: 392 layers, 26,299,704 parameters, 26,299,704 gradients, 93.8 GFLOPs x: [1.00, 1.50, 512] # summary: 392 layers, 58,993,368 parameters, 58,993,368 gradients, 209.5 GFLOPs # YOLO26n backbone backbone: # [from, repeats, module, args] - [-1, 1, HGStem, [32, 48]] # 0-P2/4 - [-1, 6, HGBlock, [48, 128, 3]] # stage 1 - [-1, 1, DWConv, [128, 3, 2, 1, False]] # 2-P3/8 - [-1, 6, HGBlock, [96, 512, 3]] # stage 2 - [-1, 1, DWConv, [512, 3, 2, 1, False]] # 4-P3/16 - [-1, 6, HGBlock, [192, 1024, 5, True, False]] # cm, c2, k, light, shortcut - [-1, 6, HGBlock, [192, 1024, 5, True, True]] - [-1, 6, HGBlock, [192, 1024, 5, True, True]] # stage 3 - [-1, 1, DWConv, [1024, 3, 2, 1, False]] # 8-P4/32 - [-1, 6, HGBlock, [384, 2048, 5, True, False]] # stage 4 - [-1, 1, SPPF, [1024, 5, 3, True]] # 10 - [-1, 2, C2PSA, [1024]] # 11 # YOLO26n head head: - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 7], 1, Concat, [1]] # cat backbone P4 - [-1, 2, C3k2, [512, True]] # 14 - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 3], 1, Concat, [1]] # cat backbone P3 - [-1, 2, C3k2, [256, True]] # 17 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 14], 1, Concat, [1]] # cat head P4 - [-1, 2, C3k2, [512, True]] # 20 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 11], 1, Concat, [1]] # cat head P5 - [-1, 2, C3k2, [1024, True, 0.5, True]] # 23 (P5/32-large) - [[17, 20, 23], 1, Detect, [nc]] # Detect(P3, P4, P5)5.2 HGNetV2-x的yaml文件
此版本的信息为:YOLO26-Backbone-HGNetV2-x summary: 346 layers, 2,505,324 parameters, 2,505,324 gradients, 6.8 GFLOPs
# 需要注意模型轻量化了往往代表学习能力变弱相同的数据集需要训练的轮次(拟合)的次数需要变多.
# Ultralytics YOLO 🚀, AGPL-3.0 license # YOLO11 object detection model with P3-P5 outputs. For Usage examples see https://docs.ultralytics.com/tasks/detect # Parameters nc: 80 # number of classes end2end: True # whether to use end-to-end mode reg_max: 1 # DFL bins scales: # model compound scaling constants, i.e. 'model=yolo26n.yaml' will call yolo26.yaml with scale 'n' # [depth, width, max_channels] n: [0.50, 0.25, 1024] # summary: 260 layers, 2,572,280 parameters, 2,572,280 gradients, 6.1 GFLOPs s: [0.50, 0.50, 1024] # summary: 260 layers, 10,009,784 parameters, 10,009,784 gradients, 22.8 GFLOPs m: [0.50, 1.00, 512] # summary: 280 layers, 21,896,248 parameters, 21,896,248 gradients, 75.4 GFLOPs l: [1.00, 1.00, 512] # summary: 392 layers, 26,299,704 parameters, 26,299,704 gradients, 93.8 GFLOPs x: [1.00, 1.50, 512] # summary: 392 layers, 58,993,368 parameters, 58,993,368 gradients, 209.5 GFLOPs # YOLO26n backbone backbone: # [from, repeats, module, args] - [-1, 1, HGStem, [32, 64]] # 0-P2/4 - [-1, 6, HGBlock, [64, 128, 3]] # stage 1 - [-1, 1, DWConv, [128, 3, 2, 1, False]] # 2-P3/8 - [-1, 6, HGBlock, [128, 512, 3]] - [-1, 6, HGBlock, [128, 512, 3, False, True]] # 4-stage 2 - [-1, 1, DWConv, [512, 3, 2, 1, False]] # 5-P3/16 - [-1, 6, HGBlock, [256, 1024, 5, True, False]] # cm, c2, k, light, shortcut - [-1, 6, HGBlock, [256, 1024, 5, True, True]] - [-1, 6, HGBlock, [256, 1024, 5, True, True]] - [-1, 6, HGBlock, [256, 1024, 5, True, True]] - [-1, 6, HGBlock, [256, 1024, 5, True, True]] # 10-stage 3 - [-1, 1, DWConv, [1024, 3, 2, 1, False]] # 11-P4/32 - [-1, 6, HGBlock, [512, 2048, 5, True, False]] - [-1, 6, HGBlock, [512, 2048, 5, True, True]] # 13-stage 4 - [-1, 1, SPPF, [1024, 5, 3, True]] # 14 - [-1, 2, C2PSA, [1024]] # 15 # YOLO26n head head: - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 10], 1, Concat, [1]] # cat backbone P4 - [-1, 2, C3k2, [512, True]] # 18 - [-1, 1, nn.Upsample, [None, 2, "nearest"]] - [[-1, 4], 1, Concat, [1]] # cat backbone P3 - [-1, 2, C3k2, [256, True]] # 21 (P3/8-small) - [-1, 1, Conv, [256, 3, 2]] - [[-1, 18], 1, Concat, [1]] # cat head P4 - [-1, 2, C3k2, [512, True]] # 24 (P4/16-medium) - [-1, 1, Conv, [512, 3, 2]] - [[-1, 15], 1, Concat, [1]] # cat head P5 - [-1, 2, C3k2, [1024, True, 0.5, True]] # 27 (P5/32-large) - [[21, 24, 27], 1, Detect, [nc]] # Detect(P3, P4, P5)六、运行成功记录
6.1 运行记录
6.2 训练代码
import warnings warnings.filterwarnings('ignore') from ultralytics import YOLO if __name__ == '__main__': model = YOLO('模型配置文件地址,也就是5.1你保存到本地文件的地址') # 如何切换模型版本, 上面的ymal文件可以改为 yolo26s.yaml就是使用的26s, # 类似某个改进的yaml文件名称为yolo26-XXX.yaml那么如果想使用其它版本就把上面的名称改为yolo26l-XXX.yaml即可(改的是上面YOLO中间的名字不是配置文件的)! # model.load('yolo26n.pt') # 是否加载预训练权重,科研不建议大家加载否则很难提升精度 model.train( data=r"数据集文件地址", # 如果大家任务是其它的'ultralytics/cfg/default.yaml'找到这里修改task可以改成detect, segment, classify, pose cache=False, imgsz=640, epochs=20, single_cls=False, # 是否是单类别检测 batch=16, close_mosaic=0, workers=0, device='0', optimizer='MuSGD', # using SGD/MuSGD # resume=, # 这里是填写last.pt地址 amp=True, # 如果出现训练损失为Nan可以关闭amp project='runs/train', name='exp', )七、本文总结
到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv26改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~
专栏链接:YOLOv26有效涨点专栏包含:Conv、注意力机制、主干/Backbone、损失函数、优化器、后处理等改进机制