论文题目:EPSANet: An Efficient Pyramid Squeeze Attention Block on Convolutional Neural Network
收录:CVPR2021
论文地址:https://arxiv.org/abs/2105.14447
项目地址:GitHub - murufeng/EPSANet
问题&解决使用通道注意力和空间注意力可以提升性能。如,SE模块,能以较低成本提升性能,但仅考虑通道信息,忽略了空间信息;BAM和CBAM结合空间和通道注意力来丰富注意力图。但仍有两个问题:
- 如何有效捕获和利用不同尺度的特征图的空间信息来丰富特征空间;
- 通道或空间注意力只能有效捕捉局部信息,难以建立long-range的通道依赖关系。
现有方法虽能解决上述问题,但会增加模型复杂度和计算开销。论文提出一个低成本且有效的注意力模块——金字塔压缩注意力(Pyramid Squeeze Attention,PSA),旨在以较低模型复杂度学习注意力权重,并有效整合局部注意力和全局注意力以建立long-range长期通道依赖关系。PSA模块可以处理多尺度的输入特征图的空间信息并且能够有效地建立多尺度通道注意力间的长期依赖关系。然后,将PSA 模块替换掉ResNet网络Bottleneck中的3x3卷积,其余保持不变,得到新的EPSA(efficient pyramid split attention)模块。基于EPSA block论文构建了一个新的骨干网络EPSANet。它既可以提供强有力的多尺度特征表示能力。EPSANet在图像识别中的Top-1 Acc大幅度优于现有技术,而且在计算参数量上有更加高效。
主要贡献- 提出一种新的Efficient Pyramid Squeeze Attention(EPSA)块,有效提取更细粒度的多尺度空间信息,并发展long-range远程通道依赖性;灵活可扩展,适用于各种网络架构。
- 提出EPSANet主干网络,可以学习更丰富的多尺度特征表示并自适应地中心校准跨维度的通道注意力权重。
- EPSANet在ImageNet和COCO数据集上的图像分类、目标检测、实例分割取得很好的结果。
通道注意机制允许网络选择性地加权每个通道的重要性,从而生成更多信息输出。
SE模块SE块由两部分组成:Squeeze压缩和Excitation激励,分别用于编码全局信息和自适应重新校准通道关系。通道数据使用全局平均池化GAP来生成,将全局空间信息嵌入到通道描述中。全局平均池化公式为:
再用两个全连接层组合通道间的线性信息,帮助通道高维和低维信息的交互。c-th通道权重计算公式:
和分别表示两个全连接层,前一个降维,后一个维度恢复。
PSA模块本文主要是建立一个更高效的通道注意力机制。为此,提出了一种新的金字塔压缩注意力(PSA)模块。PSA模块主要通过四个步骤实现:
- 首先,利用SPC模块来对通道进行切分,然后针对每个通道特征图上的空间信息进行多尺度特征提取;
- 第二,利用SEWeight模块提取不同尺度特征图的通道注意力,得到每个不同尺度上的通道注意力向量;
- 第三,利用Softmax对多尺度通道注意力向量进行特征重新校准,得到新的多尺度通道交互后的注意力权重。
- 第四,对重新校准的权重和相应的特征图按元素进行点乘 *** 作,输出得到一个多尺度特征信息注意力加权之后的特征图。该特征图多尺度信息表示能力更丰富。
PSA模块中实现多尺度特征提取最重要的模块就是SPC。
其中,
然后将多尺度的特征图进行拼接后得到特征图F。
EPSANetEPSA block结构如最右图所示,主要将ResNet中bottleneck部分的3x3卷积替换为PSA module。
基于EPSA block,论文提出一种新的骨干网络架构:EPSANet,并且根据PSA module中的分组卷积的大小,具体网络结构配置如下:
实验 细节 结果直接看图就行,网络、主干、参数,性能比较,比较一清二楚。
ImageNet 图像分类 COCO2017 目标检测 COCO2017 实例分割 主要代码 SE_weight_module.pyimport torch.nn as nn
class SEWeightModule(nn.Module):
def __init__(self, channels, reduction=16):
super(SEWeightModule, 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):
out = self.avg_pool(x)
out = self.fc1(out)
out = self.relu(out)
out = self.fc2(out)
weight = self.sigmoid(out)
return weight
epsanet.py
import torch
import torch.nn as nn
import math
from .SE_weight_module import SEWeightModule
def conv(in_planes, out_planes, kernel_size=3, stride=1, padding=1, dilation=1, groups=1):
"""standard convolution with padding"""
return nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size, stride=stride,
padding=padding, dilation=dilation, groups=groups, bias=False)
def conv1x1(in_planes, out_planes, stride=1):
"""1x1 convolution"""
return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)
class PSAModule(nn.Module):
def __init__(self, inplans, planes, conv_kernels=[3, 5, 7, 9], stride=1, conv_groups=[1, 4, 8, 16]):
super(PSAModule, self).__init__()
self.conv_1 = conv(inplans, planes//4, kernel_size=conv_kernels[0], padding=conv_kernels[0]//2,
stride=stride, groups=conv_groups[0])
self.conv_2 = conv(inplans, planes//4, kernel_size=conv_kernels[1], padding=conv_kernels[1]//2,
stride=stride, groups=conv_groups[1])
self.conv_3 = conv(inplans, planes//4, kernel_size=conv_kernels[2], padding=conv_kernels[2]//2,
stride=stride, groups=conv_groups[2])
self.conv_4 = conv(inplans, planes//4, kernel_size=conv_kernels[3], padding=conv_kernels[3]//2,
stride=stride, groups=conv_groups[3])
self.se = SEWeightModule(planes // 4)
self.split_channel = planes // 4
self.softmax = nn.Softmax(dim=1)
def forward(self, x):
batch_size = x.shape[0]
x1 = self.conv_1(x)
x2 = self.conv_2(x)
x3 = self.conv_3(x)
x4 = self.conv_4(x)
feats = torch.cat((x1, x2, x3, x4), dim=1)
feats = feats.view(batch_size, 4, self.split_channel, feats.shape[2], feats.shape[3])
x1_se = self.se(x1)
x2_se = self.se(x2)
x3_se = self.se(x3)
x4_se = self.se(x4)
x_se = torch.cat((x1_se, x2_se, x3_se, x4_se), dim=1)
attention_vectors = x_se.view(batch_size, 4, self.split_channel, 1, 1)
attention_vectors = self.softmax(attention_vectors)
feats_weight = feats * attention_vectors
for i in range(4):
x_se_weight_fp = feats_weight[:, i, :, :]
if i == 0:
out = x_se_weight_fp
else:
out = torch.cat((x_se_weight_fp, out), 1)
return out
class EPSABlock(nn.Module):
expansion = 4
def __init__(self, inplanes, planes, stride=1, downsample=None, norm_layer=None, conv_kernels=[3, 5, 7, 9],
conv_groups=[1, 4, 8, 16]):
super(EPSABlock, self).__init__()
if norm_layer is None:
norm_layer = nn.BatchNorm2d
# Both self.conv2 and self.downsample layers downsample the input when stride != 1
self.conv1 = conv1x1(inplanes, planes)
self.bn1 = norm_layer(planes)
self.conv2 = PSAModule(planes, planes, stride=stride, conv_kernels=conv_kernels, conv_groups=conv_groups)
self.bn2 = norm_layer(planes)
self.conv3 = conv1x1(planes, planes * self.expansion)
self.bn3 = norm_layer(planes * self.expansion)
self.relu = nn.ReLU(inplace=True)
self.downsample = downsample
self.stride = stride
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out = self.relu(out)
out = self.conv3(out)
out = self.bn3(out)
if self.downsample is not None:
identity = self.downsample(x)
out += identity
out = self.relu(out)
return out
class EPSANet(nn.Module):
def __init__(self,block, layers, num_classes=1000):
super(EPSANet, self).__init__()
self.inplanes = 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_layers(block, 64, layers[0], stride=1)
self.layer2 = self._make_layers(block, 128, layers[1], stride=2)
self.layer3 = self._make_layers(block, 256, layers[2], stride=2)
self.layer4 = self._make_layers(block, 512, layers[3], stride=2)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512 * block.expansion, num_classes)
for m in self.modules():
if isinstance(m, nn.Conv2d):
n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
m.weight.data.normal_(0, math.sqrt(2. / n))
elif isinstance(m, nn.BatchNorm2d):
m.weight.data.fill_(1)
m.bias.data.zero_()
def _make_layers(self, block, planes, num_blocks, stride=1):
downsample = None
if stride != 1 or self.inplanes != planes * block.expansion:
downsample = nn.Sequential(
nn.Conv2d(self.inplanes, planes * block.expansion,
kernel_size=1, stride=stride, bias=False),
nn.BatchNorm2d(planes * block.expansion),
)
layers = []
layers.append(block(self.inplanes, planes, stride, downsample))
self.inplanes = planes * block.expansion
for i in range(1, num_blocks):
layers.append(block(self.inplanes, planes))
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
def epsanet50():
model = EPSANet(EPSABlock, [3, 4, 6, 3], num_classes=1000)
return model
def epsanet101():
model = EPSANet(EPSABlock, [3, 4, 23, 3], num_classes=1000)
return model
参考博客
EPSANet: 一种高效的多尺度通道注意力机制,主要提出了金字塔分割注意力模块,即插即用,效果显著,已开源! - 知乎
真的写得太详细了!!!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)