一、本文介绍
Hello,大家好,上一篇博客我们讲了利用 HGNetV2去替换YOLOv8的主干 ,经过结构的研究我们可以发现在HGNetV2的网络中有大量的卷积存在,所以我们可以用一种更加 轻量化 的卷积去优化HGNetV2从而达到更加轻量化的效果 ( 亲测优化后的HGNetV2网络比正常HGNetV2精度更高轻量化效果更好,非常适合轻量化的读者) ,同时HGNetV2的网络结构目前还没有推出论文,所以其理论知识在网络上也是非常的少,我也是根据网络结构图进行了分析,给大家进行讲解网络结构原理 (亲测替换之后主干GFLOPs降低到7.4,精度mAP提高0.06) 。
二、HGNetV2原理讲解
本文论文地址: RT-DETR论文地址
本文代码来源: HGNetV2的代码来源
2.1 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块的扩展视图,包括多个不同滤波器大小的卷积层,以捕获多样化的特征,然后通过一个元素级相加或连接的操作(由+符号表示)在数据传递到下一层之前。
2.2 轻量化卷积
我这里利用的轻量化卷积只是官方仓库里面包含的四种,这个文章其实是给大家打开一个思路,这里的HGNet利用大量的卷积处理,所以我们能够替换其中大量的卷积从而达到优化和涨点的效果。
这几种卷积都是非常经典的了,其中RepConv只支持卷积核为3所以我也进行了一定的处理,原理就不再描述了。
三、HGNetV2的代码
这里的结构我们复制' ultralytics /nn/modules'目录下然后创建一个py文件粘贴进去即可,添加教程看章节四。
- import torch
- import torch.nn as nn
- import math
- import numpy as np
- __all__ = ['Light_HGBlock']
- 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 LightConv(nn.Module):
- """
- Light convolution with args(ch_in, ch_out, kernel).
- https://github.com/PaddlePaddle/PaddleDetection/blob/develop/ppdet/modeling/backbones/hgnet_v2.py
- """
- def __init__(self, c1, c2, k=1, act=nn.ReLU()):
- """Initialize Conv layer with given arguments including activation."""
- super().__init__()
- self.conv1 = Conv(c1, c2, 1, act=False)
- self.conv2 = DWConv(c2, c2, k, act=act)
- def forward(self, x):
- """Apply 2 convolutions to input tensor."""
- return self.conv2(self.conv1(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)
- class DWConvTranspose2d(nn.ConvTranspose2d):
- """Depth-wise transpose convolution."""
- def __init__(self, c1, c2, k=1, s=1, p1=0, p2=0): # ch_in, ch_out, kernel, stride, padding, padding_out
- """Initialize DWConvTranspose2d class with given parameters."""
- super().__init__(c1, c2, k, s, p1, p2, groups=math.gcd(c1, c2))
- class ConvTranspose(nn.Module):
- """Convolution transpose 2d layer."""
- default_act = nn.SiLU() # default activation
- def __init__(self, c1, c2, k=2, s=2, p=0, bn=True, act=True):
- """Initialize ConvTranspose2d layer with batch normalization and activation function."""
- super().__init__()
- self.conv_transpose = nn.ConvTranspose2d(c1, c2, k, s, p, bias=not bn)
- self.bn = nn.BatchNorm2d(c2) if bn else nn.Identity()
- self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
- def forward(self, x):
- """Applies transposed convolutions, batch normalization and activation to input."""
- return self.act(self.bn(self.conv_transpose(x)))
- def forward_fuse(self, x):
- """Applies activation and convolution transpose operation to input."""
- return self.act(self.conv_transpose(x))
- class Focus(nn.Module):
- """Focus wh information into c-space."""
- def __init__(self, c1, c2, k=1, s=1, p=None, g=1, act=True):
- """Initializes Focus object with user defined channel, convolution, padding, group and activation values."""
- super().__init__()
- self.conv = Conv(c1 * 4, c2, k, s, p, g, act=act)
- # self.contract = Contract(gain=2)
- def forward(self, x):
- """
- Applies convolution to concatenated tensor and returns the output.
- Input shape is (b,c,w,h) and output shape is (b,4c,w/2,h/2).
- """
- return self.conv(torch.cat((x[..., ::2, ::2], x[..., 1::2, ::2], x[..., ::2, 1::2], x[..., 1::2, 1::2]), 1))
- # return self.conv(self.contract(x))
- class GhostConv(nn.Module):
- """Ghost Convolution https://github.com/huawei-noah/ghostnet."""
- def __init__(self, c1, c2, k=1, s=1, g=1, act=True):
- """Initializes Ghost Convolution module with primary and cheap operations for efficient feature learning."""
- super().__init__()
- c_ = c2 // 2 # hidden channels
- self.cv1 = Conv(c1, c_, k, s, None, g, act=act)
- self.cv2 = Conv(c_, c_, 5, 1, None, c_, act=act)
- def forward(self, x):
- """Forward propagation through a Ghost Bottleneck layer with skip connection."""
- y = self.cv1(x)
- return torch.cat((y, self.cv2(y)), 1)
- class RepConv(nn.Module):
- """
- RepConv is a basic rep-style block, including training and deploy status.
- This module is used in RT-DETR.
- Based on https://github.com/DingXiaoH/RepVGG/blob/main/repvgg.py
- """
- default_act = nn.SiLU() # default activation
- def __init__(self, c1, c2, k=3, s=1, p=1, g=1, d=1, act=True, bn=False, deploy=False):
- """Initializes Light Convolution layer with inputs, outputs & optional activation function."""
- super().__init__()
- assert k == 3 and p == 1
- self.g = g
- self.c1 = c1
- self.c2 = c2
- self.act = self.default_act if act is True else act if isinstance(act, nn.Module) else nn.Identity()
- self.bn = nn.BatchNorm2d(num_features=c1) if bn and c2 == c1 and s == 1 else None
- self.conv1 = Conv(c1, c2, k, s, p=p, g=g, act=False)
- self.conv2 = Conv(c1, c2, 1, s, p=(p - k // 2), g=g, act=False)
- def forward_fuse(self, x):
- """Forward process."""
- return self.act(self.conv(x))
- def forward(self, x):
- """Forward process."""
- id_out = 0 if self.bn is None else self.bn(x)
- return self.act(self.conv1(x) + self.conv2(x) + id_out)
- def get_equivalent_kernel_bias(self):
- """Returns equivalent kernel and bias by adding 3x3 kernel, 1x1 kernel and identity kernel with their biases."""
- kernel3x3, bias3x3 = self._fuse_bn_tensor(self.conv1)
- kernel1x1, bias1x1 = self._fuse_bn_tensor(self.conv2)
- kernelid, biasid = self._fuse_bn_tensor(self.bn)
- return kernel3x3 + self._pad_1x1_to_3x3_tensor(kernel1x1) + kernelid, bias3x3 + bias1x1 + biasid
- @staticmethod
- def _pad_1x1_to_3x3_tensor(kernel1x1):
- """Pads a 1x1 tensor to a 3x3 tensor."""
- if kernel1x1 is None:
- return 0
- else:
- return torch.nn.functional.pad(kernel1x1, [1, 1, 1, 1])
- def _fuse_bn_tensor(self, branch):
- """Generates appropriate kernels and biases for convolution by fusing branches of the neural network."""
- if branch is None:
- return 0, 0
- if isinstance(branch, Conv):
- kernel = branch.conv.weight
- running_mean = branch.bn.running_mean
- running_var = branch.bn.running_var
- gamma = branch.bn.weight
- beta = branch.bn.bias
- eps = branch.bn.eps
- elif isinstance(branch, nn.BatchNorm2d):
- if not hasattr(self, "id_tensor"):
- input_dim = self.c1 // self.g
- kernel_value = np.zeros((self.c1, input_dim, 3, 3), dtype=np.float32)
- for i in range(self.c1):
- kernel_value[i, i % input_dim, 1, 1] = 1
- self.id_tensor = torch.from_numpy(kernel_value).to(branch.weight.device)
- kernel = self.id_tensor
- running_mean = branch.running_mean
- running_var = branch.running_var
- gamma = branch.weight
- beta = branch.bias
- eps = branch.eps
- std = (running_var + eps).sqrt()
- t = (gamma / std).reshape(-1, 1, 1, 1)
- return kernel * t, beta - running_mean * gamma / std
- def fuse_convs(self):
- """Combines two convolution layers into a single layer and removes unused attributes from the class."""
- if hasattr(self, "conv"):
- return
- kernel, bias = self.get_equivalent_kernel_bias()
- self.conv = nn.Conv2d(
- in_channels=self.conv1.conv.in_channels,
- out_channels=self.conv1.conv.out_channels,
- kernel_size=self.conv1.conv.kernel_size,
- stride=self.conv1.conv.stride,
- padding=self.conv1.conv.padding,
- dilation=self.conv1.conv.dilation,
- groups=self.conv1.conv.groups,
- bias=True,
- ).requires_grad_(False)
- self.conv.weight.data = kernel
- self.conv.bias.data = bias
- for para in self.parameters():
- para.detach_()
- self.__delattr__("conv1")
- self.__delattr__("conv2")
- if hasattr(self, "nm"):
- self.__delattr__("nm")
- if hasattr(self, "bn"):
- self.__delattr__("bn")
- if hasattr(self, "id_tensor"):
- self.__delattr__("id_tensor")
- class Light_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, num=1, shortcut=False, act=True):
- """Initializes a CSP Bottleneck with 1 convolution using specified input and output channels."""
- super().__init__()
- block = Conv
- if num == 1:
- block = GhostConv
- elif num == 2:
- block = RepConv # RepConv Only supported k = 3
- k = 3
- elif num == 3:
- block = DWConv
- elif num == 4:
- block = LightConv
- 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
四、手把手教你添加HGNetV2
4.1 手把手教你添加HGNetV2
这里的添加方法比较特殊,和之前的都不一样,但是也很简单修改三处即可使用。
4.1.1 修改一
我们找到如下的文件'ultralytics/nn/tasks.py',在开头的地方导入我们刚才创建文件的模块。
4.1.2 修改二
按照如下图所示添加即可
4.1.3 修改三
本文以及默认大家用的是以及集成过RT-DETR代码的ultralytics仓库了(其中以及包含了HGNet的代码文件),所以我们只需要添加几行代码就能够回复官方删除掉的功能。
我们首先需要找到'ultralytics/nn/tasks.py'文件然后找到'def parse_model(d, ch, verbose=True): # model_dict, input_channels(3)'
下面的代码我们看到大概基本的样子的.
复制此处的代码按照下面的图片进行添加即可,不要自己打!
- 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
到此我们就完成了官方的代码修复,我们此时运行代码比如V8n那么你就会发现的大幅度的减少了参数量,
4. 2 使用说明
这里有一个参数,需要大家修改,在章节2.2我说了我用了好几个卷积可以提供大家选择,所以这里分别可以有四个卷积可以给大家使用,大家可以看下面的代码。
- if num == 1:
- block = GhostConv
- elif num == 2:
- block = RepConv # RepConv Only supported k = 3
- k = 3
- elif num == 3:
- block = DWConv
- elif num == 4:
- block = LightConv
如果我们想要使用RepConv为例,那么我们修改图中的红框位置的地方我们设置为2此时使用的就是RepConv,默认使用的是3也就是DWConv,实验结果也是这个跑出来的。
4.3 HGNet-l的yaml文件
此版本训练信息:YOLO11-LightHGNetV2-l summary: 424 layers, 2,381,262 parameters, 2,381,246 gradients, 7.1 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
- scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
- # [depth, width, max_channels]
- n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
- s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
- m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
- l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
- x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
- # YOLO11n backbone
- backbone:
- # [from, repeats, module, args]
- - [-1, 1, HGStem, [32, 48]] # 0-P2/4
- - [-1, 6, Light_HGBlock, [48, 128, 3]] # stage 1
- - [-1, 1, DWConv, [128, 3, 2, 1, 1]] # 2-P3/8
- - [-1, 6, Light_HGBlock, [96, 512, 3]] # stage 2
- - [-1, 1, DWConv, [512, 3, 2, 2, False]] # 4-P3/16
- - [-1, 6, Light_HGBlock, [192, 1024, 5, 3, False]] # cm, c2, k, light, shortcut
- - [-1, 6, Light_HGBlock, [192, 1024, 5, 3, True]]
- - [-1, 6, Light_HGBlock, [192, 1024, 5, 3, True]] # stage 3
- - [-1, 1, DWConv, [1024, 3, 2, 1, False]] # 8-P4/32
- - [-1, 6, Light_HGBlock, [384, 2048, 5, 3, False]] # stage 4
- - [-1, 1, SPPF, [1024, 5]] # 10
- - [-1, 1, C2PSA, [1024]] # 11
- # YOLOv10.0n head
- head:
- - [-1, 1, nn.Upsample, [None, 2, "nearest"]] # 12
- - [[-1, 7], 1, Concat, [1]] # cat backbone P4
- - [-1, 2, C3k2, [512, False]] # 14
- - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- - [[-1, 3], 1, Concat, [1]] # cat backbone P3
- - [-1, 2, C3k2, [256, False]] # 17 (P3/8-small)
- - [-1, 1, Conv, [256, 3, 2]]
- - [[-1, 14], 1, Concat, [1]] # cat head P4
- - [-1, 2, C3k2, [512, False]] # 20 (P4/16-medium)
- - [-1, 1, SCDown, [512, 3, 2]]
- - [[-1, 11], 1, Concat, [1]] # cat head P5
- - [-1, 2, C3k2, [1024, True]] # 23 (P5/32-large)
- - [[17, 20, 23], 1, v10Detect, [nc]] # Detect(P3, P4, P5)
4.4 HGNetV2-x的yaml文件
此版本训练信息:YOLO11-LightHGNetV2-x summary: 460 layers, 2,643,782 parameters, 2,643,766 gradients, 8.0 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
- scales: # model compound scaling constants, i.e. 'model=yolo11n.yaml' will call yolo11.yaml with scale 'n'
- # [depth, width, max_channels]
- n: [0.50, 0.25, 1024] # summary: 319 layers, 2624080 parameters, 2624064 gradients, 6.6 GFLOPs
- s: [0.50, 0.50, 1024] # summary: 319 layers, 9458752 parameters, 9458736 gradients, 21.7 GFLOPs
- m: [0.50, 1.00, 512] # summary: 409 layers, 20114688 parameters, 20114672 gradients, 68.5 GFLOPs
- l: [1.00, 1.00, 512] # summary: 631 layers, 25372160 parameters, 25372144 gradients, 87.6 GFLOPs
- x: [1.00, 1.50, 512] # summary: 631 layers, 56966176 parameters, 56966160 gradients, 196.0 GFLOPs
- # YOLO11n backbone
- backbone:
- # [from, repeats, module, args]
- - [-1, 1, HGStem, [32, 64]] # 0-P2/4
- - [-1, 6, Light_HGBlock, [64, 128, 3]] # stage 1
- - [-1, 1, DWConv, [128, 3, 2, 1, False]] # 2-P3/8
- - [-1, 6, Light_HGBlock, [128, 512, 3]]
- - [-1, 6, Light_HGBlock, [128, 512, 3, 3, True]] # 4-stage 2
- - [-1, 1, DWConv, [512, 3, 2, 1, False]] # 5-P3/16
- - [-1, 6, Light_HGBlock, [256, 1024, 5, 3, False]] # cm, c2, k, light, shortcut
- - [-1, 6, Light_HGBlock, [256, 1024, 5, 3, True]]
- - [-1, 6, Light_HGBlock, [256, 1024, 5, 3, True]]
- - [-1, 6, Light_HGBlock, [256, 1024, 5, 3, True]]
- - [-1, 6, Light_HGBlock, [256, 1024, 5, 3, True]] # 10-stage 3
- - [-1, 1, DWConv, [1024, 3, 2, 1, False]] # 11-P4/32
- - [-1, 6, Light_HGBlock, [512, 2048, 5, 3, False]]
- - [-1, 6, Light_HGBlock, [512, 2048, 5, 3, True]] # 13-stage 4
- - [-1, 1, SPPF, [1024, 5]] # 14
- - [-1, 1, PSA, [1024]] # 15
- # YOLOv10.0n head
- head:
- - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- - [[-1, 10], 1, Concat, [1]] # cat backbone P4
- - [-1, 2, C3k2, [512, False]] # 18
- - [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- - [[-1, 4], 1, Concat, [1]] # cat backbone P3
- - [-1, 2, C3k2, [256, False]] # 21 (P3/8-small)
- - [-1, 1, Conv, [256, 3, 2]]
- - [[-1, 18], 1, Concat, [1]] # cat head P4
- - [-1, 2, C3k2, [512, False]] # 24 (P4/16-medium)
- - [-1, 1, SCDown, [512, 3, 2]]
- - [[-1, 15], 1, Concat, [1]] # cat head P5
- - [-1, 2, C3k2, [1024, False]] # 27 (P5/32-large)
- - [[21, 24, 27], 1, v10Detect, [nc]] # Detect(P3, P4, P5)
4.5 训练代码
- import warnings
- warnings.filterwarnings('ignore')
- from ultralytics import YOLO
- if __name__ == '__main__':
- model = YOLO('ultralytics/cfg/models/v8/yolov8-C2f-FasterBlock.yaml')
- # model.load('yolov8n.pt') # loading pretrain weights
- model.train(data=r'替换数据集yaml文件地址',
- # 如果大家任务是其它的'ultralytics/cfg/default.yaml'找到这里修改task可以改成detect, segment, classify, pose
- cache=False,
- imgsz=640,
- epochs=150,
- single_cls=False, # 是否是单类别检测
- batch=4,
- close_mosaic=10,
- workers=0,
- device='0',
- optimizer='SGD', # using SGD
- # resume='', # 如过想续训就设置last.pt的地址
- amp=False, # 如果出现训练损失为Nan可以关闭amp
- project='runs/train',
- name='exp',
- )
五、运行成功记录
下面的图片是证明成功运行的截图,确保我发的改进机制是可用的。
六、本文总结
到此本文的正式分享内容就结束了,在这里给大家推荐我的YOLOv11改进有效涨点专栏,本专栏目前为新开的平均质量分98分,后期我会根据各种最新的前沿顶会进行论文复现,也会对一些老的改进机制进行补充, 目前本专栏免费阅读(暂时,大家尽早关注不迷路~) ,如果大家觉得本文帮助到你了,订阅本专栏,关注后续更多的更新~