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

EBlock 低光增强编码块

空间频域双管齐下:沿用 Metaformer / NAFBlock 式"注意力 + FFN"结构,前半 SpAM 用倒残差、SimpleGate 门控和简化通道注意力(SCA)做空间特征提纯,

计算机视觉计算机视觉图像复原低光增强EBlock频域

结构图

EBlock 低光增强编码块结构图
EBlock 低光增强编码块结构图

空间频域双管齐下:沿用 Metaformer / NAFBlock 式"注意力 + FFN"结构,前半 SpAM 用倒残差、SimpleGate 门控和简化通道注意力(SCA)做空间特征提纯,

论文信息

  • 论文:DarkIR: Robust Low-Light Image Restoration
  • 出处:arXiv 2025

核心痛点

暗光与夜间拍摄要同时扛住高噪声、长曝光模糊和光照不足三重退化,现有方法却大多把低光增强和去模糊拆成两个独立任务,多个模型串行微调、推理,难以落地。

模块亮点

  • 空间频域双管齐下:沿用 Metaformer / NAFBlock 式"注意力 + FFN"结构,前半 SpAM 用倒残差、SimpleGate 门控和简化通道注意力(SCA)做空间特征提纯,后半 FreMLP 转战傅里叶域补光照
  • 只调振幅、保住相位:FFT 后仅用 MLP 修正振幅(低光与频域振幅高度相关),相位原封不动再 IFFT 回来;消融中加上相位变换,PSNR 反而从 26.90 掉到 26.30 dB,说明"只动振幅"正是精髓
  • 涨点实在:同设置下把编码器 EBlock 换回 NAFBlock,PSNR 从 26.90 掉到 26.51;整体模型在 LOLBlur 达 27.00 dB、LPIPS 0.162,比此前 SOTA LEDNet 高 1 dB 以上,参数少 55%

适用场景

领域为低照度图像复原;原文验证了低光增强 + 去模糊联合任务(LOLBlur、Real-LOLBlur)与纯低光增强(LOLv2,多任务版在 LOLv2-Real 23.87 dB 拿 SOTA);可迁移到去噪、去模糊、夜间图像增强等复原任务。

插入位置建议

  • 替换 NAFNet 等 U 型复原网络编码器里的 NAFBlock,原文消融就是同位置直接对比
  • 放在编码器低分辨率段专管提亮,解码器保留空间域模块去模糊,组成非对称编解码结构
  • 单独拆出 FreMLP 当频域 FFN,替换 Metaformer 块里的普通 MLP,只改振幅、保留相位

适用任务

  • 低照度图像复原

完整代码

eblock.py
import torch
from torch import nn as nn


class LayerNormFunction(torch.autograd.Function):

    @staticmethod
    def forward(ctx, x, weight, bias, eps):
        ctx.eps = eps
        N, C, H, W = x.size()
        mu = x.mean(1, keepdim=True)
        var = (x - mu).pow(2).mean(1, keepdim=True)
        y = (x - mu) / (var + eps).sqrt()
        ctx.save_for_backward(y, var, weight)
        y = weight.view(1, C, 1, 1) * y + bias.view(1, C, 1, 1)
        return y

    @staticmethod
    def backward(ctx, grad_output):
        eps = ctx.eps

        N, C, H, W = grad_output.size()
        y, var, weight = ctx.saved_variables
        g = grad_output * weight.view(1, C, 1, 1)
        mean_g = g.mean(dim=1, keepdim=True)

        mean_gy = (g * y).mean(dim=1, keepdim=True)
        gx = 1. / torch.sqrt(var + eps) * (g - y * mean_gy - mean_g)
        return gx, (grad_output * y).sum(dim=3).sum(dim=2).sum(dim=0), grad_output.sum(dim=3).sum(dim=2).sum(
            dim=0), None

class LayerNorm2d(nn.Module):

    def __init__(self, channels, eps=1e-6):
        super(LayerNorm2d, self).__init__()
        self.register_parameter('weight', nn.Parameter(torch.ones(channels)))
        self.register_parameter('bias', nn.Parameter(torch.zeros(channels)))
        self.eps = eps

    def forward(self, x):
        return LayerNormFunction.apply(x, self.weight, self.bias, self.eps)


class SimpleGate(nn.Module):
    def forward(self, x):
        x1, x2 = x.chunk(2, dim=1)
        return x1 * x2



