同一张特征图分别进入重叠窗口空间注意力、通道二阶统计和逐像素秩二互补分支。三路结果沿通道拼接,再由 1×1 卷积融合。
2×16×8×123,428 个总参数,3,428 个 requires_grad 参数(sum(p.numel());不计 buffer)。配置:dim=16,window=4,overlap_ratio=0.5,heads=4,bias=False。用途:图像恢复特征混合。forward() 数据流顺序拆解 · 点击任意子模块方块可同步查看对应源码切片同一张特征图分别进入重叠窗口空间注意力、通道二阶统计和逐像素秩二互补分支。三路结果沿通道拼接,再由 1×1 卷积融合。 本页尺寸来自本地源码示例的 CPU 前向:子模块用 forward_hook 记录,函数式算子的中间张量在真实执行时读取;全部尺寸包含批维。
119 phi_3, phi_4 = self.phi(x).chunk(2, dim=1)120 phi_3, phi_4 = map(lambda t: rearrange(t, 'b (head c) h w -> b head (h w) 1 c', head = self.num_heads), (phi_3, phi_4))121 vc = rearrange(vc, 'b head c (h w) -> b head (h w) c 1', head = self.num_heads, h = h, w = w)124 U = torch.cat([phi_3, phi_4], dim=-2)125 inv_A = lazy_inversion_2d(U)127 out_phi = (U.transpose(-2, -1) @ inv_A) @ (U @ vc)128 out_phi = rearrange(out_phi, 'b head (h w) c 1 -> b (head c) h w', head = self.num_heads, h = h, w = w) 131 out = torch.cat([out_s, out_c, out_phi], dim=1) # dim*3132 out = self.project_out(out)134 return out84 qkv = self.qkv(x)85 qs, ks, vs = qkv.chunk(3, dim=1)
输入为 BCHW,通道需整除头数,H/W 需整除 window_size;本例重叠窗口差为偶数,可对称环形填充。 示例配置已在 CPU 运行源码内置检查。消融表列的是建议实验,尚无下游任务指标。
from NLformerBayesianAttention import Attention_block model = Attention_block(dim=16,window_size=4,overlap_ratio=0.5,num_heads=4,bias=False) y = model(x)
Method 动机参考:窗口交互补充邻域信息,通道协方差描述跨通道依赖,秩二分支提供逐 token 的补充变换;各分支作用应通过相同训练条件下的消融判断。
| 消融变体 | 结构配置 | 验证目的 |
|---|---|---|
| 仅空间路 | 屏蔽 out_c/out_phi | 比较恢复质量和窗口边缘误差 |
| 空间 + 通道路 | 屏蔽 out_phi | 检查秩二互补项的增量作用 |
| 三路融合 | 保留全部分支 | 同时记录质量、耗时和峰值显存 |