学术
模块更新于 2026-11-20· 浏览 0· 下载 0

WConv 加权卷积

位置感知加权:把空间密度函数 Φ 逐元素乘进卷积核(WΦ = Φ ∘ W),按相对位置差异化缩放邻居像素的贡献;Φ 取全 1 时严格退化为标准卷积

计算机视觉计算机视觉WConv加权卷积卷积神经网络图像去噪

结构图

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 主路、高频走残差支路,分别增强后再融合,下采样不丢细节、不混频

相关资源