目标检测算法——YOLOv5/YOLOv7改进之结合CBAM注意力机制

时间:2025-04-02 09:10:19
class ChannelAttentionModule(): def __init__(self, c1, reduction=16): super(ChannelAttentionModule, self).__init__() mid_channel = c1 // reduction self.avg_pool = nn.AdaptiveAvgPool2d(1) self.max_pool = nn.AdaptiveMaxPool2d(1) self.shared_MLP = ( (in_features=c1, out_features=mid_channel), (), (in_features=mid_channel, out_features=c1) ) = () #=SiLU() def forward(self, x): avgout = self.shared_MLP(self.avg_pool(x).view((0),-1)).unsqueeze(2).unsqueeze(3) maxout = self.shared_MLP(self.max_pool(x).view((0),-1)).unsqueeze(2).unsqueeze(3) return (avgout + maxout) class SpatialAttentionModule(): def __init__(self): super(SpatialAttentionModule, self).__init__() self.conv2d = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=7, stride=1, padding=3) #=SiLU() = () def forward(self, x): avgout = (x, dim=1, keepdim=True) maxout, _ = (x, dim=1, keepdim=True) out = ([avgout, maxout], dim=1) out = (self.conv2d(out)) return out class CBAM(): def __init__(self, c1,c2): super(CBAM, self).__init__() self.channel_attention = ChannelAttentionModule(c1) self.spatial_attention = SpatialAttentionModule() def forward(self, x): out = self.channel_attention(x) * x out = self.spatial_attention(out) * out return out