学习资源站

RT-DETR改进策略【模型轻量化】替换骨干网络为MoblieNetV2,含模型详解和完整配置步骤-

RT-DETR改进策略【模型轻量化】| 替换骨干网络为MoblieNetV2,含模型详解和完整配置步骤

一、本文介绍

本文记录的是 基于MobileNet v2的 RT-DETR轻量化改进方法研究 MobileNet v2 采用 深度可分离卷积 标准卷积 分解为 深度卷积 1×1卷积 大幅削减计算量 。同时,引入 线性瓶颈层 防止非线性在低维空间破坏信息 ,避免非线性层导致的性能下降问题。本文将 MobileNet v2 应用到 RT-DETR 中,借助其高效的结构和特性,在保持一定精度的前提下,显著降低 RT-DETR 的计算复杂度和内存占用。

模型 参数量 计算量
rtdetr-l 32.8M 108.0GFLOPs
Improved 20.3M 65.3GFLOPs


二、MoblieNet V4设计原理

MobileNetV2: Inverted Residuals and Linear Bottlenecks

2.1 出发点

随着神经网络在图像识别等领域的广泛应用,对高精度的追求使得现代先进网络需要大量计算资源,这超出了许多移动和嵌入式设备的能力。

因此, 需要设计一种能够在保证一定精度的前提下,大幅减少计算量和内存占用的网络架构 ,以满足资源受限环境的需求,这就促使了 MobileNet v2 轻量模块的设计。

2.2 结构原理

2.2.1 深度可分离卷积(Depthwise Separable Convolutions)

  • 这是 MobileNet v2 的重要基础结构。它将标准卷积分解为两个步骤,首先是 深度卷积(depthwise convolution) ,对每个输入通道应用单个卷积滤波器进行滤波操作;然后是 1×1 卷积(pointwise convolution) ,负责组合深度卷积的输出,构建新的特征。

  • 对于一个输入张量 L i L_{i} L i (维度为 h i × w i × d i h_{i}×w_{i}×d_{i} h i × w i × d i )和卷积核 K K K (维度为 k × k × d i × d j k×k×d_{i}×d_{j} k × k × d i × d j ),标准卷积产生输出张量 L j L_{j} L j (维度为 h i × w i × d j h_{i}×w_{i}×d_{j} h i × w i × d j ),其计算成本为 h i ⋅ w i ⋅ d i ⋅ d j ⋅ k ⋅ k h_{i}·w_{i}·d_{i}·d_{j}·k·k h i w i d i d j k k ,而深度可分离卷积的计算成本为 h i ⋅ w i ⋅ d i ( k 2 + d j ) h_{i}·w_{i}·d_{i}(k^{2}+d_{j}) h i w i d i ( k 2 + d j ) ,相比之下计算量大幅减少。

例如,当 k = 3 k = 3 k = 3 时,MobileNet v2使用的 3×3 深度可分离卷积计算成本比标准卷积小 8 到 9 倍,且精度损失较小(如在一些常见的图像分类任务中得到验证)。这一结构在图 2 中有所体现,从图中可以清晰看到深度可分离卷积与标准卷积在操作上的差异。

2.2.2 线性瓶颈(Linear Bottlenecks)

ReLU 应用于1D空间中的线时会产生“射线”,在 R n R^{n} R n 空间中通常会导致具有 π - 关节的分段线性曲线。若 ReLU 变换 后的结果具有非零体 S S S ,则映射到内部 S S S 的点是通过输入的线性变换 B B B 获得的,这表明输出域的非零体积部分对应的输入空间仅限于线性变换。而且当 ReLU 使通道塌陷时,会在该通道中 丢失信息

因此,在卷积块中插入 线性瓶颈层 ,假设感兴趣的流形是低维的,以此来 防止非线性破坏太多信息

2.2.3 倒置残差(Inverted residuals)

