当前位置:网站首页>[network] channel attention network and spatial attention network

[network] channel attention network and spatial attention network

2022-07-06 04:18:00 Hard working yuan

  • CBAM: Address of thesis

  • Purpose :
    Convolution is to extract features by mixing the information of channel and space dimensions . In terms of attention ,SE Only pay attention to channel attention , Did not consider spatial attention . therefore , This paper proposes CBAM—— A convolution module that focuses on both channels and spatial attention , It can be used for CNNs Architecture , To improve feature map The ability to express the characteristics of .

  • Network structure :
    Main network structure  Insert picture description here
    CAM and SAM Structure
     Insert picture description here
    CAM: The channel attention mechanism is to learn the weighting coefficient of a different channel , At the same time, all areas are considered
     Insert picture description here
     Insert picture description here
    SAM: Spatial attention mechanism is to learn the coefficients of different areas of the whole picture , All channels are considered at the same time .
     Insert picture description here

  • Pytorch Code implementation :

    import torch
    from torch import nn
     
     
    class ChannelAttention(nn.Module):
        def __init__(self, in_planes, ratio=16):
            super(ChannelAttention, self).__init__()
            self.avg_pool = nn.AdaptiveAvgPool2d(1)
            self.max_pool = nn.AdaptiveMaxPool2d(1)
     
            self.fc1   = nn.Conv2d(in_planes, in_planes // 16, 1, bias=False)
            self.relu1 = nn.ReLU()
            self.fc2   = nn.Conv2d(in_planes // 16, in_planes, 1, bias=False)
     
            self.sigmoid = nn.Sigmoid()
     
        def forward(self, x):
            avg_out = self.fc2(self.relu1(self.fc1(self.avg_pool(x))))
            max_out = self.fc2(self.relu1(self.fc1(self.max_pool(x))))
            out = avg_out + max_out
            return self.sigmoid(out)
     
     
    class SpatialAttention(nn.Module):
        def __init__(self, kernel_size=7):
            super(SpatialAttention, self).__init__()
     
            assert kernel_size in (3, 7), 'kernel size must be 3 or 7'
            padding = 3 if kernel_size == 7 else 1
     
            self.conv1 = nn.Conv2d(2, 1, kernel_size, padding=padding, bias=False)
            self.sigmoid = nn.Sigmoid()
            self.register_buffer()
     
        def forward(self, x):
            avg_out = torch.mean(x, dim=1, keepdim=True)
            max_out, _ = torch.max(x, dim=1, keepdim=True)
            x = torch.cat([avg_out, max_out], dim=1)
            x = self.conv1(x)
            return self.sigmoid(x)
    
  • Reference resources :https://blog.csdn.net/oYeZhou/article/details/116664508

原网站

版权声明
本文为[Hard working yuan]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/02/202202132234013601.html