目录
1. 注意力机制类 (1-25)
Python
# ============ 模块1: CBAM注意力机制 ============
class CBAM(nn.Module):
"""通道+空间双重注意力"""
def __init__(self, channels, reduction=16):
super().__init__()
# 通道注意力
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.max_pool = nn.AdaptiveMaxPool2d(1)
self.mlp = nn.Sequential(
nn.Conv2d(channels, channels//reduction, 1),
nn.ReLU(inplace=True),
nn.Conv2d(channels//reduction, channels, 1)
)
# 空间注意力
self.conv_spatial = nn.Conv2d(2, 1, 7, padding=3)
def forward(self, x):
# 通道注意力
avg_out = self.mlp(self.avg_pool(x))
max_out = self.mlp(self.max_pool(x))
x = x * torch.sigmoid(avg_out + max_out)
# 空间注意力
avg_spatial = torch.mean(x, dim=1, keepdim=True)
max_spatial, _ = torch.max(x, dim=1, keepdim=True)
spatial = torch.cat([avg_spatial, max_spatial], dim=1)
x = x * torch.sigmoid(self.conv_spatial(spatial))
return x
# 改进思路: 结合通道和空间维度,自适应增强重要特征,抑制背景噪声
# ============ 模块2: ECA注意力机制 ============
class ECA(nn.Module):
"""高效通道注意力,避免降维"""
def __init__(self, channels, gamma=2, b=1):
super().__init__()
kernel_size = int(abs((math.log(channels, 2) + b) / gamma))
kernel_size = kernel_size if kernel_size % 2 else kernel_size + 1
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.conv = nn.Conv1d(1, 1, kernel_size, padding=kernel_size//2, bias=False)
def forward(self, x):
y = self.avg_pool(x)
y = self.conv(y.squeeze(-1).transpose(-1, -2))
y = y.transpose(-1, -2).unsqueeze(-1)
return x * torch.sigmoid(y)
# 改进思路: 1D卷积替代全连接,避免通道降维带来的信息损失,参数更少更高效
# ============ 模块3: SimAM注意力机制 ============
class SimAM(nn.Module):
"""无参数三维注意力"""
def __init__(self, e_lambda=1e-4):
super().__init__()
self.activation = nn.Sigmoid()
self.e_lambda = e_lambda
def forward(self, x):
b, c, h, w = x.size()
n = w * h - 1
x_minus_mu_square = (x - x.mean(dim=[2, 3], keepdim=True)).pow(2)
y = x_minus_mu_square / (4 * (x_minus_mu_square.sum(dim=[2, 3], keepdim=True) / n + self.e_lambda)) + 0.5
return x * self.activation(y)
# 改进思路: 基于神经科学的三维权重,无需额外参数,计算能量函数突出重要神经元
# ============ 模块4: Coordinate Attention ============
class CoordAtt(nn.Module):
"""坐标注意力,编码位置信息"""
def __init__(self, inp, reduction=32):
super().__init__()
self.pool_h = nn.AdaptiveAvgPool2d((None, 1))
self.pool_w = nn.AdaptiveAvgPool2d((1, None))
mip = max(8, inp // reduction)
self.conv1 = nn.Conv2d(inp, mip, kernel_size=1, stride=1, padding=0)
self.bn1 = nn.BatchNorm2d(mip)
self.act = nn.Hardswish()
self.conv_h = nn.Conv2d(mip, inp, kernel_size=1, stride=1, padding=0)
self.conv_w = nn.Conv2d(mip, inp, kernel_size=1, stride=1, padding=0)
def forward(self, x):
identity = x
n, c, h, w = x.size()
x_h = self.pool_h(x)
x_w = self.pool_w(x).permute(0, 1, 3, 2)
y = torch.cat([x_h, x_w], dim=2)
y = self.conv1(y)
y = self.bn1(y)
y = self.act(y)
x_h, x_w = torch.split(y, [h, w], dim=2)
x_w = x_w.permute(0, 1, 3, 2)
a_h = self.conv_h(x_h).sigmoid()
a_w = self.conv_w(x_w).sigmoid()
return identity * a_w * a_h
# 改进思路: 将通道注意力分解为两个1D特征编码,捕获精确的位置信息
# ============ 模块5: Global Context Block ============
class GCBlock(nn.Module):
"""全局上下文建模"""
def __init__(self, inplanes, ratio=16):
super().__init__()
self.inplanes = inplanes
self.inter_channels = inplanes // ratio
self.conv_mask = nn.Conv2d(inplanes, 1, kernel_size=1)
self.transform = nn.Sequential(
nn.Conv2d(inplanes, self.inter_channels, 1),
nn.LayerNorm([self.inter_channels, 1, 1]),
nn.ReLU(inplace=True),
nn.Conv2d(self.inter_channels, inplanes, 1)
)
def forward(self, x):
# 全局注意力池化
context_mask = self.conv_mask(x)
context_mask = context_mask.view(x.size(0), 1, -1)
context_mask = F.softmax(context_mask, dim=2)
context_mask = context_mask.view(x.size(0), 1, x.size(2), x.size(3))
# 全局上下文特征
context = torch.matmul(x.view(x.size(0), x.size(1), -1),
context_mask.view(x.size(0), 1, -1).permute(0, 2, 1))
context = context.unsqueeze(-1)
return x + self.transform(context)
# 改进思路: 结合Non-local和SE-Net,轻量级全局上下文建模,增强长距离依赖
# ============ 模块6: Double Attention ============
class DoubleAttention(nn.Module):
"""双重注意力:特征聚集和分配"""
def __init__(self, in_channels, c_m, c_n):
super().__init__()
self.c_m = c_m
self.c_n = c_n
self.convA = nn.Conv2d(in_channels, c_m, 1)
self.convB = nn.Conv2d(in_channels, c_n, 1)
self.convV = nn.Conv2d(in_channels, c_n, 1)
def forward(self, x):
b, c, h, w = x.size()
A = self.convA(x).view(b, self.c_m, -1)
B = self.convB(x).view(b, self.c_n, -1)
V = self.convV(x).view(b, self.c_n, -1)
tmp = torch.bmm(B, V.permute(0, 2, 1)) / (h * w)
tmp = F.softmax(tmp, dim=-1)
Z = torch.bmm(A, tmp)
return Z.view(b, self.c_m, h, w)
# 改进思路: 先聚集关键特征再分配到各位置,两步注意力机制提高特征表达
# ============ 模块7: A2Net Double Attention ============
class A2Attn(nn.Module):
"""A2-Net双重注意力"""
def __init__(self, in_ch, c_m=128, c_n=128, reconstruct=True):
super().__init__()
self.c_m = c_m
self.c_n = c_n
self.reconstruct = reconstruct
self.convA = nn.Conv2d(in_ch, c_m, 1)
self.convB = nn.Conv2d(in_ch, c_n, 1)
self.convV = nn.Conv2d(in_ch, c_n, 1)
if reconstruct:
self.conv_reconstruct = nn.Conv2d(c_m, in_ch, kernel_size=1)
def forward(self, x):
b, c, h, w = x.shape
A = self.convA(x)
B = self.convB(x)
V = self.convV(x)
tmpA = A.view(b, self.c_m, -1)
attention_maps = F.softmax(B.view(b, self.c_n, -1), dim=-1)
attention_vectors = F.softmax(V.view(b, self.c_n, -1), dim=1)
global_descriptors = torch.bmm(tmpA, attention_maps.permute(0, 2, 1))
tmpZ = torch.bmm(global_descriptors, attention_vectors)
tmpZ = tmpZ.view(b, self.c_m, h, w)
if self.reconstruct:
tmpZ = self.conv_reconstruct(tmpZ)
return tmpZ
# 改进思路: 收集-分配机制,首先收集所有位置信息,然后自适应分配到各位置
# ============ 模块8: Self-Calibrated Attention ============
class SCConv(nn.Module):
"""自校准卷积"""
def __init__(self, inplanes, planes, stride=1, padding=1, dilation=1, groups=1):
super().__init__()
self.conv_k = nn.Conv2d(inplanes, planes, kernel_size=3,
stride=stride, padding=padding,
dilation=dilation, groups=groups, bias=False)
self.conv_scale = nn.Conv2d(inplanes//2, inplanes//2, kernel_size=3,
stride=stride, padding=padding,
dilation=dilation, groups=groups, bias=False)
self.conv_up = nn.Conv2d(inplanes//2, inplanes, kernel_size=3,
stride=1, padding=1, bias=False)
self.bn = nn.BatchNorm2d(planes)
self.relu = nn.ReLU(inplace=True)
def forward(self, x):
b, c, h, w = x.size()
x1, x2 = torch.split(x, c//2, dim=1)
x2 = self.conv_scale(x2)
x2 = F.interpolate(x2, scale_factor=0.5, mode='bilinear', align_corners=False)
x2 = self.conv_up(x2)
x2 = F.interpolate(x2, size=(h, w), mode='bilinear', align_corners=False)
x = torch.cat([x1, x2], dim=1)
return self.relu(self.bn(self.conv_k(x)))
# 改进思路: 多尺度自校准,通过下采样-上采样建立长距离空间关联
# ============ 模块9: Triplet Attention ============
class TripletAttention(nn.Module):
"""三元组注意力,跨维度交互"""
def __init__(self, no_spatial=False):
super().__init__()
self.cw = AttentionGate()
self.hc = AttentionGate()
self.no_spatial = no_spatial
if not no_spatial:
self.hw = AttentionGate()
def forward(self, x):
x_perm1 = x.permute(0, 2, 1, 3).contiguous()
x_out1 = self.cw(x_perm1)
x_out11 = x_out1.permute(0, 2, 1, 3).contiguous()
x_perm2 = x.permute(0, 3, 2, 1).contiguous()
x_out2 = self.hc(x_perm2)
x_out21 = x_out2.permute(0, 3, 2, 1).contiguous()
if not self.no_spatial:
x_out = self.hw(x)
x_out = 1/3 * (x_out + x_out11 + x_out21)
else:
x_out = 1/2 * (x_out11 + x_out21)
return x_out
class AttentionGate(nn.Module):
def __init__(self):
super().__init__()
kernel_size = 7
self.compress = ZPool()
self.conv = BasicConv(2, 1, kernel_size, stride=1, padding=(kernel_size-1)//2, relu=False)
def forward(self, x):
x_compress = self.compress(x)
x_out = self.conv(x_compress)
scale = torch.sigmoid_(x_out)
return x * scale
class ZPool(nn.Module):
def forward(self, x):
return torch.cat((torch.max(x, 1)[0].unsqueeze(1), torch.mean(x, 1).unsqueeze(1)), dim=1)
class BasicConv(nn.Module):
def __init__(self, in_planes, out_planes, kernel_size, stride=1, padding=0, relu=True):
super().__init__()
self.out_channels = out_planes
self.conv = nn.Conv2d(in_planes, out_planes, kernel_size=kernel_size,
stride=stride, padding=padding, bias=False)
self.bn = nn.BatchNorm2d(out_planes)
self.relu = nn.ReLU() if relu else None
def forward(self, x):
x = self.conv(x)
x = self.bn(x)
if self.relu is not None:
x = self.relu(x)
return x
# 改进思路: 通过permute操作在三个维度对(CW,HC,HW)分别计算注意力,捕获跨维度交互
# ============ 模块10: Shuffle Attention ============
class ShuffleAttention(nn.Module):
"""混洗注意力"""
def __init__(self, channel=512, reduction=16, G=8):
super().__init__()
self.G = G
self.channel = channel
self.avg_pool = nn.AdaptiveAvgPool2d(1)
self.gn = nn.GroupNorm(channel // (2 * G), channel // (2 * G))
self.cweight = nn.Parameter(torch.zeros(1, channel // (2 * G), 1, 1))
self.cbias = nn.Parameter(torch.ones(1, channel // (2 * G), 1, 1))
self.sweight = nn.Parameter(torch.zeros(1, channel // (2 * G), 1, 1))
self.sbias = nn.Parameter(torch.ones(1, channel // (2 * G), 1, 1))
self.sigmoid = nn.Sigmoid()
def forward(self, x):
b, c, h, w = x.size()
x = x.view(b * self.G, -1, h, w)
x_0, x_1 = x.chunk(2, dim=1)
# 通道注意力
xn = self.avg_pool(x_0)
xn = self.cweight * xn + self.cbias
xn = x_0 * self.sigmoid(xn)
# 空间注意力
xs = self.gn(x_1)
xs = self.sweight * xs + self.sbias
xs = x_1 * self.sigmoid(xs)
out = torch.cat([xn, xs], dim=1)
out = out.view(b, -1, h, w)
# 通道混洗
out = self.channel_shuffle(out, 2)
return out
def channel_shuffle(self, x, groups):
b, c, h, w = x.size()
x = x.view(b, groups, c // groups, h, w)
x = x.permute(0, 2, 1, 3, 4).contiguous()
x = x.view(b, c, h, w)
return x
# 改进思路: 将通道分组后分别进行通道和空间注意力,通过混洗促进组间信息交流
# ============ 模块11: Residual Attention ============
class ResidualAttention(nn.Module):
"""残差注意力模块"""
def __init__(self, in_channels, out_channels, stride=1):
super().__init__()
self.conv1 = nn.Conv2d(in_channels, out_channels, 3, stride, 1)
self.bn1 = nn.BatchNorm2d(out_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(out_channels, out_channels, 3, 1, 1)
self.bn2 = nn.BatchNorm2d(out_channels)
self.attention = AttentionModule(out_channels)
if stride != 1 or in_channels != out_channels:
self.shortcut = nn.Sequential(
nn.Conv2d(in_channels, out_channels, 1, stride),
nn.BatchNorm2d(out_channels)
)
else:
self.shortcut = nn.Identity()
def forward(self, x):
out = self.relu(self.bn1(self.conv1(x)))
out = self.bn2(self.conv2(out))
out = self.attention(out)
out += self.shortcut(x)
return self.relu(out)
class AttentionModule(nn.Module):
def __init__(self, channels):
super().__init__()
self.mask = nn.Sequential(
nn.Conv2d(channels, channels//4, 1),
nn.BatchNorm2d(channels//4),
nn.ReLU(inplace=True),
nn.Conv2d(channels//4, channels, 1),
nn.BatchNorm2d(channels),
nn.Sigmoid()
)
def forward(self, x):
return x * self.mask(x)
# 改进思路: 软注意力掩码与残差连接结合,逐步细化特征,避免信息损失
# ============ 模块12: Squeeze-and-Excitation改进版 ============
class SEWeightModule(nn.Module):
"""SE模块权重生成"""
def __init__(self, channels, reduction=16):
super().__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
class SENet(nn.Module):
def __init__(self, channels, reduction=16):
super().__init__()
self.weight = SEWeightModule(channels, reduction)
def forward(self, x):
weight = self.weight(x)
return x * weight
# 改进思路: 经典通道注意力,显式建模通道间相互依赖关系,自适应重标定通道特征
835



被折叠的 条评论
为什么被折叠?



