CA和SA双重注意力机制添加,对应代码
时间: 2023-07-07 09:45:38 浏览: 150
以下是使用PyTorch实现添加CA和SA双重注意力机制的代码:
```python
import torch
import torch.nn as nn
import torch.nn.functional as F
class CA(nn.Module):
def __init__(self, in_channels, reduction=16):
super(CA, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv1 = nn.Conv2d(in_channels, in_channels // reduction, kernel_size=1, stride=1, padding=0, bias=False)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(in_channels // reduction, in_channels, kernel_size=1, stride=1, padding=0, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
y = self.avg_pool(x)
y = self.conv1(y)
y = self.relu(y)
y = self.conv2(y)
y = self.sigmoid(y)
return x * y
class SA(nn.Module):
def __init__(self, in_channels, reduction=16):
super(SA, self).__init__()
self.conv1 = nn.Conv2d(in_channels, in_channels // reduction, kernel_size=1, stride=1, padding=0, bias=False)
self.conv2 = nn.Conv2d(in_channels // reduction, in_channels // reduction, kernel_size=1, stride=1, padding=0, bias=False)
self.conv3 = nn.Conv2d(in_channels // reduction, in_channels, kernel_size=1, stride=1, padding=0, bias=False)
self.gamma = nn.Parameter(torch.zeros(1))
def forward(self, x):
batch_size, channels, height, width = x.size()
y = self.conv1(x)
y = F.relu(y, inplace=True)
y = self.conv2(y)
y = F.relu(y, inplace=True)
y = self.conv3(y)
y = F.avg_pool2d(y, kernel_size=(height, width))
y = y.view(batch_size, channels, 1, 1)
y = self.gamma * y
return x + y
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1)
self.ca = CA(64)
self.sa = SA(64)
self.conv2 = nn.Conv2d(64, 128, kernel_size=3, stride=1, padding=1)
self.conv3 = nn.Conv2d(128, 256, kernel_size=3, stride=1, padding=1)
self.fc1 = nn.Linear(256 * 8 * 8, 512)
self.fc2 = nn.Linear(512, 10)
def forward(self, x):
x = self.conv1(x)
x = self.ca(x)
x = self.sa(x)
x = F.relu(x, inplace=True)
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = self.conv2(x)
x = self.ca(x)
x = self.sa(x)
x = F.relu(x, inplace=True)
x = F.max_pool2d(x, kernel_size=2, stride=2)
x = self.conv3(x)
x = self.ca(x)
x = self.sa(x)
x = F.relu(x, inplace=True)
x = x.view(-1, 256 * 8 * 8)
x = self.fc1(x)
x = F.relu(x, inplace=True)
x = self.fc2(x)
return x
```
在上面的代码中,我们首先定义了CA和SA两个注意力模块,然后在Net模型中添加了这两个模块。在forward函数中,我们先将输入的x通过conv1卷积层进行特征提取,然后分别通过CA和SA模块进行注意力加权,再通过ReLU激活函数和max pooling操作进行特征提取和压缩,最后通过全连接层输出分类结果。
阅读全文