通道分组后并行执行轴向平均与最大池化,产生两组坐标门控;两路全局描述交叉乘对方空间特征,形成每组一张空间权重图。
(2, 64, 16, 16) · 64 通道,factor=8,组内 8 通道(2, 64, 16, 16) · 分组空间权重乘原输入672 个参数 · CPU / eval / FP32;sum(p.numel());高光谱与多通道特征调制。配置 HPA(channels=64,factor=8)forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片通道分组后并行执行轴向平均与最大池化,产生两组坐标门控;两路全局描述交叉乘对方空间特征,形成每组一张空间权重图。
36 b, c, h, w = x.size() 37 group_x = x.reshape(b * self.groups, -1, h, w)40 x_h = self.pool_h(group_x)41 x_w = self.pool_w(group_x).permute(0, 1, 3, 2)42 hw = self.conv1x1(torch.cat([x_h, x_w], dim=2))44 x1 = self.gn(group_x * x_h.sigmoid() * x_w.permute(0, 1, 3, 2).sigmoid())47 y_h = self.max_h(group_x)49 yhw = self.conv1x1(torch.cat([y_h, y_w], dim=2))51 y1 = self.gn(group_x * y_h.sigmoid() * y_w.permute(0, 1, 3, 2).sigmoid())55 x12 = self.softmax(self.agp(x1).reshape(b * self.groups, -1, 1).permute(0, 2, 1))59 weights = (torch.matmul(x12, y11) + torch.matmul(y12, x11)).reshape(b * self.groups, 1, h, w)60 return (group_x * weights.sigmoid()).reshape(b, c, h, w)
将 HPA.py 所在目录加入 Python 搜索路径后使用。64 通道,factor=8,组内 8 通道。下面给出与参数卡一致的构造配置,输入张量 x 应采用上方实测形状。
from HPA import HPA block = HPA(channels=64,factor=8).eval() y = block(x)
Method 动机参考:通过分组与双池化保留不同响应统计,以小规模矩阵乘法生成空间调制。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 平均池化 | 仅平均坐标分支 | 检查最大响应补充 |
| 分组设置 | factor=4 / 8 / 16 | 检查组内通道容量 |
| 完整 HPA | 双池化 + 交叉权重 | 比较任务指标与开销 |