RT-DETR改进策略【注意力机制篇】| 引入MobileNetv4中的Mobile MQA,轻量化注意力模块 提高模型效率
一、本文介绍
本文记录的是
基于Mobile MQA模块的RT-DETR目标检测改进方法研究
。
MobileNetv4
中的
Mobile MQA模块
是用于模型加速,减少内存访问的模块,相比其他全局的自注意力,
其不仅加强了模型对全局信息的关注,同时也显著提高了模型效率。
二、Mobile MQA注意力原理
在论文
《MobileNetV4 - Universal Models for the Mobile Ecosystem》
中,提出了
Mobile MQA
。
一、原理
-
基于MQA改进并结合不对称空间下采样
:
-
MQA(Multi-Query Attention)简化了传统的多头注意力机制,通过共享keys和values来减少内存访问需求。在移动混合模型中,当批量大小较小时,这种方式能有效提高运算强度。 -
借鉴
MQA中对queries、keys和values的不对称计算方式,Mobile MQA引入了空间缩减注意力(SRA),对keys和values进行下采样,同时保持高分辨率的queries。这是因为在混合模型中,早期层的空间混合卷积滤波器使得空间上相邻的标记具有相关性。 -
Mobile MQA的计算公式为:
M o b i l e _ M Q A ( X ) = C o n c a t ( a t t e n t i o n 1 , . . . , a t t e n t i o n n ) W O Mobile\_MQA(X)= Concat(attention_1,...,attention_n)W^{O} M o bi l e _ MQ A ( X ) = C o n c a t ( a tt e n t i o n 1 , ... , a tt e n t i o n n ) W O ,
其中 a t t e n t i o n j = s o f t m a x ( ( X W Q j ) ( S R ( X ) W K ) T d k ) ( S R ( X ) W V ) attention_j = softmax(\frac{(XW^{Q_j})(SR(X)W^{K})^{T}}{\sqrt{d_k}})(SR(X)W^{V}) a tt e n t i o n j = so f t ma x ( d k ( X W Q j ) ( SR ( X ) W K ) T ) ( SR ( X ) W V ) ,这里SR可以是空间缩减操作(在设计中是一个步长为2的3x3深度卷积),也可以是恒等函数(当不进行空间缩减时)。
-
二、特点
- 针对加速器优化 :专门为移动加速器进行了优化,考虑了移动加速器的计算和内存特性。
-
不对称空间下采样
:通过对
keys和values进行下采样,保持queries的高分辨率,在不损失太多精度的情况下,显著提高了效率。 -
操作简单高效
:相比传统的注意力机制,
Mobile MQA的设计更加简单,操作更加高效,更适合在移动设备上运行。
论文: http://arxiv.org/abs/2404.10518
源码: https://github.com/tensorflow/models/blob/master/official/vision/modeling/backbones/mobilenet.py
三、Mobile MQA的实现代码
Mobile MQA模块
的实现代码如下:
import torch
import torch.nn as nn
import torch.nn.functional as F
def conv2d(in_channels, out_channels, kernel_size=3, stride=1, groups=1, bias=False, norm=True, act=True):
conv = nn.Sequential()
padding = (kernel_size - 1) // 2
conv.append(nn.Conv2d(in_channels, out_channels, kernel_size, stride, padding, bias=bias, groups=groups))
if norm:
conv.append(nn.BatchNorm2d(out_channels))
if act:
conv.append(nn.ReLU6())
return conv
class MultiQueryAttentionLayerWithDownSampling(nn.Module):
def __init__(self, in_channels, num_heads, key_dim, value_dim, query_h_strides, query_w_strides, kv_strides, dw_kernel_size=3, dropout=0.0):
"""Multi Query Attention with spatial downsampling.
Referenced from here https://github.com/tensorflow/models/blob/master/official/vision/modeling/layers/nn_blocks.py
3 parameters are introduced for the spatial downsampling:
1. kv_strides: downsampling factor on Key and Values only.
2. query_h_strides: vertical strides on Query only.
3. query_w_strides: horizontal strides on Query only.
This is an optimized version.
1. Projections in Attention is explict written out as 1x1 Conv2D.
2. Additional reshapes are introduced to bring a up to 3x speed up.
"""
super(MultiQueryAttentionLayerWithDownSampling, self).__init__()
self.num_heads = num_heads
self.key_dim = key_dim
self.value_dim = value_dim
self.query_h_strides = query_h_strides
self.query_w_strides = query_w_strides
self.kv_strides = kv_strides
self.dw_kernel_size = dw_kernel_size
self.dropout = dropout
self.head_dim = self.key_dim // num_heads
if self.query_h_strides > 1 or self.query_w_strides > 1:
self._query_downsampling_norm = nn.BatchNorm2d(in_channels)
self._query_proj = conv2d(in_channels, self.num_heads * self.key_dim, 1, 1, norm=False, act=False)
if self.kv_strides > 1:
self._key_dw_conv = conv2d(in_channels, in_channels, dw_kernel_size, kv_strides, groups=in_channels,
norm=True, act=False)
self._value_dw_conv = conv2d(in_channels, in_channels, dw_kernel_size, kv_strides, groups=in_channels,
norm=True, act=False)
self._key_proj = conv2d(in_channels, key_dim, 1, 1, norm=False, act=False)
self._value_proj = conv2d(in_channels, key_dim, 1, 1, norm=False, act=False)
self._output_proj = conv2d(num_heads * key_dim, in_channels, 1, 1, norm=False, act=False)
self.dropout = nn.Dropout(p=dropout)
def forward(self, x):
bs, seq_len, _, _ = x.size()
# print(x.size())
if self.query_h_strides > 1 or self.query_w_strides > 1:
q = F.avg_pool2d(self.query_h_strides, self.query_w_strides)
q = self._query_downsampling_norm(q)
q = self._query_proj(q)
else:
q = self._query_proj(x)
px = q.size(2)
q = q.view(bs, self.num_heads, -1, self.key_dim) # [batch_size, num_heads, seq_len, key_dim]
if self.kv_strides > 1:
k = self._key_dw_conv(x)
k = self._key_proj(k)
v = self._value_dw_conv(x)
v = self._value_proj(v)
else:
k = self._key_proj(x)
v = self._value_proj(x)
k = k.view(bs, 1, self.key_dim, -1) # [batch_size, 1, key_dim, seq_length]
v = v.view(bs, 1, -1, self.key_dim) # [batch_size, 1, seq_length, key_dim]
# calculate attention score
# print(q.shape, k.shape, v.shape)
attn_score = torch.matmul(q, k) / (self.head_dim ** 0.5)
attn_score = self.dropout(attn_score)
attn_score = F.softmax(attn_score, dim=-1)
# context = torch.einsum('bnhm,bmv->bnhv', attn_score, v)
# print(attn_score.shape, v.shape)
context = torch.matmul(attn_score, v)
context = context.view(bs, self.num_heads * self.key_dim, px, px)
output = self._output_proj(context)
# print(output.shape)
return output
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 ResNetBlock(nn.Module):
"""ResNet block with standard convolution layers."""
def __init__(self, c1, c2, s=1, e=4):
"""Initialize convolution with given parameters."""
super().__init__()
c3 = e * c2
self.cv1 = Conv(c1, c2, k=1, s=1, act=True)
self.cv2 = Conv(c2, c2, k=3, s=s, p=1, act=True)
self.cv3 = Conv(c2, c3, k=1, act=False)
self.cv4 = MultiQueryAttentionLayerWithDownSampling(c2, 2, 48, 48, 1, 1, 1)
self.shortcut = nn.Sequential(Conv(c1, c3, k=1, s=s, act=False)) if s != 1 or c1 != c3 else nn.Identity()
def forward(self, x):
"""Forward pass through the ResNet block."""
return F.relu(self.cv3(self.cv4(self.cv2(self.cv1(x)))) + self.shortcut(x))
class ResNetLayer_MQA(nn.Module):
"""ResNet layer with multiple ResNet blocks."""
def __init__(self, c1, c2, s=1, is_first=False, n=1, e=4):
"""Initializes the ResNetLayer given arguments."""
super().__init__()
self.is_first = is_first
if self.is_first:
self.layer = nn.Sequential(
Conv(c1, c2, k=7, s=2, p=3, act=True), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
else:
blocks = [ResNetBlock(c1, c2, s, e=e)]
blocks.extend([ResNetBlock(e * c2, c2, 1, e=e) for _ in range(n - 1)])
self.layer = nn.Sequential(*blocks)
def forward(self, x):
"""Forward pass through the ResNet layer."""
return self.layer(x)
| 参数 | 解释 |
|---|---|
| in_channels | 输入通道数 |
| num_heads | 自注意力头的数量 |
| key_dim | 键的维度 |
| key_dim | 值的维度 |
| value_dim | 仅对键和值进行下采样,1不进行下采样,2下采样 |
| query_h_strides | 仅用于查询的,在H方向上的步长 |
| query_w_strides | 仅用于查询的,在W方向上的步长 |
| dw_kernel_size=3 | 深度可分离卷积的卷积核大小 |
| dropout=0.0 | 随机丢失比例 |
四、添加步骤
4.1 改进点⭐
模块改进方法
:基于
Mobile MQA模块
的
ResNetLayer
。
改进方法是对
RT-DETR
中的
ResNetLayer模块
进行改进。
MobileNetv4
中的
Mobile MQA模块
可用于模型加速,减少内存访问的模块,相比其他全局的自注意力,利用
Mobile MQA
改进
ResNetLayer模块
后,
不仅加强了模型对全局信息的关注,同时也显著提高了模型效率。
改进代码如下:
class ResNetBlock(nn.Module):
"""ResNet block with standard convolution layers."""
def __init__(self, c1, c2, s=1, e=4):
"""Initialize convolution with given parameters."""
super().__init__()
c3 = e * c2
self.cv1 = Conv(c1, c2, k=1, s=1, act=True)
self.cv2 = Conv(c2, c2, k=3, s=s, p=1, act=True)
self.cv3 = Conv(c2, c3, k=1, act=False)
self.cv4 = MultiQueryAttentionLayerWithDownSampling(c2, 2, 48, 48, 1, 1, 1)
self.shortcut = nn.Sequential(Conv(c1, c3, k=1, s=s, act=False)) if s != 1 or c1 != c3 else nn.Identity()
def forward(self, x):
"""Forward pass through the ResNet block."""
return F.relu(self.cv3(self.cv4(self.cv2(self.cv1(x)))) + self.shortcut(x))
class ResNetLayer_MQA(nn.Module):
"""ResNet layer with multiple ResNet blocks."""
def __init__(self, c1, c2, s=1, is_first=False, n=1, e=4):
"""Initializes the ResNetLayer given arguments."""
super().__init__()
self.is_first = is_first
if self.is_first:
self.layer = nn.Sequential(
Conv(c1, c2, k=7, s=2, p=3, act=True), nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
)
else:
blocks = [ResNetBlock(c1, c2, s, e=e)]
blocks.extend([ResNetBlock(e * c2, c2, 1, e=e) for _ in range(n - 1)])
self.layer = nn.Sequential(*blocks)
def forward(self, x):
"""Forward pass through the ResNet layer."""
return self.layer(x)
注意❗:需要声明的模块名称为:
ResNetLayer_MQA
。
五、添加步骤
5.1 修改一
① 在
ultralytics/nn/
目录下新建
AddModules
文件夹用于存放模块代码
② 在
AddModules
文件夹下新建
MQA.py
,将
第三节
中的代码粘贴到此处
5.2 修改二
在
AddModules
文件夹下新建
__init__.py
(已有则不用新建),在文件内导入模块:
from .MQA import *
5.3 修改三
在
ultralytics/nn/modules/tasks.py
文件中,需要在两处位置添加各模块类名称。
首先:导入模块
其次:在
parse_model函数
中注册
ResNetLayer_MQA
模块
六、yaml模型文件
6.1 模型改进版本⭐
此处以
ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml
为例,在同目录下创建一个用于自己数据集训练的模型文件
rtdetr-HGBlock_CAA.yaml
。
将
rtdetr-resnet50.yaml
中的内容复制到
rtdetr-HGBlock_CAA.yaml
文件下,修改
nc
数量等于自己数据中目标的数量。
📌 模型的修改方法是将
骨干网络
中的
ResNetLayer模块
替换成
ResNetLayer_MQA模块
。
# Ultralytics YOLO 🚀, AGPL-3.0 license
# RT-DETR-ResNet50 object detection model with P3-P5 outputs.
# 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, ResNetLayer_MQA, [3, 64, 1, True, 1]] # 0
- [-1, 1, ResNetLayer_MQA, [64, 64, 1, False, 3]] # 1
- [-1, 1, ResNetLayer_MQA, [256, 128, 2, False, 4]] # 2
- [-1, 1, ResNetLayer_MQA, [512, 256, 2, False, 6]] # 3
- [-1, 1, ResNetLayer_MQA, [1024, 512, 2, False, 3]] # 4
head:
- [-1, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 5
- [-1, 1, AIFI, [1024, 8]]
- [-1, 1, Conv, [256, 1, 1]] # 7
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [3, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 9
- [[-2, -1], 1, Concat, [1]]
- [-1, 3, RepC3, [256]] # 11
- [-1, 1, Conv, [256, 1, 1]] # 12
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [2, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 14
- [[-2, -1], 1, Concat, [1]] # 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]] # 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]] # cat Y5
- [-1, 3, RepC3, [256]] # F5 (22), pan_blocks.1
- [[16, 19, 22], 1, RTDETRDecoder, [nc]] # Detect(P3, P4, P5)
六、成功运行结果
分别打印网络模型可以看到
ResNetLayer_MQA
已经加入到模型中,并可以进行训练了。
rtdetr-ResNetLayer_MQA :
rtdetr-ResNetLayer_MQA summary: 753 layers, 43,850,275 parameters, 43,850,275 gradients, 137.0 GFLOPs
from n params module arguments
0 -1 1 9536 ultralytics.nn.AddModules.MQA.ResNetLayer_MQA[3, 64, 1, True, 1]
1 -1 1 271104 ultralytics.nn.AddModules.MQA.ResNetLayer_MQA[64, 64, 1, False, 3]
2 -1 1 1367040 ultralytics.nn.AddModules.MQA.ResNetLayer_MQA[256, 128, 2, False, 4]
3 -1 1 7540736 ultralytics.nn.AddModules.MQA.ResNetLayer_MQA[512, 256, 2, False, 6]
4 -1 1 15407104 ultralytics.nn.AddModules.MQA.ResNetLayer_MQA[1024, 512, 2, False, 3]
5 -1 1 524800 ultralytics.nn.modules.conv.Conv [2048, 256, 1, 1, None, 1, 1, False]
6 -1 1 789760 ultralytics.nn.modules.transformer.AIFI [256, 1024, 8]
7 -1 1 66048 ultralytics.nn.modules.conv.Conv [256, 256, 1, 1]
8 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
9 3 1 262656 ultralytics.nn.modules.conv.Conv [1024, 256, 1, 1, None, 1, 1, False]
10 [-2, -1] 1 0 ultralytics.nn.modules.conv.Concat [1]
11 -1 3 2232320 ultralytics.nn.modules.block.RepC3 [512, 256, 3]
12 -1 1 66048 ultralytics.nn.modules.conv.Conv [256, 256, 1, 1]
13 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
14 2 1 131584 ultralytics.nn.modules.conv.Conv [512, 256, 1, 1, None, 1, 1, False]
15 [-2, -1] 1 0 ultralytics.nn.modules.conv.Concat [1]
16 -1 3 2232320 ultralytics.nn.modules.block.RepC3 [512, 256, 3]
17 -1 1 590336 ultralytics.nn.modules.conv.Conv [256, 256, 3, 2]
18 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1]
19 -1 3 2232320 ultralytics.nn.modules.block.RepC3 [512, 256, 3]
20 -1 1 590336 ultralytics.nn.modules.conv.Conv [256, 256, 3, 2]
21 [-1, 7] 1 0 ultralytics.nn.modules.conv.Concat [1]
22 -1 3 2232320 ultralytics.nn.modules.block.RepC3 [512, 256, 3]
23 [16, 19, 22] 1 7303907 ultralytics.nn.modules.head.RTDETRDecoder [1, [256, 256, 256]]
rtdetr-ResNetLayer_MQA summary: 753 layers, 43,850,275 parameters, 43,850,275 gradients, 137.0 GFLOPs