三组 1×1 投影构造内容 A、聚合图 B 和分发图 V,先压缩为 16×8 全局描述,再分发回 64 个位置。本图使用本文件的 Softmax 维度行为。
(2, 32, 8, 8) · NCHW;32 个输入通道(2, 32, 8, 8) · reconstruct=True,恢复 32 通道1,600 个参数 · CPU / eval / FP32;sum(p.numel());图像分类与低分辨特征聚合。配置 DoubleAttention(in_channels=32,c_m=16,c_n=8,reconstruct=True)forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片三组 1×1 投影构造内容 A、聚合图 B 和分发图 V,先压缩为 16×8 全局描述,再分发回 64 个位置。本图使用本文件的 Softmax 维度行为。
39 def forward(self, x): 40 b, c, h,w=x.shape 42 A=self.convA(x) #b,c_m,h,w43 B=self.convB(x) #b,c_n,h,w44 V=self.convV(x) #b,c_n,h,w45 tmpA=A.view(b,self.c_m,-1)46 attention_maps=F.softmax(B.view(b,self.c_n,-1))47 attention_vectors=F.softmax(V.view(b,self.c_n,-1))49 global_descriptors=torch.bmm(tmpA,attention_maps.permute(0,2,1)) #b.c_m,c_n51 tmpZ = global_descriptors.matmul(attention_vectors) #b,c_m,h*w54 tmpZ=self.conv_reconstruct(tmpZ)56 return tmpZ
将 A2Atttention.py 所在目录加入 Python 搜索路径后使用。NCHW;32 个输入通道。下面给出与参数卡一致的构造配置,输入张量 x 应采用上方实测形状。
from A2Atttention import DoubleAttention block = DoubleAttention(in_channels=32,c_m=16,c_n=8,reconstruct=True).eval() y = block(x)
Method 动机参考:以少量全局描述传递跨位置的信息,避免构造空间两两注意力矩阵。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 局部基线 | 1×1 卷积 | 比较通道混合与全局交互 |
| 描述数 | c_n=4 / 8 / 16 | 比较描述容量和开销 |
| 完整模块 | 聚合 + 分发 + 重建 | 记录任务指标、参数量和延迟 |