论文传送门:BAM: Bottleneck Attention Module
BAM的目的:
为网络添加注意力机制。
BAM的结构:
①通道注意力机制(Channel attention branch):与SEblock相似;
②空间注意力机制(Spatial attention branch):1x1卷积进行维度缩减,2个3x3空洞卷积(dilated convolution)增加感受野,1x1卷积输出单通道权重;
③二者并联。
import torch
import torch.nn as nn
class ChannelAttention(nn.Module): # Channel attention branch
def __init__(self, channels, r=16): # r: reduction ratio
super(ChannelAttention, self).__init__()
hidden_channels = channels // r
self.attn = nn.Sequential(
nn.AdaptiveAvgPool2d(1), # avgpool
nn.Conv2d(channels, hid