最近看了Attentional Feature Fusion这篇文章,对其提出的注意力机制模块很感兴趣,所以就上github找了一下,为了方便自己记录和使用,所以就复制到这里了。
源代码的github的网址:https://github.com/YimianDai/open-aff/aff_pytorch/aff_net/fusion.py
论文的下载地址:https://arxiv.org/abs/2009.14082
MS-CAM的代码如下:
class MS_CAM(nn.Module):
'''
单特征 进行通道加权,作用类似SE模块
'''
def __init__(self, channels=64, r=4):
super(MS_CAM, self).__init__()
inter_channels = int(channels // r)
self.local_att = nn.Sequential(
nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(inter_channels),
nn.ReLU(inplace=True),
nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(channels),
)
self.global_att = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0),
nn.BatchNorm2d(inter_channels),
nn.ReLU(inplace=True),
nn.Conv2d(inter_channels, channels, kernel_size=1, stride=