注意力機制(通道注意機制、空間注意力機制、CBAM、SELayer)

參考連接: 注意力機制
參考連接: 深度學習卷積神經網絡重要結構之通道注意力和空間注意力模塊
參考連接: 用於卷積神經網絡的注意力機制(Attention)----CBAM: Convolutional Block Attention Module
參考連接: moskomule/senet.pytorch
參考連接: Squeeze-and-Excitation Networks
參考連接: CBAM: Convolutional Block Attention Modulepython

空間注意力機制:
在這裏插入圖片描述
代碼實驗展現:git

import torch
from torch import nn

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)  # 7,3 3,1
        self.sigmoid = nn.Sigmoid()

    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)

if __name__ == '__main__':
    SA = SpatialAttention(7)
    data_in = torch.randn(8,32,300,300)
    data_out = SA(data_in)
    print(data_in.shape)  # torch.Size([8, 32, 300, 300])
    print(data_out.shape)  # torch.Size([8, 1, 300, 300])

控制檯結果輸出展現:github

Windows PowerShell
版權全部 (C) Microsoft Corporation。保留全部權利。

嘗試新的跨平臺 PowerShell https://aka.ms/pscore6

加載我的及系統配置文件用了 1003 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM>  & 'D:\Anaconda3\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '55088' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM\ 
空間注意力機制.py'
torch.Size([8, 32, 300, 300])
torch.Size([8, 1, 300, 300])
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM> conda activate base
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM>

通道注意力機制:web

在這裏插入圖片描述代碼實驗展現:網絡

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 // ratio, 1, bias=False)
		self.relu1 = nn.ReLU()
		self.fc2 = nn.Conv2d(in_planes // ratio, 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)


if __name__ == '__main__':
    CA = ChannelAttention(32)
    data_in = torch.randn(8,32,300,300)
    data_out = CA(data_in)
    print(data_in.shape)  # torch.Size([8, 32, 300, 300])
    print(data_out.shape)  # torch.Size([8, 32, 1, 1])

控制檯結果輸出展現:svg

Windows PowerShell
版權全部 (C) Microsoft Corporation。保留全部權利。

嘗試新的跨平臺 PowerShell https://aka.ms/pscore6

加載我的及系統配置文件用了 882 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM> conda activate base
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM>  & 'D:\Anaconda3\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '55339' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM\ 
通道注意力機制.py'
torch.Size([8, 32, 300, 300])
torch.Size([8, 32, 1, 1])
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM>

CBAM注意力機制:
在這裏插入圖片描述
代碼實驗展現:學習

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 // ratio, 1, bias=False)
        self.relu1 = nn.ReLU()
        self.fc2 = nn.Conv2d(in_planes // ratio, 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)  # 7,3 3,1
        self.sigmoid = nn.Sigmoid()

    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)

class CBAM(nn.Module):
    def __init__(self, in_planes, ratio=16, kernel_size=7):
        super(CBAM, self).__init__()
        self.ca = ChannelAttention(in_planes, ratio)
        self.sa = SpatialAttention(kernel_size)
        
    def forward(self, x):
        out = x * self.ca(x)
        result = out * self.sa(out)
        return result


if __name__ == '__main__':
    print('testing ChannelAttention'.center(100,'-'))
    torch.manual_seed(seed=20200910)
    CA = ChannelAttention(32)
    data_in = torch.randn(8,32,300,300)
    data_out = CA(data_in)
    print(data_in.shape)  # torch.Size([8, 32, 300, 300])
    print(data_out.shape)  # torch.Size([8, 32, 1, 1])




if __name__ == '__main__':
    print('testing SpatialAttention'.center(100,'-'))
    torch.manual_seed(seed=20200910)
    SA = SpatialAttention(7)
    data_in = torch.randn(8,32,300,300)
    data_out = SA(data_in)
    print(data_in.shape)  # torch.Size([8, 32, 300, 300])
    print(data_out.shape)  # torch.Size([8, 1, 300, 300])



if __name__ == '__main__':
    print('testing CBAM'.center(100,'-'))
    torch.manual_seed(seed=20200910)
    cbam = CBAM(32, 16, 7)
    data_in = torch.randn(8,32,300,300)
    data_out = cbam(data_in)
    print(data_in.shape)  # torch.Size([8, 32, 300, 300])
    print(data_out.shape)  # torch.Size([8, 1, 300, 300])

控制檯結果輸出展現:測試

Windows PowerShell
版權全部 (C) Microsoft Corporation。保留全部權利。

嘗試新的跨平臺 PowerShell https://aka.ms/pscore6

加載我的及系統配置文件用了 1029 毫秒。
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM>  & 'D:\Anaconda3\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '55659' '--' 'c:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM\cbam注意力機制.py'
--------------------------------------testing ChannelAttention--------------------------------------
torch.Size([8, 32, 300, 300])
torch.Size([8, 32, 1, 1])
--------------------------------------testing SpatialAttention--------------------------------------
torch.Size([8, 32, 300, 300])
torch.Size([8, 1, 300, 300])
--------------------------------------------testing CBAM--------------------------------------------
torch.Size([8, 32, 300, 300])
torch.Size([8, 32, 300, 300])
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM> conda activate base
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM> 
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM> 
(base) PS C:\Users\chenxuqi\Desktop\News4cxq\測試注意力機制CBAM>

SE注意力機制:ui

在這裏插入圖片描述
代碼實驗展現:url

from torch import nn
import torch

class SELayer(nn.Module):
    def __init__(self, channel, reduction=16):
        super(SELayer, self).__init__()
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // reduction, bias=False),
            nn.ReLU(inplace=True),
            nn.Linear(channel // reduction, channel, bias=False),
            nn.Sigmoid()
        )

    def forward(self, x):
        b, c, _, _ = x.size()
        y = self.avg_pool(x).view(b, c)
        y = self.fc(y).view(b, c, 1, 1)
        return x * y.expand_as(x)
        # return x * y


if __name__ == '__main__':
    torch.manual_seed(seed=20200910)
    data_in = torch.randn(8,32,300,300)
    SE = SELayer(32) 
    data_out = SE(data_in)
    print(data_in.shape)  # torch.Size([8, 32, 300, 300])
    print(data_out.shape)  # torch.Size([8, 32, 300, 300])

控制檯輸出結果展現:

Windows PowerShell
版權全部 (C) Microsoft Corporation。保留全部權利。

嘗試新的跨平臺 PowerShell https://aka.ms/pscore6

加載我的及系統配置文件用了 979 毫秒。
(base) PS F:\Iris_SSD_small\senet.pytorch-master>  & 'D:\Anaconda3\envs\pytorch_1.7.1_cu102\python.exe' 'c:\Users\chenxuqi\.vscode\extensions\ms-python.python-2021.1.502429796\pythonFiles\lib\python\debugpy\launcher' '54904' '--' 'f:\Iris_SSD_small\senet.pytorch-master\senet\se_module.py'
torch.Size([8, 32, 300, 300])
torch.Size([8, 32, 300, 300])
(base) PS F:\Iris_SSD_small\senet.pytorch-master> conda activate pytorch_1.7.1_cu102
(pytorch_1.7.1_cu102) PS F:\Iris_SSD_small\senet.pytorch-master>

本文同步分享在 博客「敲代碼的小風」(CSDN)。
若有侵權,請聯繫 support@oschina.cn 刪除。
本文參與「OSC源創計劃」,歡迎正在閱讀的你也加入,一塊兒分享。

相關文章
相關標籤/搜索