class FreMLP(nn.Module):

    def __init__(self, nc, expand=2):
        super(FreMLP, self).__init__()
        self.process1 = nn.Sequential(
            nn.Conv2d(nc, expand * nc, 1, 1, 0),
            nn.LeakyReLU(0.1, inplace=True),
            nn.Conv2d(expand * nc, nc, 1, 1, 0))

    def forward(self, x):
        _, _, H, W = x.shape
        x_freq = torch.fft.rfft2(x, norm='backward')
        mag = torch.abs(x_freq)
        pha = torch.angle(x_freq)
        mag = self.process1(mag)
        real = mag * torch.cos(pha)
        imag = mag * torch.sin(pha)
        x_out = torch.complex(real, imag)
        x_out = torch.fft.irfft2(x_out, s=(H, W), norm='backward')
        return x_out


class Branch(nn.Module):
    '''
    Branch that lasts lonly the dilated convolutions
    '''

    def __init__(self, c, DW_Expand, dilation=1):
        super().__init__()
        self.dw_channel = DW_Expand * c

        self.branch = nn.Sequential(
            nn.Conv2d(in_channels=self.dw_channel, out_channels=self.dw_channel, kernel_size=3, padding=dilation,
                      stride=1, groups=self.dw_channel,
                      bias=True, dilation=dilation)  # the dconv
        )

    def forward(self, input):
        return self.branch(input)



class EBlock(nn.Module):
    '''
    Change this block using Branch
    '''

    def __init__(self, c, DW_Expand=2, dilations=[1], extra_depth_wise=False):
        super().__init__()
        # we define the 2 branches
        self.dw_channel = DW_Expand * c
        self.extra_conv = nn.Conv2d(c, c, kernel_size=3, padding=1, stride=1, groups=c, bias=True,
                                    dilation=1) if extra_depth_wise else nn.Identity()  # optional extra dw
        self.conv1 = nn.Conv2d(in_channels=c, out_channels=self.dw_channel, kernel_size=1, padding=0, stride=1,
                               groups=1, bias=True, dilation=1)

        self.branches = nn.ModuleList()
        for dilation in dilations:
            self.branches.append(Branch(c, DW_Expand, dilation=dilation))

        assert len(dilations) == len(self.branches)
        self.dw_channel = DW_Expand * c
        self.sca = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),
            nn.Conv2d(in_channels=self.dw_channel // 2, out_channels=self.dw_channel // 2, kernel_size=1, padding=0,
                      stride=1,
                      groups=1, bias=True, dilation=1),
        )
        self.sg1 = SimpleGate()
        self.conv3 = nn.Conv2d(in_channels=self.dw_channel // 2, out_channels=c, kernel_size=1, padding=0, stride=1,
                               groups=1, bias=True, dilation=1)
        # second step

        self.norm1 = LayerNorm2d(c)
        self.norm2 = LayerNorm2d(c)
        self.freq = FreMLP(nc=c, expand=2)
        self.gamma = nn.Parameter(torch.zeros((1, c, 1, 1)), requires_grad=True)
        self.beta = nn.Parameter(torch.zeros((1, c, 1, 1)), requires_grad=True)

    def forward(self, inp):
        # 第一步:空间域特征提取
        # 1. 层归一化
        x = self.norm1(inp)

        # 2. 可选的深度卷积
        x = self.conv1(self.extra_conv(x))  # 1x1卷积扩展通道数(DW_Expand倍)

        # 3. 多分支空洞卷积
        z = 0
        for branch in self.branches:  # 不同膨胀率的并行分支
            z += branch(x)  # 分支结果相加

        # 4. 门控机制
        z = self.sg1(z)

        # 5. 简化通道注意力SCA
        x = self.sca(z) * z  # 全局平均池化+1x1卷积生成通道权重

        # 6. 通道压缩回原始维度
        x = self.conv3(x)  # 1x1卷积降维

        # 7. 残差连接
        y = inp + self.beta * x  # 可学习的β参数控制残差强度

        # 第二步:频域特征增强
        # 8. 二次归一化
        x_step2 = self.norm2(y)

        # 9. 频域MLP处理
        x_freq = self.freq(x_step2)
        # 核心操作:
        # 1) FFT变换到频域
        # 2) 仅调整振幅(process1 MLP)
        # 3) 保留原始相位
        # 4) IFFT返回空间域

        # 10. 频域-空间域特征融合
        x = y * x_freq  # 频域增强特征作为注意力权重

        # 11. 最终残差连接
        x = y + x * self.gamma  # 可学习的γ参数控制融合强度
        return x


if __name__ == '__main__':
    block = EBlock(c=8).to('cuda')
    input = torch.rand(1, 8, 4, 4).to('cuda')
    output = block(input)
    print(input.size())
    print(output.size())

环境与依赖

Python 3.9+

继续浏览

下一篇EFIGNN 显式特征交互感知图神经网络显式学任意阶交互:每层做无激活函数的图卷积聚合邻居信息,再哈达玛乘上 1 阶特征之和,交互阶数随层数递增,2 层学 2 阶、4 层学 4 阶

相关资源