🚩 Channel Attention Module
利用 特征间的通道关系
来生成通道注意图 ;
由于feature map的每个channel都被认为是 一个feature检测器
,因此 channel 的注意力集中在 给定输入图像的 "什么" 是有意义的
;
为了有效地计算通道注意力,采用 压缩输入特征映射的空间维度
的方法 ;
文中同时使用 AvgPool (平均池化)
和 MaxPool (最大池化)
的方法,并证明了这种做法比单独使用一种池化方法更具有表征力;
式中,σ 为 sigmoid
函数 ,W0 ∈ RC/r×C ,W1 ∈ RC×C/r ,MLP的权重 W0 和 W1 共享,在W0 前是 ReLU
激活函数 ;
🚩 Spatial Attention Module
利用 特征间的空间关系
生成空间注意图 ;
与通道注意模块不同的是,空间注意模块关注的是 信息部分 "在哪里"
,作为通道注意模块的补充 ;
为了计算空间注意力,首先沿着通道轴应用 平均池化和最大池化
操作,并将它们连接起来以生成一个有效的 特征描述符
;
使用两个池化操作聚合一个feature map的通道信息,生成两个2D maps :
Fsavg ∈ R1×H×W 和 Fsmax ∈ R1×H×W ;
每个都表示通道的 平均池化特性
和 最大池化特性
,然后利用一个标准的卷积层进行连接和卷积操作,得到二维空间注意力图 ;
式中,σ 为 sigmoid
函数 ,f 7x7 为 7 x 7 大小的卷积核 ;
🚩 CBAM 的应用
以上是将 CBAM
结合 ResBlock
应用于ResNet中 ;
两个模块可以以并行或顺序的方式放置,实验测试发现 顺序排列
比 并行排列
有更好的结果 ;
最后,分别使用 ResNet50 、ResNet50+SENet 、ResNet50+CBAM 进行实验得到可视化结果 :
实验表明 CBAM 性能超越了 SENet
🚀 CBAM 复现
这里实现的是 CBAM-ResNet
系列网络 :
# Here is the code :
import torch
import torch.nn as nn
import torch.nn.functional as F
from torchinfo import summary
class ChannelAttention(nn.Module): # Channel Attention Module
def \_\_init\_\_(self, in_planes):
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, kernel_size=1, bias=False)
self.relu = nn.ReLU()
self.fc2 = nn.Conv2d(in_planes // 16, in_planes, kernel_size=1, bias=False)
self.sigmoid = nn.Sigmoid()
def forward(self, x):
avg_out = self.avg_pool(x)
avg_out = self.fc1(avg_out)
avg_out = self.relu(avg_out)
avg_out = self.fc2(avg_out)
max_out = self.max_pool(x)
max_out = self.fc1(max_out)
max_out = self.relu(max_out)
max_out = self.fc2(max_out)
out = avg_out + max_out
out = self.sigmoid(out)
return out
class SpatialAttention(nn.Module): # Spatial Attention Module
def \_\_init\_\_(self):
super(SpatialAttention, self).__init__()
self.conv1 = nn.Conv2d(2, 1, kernel_size=7, padding=3, bias=False)
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)
out = torch.cat([avg_out, max_out], dim=1)
out = self.conv1(out)
out = self.sigmoid(out)
return out
class BasicBlock(nn.Module): # 左侧的 residual block 结构(18-layer、34-layer)
expansion = 1
def \_\_init\_\_(self, in_planes, planes, stride=1): # 两层卷积 Conv2d + Shutcuts
super(BasicBlock, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3,
stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.channel = ChannelAttention(self.expansion\*planes) # Channel Attention Module
self.spatial = SpatialAttention() # Spatial Attention Module
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion\*planes: # Shutcuts用于构建 Conv Block 和 Identity Block
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, self.expansion\*planes,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(self.expansion\*planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
CBAM_Cout = self.channel(out)
out = out \* CBAM_Cout
CBAM_Sout = self.spatial(out)
out = out \* CBAM_Sout
out += self.shortcut(x)
out = F.relu(out)
return out
class Bottleneck(nn.Module): # 右侧的 residual block 结构(50-layer、101-layer、152-layer)
expansion = 4
def \_\_init\_\_(self, in_planes, planes, stride=1): # 三层卷积 Conv2d + Shutcuts
super(Bottleneck, self).__init__()
self.conv1 = nn.Conv2d(in_planes, planes, kernel_size=1, bias=False)
self.bn1 = nn.BatchNorm2d(planes)
self.conv2 = nn.Conv2d(planes, planes, kernel_size=3,
stride=stride, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(planes)
self.conv3 = nn.Conv2d(planes, self.expansion\*planes,
kernel_size=1, bias=False)
self.bn3 = nn.BatchNorm2d(self.expansion\*planes)
self.channel = ChannelAttention(self.expansion\*planes) # Channel Attention Module
self.spatial = SpatialAttention() # Spatial Attention Module
self.shortcut = nn.Sequential()
if stride != 1 or in_planes != self.expansion\*planes: # Shutcuts用于构建 Conv Block 和 Identity Block
self.shortcut = nn.Sequential(
nn.Conv2d(in_planes, self.expansion\*planes,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(self.expansion\*planes)
)
def forward(self, x):
out = F.relu(self.bn1(self.conv1(x)))
out = F.relu(self.bn2(self.conv2(out)))
out = self.bn3(self.conv3(out))
CBAM_Cout = self.channel(out)
out = out \* CBAM_Cout
CBAM_Sout = self.spatial(out)
out = out \* CBAM_Sout
out += self.shortcut(x)
out = F.relu(out)
return out
class CBAM\_ResNet(nn.Module):
def \_\_init\_\_(self, block, num_blocks, num_classes=1000):
super(CBAM_ResNet, self).__init__()
self.in_planes = 64
self.conv1 = nn.Conv2d(3, 64, kernel_size=3,
stride=1, padding=1, bias=False) # conv1
self.bn1 = nn.BatchNorm2d(64)
self.layer1 = self._make_layer(block, 64, num_blocks[0], stride=1) # conv2\_x
self.layer2 = self._make_layer(block, 128, num_blocks[1], stride=2) # conv3\_x
self.layer3 = self._make_layer(block, 256, num_blocks[2], stride=2) # conv4\_x
self.layer4 = self._make_layer(block, 512, num_blocks[3], stride=2) # conv5\_x
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.linear = nn.Linear(512 \* block.expansion, num_classes)
def \_make\_layer(self, block, planes, num_blocks, stride):
strides = [stride] + [1]\*(num_blocks-1)
layers = []
for stride in strides:
layers.append(block(self.in_planes, planes, stride))
self.in_planes = planes \* block.expansion
return nn.Sequential(\*layers)
def forward(self, x):
x = F.relu(self.bn1(self.conv1(x)))
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = torch.flatten(x, 1)
out = self.linear(x)
return out
def CBAM\_ResNet18():
return CBAM_ResNet(BasicBlock, [2, 2, 2, 2])
def CBAM\_ResNet34():
return CBAM_ResNet(BasicBlock, [3, 4, 6, 3])
def CBAM\_ResNet50():
return CBAM_ResNet(Bottleneck, [3, 4, 6, 3])
def CBAM\_ResNet101():
return CBAM_ResNet(Bottleneck, [3, 4, 23, 3])
def CBAM\_ResNet152():
return CBAM_ResNet(Bottleneck, [3, 8, 36, 3])
def test():
net = CBAM_ResNet50()
y = net(torch.randn(1, 3, 224, 224))
print(y.size())
summary(net, (1, 3, 224, 224))
if __name__ == '\_\_main\_\_':
test()
输出结果:
torch.Size([1, 1000])
===============================================================================================
Layer (type:depth-idx) Output Shape Param #
===============================================================================================
CBAM_ResNet -- --
├─Conv2d: 1-1 [1, 64, 224, 224] 1,728
├─BatchNorm2d: 1-2 [1, 64, 224, 224] 128
├─Sequential: 1-3 [1, 256, 224, 224] --
│ └─Bottleneck: 2-1 [1, 256, 224, 224] --
│ │ └─Conv2d: 3-1 [1, 64, 224, 224] 4,096
│ │ └─BatchNorm2d: 3-2 [1, 64, 224, 224] 128
│ │ └─Conv2d: 3-3 [1, 64, 224, 224] 36,864
│ │ └─BatchNorm2d: 3-4 [1, 64, 224, 224] 128
│ │ └─Conv2d: 3-5 [1, 256, 224, 224] 16,384
│ │ └─BatchNorm2d: 3-6 [1, 256, 224, 224] 512
│ │ └─ChannelAttention: 3-7 [1, 256, 1, 1] 8,192
│ │ └─SpatialAttention: 3-8 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-9 [1, 256, 224, 224] 16,896
│ └─Bottleneck: 2-2 [1, 256, 224, 224] --
│ │ └─Conv2d: 3-10 [1, 64, 224, 224] 16,384
│ │ └─BatchNorm2d: 3-11 [1, 64, 224, 224] 128
│ │ └─Conv2d: 3-12 [1, 64, 224, 224] 36,864
│ │ └─BatchNorm2d: 3-13 [1, 64, 224, 224] 128
│ │ └─Conv2d: 3-14 [1, 256, 224, 224] 16,384
│ │ └─BatchNorm2d: 3-15 [1, 256, 224, 224] 512
│ │ └─ChannelAttention: 3-16 [1, 256, 1, 1] 8,192
│ │ └─SpatialAttention: 3-17 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-18 [1, 256, 224, 224] --
│ └─Bottleneck: 2-3 [1, 256, 224, 224] --
│ │ └─Conv2d: 3-19 [1, 64, 224, 224] 16,384
│ │ └─BatchNorm2d: 3-20 [1, 64, 224, 224] 128
│ │ └─Conv2d: 3-21 [1, 64, 224, 224] 36,864
│ │ └─BatchNorm2d: 3-22 [1, 64, 224, 224] 128
│ │ └─Conv2d: 3-23 [1, 256, 224, 224] 16,384
│ │ └─BatchNorm2d: 3-24 [1, 256, 224, 224] 512
│ │ └─ChannelAttention: 3-25 [1, 256, 1, 1] 8,192
│ │ └─SpatialAttention: 3-26 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-27 [1, 256, 224, 224] --
├─Sequential: 1-4 [1, 512, 112, 112] --
│ └─Bottleneck: 2-4 [1, 512, 112, 112] --
│ │ └─Conv2d: 3-28 [1, 128, 224, 224] 32,768
│ │ └─BatchNorm2d: 3-29 [1, 128, 224, 224] 256
│ │ └─Conv2d: 3-30 [1, 128, 112, 112] 147,456
│ │ └─BatchNorm2d: 3-31 [1, 128, 112, 112] 256
│ │ └─Conv2d: 3-32 [1, 512, 112, 112] 65,536
│ │ └─BatchNorm2d: 3-33 [1, 512, 112, 112] 1,024
│ │ └─ChannelAttention: 3-34 [1, 512, 1, 1] 32,768
│ │ └─SpatialAttention: 3-35 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-36 [1, 512, 112, 112] 132,096
│ └─Bottleneck: 2-5 [1, 512, 112, 112] --
│ │ └─Conv2d: 3-37 [1, 128, 112, 112] 65,536
│ │ └─BatchNorm2d: 3-38 [1, 128, 112, 112] 256
│ │ └─Conv2d: 3-39 [1, 128, 112, 112] 147,456
│ │ └─BatchNorm2d: 3-40 [1, 128, 112, 112] 256
│ │ └─Conv2d: 3-41 [1, 512, 112, 112] 65,536
│ │ └─BatchNorm2d: 3-42 [1, 512, 112, 112] 1,024
│ │ └─ChannelAttention: 3-43 [1, 512, 1, 1] 32,768
│ │ └─SpatialAttention: 3-44 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-45 [1, 512, 112, 112] --
│ └─Bottleneck: 2-6 [1, 512, 112, 112] --
│ │ └─Conv2d: 3-46 [1, 128, 112, 112] 65,536
│ │ └─BatchNorm2d: 3-47 [1, 128, 112, 112] 256
│ │ └─Conv2d: 3-48 [1, 128, 112, 112] 147,456
│ │ └─BatchNorm2d: 3-49 [1, 128, 112, 112] 256
│ │ └─Conv2d: 3-50 [1, 512, 112, 112] 65,536
│ │ └─BatchNorm2d: 3-51 [1, 512, 112, 112] 1,024
│ │ └─ChannelAttention: 3-52 [1, 512, 1, 1] 32,768
│ │ └─SpatialAttention: 3-53 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-54 [1, 512, 112, 112] --
│ └─Bottleneck: 2-7 [1, 512, 112, 112] --
│ │ └─Conv2d: 3-55 [1, 128, 112, 112] 65,536
│ │ └─BatchNorm2d: 3-56 [1, 128, 112, 112] 256
│ │ └─Conv2d: 3-57 [1, 128, 112, 112] 147,456
│ │ └─BatchNorm2d: 3-58 [1, 128, 112, 112] 256
│ │ └─Conv2d: 3-59 [1, 512, 112, 112] 65,536
│ │ └─BatchNorm2d: 3-60 [1, 512, 112, 112] 1,024
│ │ └─ChannelAttention: 3-61 [1, 512, 1, 1] 32,768
│ │ └─SpatialAttention: 3-62 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-63 [1, 512, 112, 112] --
├─Sequential: 1-5 [1, 1024, 56, 56] --
│ └─Bottleneck: 2-8 [1, 1024, 56, 56] --
│ │ └─Conv2d: 3-64 [1, 256, 112, 112] 131,072
│ │ └─BatchNorm2d: 3-65 [1, 256, 112, 112] 512
│ │ └─Conv2d: 3-66 [1, 256, 56, 56] 589,824
│ │ └─BatchNorm2d: 3-67 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-68 [1, 1024, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-69 [1, 1024, 56, 56] 2,048
│ │ └─ChannelAttention: 3-70 [1, 1024, 1, 1] 131,072
│ │ └─SpatialAttention: 3-71 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-72 [1, 1024, 56, 56] 526,336
│ └─Bottleneck: 2-9 [1, 1024, 56, 56] --
│ │ └─Conv2d: 3-73 [1, 256, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-74 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-75 [1, 256, 56, 56] 589,824
│ │ └─BatchNorm2d: 3-76 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-77 [1, 1024, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-78 [1, 1024, 56, 56] 2,048
│ │ └─ChannelAttention: 3-79 [1, 1024, 1, 1] 131,072
│ │ └─SpatialAttention: 3-80 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-81 [1, 1024, 56, 56] --
│ └─Bottleneck: 2-10 [1, 1024, 56, 56] --
│ │ └─Conv2d: 3-82 [1, 256, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-83 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-84 [1, 256, 56, 56] 589,824
│ │ └─BatchNorm2d: 3-85 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-86 [1, 1024, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-87 [1, 1024, 56, 56] 2,048
│ │ └─ChannelAttention: 3-88 [1, 1024, 1, 1] 131,072
│ │ └─SpatialAttention: 3-89 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-90 [1, 1024, 56, 56] --
│ └─Bottleneck: 2-11 [1, 1024, 56, 56] --
│ │ └─Conv2d: 3-91 [1, 256, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-92 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-93 [1, 256, 56, 56] 589,824
│ │ └─BatchNorm2d: 3-94 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-95 [1, 1024, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-96 [1, 1024, 56, 56] 2,048
│ │ └─ChannelAttention: 3-97 [1, 1024, 1, 1] 131,072
│ │ └─SpatialAttention: 3-98 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-99 [1, 1024, 56, 56] --
│ └─Bottleneck: 2-12 [1, 1024, 56, 56] --
│ │ └─Conv2d: 3-100 [1, 256, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-101 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-102 [1, 256, 56, 56] 589,824
│ │ └─BatchNorm2d: 3-103 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-104 [1, 1024, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-105 [1, 1024, 56, 56] 2,048
│ │ └─ChannelAttention: 3-106 [1, 1024, 1, 1] 131,072
│ │ └─SpatialAttention: 3-107 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-108 [1, 1024, 56, 56] --
│ └─Bottleneck: 2-13 [1, 1024, 56, 56] --
│ │ └─Conv2d: 3-109 [1, 256, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-110 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-111 [1, 256, 56, 56] 589,824
│ │ └─BatchNorm2d: 3-112 [1, 256, 56, 56] 512
│ │ └─Conv2d: 3-113 [1, 1024, 56, 56] 262,144
│ │ └─BatchNorm2d: 3-114 [1, 1024, 56, 56] 2,048
│ │ └─ChannelAttention: 3-115 [1, 1024, 1, 1] 131,072
│ │ └─SpatialAttention: 3-116 [1, 1, 1, 1] 98
│ │ └─Sequential: 3-117 [1, 1024, 56, 56] --
├─Sequential: 1-6 [1, 2048, 28, 28] --
│ └─Bottleneck: 2-14 [1, 2048, 28, 28] --
│ │ └─Conv2d: 3-118 [1, 512, 56, 56] 524,288
│ │ └─BatchNorm2d: 3-119 [1, 512, 56, 56] 1,024
│ │ └─Conv2d: 3-120 [1, 512, 28, 28] 2,359,296
│ │ └─BatchNorm2d: 3-121 [1, 512, 28, 28] 1,024
│ │ └─Conv2d: 3-122 [1, 2048, 28, 28] 1,048,576