当前位置: 首页 > news >正文

【Block总结】DBlock,结合膨胀空间注意模块(Di-SpAM)和频域模块Gated-FFN|即插即用|CVPR2025

论文信息

标题: DarkIR: Robust Low-Light Image Restoration
作者: Daniel Feijoo, Juan C. Benito, Alvaro Garcia, Marcos Conde
论文链接:https://arxiv.org/pdf/2412.13443
GitHub链接:https://github.com/cidautai/DarkIR
在这里插入图片描述

创新点

DarkIR提出了一种新的卷积神经网络(CNN)框架,旨在同时处理低光图像增强和去模糊任务。与现有方法通常分开处理这两项任务不同,DarkIR通过多任务学习的方式,利用图像退化之间的相关性来提高恢复效果。该模型在参数和计算量上均优于之前的方法,且在多个标准数据集(如LOLBlur、LOLv2和Real-LOLBlur)上取得了新的最先进结果。
在这里插入图片描述

方法

DarkIR的架构采用了编码器-解码器设计,主要包括以下几个部分:

  1. 编码器块(EBlock): 主要在频域中改善低光条件,使用快速傅里叶变换(FFT)增强输入图像的幅度。 通过空间注意模块(SpAM)增强特征提取,确保网络能够关注重要的空间信息。
  2. 解码器块(DBlock): 负责上采样编码器的低分辨率输出并减少模糊。 实现了膨胀空间注意模块(Di-SpAM),通过不同膨胀率的深度卷积捕捉多种感受野的特征。该方法结合了频域技术和创新的注意力机制,优化了低光图像的恢复过程。
    在这里插入图片描述

DBlock模块详解

**DBlock(解码器块)**是DarkIR模型中的重要组成部分,主要负责从编码器传递的特征中恢复高分辨率的清晰图像。其设计旨在有效地处理低光照条件下的图像模糊问题,具体实现如下:

1. 结构组成

DBlock的主要组成部分包括:

  • 深度卷积(Dilated Convolution): DBlock利用多种膨胀因子的深度卷积来增大感受野,从而更好地捕捉图像中的细节和上下文信息。
  • 空间注意力模块(SCA): 该模块用于增强特征的空间表示,确保网络能够关注到图像中的重要区域。
  • 门控机制(SimpleGate): 通过门控机制对特征进行加权,进一步提升解码效果。

2. 工作原理
DBlock的工作流程如下:

  • 输入处理: DBlock接收来自编码器的特征图,并进行Layer Normalization以提高训练的稳定性。
  • 特征融合: 通过多个分支的深度卷积,DBlock能够在不同的膨胀率下捕捉多尺度的空间信息。各分支的输出通过门控机制进行融合,以增强特征的表达能力。
  • 上采样:DBlock执行上采样操作,将低分辨率特征图转换为高分辨率图像。通过1x1卷积层恢复到原始通道数,并与输入进行残差连接,以保持信息的完整性。

DBlock通过结合多种膨胀卷积和空间注意力机制,显著提高了低光照条件下图像的清晰度和细节恢复能力。其设计不仅提升了图像的质量,还有效减少了模糊,使得DarkIR在多个标准数据集上表现出色,达到了新的最先进结果。
在这里插入图片描述

效果

DarkIR在真实世界的夜间和暗光图像中表现出色,能够有效地减少噪声和模糊,同时保持高保真度。其在多个数据集上的表现超越了现有的最先进技术,展示了其在计算摄影、智能手机等资源受限设备上的应用潜力。
在这里插入图片描述

完整代码

