残差网络添加SE注意力机制
时间: 2023-11-20 12:57:20 浏览: 213
杭电人工智能导论课程验收作业-完成一个卷积神经网络的修改,加入残差模块,加入注意力机制+源代码+文档说明
残差网络添加SE注意力机制的步骤如下:
1. 定义SE模块,它由两个全连接层组成,第一个全连接层将输入特征图压缩到一定的维度,第二个全连接层将其还原到原始维度,并进行sigmoid激活。
```python
import torch.nn as nn
class SEModule(nn.Module):
def __init__(self, channels, reduction):
super(SEModule, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc1 = nn.Conv2d(channels, channels // reduction, kernel_size=1, padding=0)
self.relu = nn.ReLU(inplace=True)
self.fc2 = nn.Conv2d(channels // reduction, channels, kernel_size=1, padding=0)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
module_input = x
x = self.avg_pool(x)
x = self.fc1(x)
x = self.relu(x)
x = self.fc2(x)
x = self.sigmoid(x)
return module_input * x
```
2. 在残差块的最后一层卷积层后面添加SE模块。
```python
import torch.nn as nn
class ResidualBlock(nn.Module):
def __init__(self, in_channels, out_channels, stride=1, downsample=None, reduction=16):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, kernel_size=3, stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(out_channels)
self.downsample = downsample
self.stride = stride
self.se_module = SEModule(out_channels, reduction)
def forward(self, x):
residual = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
if self.downsample is not None:
residual = self.downsample(x)
out = self.se_module(out) # 添加SE模块
out += residual
out = self.relu(out)
return out
```
3. 在主干网络中使用ResidualBlock构建残差网络,并在需要的地方添加SE模块。
```python
import torch.nn as nn
class ResNet(nn.Module):
def __init__(self, block, layers, num_classes=1000, reduction=16):
super(ResNet, self).__init__()
self.in_channels = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=7, stride=2, padding=3, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
self.layer1 = self.make_layer(block, 64, layers[0], stride=1, reduction=reduction)
self.layer2 = self.make_layer(block, 128, layers[1], stride=2, reduction=reduction)
self.layer3 = self.make_layer(block, 256, layers[2], stride=2, reduction=reduction)
self.layer4 = self.make_layer(block, 512, layers[3], stride=2, reduction=reduction)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
def make_layer(self, block, out_channels, blocks, stride=1, reduction=16):
downsample = None
if stride != 1 or self.in_channels != out_channels * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.in_channels, out_channels * block.expansion, kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(out_channels * block.expansion),
)
layers = []
layers.append(block(self.in_channels, out_channels, stride, downsample, reduction=reduction))
self.in_channels = out_channels * block.expansion
for i in range(1, blocks):
layers.append(block(self.in_channels, out_channels, reduction=reduction))
return nn.Sequential(*layers)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.maxpool(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
```
阅读全文