瓶颈块 在形式上类似于 残差块 ,但在 MobileNet v2 中,受瓶颈实际包含所有必要信息的启发,采用 捷径连接 直接在瓶颈之间连接,而扩展层仅作为张量非线性变换的实现细节。这种设计与传统的残差连接类似,有助于 提高梯度在多层之间传播的能力 ,且在 内存效率上更具优势 ,在实验中也表现出更好的效果。

在这里插入图片描述

对于一个大小为 h × w h×w h × w ,扩展因子为 t t t ,核大小为 k k k ,输入通道为 d ′ d' d ,输出通道为 d ′ ′ d'' d ′′ 的块,其总的乘加运算数量为 h ⋅ w ⋅ d ′ ⋅ t ( d ′ + k 2 + d ′ ′ ) h·w·d'·t(d'+k^{2}+d'') h w d t ( d + k 2 + d ′′ )

2.2.4 整体架构

MobileNet v2 的基本构建块是 带有残差的瓶颈深度可分离卷积 。其网络架构包含初始的全卷积层(有 32 个滤波器),后跟 19 个残差瓶颈层。

除第一层外,网络中使用恒定的扩展率,在实验中发现扩展率在 5 到 10 之间性能曲线几乎相同,较小的网络使用稍小的扩展率更好,较大的网络使用较大的扩展率性能稍好。

2.3 优势

  • 高效的推理和内存利用 :倒置残差瓶颈层允许非常内存高效的实现,这对于移动应用至关重要。通过将瓶颈块视为单个操作,并利用内部变换是按通道进行以及连续非按通道操作的输入输出大小比例等特性,可以显著减少内存需求。
  • 性能优异 :在多个任务和基准测试中取得了先进的性能。
  • 理论优势 :提出的卷积块具有独特的属性,能够将网络的表达能力(由扩展层编码)与其容量(由瓶颈输入编码)分离开来,为进一步的研究提供了重要的方向。

论文: https://arxiv.org/abs/1801.04381

三、MobileNetV2的实现代码

MobileNetV2模块 的实现代码如下:

"""A from-scratch implementation of MobileNetV2 paper ( for educational purposes ).
Paper
    MobileNetV2: Inverted Residuals and Linear Bottlenecks - https://arxiv.org/abs/1801.04381
author : shubham.aiengineer@gmail.com
"""

import torch
from torch import nn

__all__ = ['MobileNetV2']

class ConvNormReLUBlock(nn.Module):
    def __init__(
            self,
            in_channels: int,
            out_channels: int,
            kernel_size: list,
            stride: int = 1,
            padding: int = 0,
            groups: int = 1,
            bias: bool = False,
            activation: bool = nn.ReLU6,
    ):
        """Constructs a block containing a combination of convolution, batchnorm and relu
        Args:
            in_channels (int): input channels
            out_channels (int): output channels
            kernel_size (list): kernel size parameter for convolution
            stride (int, optional): stride parameter for convolution. Defaults to 1.
            padding (int, optional): padding parameter for convolution. Defaults to 0.
            groups (int, optional): number of blocked connections from input channel to output channel for convolution. Defaults to 1.
            bias (bool, optional): whether to enable bias in convolution. Defaults to False.
            activation (bool, optional): activation function to use. Defaults to nn.ReLU6.
        """

        super().__init__()

        self.conv = nn.Conv2d(
            in_channels,
            out_channels,
            kernel_size,
            stride=stride,
            padding=padding,
            groups=groups,
            bias=bias,
        )
        self.bn = nn.BatchNorm2d(out_channels)
        self.activation = activation()

    def forward(self, x):
        """Perform forward pass."""

        x = self.conv(x)
        x = self.bn(x)
        x = self.activation(x)

        return x

class InverseResidualBlock(nn.Module):
    def __init__(
            self,
            in_channels: int,
            out_channels: int,
            expansion_factor: int = 6,
            stride: int = 1,
    ):
        """Constructs a inverse residual block with depthwise seperable convolution
        Args:
            in_channels (int): input channels
            out_channels (int): output channels
            expansion_factor (int, optional): Calculating the input & output channel for depthwise convolution by multiplying the expansion factor with input channels. Defaults to 6.
            stride (int, optional): stride paramemeter for depthwise convolution. Defaults to 1.
        """

        super().__init__()

        hidden_channels = in_channels * expansion_factor
        self.residual = in_channels == out_channels and stride == 1

        self.conv1 = (
            ConvNormReLUBlock(in_channels, hidden_channels, (1, 1))
            if in_channels != hidden_channels
            else nn.Identity()
        # If it's not the first layer, then we need to add a 1x1 convolutional layer to expand the number of channels
        )
        self.depthwise_conv = ConvNormReLUBlock(
            hidden_channels,
            hidden_channels,
            (3, 3),
            stride=stride,
            padding=1,
            groups=hidden_channels,
        )
        self.conv2 = ConvNormReLUBlock(
            hidden_channels, out_channels, (1, 1), activation=nn.Identity
        )

    def forward(self, x):
        """Perform forward pass."""

        identity = x

        x = self.conv1(x)
        x = self.depthwise_conv(x)
        x = self.conv2(x)

        if self.residual:
            x = torch.add(x, identity)

        return x

class MobileNetV2(nn.Module):
    def __init__(
            self,
            n_classes: int = 1000,
            input_channel: int = 3,
            dropout: float = 0.2,
    ):
        """Constructs MobileNetV2 architecture
        Args:
            n_classes (int, optional): output neuron in last layer. Defaults to 1000.
            input_channel (int, optional): input channels in first conv layer. Defaults to 3.
            dropout (float, optional): dropout in last layer. Defaults to 0.2.
        """

        super().__init__()

        # The configuration of MobileNetV2
        # input channels, expansion factor, output channels, repeat, stride,
        config = (
            (32, 1, 16, 1, 1),
            (16, 6, 24, 2, 2),
            (24, 6, 32, 3, 2),
            (32, 6, 64, 4, 2),
            (64, 6, 96, 3, 1),
            (96, 6, 160, 3, 2),
            (160, 6, 320, 1, 1),
        )

        self.model = nn.Sequential(
            ConvNormReLUBlock(input_channel, 32, (3, 3), stride=2, padding=1)
        )

        for in_channels, expansion_factor, out_channels, repeat, stride in config:
            for _ in range(repeat):
                self.model.append(
                    InverseResidualBlock(
                        in_channels=in_channels,
                        out_channels=out_channels,
                        expansion_factor=expansion_factor,
                        stride=stride,
                    )
                )
                in_channels = out_channels
                stride = 1

        self.index = [24, 32, 96, 320]
        self.width_list = [i.size(1) for i in self.forward(torch.randn(1, 3, 640, 640))]

    def forward(self, x):
        """Perform forward pass."""
        results = [None, None, None, None]

        for model in self.model:
            x = model(x)
            if x.size(1) in self.index:
                position = self.index.index(x.size(1))  # Find the position in the index list
                results[position] = x
        return results

if __name__ == "__main__":
    # Generating Sample image
    image_size = (1, 3, 224, 224)
    image = torch.rand(*image_size)

    # Model
    mobilenet_v2 = MobileNetV2()

    # summary(
    #     mobilenet_v2,
    #     input_data=image,
    #     col_names=["input_size", "output_size", "num_params"],
    #     device="cpu",
    #     depth=2,
    # )

    out = mobilenet_v2(image)
    print("Output shape : ", out.shape)


四、修改步骤

4.1 修改一

① 在 ultralytics/nn/ 目录下新建 AddModules 文件夹用于存放模块代码

② 在 AddModules 文件夹下新建 MobileNetV2.py ,将 第三节 中的代码粘贴到此处

在这里插入图片描述

4.2 修改二

AddModules 文件夹下新建 __init__.py (已有则不用新建),在文件内导入模块: from .MobileNetV2 import *

在这里插入图片描述

4.3 修改三

ultralytics/nn/modules/tasks.py 文件中,需要在两处位置添加各模块类名称。

① 首先:导入模块

在这里插入图片描述

② 其次:在 parse_model函数 的如下位置添加两行代码:

在这里插入图片描述

backbone = False
t=m

在这里插入图片描述

③ 接着,在此函数下添加如下代码:

elif m in {MobileNetV2}:
    m = m(*args)
    c2 = m.width_list
    backbone = True

在这里插入图片描述

④ 然后,将下方红框内的代码全部替换:

if isinstance(c2, list):
    is_backbone = True
    m_ = m
    m_.backbone = True
else:
    m_ = nn.Sequential(*(m(*args) for _ in range(n))) if n > 1 else m(*args)  # module
    t = str(m)[8:-2].replace('__main__.', '')  # module type
m.np = sum(x.numel() for x in m_.parameters())  # number params
m_.i, m_.f, m_.type = i + 4 if is_backbone else i, f, t  # attach index, 'from' index, type
if verbose:
    LOGGER.info(f'{i:>3}{str(f):>20}{n_:>3}{m.np:10.0f}  {t:<45}{str(args):<30}')  # print
save.extend(x % (i + 4 if is_backbone else i) for x in ([f] if isinstance(f, int) else f) if
            x != -1)  # append to savelist
layers.append(m_)
if i == 0:
    ch = []
if isinstance(c2, list):
    ch.extend(c2)
    for _ in range(5 - len(ch)):
        ch.insert(0, 0)
else:
    ch.append(c2)

替换后如下:

在这里插入图片描述

⑤ 在此文件下找到 base_model _predict_once ,并将其替换成如下代码。

def _predict_once(self, x, profile=False, visualize=False, embed=None):
    y, dt, embeddings = [], [], []  # outputs
    for m in self.model:
        if m.f != -1:  # if not from previous layer
            x = y[m.f] if isinstance(m.f, int) else [x if j == -1 else y[j] for j in m.f]  # from earlier layers
        if profile:
            self._profile_one_layer(m, x, dt)
        if hasattr(m, 'backbone'):
            x = m(x)
            if len(x) != 5:  # 0 - 5
                x.insert(0, None)
            for index, i in enumerate(x):
                if index in self.save:
                    y.append(i)
                else:
                    y.append(None)
            x = x[-1]  # 最后一个输出传给下一层
        else:
            x = m(x)  # run
            y.append(x if m.i in self.save else None)  # save output
        if visualize:
            feature_visualization(x, m.type, m.i, save_dir=visualize)
        if embed and m.i in embed:
            embeddings.append(nn.functional.adaptive_avg_pool2d(x, (1, 1)).squeeze(-1).squeeze(-1))  # flatten
            if m.i == max(embed):
                return torch.unbind(torch.cat(embeddings, 1), dim=0)
    return x

在这里插入图片描述

至此就修改完成了,可以配置模型开始训练了


五、yaml模型文件

5.1 模型改进⭐

在代码配置完成后,配置模型的YAML文件。

此处以 ultralytics/cfg/models/rt-detr/rtdetr-l.yaml 为例,在同目录下创建一个用于自己数据集训练的模型文件 rtdetr-l-MobileNetV2.yaml

rtdetr-l.yaml 中的内容复制到 rtdetr-l-MobileNetV2.yaml 文件下,修改 nc 数量等于自己数据中目标的数量。

📌 模型的修改方法是将 骨干网络 替换成 MobileNetV2

# Ultralytics YOLO 🚀, AGPL-3.0 license
# RT-DETR-l object detection model with P3-P5 outputs. For details see https://docs.ultralytics.com/models/rtdetr

# Parameters
nc: 1  # number of classes
scales: # model compound scaling constants, i.e. 'model=yolov8n-cls.yaml' will call yolov8-cls.yaml with scale 'n'
  # [depth, width, max_channels]
  l: [1.00, 1.00, 1024]

backbone:
  # [from, repeats, module, args]
  - [-1, 1, MobileNetV2, []]  # 4

head:
  - [-1, 1, Conv, [256, 1, 1, None, 1, 1, False]]  # 5 input_proj.2
  - [-1, 1, AIFI, [1024, 8]] # 6
  - [-1, 1, Conv, [256, 1, 1]]  # 7, Y5, lateral_convs.0

  - [-1, 1, nn.Upsample, [None, 2, 'nearest']] # 8
  - [3, 1, Conv, [256, 1, 1, None, 1, 1, False]]  # 9 input_proj.1
  - [[-2, -1], 1, Concat, [1]] # 10
  - [-1, 3, RepC3, [256]]  # 11, fpn_blocks.0
  - [-1, 1, Conv, [256, 1, 1]]   # 12, Y4, lateral_convs.1

  - [-1, 1, nn.Upsample, [None, 2, 'nearest']] # 13
  - [2, 1, Conv, [256, 1, 1, None, 1, 1, False]]  # 14 input_proj.0
  - [[-2, -1], 1, Concat, [1]]  # 15 cat backbone P4
  - [-1, 3, RepC3, [256]]    # X3 (16), fpn_blocks.1

  - [-1, 1, Conv, [256, 3, 2]]   # 17, downsample_convs.0
  - [[-1, 12], 1, Concat, [1]]  # 18 cat Y4
  - [-1, 3, RepC3, [256]]    # F4 (19), pan_blocks.0

  - [-1, 1, Conv, [256, 3, 2]]   # 20, downsample_convs.1
  - [[-1, 7], 1, Concat, [1]]  # 21 cat Y5
  - [-1, 3, RepC3, [256]]    # F5 (22), pan_blocks.1

  - [[16, 19, 22], 1, RTDETRDecoder, [nc]]  # Detect(P3, P4, P5)


六、成功运行结果

分别打印网络模型可以看到 MobileNetV2模块 已经加入到模型中,并可以进行训练了。

rtdetr-l-MobileNetV2

rtdetr-MobileNetV2 summary: 594 layers, 20,263,651 parameters, 20,263,651 gradients, 65.3 GFLOPs

                   from  n    params  module                                       arguments                     
  0                  -1  1   1811712  MobileNetV2                                  []                            
  1                  -1  1     82432  ultralytics.nn.modules.conv.Conv             [320, 256, 1, 1, None, 1, 1, False]
  2                  -1  1    789760  ultralytics.nn.modules.transformer.AIFI      [256, 1024, 8]                
  3                  -1  1     66048  ultralytics.nn.modules.conv.Conv             [256, 256, 1, 1]              
  4                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
  5                   3  1     25088  ultralytics.nn.modules.conv.Conv             [96, 256, 1, 1, None, 1, 1, False]
  6            [-2, -1]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
  7                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
  8                  -1  1     66048  ultralytics.nn.modules.conv.Conv             [256, 256, 1, 1]              
  9                  -1  1         0  torch.nn.modules.upsampling.Upsample         [None, 2, 'nearest']          
 10                   2  1      8704  ultralytics.nn.modules.conv.Conv             [32, 256, 1, 1, None, 1, 1, False]
 11            [-2, -1]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 12                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 13                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 14            [-1, 12]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 15                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 16                  -1  1    590336  ultralytics.nn.modules.conv.Conv             [256, 256, 3, 2]              
 17             [-1, 7]  1         0  ultralytics.nn.modules.conv.Concat           [1]                           
 18                  -1  3   2232320  ultralytics.nn.modules.block.RepC3           [512, 256, 3]                 
 19        [16, 19, 22]  1   7303907  ultralytics.nn.modules.head.RTDETRDecoder    [1, [256, 256, 256]]          
rtdetr-MobileNetV2 summary: 594 layers, 20,263,651 parameters, 20,263,651 gradients, 65.3 GFLOPs