import torch
import torch.nn as nnclass LayerNormFunction(torch.autograd.Function):@staticmethoddef forward(ctx, x, weight, bias, eps):ctx.eps = epsN, 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@staticmethoddef backward(ctx, grad_output):eps = ctx.epsN, C, H, W = grad_output.size()y, var, weight = ctx.saved_variablesg = 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), Noneclass 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 = epsdef 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 * x2class Adapter(nn.Module):def __init__(self, c, ffn_channel=None):super().__init__()if ffn_channel:ffn_channel = 2else:ffn_channel = cself.conv1 = nn.Conv2d(in_channels=c, out_channels=ffn_channel, kernel_size=1, padding=0, stride=1, groups=1,bias=True)self.conv2 = nn.Conv2d(in_channels=ffn_channel, out_channels=c, kernel_size=1, padding=0, stride=1, groups=1,bias=True)self.depthwise = nn.Conv2d(in_channels=c, out_channels=ffn_channel, kernel_size=3, padding=1, stride=1,groups=c, bias=True, dilation=1)def forward(self, input):x = self.conv1(input) + self.depthwise(input)x = self.conv2(x)return xclass 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.shapex_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_outclass 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 * cself.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 DBlock(nn.Module):'''Change this block using Branch'''def __init__(self, c, DW_Expand=2, FFN_Expand=2, dilations=[1], extra_depth_wise=False):super().__init__()# we define the 2 branchesself.dw_channel = DW_Expand * cself.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.extra_conv = nn.Conv2d(self.dw_channel, self.dw_channel, kernel_size=3, padding=1, stride=1, groups=c,bias=True, dilation=1) if extra_depth_wise else nn.Identity()  # optional extra dwself.branches = nn.ModuleList()for dilation in dilations:self.branches.append(Branch(self.dw_channel, DW_Expand=1, dilation=dilation))assert len(dilations) == len(self.branches)self.dw_channel = DW_Expand * cself.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.sg2 = 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)ffn_channel = FFN_Expand * cself.conv4 = nn.Conv2d(in_channels=c, out_channels=ffn_channel, kernel_size=1, padding=0, stride=1, groups=1,bias=True)self.conv5 = nn.Conv2d(in_channels=ffn_channel // 2, out_channels=c, kernel_size=1, padding=0, stride=1,groups=1, bias=True)self.norm1 = LayerNorm2d(c)self.norm2 = LayerNorm2d(c)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):y = inpx = self.norm1(inp)# x = self.conv1(self.extra_conv(x))x = self.extra_conv(self.conv1(x))z = 0for branch in self.branches:z += branch(x)z = self.sg1(z)x = self.sca(z) * zx = self.conv3(x)y = inp + self.beta * x# second stepx = self.conv4(self.norm2(y))  # size [B, 2*C, H, W]x = self.sg2(x)  # size [B, C, H, W]x = self.conv5(x)  # size [B, C, H, W]x = y + x * self.gammareturn xclass 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 branchesself.dw_channel = DW_Expand * cself.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 dwself.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 * cself.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 stepself.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):y = inpx = self.norm1(inp)x = self.conv1(self.extra_conv(x))z = 0for branch in self.branches:z += branch(x)z = self.sg1(z)x = self.sca(z) * zx = self.conv3(x)y = inp + self.beta * x# second stepx_step2 = self.norm2(y)  # size [B, 2*C, H, W]x_freq = self.freq(x_step2)  # size [B, C, H, W]x = y * x_freqx = y + x * self.gammareturn xif __name__ == "__main__":# 定义输入张量大小(Batch、Channel、Height、Wight)B, C, H, W = 16, 64, 8, 8input_tensor = torch.randn(B,C,H,W)  # 随机生成输入张量dim=C# 创建 DynamicTanh 实例block = DBlock(dim,dilations=[1, 4, 9])device = torch.device("cuda" if torch.cuda.is_available() else "cpu")block = block.to(device)print(block)input_tensor = input_tensor.to(device)# 执行前向传播output = block(input_tensor)# 打印输入和输出的形状print(f"Input: {input_tensor.shape}")print(f"Output: {output.shape}")

在这里插入图片描述

http://www.xdnf.cn/news/908587.html

相关文章:

  • FineReport模板认证找不到模板
  • pyarmor加密python程序
  • 【DAY41】简单CNN
  • 深入浅出Java ParallelStream:高效并行利器还是隐藏的陷阱?
  • 【使用conda】安装pytorch
  • python:基于pyside6的桌宠源码分享
  • java面试场景提题:
  • 全球知名具身智能/AI机器人实验室介绍之AI FACTORY基于慕尼黑工业大学
  • 数字孪生:如同为现实世界打造的“克隆体”,解锁无限可能
  • RabbitMQ 队列模式
  • CRM管理软件的审批流程设计与优化:提升企业运营效率的关键策略
  • DLL动态库实现文件遍历功能(Windows编程)
  • 浅谈不同二分算法的查找情况
  • hot100 -- 8.二叉树系列
  • 3D Web轻量化引擎HOOPS Communicator的定制化能力全面解析
  • LlamaIndex 工作流简介以及基础工作流
  • Linux驱动:class_create、device_create
  • java面试场景题:电商平台中订单未⽀付过期如何实现⾃动关单
  • 本地部署企业邮箱,让企业办公更安全高效
  • 【51单片机】0. 基础软件安装
  • Blazor-表单提交的艺术:如何优雅地实现 (下)
  • WorldExplorer:基于文本生成的可探索3D虚拟世界
  • 深克隆java对象的方式
  • 基于 openEuler 22.03 LTS SP1 构建 DPDK 22.11.8 开发环境指南
  • Xshell 详细安装与配置教程:从下载到高效使用
  • error: subprocess-exited-with-error【已解决】
  • docker 部署redis集群 配置
  • 【学习笔记】单例类模板
  • 深入理解二叉搜索树:原理到实践
  • libGL error