RT-DETR改进策略【Conv和Transformer】| IJCAI 2024 利用FreqFormer中的SFA 空间 - 频率注意力 二次改进HGBlock、ResNetLayer
一、本文介绍
本文记录的是 利用 SFA 空间 - 频率注意力(Spatial-Frequency Attention)和 CTA 通道转置注意力(Channel Transposed Attention)模块优化 RT-DETR的目标检测网络模型 。
SFA
和
CTA
源自图像超分辨率领域的
FreqFormer
模型,
前者结合高频和通道信息到自注意力中
,
后者从通道维度进行自注意力计算
,二者结合能够
有效处理复杂的图像信息
。本文将其应用到
RT-DETR
中,并进行
HGBlock、ResNetLayer的二次创新
,
使网络能够综合空间、频率和通道等多种维度信息,更好地突出重要特征
,从而提升对不同尺度目标和不规则形状目标的特征提取能力。
本文主要使用SFA 空间 - 频率注意力(Spatial-Frequency Attention)模块进行改进。
二、FreqFormer模块介绍
FreqFormer: Frequency-aware Transformer for Lightweight Image Super-resolution
在FreqFormer模型中,SFA(Spatial-Frequency Attention)和CTA(Channel Transposed Attention)模块是关键组成部分,它们针对传统Transformer在图像超分辨率任务中的不足而设计,旨在更好地恢复高频细节,提升模型性能。
2.1 出发点
传统基于Transformer的超分辨率方法存在计算量大和低频偏好问题,自注意力机制在重建高频细节方面表现不佳,因此需要新的模块设计来增强高频信息的恢复能力。
2.2 结构
- SFA模块 :先将输入划分为多个矩形窗口,对每个窗口的特征分别生成查询、键和值,通过自注意力计算得到空间自注意力特征 。接着,使用 Y S F − A Y_{SF - A} Y SF − A 、 X X X 和 V S F V_{SF} V SF 进行多特征提取,经过空间投影、通道投影和频率投影模块,分别提取空间、频率和通道特征。最后通过跨分支加权融合这些特征。
- CTA模块 :沿通道维度进行自注意力计算,将通道划分为多个头,计算查询、键和值进行自注意力操作。之后通过重塑不同注意力头的连接得到通道注意力特征。与SFA不同,CTA仅在通道注意力中聚合空间和通道特征,对于其他特征维度,使用相同的投影方法获取相关特征,再进行信息提取和交叉权重传递。
优势
- SFA模块 :结合高频和通道信息到自注意力中,增强了对高频细节的恢复能力。通过多特征提取和跨分支加权融合,能更好地捕捉空间、频率和通道上下文信息,促进跨维度的块间特征聚合,补偿注意力中长距离细节信息的损失。
- CTA模块 :作为SFA的互补模块,从通道维度进行自注意力计算,进一步聚合空间和通道信息,在减少计算开销的同时,有助于更全面地恢复图像的细节信息,提升模型的整体性能 。通过消融实验可知,CTA和SFA结合使用时,能在减少参数和计算量的情况下取得更好的性能。
论文: https://www.ijcai.org/proceedings/2024/0081.pdf
源码: https://github.com/JPWang-CS/FreqFormer
三、SFA的实现代码
SFA
及其改进的实现代码如下:
import torch
import torch.nn as nn
from einops import reduce
import torch.nn.functional as F
from timm.models.layers import DropPath
from ultralytics.nn.modules.conv import LightConv
def img2windows(img, H_sp, W_sp):
"""
Input: Image (B, C, H, W)
Output: Window Partition (B', N, C)
"""
B, C, H, W = img.shape
img_reshape = img.view(B, C, H // H_sp, H_sp, W // W_sp, W_sp)
img_perm = img_reshape.permute(0, 2, 4, 3, 5, 1).contiguous().reshape(-1, H_sp* W_sp, C)
return img_perm
def windows2img(img_splits_hw, H_sp, W_sp, H, W):
"""
Input: Window Partition (B', N, C)
Output: Image (B, H, W, C)
"""
B = int(img_splits_hw.shape[0] / (H * W / H_sp / W_sp))
img = img_splits_hw.view(B, H // H_sp, W // W_sp, H_sp, W_sp, -1)
img = img.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
return img
class FrequencyProjection(nn.Module):
""" Frequency Projection.
Args:
dim (int): input channels.
"""
def __init__(self, dim):
super().__init__()
self.conv_1 = nn.Conv2d(dim, dim // 2, 1, 1, 0)
self.act = nn.GELU()
self.res_2 = nn.Sequential(
nn.MaxPool2d(3, 1, 1),
nn.Conv2d(dim // 4, dim // 4, 1, 1, 0),
nn.GELU()
)
self.conv_out = nn.Conv2d(dim // 2, dim, 1, 1, 0)
def forward(self, x):
"""
Input: x: (B, C, H, W)
Output: x: (B, C, H, W)
"""
res = x
x = self.conv_1(x)
x1, x2 = x.chunk(2, dim=1)
out = torch.cat((self.act(x1), self.res_2(x2)), dim=1)
out = self.conv_out(out)
return out + res
class ChannelProjection(nn.Module):
""" Channel Projection.
Args:
dim (int): input channels.
"""
def __init__(self, dim):
super().__init__()
self.pro_in = nn.Conv2d(dim, dim // 6, 1, 1, 0)
self.CI1 = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(dim // 6, dim // 6, kernel_size=1)
)
self.CI2 = nn.Sequential(
nn.Conv2d(dim // 6, dim // 6, kernel_size=3, stride=1, padding=1, groups=dim // 6),
nn.Conv2d(dim // 6, dim // 6, 7, stride=1, padding=9, groups=dim // 6, dilation=3),
nn.Conv2d(dim // 6, dim // 6, kernel_size=1)
)
self.pro_out = nn.Conv2d(dim // 6, dim, kernel_size=1)
def forward(self, x):
"""
Input: x: (B, C, H, W)
Output: x: (B, C, H, W)
"""
x = self.pro_in(x)
res = x
ci1 = self.CI1(x)
ci2 = self.CI2(x)
out = self.pro_out(res * ci1 * ci2)
return out
class SpatialProjection(nn.Module):
""" Spatial Projection.
Args:
dim (int): input channels.
"""
def __init__(self, dim):
super().__init__()
self.pro_in = nn.Conv2d(dim, dim // 2, 1, 1, 0)
self.dwconv = nn.Conv2d(dim // 2, dim // 2, kernel_size=3, stride=1, padding=1, groups= dim // 2)
self.pro_out = nn.Conv2d(dim // 4, dim, kernel_size=1)
def forward(self, x):
"""
Input: x: (B, C, H, W)
Output: x: (B, C, H, W)
"""
x = self.pro_in(x)
x1, x2 = self.dwconv(x).chunk(2, dim=1)
x = F.gelu(x1) * x2
x = self.pro_out(x)
return x
class DynamicPosBias(nn.Module):
# The implementation builds on Crossformer code https://github.com/cheerss/CrossFormer/blob/main/models/crossformer.py
""" Dynamic Relative Position Bias.
Args:
dim (int): Number of input channels.
num_heads (int): Number of attention heads.
residual (bool): If True, use residual strage to connect conv.
"""
def __init__(self, dim, num_heads, residual):
super().__init__()
self.residual = residual
self.num_heads = num_heads
self.pos_dim = dim // 4
self.pos_proj = nn.Linear(2, self.pos_dim)
self.pos1 = nn.Sequential(
nn.LayerNorm(self.pos_dim),
nn.ReLU(inplace=True),
nn.Linear(self.pos_dim, self.pos_dim),
)
self.pos2 = nn.Sequential(
nn.LayerNorm(self.pos_dim),
nn.ReLU(inplace=True),
nn.Linear(self.pos_dim, self.pos_dim)
)
self.pos3 = nn.Sequential(
nn.LayerNorm(self.pos_dim),
nn.ReLU(inplace=True),
nn.Linear(self.pos_dim, self.num_heads)
)
def forward(self, biases):
if self.residual:
pos = self.pos_proj(biases) # 2Gh-1 * 2Gw-1, heads
pos = pos + self.pos1(pos)
pos = pos + self.pos2(pos)
pos = self.pos3(pos)
else:
pos = self.pos3(self.pos2(self.pos1(self.pos_proj(biases))))
return pos
class Spatial_Attention(nn.Module):
""" Spatial Self-Attention.
It supports rectangle window (containing square window).
Args:
dim (int): Number of input channels.
idx (int): The indentix of window. (0/1)
split_size (tuple(int)): Height and Width of spatial window.
dim_out (int | None): The dimension of the attention output. Default: None
num_heads (int): Number of attention heads. Default: 6
attn_drop (float): Dropout ratio of attention weight. Default: 0.0
proj_drop (float): Dropout ratio of output. Default: 0.0
qk_scale (float | None): Override default qk scale of head_dim ** -0.5 if set
position_bias (bool): The dynamic relative position bias. Default: True
"""
def __init__(self, dim, idx, split_size=[8,8], dim_out=None, num_heads=6, attn_drop=0., proj_drop=0., qk_scale=None, position_bias=True):
super().__init__()
self.dim = dim
self.dim_out = dim_out or dim
self.split_size = split_size
self.num_heads = num_heads
self.idx = idx
self.position_bias = position_bias
head_dim = dim // num_heads
self.scale = qk_scale or head_dim ** -0.5
if idx == 0:
H_sp, W_sp = self.split_size[0], self.split_size[1]
elif idx == 1:
W_sp, H_sp = self.split_size[0], self.split_size[1]
else:
print ("ERROR MODE", idx)
exit(0)
self.H_sp = H_sp
self.W_sp = W_sp
if self.position_bias:
self.pos = DynamicPosBias(self.dim // 4, self.num_heads, residual=False)
# generate mother-set
position_bias_h = torch.arange(1 - self.H_sp, self.H_sp)
position_bias_w = torch.arange(1 - self.W_sp, self.W_sp)
biases = torch.stack(torch.meshgrid([position_bias_h, position_bias_w]))
biases = biases.flatten(1).transpose(0, 1).contiguous().float()
self.register_buffer('rpe_biases', biases)
# get pair-wise relative position index for each token inside the window
coords_h = torch.arange(self.H_sp)
coords_w = torch.arange(self.W_sp)
coords = torch.stack(torch.meshgrid([coords_h, coords_w]))
coords_flatten = torch.flatten(coords, 1)
relative_coords = coords_flatten[:, :, None] - coords_flatten[:, None, :]
relative_coords = relative_coords.permute(1, 2, 0).contiguous()
relative_coords[:, :, 0] += self.H_sp - 1
relative_coords[:, :, 1] += self.W_sp - 1
relative_coords[:, :, 0] *= 2 * self.W_sp - 1
relative_position_index = relative_coords.sum(-1)
self.register_buffer('relative_position_index', relative_position_index)
self.attn_drop = nn.Dropout(attn_drop)
def im2win(self, x, H, W):
B, N, C = x.shape
x = x.transpose(-2,-1).contiguous().view(B, C, H, W)
x = img2windows(x, self.H_sp, self.W_sp)
# (b win_num_h win_num_w) (win_h win_w) c
# -> (b win_num_h win_num_w) (win_h win_w) num_heads d
# -> (b win_num_h win_num_w) num_heads (win_h win_w) d
x = x.reshape(-1, self.H_sp* self.W_sp, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3).contiguous()
return x
def forward(self, qkv, H, W, mask=None):
"""
Input: qkv: (B, 3*L, C), H, W, mask: (B, N, N), N is the window size
Output: x (B, H, W, C)
"""
q,k,v = qkv[0], qkv[1], qkv[2]
B, L, C = q.shape
assert L == H * W, "flatten img_tokens has wrong size"
# partition the q,k,v, image to window
q = self.im2win(q, H, W)
k = self.im2win(k, H, W)
v = self.im2win(v, H, W)
q = q * self.scale
attn = (q @ k.transpose(-2, -1)) # B head N C @ B head C N --> B head N N
# calculate drpe
if self.position_bias:
pos = self.pos(self.rpe_biases)
# select position bias
relative_position_bias = pos[self.relative_position_index.view(-1)].view(
self.H_sp * self.W_sp, self.H_sp * self.W_sp, -1)
relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous()
attn = attn + relative_position_bias.unsqueeze(0)
N = attn.shape[3]
# use mask for shift window
if mask is not None:
nW = mask.shape[0]
attn = attn.view(B, nW, self.num_heads, N, N) + mask.unsqueeze(1).unsqueeze(0)
attn = attn.view(-1, self.num_heads, N, N)
attn = nn.functional.softmax(attn, dim=-1, dtype=attn.dtype)
attn = self.attn_drop(attn)
x = (attn @ v)
x = x.transpose(1, 2).reshape(-1, self.H_sp* self.W_sp, C) # B head N N @ B head N C
# merge the window, window to image
x = windows2img(x, self.H_sp, self.W_sp, H, W) # B H' W' C
return x
class Spatial_Frequency_Attention(nn.Module):
# The implementation builds on CAT code https://github.com/Zhengchen1999/CAT
""" Spatial Frequency Self-Attention
Args:
dim (int): Number of input channels.
num_heads (int): Number of attention heads. Default: 6
split_size (tuple(int)): Height and Width of spatial window.
shift_size (tuple(int)): Shift size for spatial window.
qkv_bias (bool): If True, add a learnable bias to query, key, value. Default: True
qk_scale (float | None): Override default qk scale of head_dim ** -0.5 if set.
drop (float): Dropout rate. Default: 0.0
attn_drop (float): Attention dropout rate. Default: 0.0
b_idx (int): The index of Block
"""
def __init__(self, dim, num_heads,
reso=64, split_size=[8,8], shift_size=[1,2], qkv_bias=False, qk_scale=None,
drop=0., attn_drop=0., b_idx=0):
super().__init__()
self.dim = dim
self.num_heads = num_heads
self.split_size = split_size
self.shift_size = shift_size
self.b_idx = b_idx
self.patches_resolution = reso
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.hf = nn.Linear(dim, dim, bias=qkv_bias)
assert 0 <= self.shift_size[0] < self.split_size[0], "shift_size must in 0-split_size0"
assert 0 <= self.shift_size[1] < self.split_size[1], "shift_size must in 0-split_size1"
self.branch_num = 2
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(drop)
self.dw_block = nn.Sequential(
nn.Conv2d(dim, dim, 1, 1, 0),
nn.Conv2d(dim, dim, 3, 1, 1, groups=dim)
)
self.attns = nn.ModuleList([
Spatial_Attention(
dim//2, idx = i,
split_size=split_size, num_heads=num_heads//2, dim_out=dim//2,
qk_scale=qk_scale, attn_drop=attn_drop, proj_drop=drop, position_bias=True)
for i in range(self.branch_num)])
if self.b_idx > 0 and (self.b_idx - 2) % 4 == 0:
attn_mask = self.calculate_mask(self.patches_resolution, self.patches_resolution)
self.register_buffer("attn_mask_0", attn_mask[0])
self.register_buffer("attn_mask_1", attn_mask[1])
else:
self.register_buffer("attn_mask_0", None)
self.register_buffer("attn_mask_1", None)
self.channel_projection = ChannelProjection(dim)
self.spatial_projection = SpatialProjection(dim)
self.frequency_projection = FrequencyProjection(dim)
def calculate_mask(self, H, W):
# The implementation builds on Swin Transformer code https://github.com/microsoft/Swin-Transformer/blob/main/models/swin_transformer.py
# calculate attention mask for shift window
img_mask_0 = torch.zeros((1, H, W, 1)) # 1 H W 1 idx=0
img_mask_1 = torch.zeros((1, H, W, 1)) # 1 H W 1 idx=1
h_slices_0 = (slice(0, -self.split_size[0]),
slice(-self.split_size[0], -self.shift_size[0]),
slice(-self.shift_size[0], None))
w_slices_0 = (slice(0, -self.split_size[1]),
slice(-self.split_size[1], -self.shift_size[1]),
slice(-self.shift_size[1], None))
h_slices_1 = (slice(0, -self.split_size[1]),
slice(-self.split_size[1], -self.shift_size[1]),
slice(-self.shift_size[1], None))
w_slices_1 = (slice(0, -self.split_size[0]),
slice(-self.split_size[0], -self.shift_size[0]),
slice(-self.shift_size[0], None))
cnt = 0
for h in h_slices_0:
for w in w_slices_0:
img_mask_0[:, h, w, :] = cnt
cnt += 1
cnt = 0
for h in h_slices_1:
for w in w_slices_1:
img_mask_1[:, h, w, :] = cnt
cnt += 1
# calculate mask for window-0
img_mask_0 = img_mask_0.view(1, H // self.split_size[0], self.split_size[0], W // self.split_size[1], self.split_size[1], 1)
img_mask_0 = img_mask_0.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, self.split_size[0], self.split_size[1], 1) # nW, sw[0], sw[1], 1
mask_windows_0 = img_mask_0.view(-1, self.split_size[0] * self.split_size[1])
attn_mask_0 = mask_windows_0.unsqueeze(1) - mask_windows_0.unsqueeze(2)
attn_mask_0 = attn_mask_0.masked_fill(attn_mask_0 != 0, float(-100.0)).masked_fill(attn_mask_0 == 0, float(0.0))
# calculate mask for window-1
img_mask_1 = img_mask_1.view(1, H // self.split_size[1], self.split_size[1], W // self.split_size[0], self.split_size[0], 1)
img_mask_1 = img_mask_1.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, self.split_size[1], self.split_size[0], 1) # nW, sw[1], sw[0], 1
mask_windows_1 = img_mask_1.view(-1, self.split_size[1] * self.split_size[0])
attn_mask_1 = mask_windows_1.unsqueeze(1) - mask_windows_1.unsqueeze(2)
attn_mask_1 = attn_mask_1.masked_fill(attn_mask_1 != 0, float(-100.0)).masked_fill(attn_mask_1 == 0, float(0.0))
return attn_mask_0, attn_mask_1
def forward(self, x, H, W):
"""
Input: x: (B, H*W, C), H, W
Output: x: (B, H*W, C)
"""
B, L, C = x.shape
assert L == H * W, "flatten img_tokens has wrong size"
hf = self.hf(x).transpose(-2,-1).contiguous().view(B, C, H, W)
hf = self.frequency_projection(hf)
qkv = self.qkv(x).reshape(B, -1, 3, C).permute(2, 0, 1, 3) # 3, B, HW, C
v = qkv[2].transpose(-2,-1).contiguous().view(B, C, H, W)
# image padding
max_split_size = max(self.split_size[0], self.split_size[1])
pad_l = pad_t = 0
pad_r = (max_split_size - W % max_split_size) % max_split_size
pad_b = (max_split_size - H % max_split_size) % max_split_size
qkv = qkv.reshape(3*B, H, W, C).permute(0, 3, 1, 2) # 3B C H W
# hw填充
qkv = F.pad(qkv, (pad_l, pad_r, pad_t, pad_b)).reshape(3, B, C, -1).transpose(-2, -1) # l r t b
_H = pad_b + H
_W = pad_r + W
_L = _H * _W
# window-0 and window-1 on split channels [C/2, C/2]; for square windows (e.g., 8x8), window-0 and window-1 can be merged
# shift in block: (0, 4, 8, ...), (2, 6, 10, ...), (0, 4, 8, ...), (2, 6, 10, ...), ...
if self.b_idx > 0 and (self.b_idx - 2) % 4 == 0:
qkv = qkv.view(3, B, _H, _W, C)
qkv_0 = torch.roll(qkv[:,:,:,:,:C//2], shifts=(-self.shift_size[0], -self.shift_size[1]), dims=(2, 3))
qkv_0 = qkv_0.view(3, B, _L, C//2)
qkv_1 = torch.roll(qkv[:,:,:,:,C//2:], shifts=(-self.shift_size[1], -self.shift_size[0]), dims=(2, 3))
qkv_1 = qkv_1.view(3, B, _L, C//2)
if self.patches_resolution != _H or self.patches_resolution != _W:
mask_tmp = self.calculate_mask(_H, _W)
x1_shift = self.attns[0](qkv_0, _H, _W, mask=mask_tmp[0].to(x.device))
x2_shift = self.attns[1](qkv_1, _H, _W, mask=mask_tmp[1].to(x.device))
else:
x1_shift = self.attns[0](qkv_0, _H, _W, mask=self.attn_mask_0)
x2_shift = self.attns[1](qkv_1, _H, _W, mask=self.attn_mask_1)
x1 = torch.roll(x1_shift, shifts=(self.shift_size[0], self.shift_size[1]), dims=(1, 2))
x2 = torch.roll(x2_shift, shifts=(self.shift_size[1], self.shift_size[0]), dims=(1, 2))
x1 = x1[:, :H, :W, :].reshape(B, L, C//2)
x2 = x2[:, :H, :W, :].reshape(B, L, C//2)
# attention output
attened_x = torch.cat([x1,x2], dim=2)
else:
x1 = self.attns[0](qkv[:,:,:,:C//2], _H, _W)[:, :H, :W, :].reshape(B, L, C//2)
x2 = self.attns[1](qkv[:,:,:,C//2:], _H, _W)[:, :H, :W, :].reshape(B, L, C//2)
# attention output
attened_x = torch.cat([x1,x2], dim=2)
conv_x = self.dw_block(v)
# C-Map (before sigmoid)
channel_map = self.channel_projection(conv_x)
conv_x = conv_x + channel_map
# high_fre info mix channel
hf = hf + channel_map
channel_map = reduce(channel_map, 'b c h w -> b c 1 1', 'mean').permute(0, 2, 3, 1).contiguous().view(B, 1, C)
# S-Map (before sigmoid)
attention_reshape = attened_x.transpose(-2,-1).contiguous().view(B, C, H, W)
spatial_map = self.spatial_projection(attention_reshape)
# high_fre info mix spatial
hf = hf + attention_reshape
# C-I
attened_x = attened_x * torch.sigmoid(channel_map) * torch.sigmoid(reduce(hf, 'b c h w -> b c 1 1', 'mean').permute(0, 2, 3, 1).contiguous().view(B, 1, C))
# S-I
conv_x = torch.sigmoid(spatial_map) * conv_x * torch.sigmoid(hf)
conv_x = conv_x.permute(0, 2, 3, 1).contiguous().view(B, L, C)
x = attened_x + conv_x + hf.permute(0, 2, 3, 1).contiguous().view(B, L, C)
x = self.proj(x)
x = self.proj_drop(x)
return x
class Channel_Transposed_Attention(nn.Module):
# The implementation builds on XCiT code https://github.com/facebookresearch/xcit
""" Channel Transposed Self-Attention
Args:
dim (int): Number of input channels.
num_heads (int): Number of attention heads. Default: 6
qkv_bias (bool): If True, add a learnable bias to query, key, value. Default: True
qk_scale (float | None): Override default qk scale of head_dim ** -0.5 if set.
attn_drop (float): Attention dropout rate. Default: 0.0
drop_path (float): Stochastic depth rate. Default: 0.0
"""
def __init__(self, dim, num_heads=8, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0.):
super().__init__()
self.num_heads = num_heads
self.temperature = nn.Parameter(torch.ones(num_heads, 1, 1))
self.qkv = nn.Linear(dim, dim * 3, bias=qkv_bias)
self.attn_drop = nn.Dropout(attn_drop)
self.proj = nn.Linear(dim, dim)
self.proj_drop = nn.Dropout(proj_drop)
self.channel_projection = ChannelProjection(dim)
self.spatial_projection = SpatialProjection(dim)
self.dwconv = nn.Sequential(
nn.Conv2d(dim, dim, kernel_size=1),
nn.Conv2d(dim, dim, kernel_size=3, stride=1, padding=1, groups=dim),
)
# self.frequency_projection = FrequencyProjection(dim)
def forward(self, x, H, W):
"""
Input: x: (B, H*W, C), H, W
Output: x: (B, H*W, C)
"""
B, N, C = x.shape
qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads)
qkv = qkv.permute(2, 0, 3, 1, 4) # 3 B num_heads N D
q, k, v = qkv[0], qkv[1], qkv[2]
# B num_heads D N
q = q.transpose(-2, -1)
k = k.transpose(-2, -1)
v = v.transpose(-2, -1)
v_ = v.reshape(B, C, N).contiguous().view(B, C, H, W)
q = torch.nn.functional.normalize(q, dim=-1)
k = torch.nn.functional.normalize(k, dim=-1)
attn = (q @ k.transpose(-2, -1)) * self.temperature
attn = attn.softmax(dim=-1)
attn = self.attn_drop(attn)
# attention output
attened_x = (attn @ v).permute(0, 3, 1, 2).reshape(B, N, C)
# convolution output
conv_x = self.dwconv(v_)
# C-Map (before sigmoid)
attention_reshape = attened_x.transpose(-2,-1).contiguous().view(B, C, H, W)
channel_map = self.channel_projection(attention_reshape)
attened_x = attened_x + channel_map.permute(0, 2, 3, 1).contiguous().view(B, N, C)
channel_map = reduce(channel_map, 'b c h w -> b c 1 1', 'mean')
# S-Map (before sigmoid)
spatial_map = self.spatial_projection(conv_x).permute(0, 2, 3, 1).contiguous().view(B, N, C)
# S-I
attened_x = attened_x * torch.sigmoid(spatial_map)
# C-I
conv_x = conv_x * torch.sigmoid(channel_map)
conv_x = conv_x.permute(0, 2, 3, 1).contiguous().view(B, N, C)
x = attened_x + conv_x
x = self.proj(x)
x = self.proj_drop(x)
return x
class FrequencyGate(nn.Module):
""" Frequency-Gate.
Args:
dim (int): Input channels.
"""
def __init__(self, dim):
super().__init__()
self.norm = nn.LayerNorm(dim)
self.conv = nn.Sequential(
nn.Conv2d(dim, dim, 1, 1, 0),
nn.Conv2d(dim, dim, 3, 1, 1, groups=dim),
)
def forward(self, x, H, W):
"""
Input: x: (B, H*W, C), H, W
Output: x: (B, H*W, C)
"""
B, N, C = x.shape
x1, x2 = x.chunk(2, dim = -1)
x2 = self.conv(self.norm(x2).transpose(1, 2).contiguous().view(B, C//2, H, W)).flatten(2).transpose(-1, -2).contiguous()
return x1 * x2
class DFFN(nn.Module):
""" Dual frequency aggregation Feed-Forward Network.
Args:
in_features (int): Number of input channels.
hidden_features (int | None): Number of hidden channels. Default: None
out_features (int | None): Number of output channels. Default: None
act_layer (nn.Module): Activation layer. Default: nn.GELU
drop (float): Dropout rate. Default: 0.0
"""
def __init__(self, in_features, hidden_features=None, out_features=None, act_layer=nn.GELU, drop=0.):
super().__init__()
out_features = out_features or in_features
hidden_features = hidden_features or in_features
self.fc1 = nn.Linear(in_features, hidden_features)
self.act = act_layer()
self.fg = FrequencyGate(hidden_features//2)
self.fc2 = nn.Linear(hidden_features//2, out_features)
self.drop = nn.Dropout(drop)
def forward(self, x, H, W):
"""
Input: x: (B, H*W, C), H, W
Output: x: (B, H*W, C)
"""
x = self.fc1(x)
x = self.act(x)
x = self.drop(x)
x = self.fg(x, H, W)
x = self.drop(x)
x = self.fc2(x)
x = self.drop(x)
return x
class FCA_SFA(nn.Module):
def __init__(self, dim, num_heads=4, reso=64, split_size=[2,4],shift_size=[1,2], expansion_factor=4., qkv_bias=False, qk_scale=None, drop=0.,
attn_drop=0., drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm, b_idx=0):
super().__init__()
self.norm1 = norm_layer(dim)
self.norm2 = norm_layer(dim)
# SFA
self.attn = Spatial_Frequency_Attention(
dim, num_heads=num_heads, reso=reso, split_size=split_size, shift_size=shift_size, qkv_bias=qkv_bias, qk_scale=qk_scale,
drop=drop, attn_drop=attn_drop, b_idx=b_idx
)
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
ffn_hidden_dim = int(dim * expansion_factor)
# DFFN
self.ffn = DFFN(in_features=dim, hidden_features=ffn_hidden_dim, out_features=dim, act_layer=act_layer)
def forward(self, x):
"""
Input: x: (B, H*W, C), x_size: (H, W)
Output: x: (B, H*W, C)
"""
b, n, H, W = x.size()
x = x.flatten(2).transpose(1, 2)
x = x + self.drop_path(self.attn(self.norm1(x), H, W))
x = x + self.drop_path(self.ffn(self.norm2(x), H, W))
return x.transpose(1, 2).reshape((b, n, H, W))
class FCA_CTA(nn.Module):
def __init__(self, dim, num_heads=4, expansion_factor=4., qkv_bias=False, qk_scale=None, drop=0.,
attn_drop=0., drop_path=0., act_layer=nn.GELU, norm_layer=nn.LayerNorm, b_idx=0):
super().__init__()
self.norm1 = norm_layer(dim)
self.norm2 = norm_layer(dim)
# CTA
self.attn = Channel_Transposed_Attention(
dim, num_heads=num_heads, qkv_bias=qkv_bias, qk_scale=qk_scale, attn_drop=attn_drop,
proj_drop=drop
)
self.drop_path = DropPath(drop_path) if drop_path > 0. else nn.Identity()
ffn_hidden_dim = int(dim * expansion_factor)
# DFFN
self.ffn = DFFN(in_features=dim, hidden_features=ffn_hidden_dim, out_features=dim, act_layer=act_layer)
def forward(self, x):
"""
Input: x: (B, H*W, C), x_size: (H, W)
Output: x: (B, H*W, C)
"""
b, n, H, W = x.size()
x = x.flatten(2).transpose(1, 2)
x = x + self.drop_path(self.attn(self.norm1(x), H, W))
x = x + self.drop_path(self.ffn(self.norm2(x), H, W))
return x.transpose(1, 2).reshape((b, n, H, W))
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 HGBlock_FCA_SFA(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=nn.ReLU()):
"""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
self.cv = FCA_SFA(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.cv(self.ec(self.sc(torch.cat(y, 1))))
return y + x if self.add else y
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 = FCA_SFA(c2)
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_FCA_SFA(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)
四、创新模块
4.1 改进点⭐
模块改进方法
:在
HGBlock
中加入
SFA
(
第五节讲解添加步骤
)。
SFA
添加后如下:
class HGBlock_FCA_SFA(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=nn.ReLU()):
"""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
self.cv = FCA_SFA(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.cv(self.ec(self.sc(torch.cat(y, 1))))
return y + x if self.add else y
4.2 改进点2⭐
模块改进方法
:基于
SFA模块
的
ResNetLayer
(
第五节讲解添加步骤
)。
改进代码如下:
首先添加
SFA
改进
ResNetBlock
模块。
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 = FCA_SFA(c2)
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))
然后将
ResNetLayer
重命名为
ResNetLayer_FCA_SFA
class ResNetLayer_FCA_SFA(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)
注意❗:在
第五小节
中需要声明的模块名称为:
HGBlock_FCA_SFA
和
ResNetLayer_FCA_SFA
。
五、添加步骤
5.1 修改一
① 在
ultralytics/nn/
目录下新建
AddModules
文件夹用于存放模块代码
② 在
AddModules
文件夹下新建
SFA.py
,将
第三节
中的代码粘贴到此处
5.2 修改二
在
AddModules
文件夹下新建
__init__.py
(已有则不用新建),在文件内导入模块:
from .SFA import *
5.3 修改三
在
ultralytics/nn/modules/tasks.py
文件中,需要在指定位置添加各模块类名称。
首先:导入模块
其次:在
parse_model函数
中注册
HGBlock_FCA_SFA
和
ResNetLayer_FCA_SFA
模块
在
parse_model函数
的如下添加模块:
六、yaml模型文件
6.1 模型改进版本⭐
此处以
ultralytics/cfg/models/rt-detr/rtdetr-l.yaml
为例,在同目录下创建一个用于自己数据集训练的模型文件
rtdetr-l-HGBlock_FCA_SFA.yaml
。
将
rtdetr-l.yaml
中的内容复制到
rtdetr-l-HGBlock_FCA_SFA.yaml
文件下,修改
nc
数量等于自己数据中目标的数量。
📌 模型的修改方法是将
骨干网络
中的
HGBlock模块
替换成
HGBlock_FCA_SFA模块
。
# 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, 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-P4/16
- [-1, 6, HGBlock_FCA_SFA, [192, 1024, 5, True, False]] # cm, c2, k, light, shortcut
- [-1, 6, HGBlock_FCA_SFA, [192, 1024, 5, True, True]]
- [-1, 6, HGBlock_FCA_SFA, [192, 1024, 5, True, True]] # stage 3
- [-1, 1, DWConv, [1024, 3, 2, 1, False]] # 8-P5/32
- [-1, 6, HGBlock, [384, 2048, 5, True, False]] # stage 4
head:
- [-1, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 10 input_proj.2
- [-1, 1, AIFI, [1024, 8]]
- [-1, 1, Conv, [256, 1, 1]] # 12, Y5, lateral_convs.0
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [7, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 14 input_proj.1
- [[-2, -1], 1, Concat, [1]]
- [-1, 3, RepC3, [256]] # 16, fpn_blocks.0
- [-1, 1, Conv, [256, 1, 1]] # 17, Y4, lateral_convs.1
- [-1, 1, nn.Upsample, [None, 2, "nearest"]]
- [3, 1, Conv, [256, 1, 1, None, 1, 1, False]] # 19 input_proj.0
- [[-2, -1], 1, Concat, [1]] # cat backbone P4
- [-1, 3, RepC3, [256]] # X3 (21), fpn_blocks.1
- [-1, 1, Conv, [256, 3, 2]] # 22, downsample_convs.0
- [[-1, 17], 1, Concat, [1]] # cat Y4
- [-1, 3, RepC3, [256]] # F4 (24), pan_blocks.0
- [-1, 1, Conv, [256, 3, 2]] # 25, downsample_convs.1
- [[-1, 12], 1, Concat, [1]] # cat Y5
- [-1, 3, RepC3, [256]] # F5 (27), pan_blocks.1
- [[21, 24, 27], 1, RTDETRDecoder, [nc]] # Detect(P3, P4, P5)
6.2 模型改进版本2⭐
此处以
ultralytics/cfg/models/rt-detr/rtdetr-resnet50.yaml
为例,在同目录下创建一个用于自己数据集训练的模型文件
rtdetr-ResNetLayer_FCA_SFA.yaml
。
将
rtdetr-resnet50.yaml
中的内容复制到
rtdetr-ResNetLayer_FCA_SFA.yaml
文件下,修改
nc
数量等于自己数据中目标的数量。
📌 模型的修改方法是将
骨干网络
中的所有
ResNetLayer模块
替换成
ResNetLayer_FCA_SFA模块
。
# 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_FCA_SFA, [3, 64, 1, True, 1]] # 0
- [-1, 1, ResNetLayer_FCA_SFA, [64, 64, 1, False, 3]] # 1
- [-1, 1, ResNetLayer_FCA_SFA, [256, 128, 2, False, 4]] # 2
- [-1, 1, ResNetLayer_FCA_SFA, [512, 256, 2, False, 6]] # 3
- [-1, 1, ResNetLayer_FCA_SFA, [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)
七、成功运行结果
打印网络模型可以看到
HGBlock_FCA_SFA
和
ResNetLayer_FCA_SFA
已经加入到模型中,并可以进行训练了。
rtdetr-l-HGBlock_FCA_SFA :
rtdetr-l-HGBlock_FCA_SFA summary: 913 layers, 90,279,017 parameters, 90,279,017 gradients, 291.5 GFLOPs
from n params module arguments
0 -1 1 25248 ultralytics.nn.modules.block.HGStem [3, 32, 48]
1 -1 6 155072 ultralytics.nn.modules.block.HGBlock [48, 48, 128, 3, 6]
2 -1 1 1408 ultralytics.nn.modules.conv.DWConv [128, 128, 3, 2, 1, False]
3 -1 6 839296 ultralytics.nn.modules.block.HGBlock [128, 96, 512, 3, 6]
4 -1 1 5632 ultralytics.nn.modules.conv.DWConv [512, 512, 3, 2, 1, False]
5 -1 6 20852322 ultralytics.nn.AddModules.SFA_CTA.HGBlock_FCA_SFA[512, 192, 1024, 5, 6, True, False]
6 -1 6 21212770 ultralytics.nn.AddModules.SFA_CTA.HGBlock_FCA_SFA[1024, 192, 1024, 5, 6, True, True]
7 -1 6 21212770 ultralytics.nn.AddModules.SFA_CTA.HGBlock_FCA_SFA[1024, 192, 1024, 5, 6, True, True]
8 -1 1 11264 ultralytics.nn.modules.conv.DWConv [1024, 1024, 3, 2, 1, False]
9 -1 6 6708480 ultralytics.nn.modules.block.HGBlock [1024, 384, 2048, 5, 6, True, False]
10 -1 1 524800 ultralytics.nn.modules.conv.Conv [2048, 256, 1, 1, None, 1, 1, False]
11 -1 1 789760 ultralytics.nn.modules.transformer.AIFI [256, 1024, 8]
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 7 1 262656 ultralytics.nn.modules.conv.Conv [1024, 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 66048 ultralytics.nn.modules.conv.Conv [256, 256, 1, 1]
18 -1 1 0 torch.nn.modules.upsampling.Upsample [None, 2, 'nearest']
19 3 1 131584 ultralytics.nn.modules.conv.Conv [512, 256, 1, 1, None, 1, 1, False]
20 [-2, -1] 1 0 ultralytics.nn.modules.conv.Concat [1]
21 -1 3 2232320 ultralytics.nn.modules.block.RepC3 [512, 256, 3]
22 -1 1 590336 ultralytics.nn.modules.conv.Conv [256, 256, 3, 2]
23 [-1, 17] 1 0 ultralytics.nn.modules.conv.Concat [1]
24 -1 3 2232320 ultralytics.nn.modules.block.RepC3 [512, 256, 3]
25 -1 1 590336 ultralytics.nn.modules.conv.Conv [256, 256, 3, 2]
26 [-1, 12] 1 0 ultralytics.nn.modules.conv.Concat [1]
27 -1 3 2232320 ultralytics.nn.modules.block.RepC3 [512, 256, 3]
28 [21, 24, 27] 1 7303907 ultralytics.nn.modules.head.RTDETRDecoder [1, [256, 256, 256]]
rtdetr-l-HGBlock_FCA_SFA summary: 913 layers, 90,279,017 parameters, 90,279,017 gradients, 291.5 GFLOPs
rtdetr-ResNetLayer_FCA_SFA :
rtdetr-ResNetLayer_FCA_SFA summary: 1,825 layers, 65,905,208 parameters, 65,905,208 gradients, 192.7 GFLOPs
from n params module arguments
0 -1 1 9536 ultralytics.nn.AddModules.SFA_CTA.ResNetLayer_FCA_SFA[3, 64, 1, True, 1]
1 -1 1 452034 ultralytics.nn.AddModules.SFA_CTA.ResNetLayer_FCA_SFA[64, 64, 1, False, 3]
2 -1 1 2446756 ultralytics.nn.AddModules.SFA_CTA.ResNetLayer_FCA_SFA[256, 128, 2, False, 4]
3 -1 1 14357804 ultralytics.nn.AddModules.SFA_CTA.ResNetLayer_FCA_SFA[512, 256, 2, False, 6]
4 -1 1 29384323 ultralytics.nn.AddModules.SFA_CTA.ResNetLayer_FCA_SFA[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_FCA_SFA summary: 1,825 layers, 65,905,208 parameters, 65,905,208 gradients, 192.7 GFLOPs