SENet、ECA
时间: 2025-04-29 09:45:32 浏览: 18
### SENet架构细节
SENet(Squeeze-and-Excitation Network)通过引入通道注意力机制来增强模型的表现力。具体来说,在传统卷积神经网络的基础上加入了两个新的操作:squeeze 和 excitation。
- **Squeeze Operation**: 这一步骤旨在获取全局感受野的信息,通过对特征图进行全局平均池化得到一个描述整个图像的向量表示[^1]。
- **Excitation Operation**: 基于上述获得的一维向量,利用全连接层构建自适应权重参数,并通过激活函数调整各个通道的重要性程度。最终这些权值被重新应用于原始输入特征图上以突出重要区域并抑制不重要的部分。
```python
import torch.nn as nn
class SELayer(nn.Module):
def __init__(self, channel, reduction=16):
super(SELayer, self).__init__()
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.fc = nn.Sequential(
nn.Linear(channel, channel // reduction),
nn.ReLU(inplace=True),
nn.Linear(channel // reduction, channel),
nn.Sigmoid()
)
def forward(self, x):
b, c, _, _ = x.size()
y = self.avg_pool(x).view(b, c)
y = self.fc(y).view(b, c, 1, 1)
return x * y.expand_as(x)
```
### ECA网络结构特点
ECA-Nets (Efficient Channel Attention Networks) 是一种更高效的改进版本,它简化了 SE 层中的计算过程。主要变化在于:
- 使用一维卷积代替原有的两层全连接层来进行建模跨通道关系;
- 引入了一种自动寻找最优核大小的方法,从而减少了超参调优的工作量以及提升了性能表现。
```python
from functools import partial
from itertools import chain
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 ECALayer(nn.Module):
def __init__(self, channels, gamma=2, b=1):
super(ECALayer, self).__init__()
t = int(abs((math.log(channels, 2) + b)/gamma))
k = t if t % 2 else t+1
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv = nn.Conv1d(1, 1, kernel_size=k, padding=(k - 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)
```
### 应用场景
这两种方法都广泛应用于各种计算机视觉任务中,如分类、检测和分割等。它们可以作为即插即用模块轻松集成到现有 CNN 架构里,显著提高模型对于复杂模式的理解能力而不增加太多额外开销。
阅读全文
相关推荐

















