通过旋转特征构造卷积三元注意力模块.

卷积神经网络中的通道注意力和空间注意力通常是并行构造的,本文作者指出应该对其同时建模。Triplet Attention分别沿着通道维度、高度维度和宽度维度应用注意力机制,其中输入特征可以通过维度交换构造;统计函数$Z$选用全局最大池化和全局平均池化。

实验结果表明,Triplet Attention相较于其他注意力方法参数量较小,但是Flops较大。

import torch.nn as nn

class BasicConv(nn.Module):
    def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0):
        super(BasicConv, self).__init__()
        self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride, padding=padding)
        self.bn = nn.BatchNorm2d(out_planes)

    def forward(self, x):
        x = self.conv(x)
        x = self.bn(x)
        return x

class SpatialGate(nn.Module):
    def __init__(self):
        super(SpatialGate, self).__init__()
        kernel_size = 7
        self.spatial = BasicConv(2, 1, kernel_size, stride=1, padding=(kernel_size-1) // 2)
    def forward(self, x):
        x_compress = torch.cat((
            torch.max(x,1)[0].unsqueeze(1),
            torch.mean(x,1).unsqueeze(1)
            ), dim=1)
        x_out = self.spatial(x_compress)
        scale = torch.sigmoid_(x_out) 
        return x * scale

class TripletAttention(nn.Module):
    def __init__(self):
        super(TripletAttention, self).__init__()
        self.ChannelGateH = SpatialGate()
        self.ChannelGateW = SpatialGate()
        elf.SpatialGate = SpatialGate()

    def forward(self, x):
        x_perm1 = x.permute(0,2,1,3).contiguous()
        x_out1 = self.ChannelGateH(x_perm1)
        x_out11 = x_out1.permute(0,2,1,3).contiguous()
        x_perm2 = x.permute(0,3,2,1).contiguous()
        x_out2 = self.ChannelGateW(x_perm2)
        x_out21 = x_out2.permute(0,3,2,1).contiguous
        x_out = self.SpatialGate(x)
        x_out = (1/3)*(x_out + x_out11 + x_out21)
        return x_out