注意力之spatial attention

spatial attention

channel attention是对通道加权,spatial attention是对spatial加权

Parameter-Free Spatial Attention Network for Person Re-Identification

注意力之spatial attention_第1张图片
feature map 对通道求和获得H*W矩阵,然后reshape, softmax, reshape获得注意力矩阵。

CBAM: Convolutional Block Attention Module

既有channel attention又有spatial attention

注意力之spatial attention_第2张图片
channel attention
注意力之spatial attention_第3张图片
spatial attention
注意力之spatial attention_第4张图片

class SpatialAttentionModule(nn.Module):
    def __init__(self):
        super(SpatialAttentionModule, self).__init__()
        self.conv2d = nn.Conv2d(in_channels=2, out_channels=1, kernel_size=7, stride=1, padding=3)
        self.sigmoid = nn.Sigmoid()

    def forward(self, x):
        avgout = torch.mean(x, dim=1, keepdim=True)
        maxout, _ = torch.max(x, dim=1, keepdim=True)
        out = torch.cat([avgout, maxout], dim=1)
        out = self.sigmoid(self.conv2d(out))
        return out

你可能感兴趣的:(深度学习,深度学习,pytorch,神经网络)