赞
踩
论文地址:ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
ECA注意力模块是在CVPR 2020的论文"ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks"中提出的。
ECA(Efficient Channel Attention)是一种轻量级的通道注意力机制,它通过一个1D卷积层来学习通道注意力,并减少计算复杂度。ECA注意力机制避免了降维,而是利用1维卷积实现了局部跨通道交互,从而提取通道间的依赖关系。
神经网络在提取特征时,所获取的特征图中并不是每一个特征层的贡献都一样,不同的特征层对于结果的作用权重是不相同。
ECA注意力机制将通道注意力机制引入卷积神经网络,通过对每个通道的特征图进行全局自适应加权,提升了特征的表达能力。
实证分析表明降维会对渠道关注度的预测产生副作用,而且对所有渠道的相关性进行捕获是低效且不必要的。SE块使用两个FC层计算权重。与之不同的是,ECA通过执行大小为k的快速一维卷积来生成通道权值,其中k通过通道维C的函数自适应地确定。
虽然SEVar2和SE-Var3都保持通道维数不变,但后者的性能更好。主要的区别是SE-Var3捕获跨通道交互,而SEVar2不捕获。这说明跨通道互动有助于学习有效注意。但是SE-Var3涉及大量的参数,导致模型复杂度过高。从有效卷积的角度来看,SE-Var2可视为深度可分离卷积(Chollet 2017)。自然,组卷积作为另一种有效的卷积,也可以用来捕获跨通道交互。给定一个FC层,组卷积将它分成多个组,并在每个组中独立地执行线性变换。
公式推理过程:
对于不降维的聚合特征 y ∈ RC,可以学习通道注意 :
W 为 C x C 的参数矩阵 ;
Wvar2 是一个对角矩阵,包含C个参数 ;
Wvar3 是一个完整的矩阵,包含 C×C 的参数 ;
关键的区别在于:SE-var3考虑了跨通道交互,而SE-var2没有考虑,因此SE-V ar3的性能更好 ;
在 ECA-Net 中,探索了另一种获取 局部跨通道交互 的方法,以保证效率和有效性,使用一个 波段矩阵Wk 来学习通道注意力:
其中,C1D
表示一维卷积 ;
由于使用1D卷积来捕获局部的跨通道交互,k决定了交互的覆盖范围,不同的通道数和不同的CNN架构的卷积块可能会有所不同。尽管k可以手动调优,但它将消耗大量计算资源。k与通道维数c有关,这是合理的。一般认为,通道尺寸越大,长期交互作用越强,而通道尺寸越小,短期交互作用越强。
大致可以分为两个方向:
(1)增强特征聚合;
(2)通道与空间注意的结合 ;
- import torch
- from torch import nn
- from torch.nn.parameter import Parameter
-
- class eca_layer(nn.Module):
- """Constructs a ECA module.
- Args:
- channel: Number of channels of the input feature map
- k_size: Adaptive selection of kernel size
- """
- def __init__(self, channel, k_size=3):
- super(eca_layer, self).__init__()
- self.avg_pool = nn.AdaptiveAvgPool2d(1)
- self.conv = nn.Conv1d(1, 1, kernel_size=k_size, padding=(k_size - 1) // 2, bias=False)
- self.sigmoid = nn.Sigmoid()
-
- def forward(self, x):
- # feature descriptor on the global spatial information
- y = self.avg_pool(x)
-
- # Two different branches of ECA module
- y = self.conv(y.squeeze(-1).transpose(-1, -2)).transpose(-1, -2).unsqueeze(-1)
-
- # Multi-scale information fusion
- y = self.sigmoid(y)
-
- return x * y.expand_as(x)

- import torch.nn as nn
- import math
- # import torch.utils.model_zoo as model_zoo
- from eca_module import eca_layer
-
-
- def conv3x3(in_planes, out_planes, stride=1):
- """3x3 convolution with padding"""
- return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
- padding=1, bias=False)
-
-
- class ECABasicBlock(nn.Module):
- expansion = 1
-
- def __init__(self, inplanes, planes, stride=1, downsample=None, k_size=3):
- super(ECABasicBlock, self).__init__()
- self.conv1 = conv3x3(inplanes, planes, stride)
- self.bn1 = nn.BatchNorm2d(planes)
- self.relu = nn.ReLU(inplace=True)
- self.conv2 = conv3x3(planes, planes, 1)
- self.bn2 = nn.BatchNorm2d(planes)
- self.eca = eca_layer(planes, k_size)
- self.downsample = downsample
- self.stride = stride
-
- 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)
- out = self.eca(out)
-
- if self.downsample is not None:
- residual = self.downsample(x)
-
- out += residual
- out = self.relu(out)
-
- return out
-
-
- class ECABottleneck(nn.Module):
- expansion = 4
-
- def __init__(self, inplanes, planes, stride=1, downsample=None, k_size=3):
- super(ECABottleneck, self).__init__()
- self.conv1 = nn.Conv2d(inplanes, 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, planes * 4, kernel_size=1, bias=False)
- self.bn3 = nn.BatchNorm2d(planes * 4)
- self.relu = nn.ReLU(inplace=True)
- self.eca = eca_layer(planes * 4, k_size)
- self.downsample = downsample
- self.stride = stride
-
- 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)
- out = self.relu(out)
-
- out = self.conv3(out)
- out = self.bn3(out)
- out = self.eca(out)
-
- if self.downsample is not None:
- residual = self.downsample(x)
-
- out += residual
- out = self.relu(out)
-
- return out
-
-
- class ResNet(nn.Module):
-
- def __init__(self, block, layers, num_classes=1000, k_size=[3, 3, 3, 3]):
- self.inplanes = 64
- super(ResNet, self).__init__()
- 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], int(k_size[0]))
- self.layer2 = self._make_layer(block, 128, layers[1], int(k_size[1]), stride=2)
- self.layer3 = self._make_layer(block, 256, layers[2], int(k_size[2]), stride=2)
- self.layer4 = self._make_layer(block, 512, layers[3], int(k_size[3]), stride=2)
- self.avgpool = nn.AvgPool2d(7, stride=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_layer(self, block, planes, blocks, k_size, 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, k_size))
- self.inplanes = planes * block.expansion
- for i in range(1, blocks):
- layers.append(block(self.inplanes, planes, k_size=k_size))
-
- 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 eca_resnet18(k_size=[3, 3, 3, 3], num_classes=1_000, pretrained=False):
- """Constructs a ResNet-18 model.
- Args:
- k_size: Adaptive selection of kernel size
- pretrained (bool): If True, returns a model pre-trained on ImageNet
- num_classes:The classes of classification
- """
- model = ResNet(ECABasicBlock, [2, 2, 2, 2], num_classes=num_classes, k_size=k_size)
- model.avgpool = nn.AdaptiveAvgPool2d(1)
- return model
-
-
- def eca_resnet34(k_size=[3, 3, 3, 3], num_classes=1_000, pretrained=False):
- """Constructs a ResNet-34 model.
- Args:
- k_size: Adaptive selection of kernel size
- pretrained (bool): If True, returns a model pre-trained on ImageNet
- num_classes:The classes of classification
- """
- model = ResNet(ECABasicBlock, [3, 4, 6, 3], num_classes=num_classes, k_size=k_size)
- model.avgpool = nn.AdaptiveAvgPool2d(1)
- return model
-
-
- def eca_resnet50(k_size=[3, 3, 3, 3], num_classes=1000, pretrained=False):
- """Constructs a ResNet-50 model.
- Args:
- k_size: Adaptive selection of kernel size
- num_classes:The classes of classification
- pretrained (bool): If True, returns a model pre-trained on ImageNet
- """
- print("Constructing eca_resnet50......")
- model = ResNet(ECABottleneck, [3, 4, 6, 3], num_classes=num_classes, k_size=k_size)
- model.avgpool = nn.AdaptiveAvgPool2d(1)
- return model
-
-
- def eca_resnet101(k_size=[3, 3, 3, 3], num_classes=1_000, pretrained=False):
- """Constructs a ResNet-101 model.
- Args:
- k_size: Adaptive selection of kernel size
- num_classes:The classes of classification
- pretrained (bool): If True, returns a model pre-trained on ImageNet
- """
- model = ResNet(ECABottleneck, [3, 4, 23, 3], num_classes=num_classes, k_size=k_size)
- model.avgpool = nn.AdaptiveAvgPool2d(1)
- return model
-
-
- def eca_resnet152(k_size=[3, 3, 3, 3], num_classes=1_000, pretrained=False):
- """Constructs a ResNet-152 model.
- Args:
- k_size: Adaptive selection of kernel size
- num_classes:The classes of classification
- pretrained (bool): If True, returns a model pre-trained on ImageNet
- """
- model = ResNet(ECABottleneck, [3, 8, 36, 3], num_classes=num_classes, k_size=k_size)
- model.avgpool = nn.AdaptiveAvgPool2d(1)
- return model

参考:
【pytorch】ECA-NET注意力机制应用于ResNet的代码实现
[ 注意力机制 ] 经典网络模型3——ECANet 详解与复现
论文翻译:ECA-Net: Efficient Channel Attention for Deep Convolutional Neural Networks
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。