模块更新于 2026-11-20· 浏览 0· 下载 0
WConv 加权卷积
位置感知加权:把空间密度函数 Φ 逐元素乘进卷积核(WΦ = Φ ∘ W),按相对位置差异化缩放邻居像素的贡献;Φ 取全 1 时严格退化为标准卷积
结构图
wconv/
wconv.py入口5.5 KB
位置感知加权:把空间密度函数 Φ 逐元素乘进卷积核(WΦ = Φ ∘ W),按相对位置差异化缩放邻居像素的贡献;Φ 取全 1 时严格退化为标准卷积
论文信息
- 论文:Optimal Weighted Convolution for Classification and Denoising
- 出处:arXiv 2025
核心痛点
标准卷积对核内每个位置同等缩放,只靠学到的核权重决定特征重要性,丢掉了"离中心近的像素更关键"这类相对位置先验;而给核内元素额外配可学习权重或按输入动态加权多套核,又会增加参数和计算开销。
模块亮点
- 位置感知加权:把空间密度函数 Φ 逐元素乘进卷积核(WΦ = Φ ∘ W),按相对位置差异化缩放邻居像素的贡献;Φ 取全 1 时严格退化为标准卷积
- 零新增可训练参数:Φ 取对称秩一形式 Φ = ααᵀ,3×3 核只需 1 个系数、K×K 核只需 (K−1)/2 个,作为超参数调优而非训练;预计算 Φ 后训练时间平均仅多约 5%
- 分类去噪双涨:CIFAR-100 上 VGG 准确率从 56.89% 提到 66.94%(+10.05),ResNet-56、NiN、gMLP、GAC-SNN 准确率和 F1 全部提升;DIV2K 去噪上 DnCNN 的 PSNR 从 20.17 涨到 22.63
适用场景
2D 图像的分类与去噪;原文在 CIFAR-100 分类和 DIV2K 去噪(DnCNN、NAFNet、CGNet)上验证;也支持任意维规则网格信号,可迁移到 3D 医学体积数据、1D 时间序列等任务。
插入位置建议
- 直接替换网络中的 nn.Conv1d / nn.Conv2d / nn.Conv3d,官方代码三个维度都给好了
- 放进 DnCNN、NAFNet、CGNet 等去噪复原网络的卷积层,原文在 DIV2K 上验证有效
- 用于 VGG、ResNet 等分类骨干,把密度函数系数 α 当超参数搜索调优
完整代码
wconv.py
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.nn.modules.utils import _single, _pair, _triple
class wConv1d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, den, stride=1, padding=2, groups=1, dilation=1,
bias=False):
super(wConv1d, self).__init__()
self.stride = _single(stride)
self.padding = _single(padding)
self.kernel_size = _single(kernel_size)
self.groups = groups
self.dilation = _single(dilation)
self.weight = nn.Parameter(torch.empty(out_channels, in_channels // groups, *self.kernel_size))
nn.init.kaiming_normal_(self.weight, mode='fan_out', nonlinearity='relu')
self.bias = nn.Parameter(torch.zeros(out_channels)) if bias else None
device = torch.device('cpu')
self.register_buffer('alfa', torch.cat([torch.tensor(den, device=device), torch.tensor([1.0], device=device),
torch.flip(torch.tensor(den, device=device), dims=[0])]))
self.register_buffer('Phi', self.alfa)
if self.Phi.shape != self.kernel_size:
raise ValueError(f"Phi shape {self.Phi.shape} must match kernel size {self.kernel_size}")
def forward(self, x):
Phi = self.Phi.to(x.device)
weight_Phi = self.weight * Phi
return F.conv1d(x, weight_Phi, bias=self.bias, stride=self.stride, padding=self.padding, groups=self.groups,
dilation=self.dilation)
class wConv2d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, den, stride=1, padding=2, groups=1, dilation=1,
bias=False):
super(wConv2d, self).__init__()
self.stride = _pair(stride)
self.padding = _pair(padding)
self.kernel_size = _pair(kernel_size)
self.groups = groups
self.dilation = _pair(dilation)
self.weight = nn.Parameter(torch.empty(out_channels, in_channels // groups, *self.kernel_size))
nn.init.kaiming_normal_(self.weight, mode='fan_out', nonlinearity='relu')
self.bias = nn.Parameter(torch.zeros(out_channels)) if bias else None
device = torch.device('cpu')
self.register_buffer('alfa', torch.cat([torch.tensor(den, device=device), torch.tensor([1.0], device=device),
torch.flip(torch.tensor(den, device=device), dims=[0])]))
self.register_buffer('Phi', torch.outer(self.alfa, self.alfa))
if self.Phi.shape != self.kernel_size:
raise ValueError(f"Phi shape {self.Phi.shape} must match kernel size {self.kernel_size}")
def forward(self, x):
Phi = self.Phi.to(x.device)
weight_Phi = self.weight * Phi
return F.conv2d(x, weight_Phi, bias=self.bias, stride=self.stride, padding=self.padding, groups=self.groups,
dilation=self.dilation)
class wConv3d(nn.Module):
def __init__(self, in_channels, out_channels, kernel_size, den, stride=1, padding=2, groups=1, dilation=1,
bias=False):
super(wConv3d, self).__init__()
self.stride = _triple(stride)
self.padding = _triple(padding)
self.kernel_size = _triple(kernel_size)
self.groups = groups
self.dilation = _triple(dilation)
self.weight = nn.Parameter(torch.empty(out_channels, in_channels // groups, *self.kernel_size))
nn.init.kaiming_normal_(self.weight, mode='fan_out', nonlinearity='relu')
self.bias = nn.Parameter(torch.zeros(out_channels)) if bias else None
device = torch.device('cpu')
self.register_buffer('alfa', torch.cat([torch.tensor(den, device=device), torch.tensor([1.0], device=device),
torch.flip(torch.tensor(den, device=device), dims=[0])]))
self.register_buffer('Phi', torch.einsum('i,j,k->ijk', self.alfa, self.alfa, self.alfa))
if self.Phi.shape != self.kernel_size:
raise ValueError(f"Phi shape {self.Phi.shape} must match kernel size {self.kernel_size}")
def forward(self, x):
Phi = self.Phi.to(x.device)
weight_Phi = self.weight * Phi
return F.conv3d(x, weight_Phi, bias=self.bias, stride=self.stride, padding=self.padding, groups=self.groups,
dilation=self.dilation)
if __name__ == '__main__':
# Test wConv1d
print("Testing wConv1d...")
den = [0.5, 0.75] # Example density values
block = wConv1d(in_channels=3, out_channels=3, kernel_size=5, den=den).to('cuda')
input = torch.rand(1, 3, 32).to('cuda') # batch_size=1, channels=3, length=32
output = block(input)
print("Input size:", input.size())
print("Output size:", output.size())
print()
# Test wConv2d
print("Testing wConv2d...")
den = [0.5, 0.75] # Example density values
block = wConv2d(in_channels=3, out_channels=3, kernel_size=5, den=den).to('cuda')
input = torch.rand(1, 3, 32, 32).to('cuda') # batch_size=1, channels=3, height=32, width=32
output = block(input)
print("Input size:", input.size())
print("Output size:", output.size())
print()
# Test wConv3d
print("Testing wConv3d...")
den = [0.5, 0.75] # Example density values
block = wConv3d(in_channels=3, out_channels=3, kernel_size=5, den=den).to('cuda')
input = torch.rand(1, 3, 32, 32, 32).to('cuda') # batch_size=1, channels=3, depth=32, height=32, width=32
output = block(input)
print("Input size:", input.size())
print("Output size:", output.size())环境与依赖
Python 3.9+
继续浏览
下一篇WFD 小波特征下采样小波无损下采样:用 Haar 小波变换把特征无损分解成 1 个低频分量 + 水平、垂直、对角 3 个高频分量,低频走 Transformer 主路、高频走残差支路,分别增强后再融合,下采样不丢细节、不混频相关资源
模块2026-11-20
AKConv 可变核卷积
参数量任意:坐标生成算法让卷积核能取 1、2、3、4、5、6、7… 任意数量的参数,标准卷积和 Deformable Conv 都做不到;参数量随核大小线性增长
计算机视觉目标检测AKConv
计算机视觉MIT
模块2026-11-20
FCENet FDSM/FEFM 频率动态选择与增强
频域动态选频:依据频率相关性先验,用动态滤波在频域分别筛选两路特征,RGB 留低频颜色、NIR 留中高频纹理
计算机视觉图像去噪FDSM
计算机视觉MIT
模块2026-11-20
RFAConv 感受野注意力卷积
感受野级注意力:把特征图重排成互不重叠的滑动窗口,用 AvgPool + 1×1 分组卷积 + Softmax 为每个感受野单独生成注意力权重,卷积参数不再共享
计算机视觉RFAConv注意力机制
计算机视觉MIT