本文全网首发独家改进:提出新颖的注意力BSAM(BiLevel Spatial Attention Module),创新度极佳,适合科研创新,效果秒杀CBAM,Channel Attention Spartial Attention升级为新颖的 BiLevel Attention Spartial Attention
1)作为注意力BSAM使用;
推荐指数:五星
BSAM | 亲测在多个数据集能够实现涨点,多尺度特性在小目标检测表现也十分出色。
1.原理介绍
1.1.CBAM:通道注意力和空间注意力的集成者
轻量级的卷积注意力模块,它结合了通道和空间的注意力机制模块
论文题目:《CBAM: Convolutional Block Attention Module》 论文地址: https://arxiv.org/pdf/1807.06521.pdf
1.2 自研模块原理
详见:https://blog.csdn.net/m0_63774211/article/details/134407373
2.1 BSAM加入ultralytics/nn/attention/BSAM.py
3.1 BSAM加入ultralytics/nn/attention/BSAM.py
核心代码:
代码语言:javascript复制class SpatialAttention(nn.Module):
# Spatial-attention module
def __init__(self, kernel_size=7):
super().__init__()
assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
padding = 3 if kernel_size == 7 else 1
self.cv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
self.act = nn.Sigmoid()
def forward(self, x):
return x * self.act(self.cv1(torch.cat([torch.mean(x, 1, keepdim=True), torch.max(x, 1, keepdim=True)[0]], 1)))
class ChannelAttentionModule(nn.Module):
def __init__(self, c1, reduction=16, light=False):
super(ChannelAttentionModule, self).__init__()
mid_channel = c1 // reduction
self.light = light
self.avg_pool = nn.AdaptiveAvgPool2d(1)
if self.light:
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.shared_MLP = nn.Sequential(
nn.Linear(in_features=c1, out_features=mid_channel),
nn.LeakyReLU(0.1, inplace=True),
nn.Linear(in_features=mid_channel, out_features=c1)
)
else:
self.shared_MLP = nn.Conv2d(c1, c1, 1, 1, 0, bias=True)
self.act = nn.Sigmoid()
def forward(self, x):
if self.light:
avgout = self.shared_MLP(self.avg_pool(x).view(x.size(0), -1)).unsqueeze(2).unsqueeze(3)
maxout = self.shared_MLP(self.max_pool(x).view(x.size(0), -1)).unsqueeze(2).unsqueeze(3)
fc_out = (avgout maxout)
else:
fc_out = (self.shared_MLP(self.avg_pool(x)))
return x * self.act(fc_out)
我正在参与2023腾讯技术创作特训营第三期有奖征文,组队打卡瓜分大奖!