论文介绍
题目:MambaIR: A simple baseline for image restoration with state-space model
论文地址:https://arxiv.org/pdf/2402.15648.pdf
QQ深度学习交流群:719278780
扫描下方二维码,加入深度学习论文指南星球!
加入即可获得,模块缝合、制作、写作技巧,学会“结构”创新、“创新点”创新,从三区到顶会,小论文大论文,毕业一站式服务
创新点
首次将状态空间模型(State Space Model, SSM)应用于低级图像恢复任务:
引入残差状态空间块(Residual State-Space Block, RSSB)
RSSB 通过本地增强(Local Enhancement)缓解了传统 Mamba 模型在二维图像中的局部像素遗忘问题。通过通道注意力(Channel Attention)减少通道冗余,从而提升模型的表达能力。
利用 2D 选择性扫描模块(2D Selective Scan Module, 2D-SSM):
提出了一种新颖的网络架构:
实验验证的优越性:
论文提出了名为 MambaIR 的简单但高效的基准模型,填补了传统 CNN 和 Transformer 方法的性能和效率之间的空白。
针对图像的非因果性特征设计了多方向扫描策略,显著提升了模型捕获长程依赖关系的能力。
包括浅层特征提取、深层特征提取和高质量图像重建三个阶段,整体设计高效且计算复杂度为线性。
论文通过广泛的实验表明,MambaIR 在图像超分辨率、图像去噪等任务上超越了其他现有强基线模型(如 SwinIR 和 Restormer),并在全球感受野和计算效率之间取得了良好平衡。
方法
整体架构
MambaIR 模型的整体结构由三个阶段组成:首先,浅层特征提取阶段使用卷积层从输入图像中提取初步特征;接着,深层特征提取阶段通过堆叠多个残差状态空间组(RSSG)学习图像的深层特征,这些组结合了状态空间模型的长程依赖建模能力、局部卷积的增强效果以及通道注意力机制,提升了全局和局部特征的表达;最后,高质量图像重建阶段融合浅层和深层特征,通过卷积操作生成高质量的输出图像。这种设计在性能与计算效率之间取得了良好平衡,非常适合图像超分辨率和去噪等低级视觉任务。
1. 浅层特征提取阶段(Shallow Feature Extraction)
功能:
提取输入图像的基础特征,为后续的深层特征提取做准备。
实现:
使用一个简单的
卷积层,从输入的低质量图像3 × 3 3 \times 3 提取浅层特征,输出特征表示为I L Q I_{LQ} 。F S F_S 输出特征维度与输入图像大小相同,仅通道数从
扩展为3 3 。C C
2. 深层特征提取阶段(Deep Feature Extraction)
功能:
学习输入图像的深层特征,捕获全局依赖和局部信息。
实现:
通过堆叠多个 残差状态空间组(RSSG) 进行深层特征提取。
每个 RSSG 包含多个 残差状态空间块(RSSB),RSSB 是该阶段的核心模块,其设计特点包括:
每个 RSSG 的末尾还加入额外的卷积层,用于优化特征。
视觉状态空间模块(VSSM):通过状态空间模型(SSM)捕获长程依赖。
局部增强(Local Enhancement):通过卷积操作解决 1D 展平导致的像素局部信息丢失问题。
通道注意力(Channel Attention):减少特征通道冗余,突出有效通道。
跳跃连接(Skip Connection):在 RSSB 内加入可调跳跃连接,提升特征融合能力。
3. 高质量图像重建阶段(High-Quality Image Reconstruction)
功能:
将浅层特征和深层特征融合,最终生成高质量的恢复图像。
实现:
深层特征
和浅层特征F D F_D 通过逐元素求和得到融合特征F S F_S 。F R = F S + F D F_R = F_S + F_D 使用一个卷积层对融合特征
进行处理,输出高质量图像F R F_R 。I H Q I_{HQ}
即插即用模块作用
RSSG 作为一个即插即用模块:
低级视觉任务:
图像超分辨率(Super-Resolution)、图像去噪(Denoising)、JPEG压缩失真修复(Compression Artifact Reduction)等需要精细处理和重建图像细节的任务。
多任务视觉处理:
在需要处理多种图像退化问题(如混合噪声、模糊和压缩失真)时,可以作为通用特征提取模块,适配不同的任务需求。
高效建模需求:
在受限硬件环境(如移动设备或嵌入式系统)上,RSSG 的线性复杂度和高效的特征提取能力特别适用。
需要长程依赖建模的场景:
在图像或序列处理中,需要捕获全局依赖关系(如大尺度的纹理恢复或全图特征聚合)的任务中,RSSG 是理想的模块。
消融实验结果
该表通过移除或替换 RSSB 中的关键模块(如卷积层和通道注意力),在 Set5、Set14 和 Urban100 数据集上比较模型的 PSNR 变化。结果表明,移除卷积层会导致性能显著下降,而移除卷积层和通道注意力的影响更大,替换为 MLP 也不能达到最佳效果。这说明局部卷积和通道注意力是提升性能的核心设计。
表2比较了单方向、双方向和四方向扫描模式在不同数据集上的性能表现。结果显示,四方向扫描模式能显著提升模型在 Set5 和 Urban100 数据集上的 PSNR,相较单方向扫描有明显优势。这验证了多方向扫描策略能够更全面地捕获图像中的全局信息,从而提升恢复效果。
即插即用模块
import math
import torch
import torch.nn as nn
import torch.utils.checkpoint as checkpoint
import torch.nn.functional as F
from functools import partial
from typing import Optional, Callable
from timm.models.layers import DropPath, to_2tuple, trunc_normal_
from mamba_ssm.ops.selective_scan_interface import selective_scan_fn, selective_scan_ref
from einops import rearrange, repeat
# 论文:MambaIR: A simple baseline for image restoration with state-space model
# 论文地址:https://arxiv.org/pdf/2402.15648.pdf
class ChannelAttention(nn.Module):
def __init__(self, num_feat, squeeze_factor=16):
super(ChannelAttention, self).__init__()
self.attention = nn.Sequential(
nn.AdaptiveAvgPool2d(1),
nn.Conv2d(num_feat, num_feat // squeeze_factor, 1, padding=0),
nn.ReLU(inplace=True),
nn.Conv2d(num_feat // squeeze_factor, num_feat, 1, padding=0),
nn.Sigmoid())
def forward(self, x):
y = self.attention(x)
return x * y
class CAB(nn.Module):
def __init__(self, num_feat, is_light_sr=False, compress_ratio=3, squeeze_factor=30):
super(CAB, self).__init__()
if is_light_sr: # a larger compression ratio is used for light-SR
compress_ratio = 6
self.cab = nn.Sequential(
nn.Conv2d(num_feat, num_feat // compress_ratio, 3, 1, 1),
nn.GELU(),
nn.Conv2d(num_feat // compress_ratio, num_feat, 3, 1, 1),
ChannelAttention(num_feat, squeeze_factor)
)
def forward(self, x):
return self.cab(x)
class SS2D(nn.Module):
def __init__(
self,
d_model,
d_state=16,
d_conv=3,
expand=2.,
dt_rank="auto",
dt_min=0.001,
dt_max=0.1,
dt_init="random",
dt_scale=1.0,
dt_init_floor=1e-4,
dropout=0.,
conv_bias=True,
bias=False,
device=None,
dtype=None,
**kwargs,
):
factory_kwargs = {"device": device, "dtype": dtype}
super().__init__()
self.d_model = d_model
self.d_state = d_state
self.d_conv = d_conv
self.expand = expand
self.d_inner = int(self.expand * self.d_model)
self.dt_rank = math.ceil(self.d_model / 16) if dt_rank == "auto" else dt_rank
self.in_proj = nn.Linear(self.d_model, self.d_inner * 2, bias=bias, **factory_kwargs)
self.conv2d = nn.Conv2d(
in_channels=self.d_inner,
out_channels=self.d_inner,
groups=self.d_inner,
bias=conv_bias,
kernel_size=d_conv,
padding=(d_conv - 1) // 2,
**factory_kwargs,
)
self.act = nn.SiLU()
self.x_proj = (
nn.Linear(self.d_inner, (self.dt_rank + self.d_state * 2), bias=False, **factory_kwargs),
nn.Linear(self.d_inner, (self.dt_rank + self.d_state * 2), bias=False, **factory_kwargs),
nn.Linear(self.d_inner, (self.dt_rank + self.d_state * 2), bias=False, **factory_kwargs),
nn.Linear(self.d_inner, (self.dt_rank + self.d_state * 2), bias=False, **factory_kwargs),
)
self.x_proj_weight = nn.Parameter(torch.stack([t.weight for t in self.x_proj], dim=0)) # (K=4, N, inner)
del self.x_proj
self.dt_projs = (
self.dt_init(self.dt_rank, self.d_inner, dt_scale, dt_init, dt_min, dt_max, dt_init_floor,
**factory_kwargs),
self.dt_init(self.dt_rank, self.d_inner, dt_scale, dt_init, dt_min, dt_max, dt_init_floor,
**factory_kwargs),
self.dt_init(self.dt_rank, self.d_inner, dt_scale, dt_init, dt_min, dt_max, dt_init_floor,
**factory_kwargs),
self.dt_init(self.dt_rank, self.d_inner, dt_scale, dt_init, dt_min, dt_max, dt_init_floor,
**factory_kwargs),
)
self.dt_projs_weight = nn.Parameter(torch.stack([t.weight for t in self.dt_projs], dim=0)) # (K=4, inner, rank)
self.dt_projs_bias = nn.Parameter(torch.stack([t.bias for t in self.dt_projs], dim=0)) # (K=4, inner)
del self.dt_projs
self.A_logs = self.A_log_init(self.d_state, self.d_inner, copies=4, merge=True) # (K=4, D, N)
self.Ds = self.D_init(self.d_inner, copies=4, merge=True) # (K=4, D, N)
self.selective_scan = selective_scan_fn
self.out_norm = nn.LayerNorm(self.d_inner)
self.out_proj = nn.Linear(self.d_inner, self.d_model, bias=bias, **factory_kwargs)
self.dropout = nn.Dropout(dropout) if dropout > 0. else None
@staticmethod
def dt_init(dt_rank, d_inner, dt_scale=1.0, dt_init="random", dt_min=0.001, dt_max=0.1, dt_init_floor=1e-4,
**factory_kwargs):
dt_proj = nn.Linear(dt_rank, d_inner, bias=True, **factory_kwargs)
# Initialize special dt projection to preserve variance at initialization
dt_init_std = dt_rank ** -0.5 * dt_scale
if dt_init == "constant":
nn.init.constant_(dt_proj.weight, dt_init_std)
elif dt_init == "random":
nn.init.uniform_(dt_proj.weight, -dt_init_std, dt_init_std)
else:
raise NotImplementedError
# Initialize dt bias so that F.softplus(dt_bias) is between dt_min and dt_max
dt = torch.exp(
torch.rand(d_inner, **factory_kwargs) * (math.log(dt_max) - math.log(dt_min))
+ math.log(dt_min)
).clamp(min=dt_init_floor)
# Inverse of softplus: https://github.com/pytorch/pytorch/issues/72759
inv_dt = dt + torch.log(-torch.expm1(-dt))
with torch.no_grad():
dt_proj.bias.copy_(inv_dt)
# Our initialization would set all Linear.bias to zero, need to mark this one as _no_reinit
dt_proj.bias._no_reinit = True
return dt_proj
@staticmethod
def A_log_init(d_state, d_inner, copies=1, device=None, merge=True):
# S4D real initialization
A = repeat(
torch.arange(1, d_state + 1, dtype=torch.float32, device=device),
"n -> d n",
d=d_inner,
).contiguous()
A_log = torch.log(A) # Keep A_log in fp32
if copies > 1:
A_log = repeat(A_log, "d n -> r d n", r=copies)
if merge:
A_log = A_log.flatten(0, 1)
A_log = nn.Parameter(A_log)
A_log._no_weight_decay = True
return A_log
@staticmethod
def D_init(d_inner, copies=1, device=None, merge=True):
# D "skip" parameter
D = torch.ones(d_inner, device=device)
if copies > 1:
D = repeat(D, "n1 -> r n1", r=copies)
if merge:
D = D.flatten(0, 1)
D = nn.Parameter(D) # Keep in fp32
D._no_weight_decay = True
return D
def forward_core(self, x: torch.Tensor):
B, C, H, W = x.shape
L = H * W
K = 4
x_hwwh = torch.stack([x.view(B, -1, L), torch.transpose(x, dim0=2, dim1=3).contiguous().view(B, -1, L)],
dim=1).view(B, 2, -1, L)
xs = torch.cat([x_hwwh, torch.flip(x_hwwh, dims=[-1])], dim=1) # (1, 4, 192, 3136)
x_dbl = torch.einsum("b k d l, k c d -> b k c l", xs.view(B, K, -1, L), self.x_proj_weight)
dts, Bs, Cs = torch.split(x_dbl, [self.dt_rank, self.d_state, self.d_state], dim=2)
dts = torch.einsum("b k r l, k d r -> b k d l", dts.view(B, K, -1, L), self.dt_projs_weight)
xs = xs.float().view(B, -1, L)
dts = dts.contiguous().float().view(B, -1, L) # (b, k * d, l)
Bs = Bs.float().view(B, K, -1, L)
Cs = Cs.float().view(B, K, -1, L) # (b, k, d_state, l)
Ds = self.Ds.float().view(-1)
As = -torch.exp(self.A_logs.float()).view(-1, self.d_state)
dt_projs_bias = self.dt_projs_bias.float().view(-1) # (k * d)
out_y = self.selective_scan(
xs, dts,
As, Bs, Cs, Ds, z=None,
delta_bias=dt_projs_bias,
delta_softplus=True,
return_last_state=False,
).view(B, K, -1, L)
assert out_y.dtype == torch.float
inv_y = torch.flip(out_y[:, 2:4], dims=[-1]).view(B, 2, -1, L)
wh_y = torch.transpose(out_y[:, 1].view(B, -1, W, H), dim0=2, dim1=3).contiguous().view(B, -1, L)
invwh_y = torch.transpose(inv_y[:, 1].view(B, -1, W, H), dim0=2, dim1=3).contiguous().view(B, -1, L)
return out_y[:, 0], inv_y[:, 0], wh_y, invwh_y
def forward(self, x: torch.Tensor, **kwargs):
B, H, W, C = x.shape
xz = self.in_proj(x)
x, z = xz.chunk(2, dim=-1)
x = x.permute(0, 3, 1, 2).contiguous()
x = self.act(self.conv2d(x))
y1, y2, y3, y4 = self.forward_core(x)
assert y1.dtype == torch.float32
y = y1 + y2 + y3 + y4
y = torch.transpose(y, dim0=1, dim1=2).contiguous().view(B, H, W, -1)
y = self.out_norm(y)
y = y * F.silu(z)
out = self.out_proj(y)
if self.dropout is not None:
out = self.dropout(out)
return out
class VSSBlock(nn.Module):
def __init__(
self,
hidden_dim: int = 0,
drop_path: float = 0,
norm_layer: Callable[..., torch.nn.Module] = partial(nn.LayerNorm, eps=1e-6),
attn_drop_rate: float = 0,
d_state: int = 16,
expand: float = 2.,
is_light_sr: bool = False,
**kwargs,
):
super().__init__()
self.ln_1 = norm_layer(hidden_dim)
self.self_attention = SS2D(d_model=hidden_dim, d_state=d_state, expand=expand, dropout=attn_drop_rate, **kwargs)
self.drop_path = DropPath(drop_path)
self.skip_scale = nn.Parameter(torch.ones(hidden_dim))
self.conv_blk = CAB(hidden_dim, is_light_sr)
self.ln_2 = nn.LayerNorm(hidden_dim)
self.skip_scale2 = nn.Parameter(torch.ones(hidden_dim))
def forward(self, input, x_size):
# x [B,HW,C]
B, L, C = input.shape
input = input.view(B, *x_size, C).contiguous() # [B,H,W,C]
x = self.ln_1(input)
x = input * self.skip_scale + self.drop_path(self.self_attention(x))
x = x * self.skip_scale2 + self.conv_blk(self.ln_2(x).permute(0, 3, 1, 2).contiguous()).permute(0, 2, 3,
1).contiguous()
x = x.view(B, -1, C).contiguous()
return x
class BasicLayer(nn.Module):
""" The Basic MambaIR Layer in one Residual State Space Group
Args:
dim (int): Number of input channels.
input_resolution (tuple[int]): Input resolution.
depth (int): Number of blocks.
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0
norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None
use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.
"""
def __init__(self,
dim,
input_resolution,
depth=6,
drop_path=0.,
d_state=16,
mlp_ratio=2.,
norm_layer=nn.LayerNorm,
downsample=None,
use_checkpoint=False, is_light_sr=False):
super().__init__()
self.dim = dim
self.input_resolution = input_resolution
self.depth = depth
self.mlp_ratio = mlp_ratio
self.use_checkpoint = use_checkpoint
# build blocks
self.blocks = nn.ModuleList()
for i in range(depth):
self.blocks.append(VSSBlock(
hidden_dim=dim,
drop_path=drop_path[i] if isinstance(drop_path, list) else drop_path,
norm_layer=nn.LayerNorm,
attn_drop_rate=0,
d_state=d_state,
expand=self.mlp_ratio,
input_resolution=input_resolution, is_light_sr=is_light_sr))
# patch merging layer
if downsample is not None:
self.downsample = downsample(input_resolution, dim=dim, norm_layer=norm_layer)
else:
self.downsample = None
def forward(self, x, x_size):
for blk in self.blocks:
if self.use_checkpoint:
x = checkpoint.checkpoint(blk, x)
else:
x = blk(x, x_size)
if self.downsample is not None:
x = self.downsample(x)
return x
def extra_repr(self) -> str:
return f'dim={self.dim}, input_resolution={self.input_resolution}, depth={self.depth}'
class ResidualGroup(nn.Module):
"""Residual State Space Group (RSSG).
Args:
dim (int): Number of input channels.
input_resolution (tuple[int]): Input resolution.
depth (int): Number of blocks.
mlp_ratio (float): Ratio of mlp hidden dim to embedding dim.
drop_path (float | tuple[float], optional): Stochastic depth rate. Default: 0.0
norm_layer (nn.Module, optional): Normalization layer. Default: nn.LayerNorm
downsample (nn.Module | None, optional): Downsample layer at the end of the layer. Default: None
use_checkpoint (bool): Whether to use checkpointing to save memory. Default: False.
img_size: Input image size.
patch_size: Patch size.
resi_connection: The convolutional block before residual connection.
"""
def __init__(self,
dim=96,
input_resolution=(64, 64),
depth=6,
d_state=16,
mlp_ratio=4.,
drop_path=0.,
norm_layer=nn.LayerNorm,
downsample=None,
use_checkpoint=False,
img_size=224,
patch_size=4,
resi_connection='1conv',
is_light_sr=False):
super(ResidualGroup, self).__init__()
self.dim = dim
self.input_resolution = input_resolution # [64, 64]
self.residual_group = BasicLayer(
dim=dim,
input_resolution=input_resolution,
depth=depth,
d_state=d_state,
mlp_ratio=mlp_ratio,
drop_path=drop_path,
norm_layer=norm_layer,
downsample=downsample,
use_checkpoint=use_checkpoint,
is_light_sr=is_light_sr)
# build the last conv layer in each residual state space group
if resi_connection == '1conv':
self.conv = nn.Conv2d(dim, dim, 3, 1, 1)
elif resi_connection == '3conv':
# to save parameters and memory
self.conv = nn.Sequential(
nn.Conv2d(dim, dim // 4, 3, 1, 1), nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(dim // 4, dim // 4, 1, 1, 0), nn.LeakyReLU(negative_slope=0.2, inplace=True),
nn.Conv2d(dim // 4, dim, 3, 1, 1))
self.patch_embed = PatchEmbed(
img_size=img_size, patch_size=patch_size, in_chans=0, embed_dim=dim, norm_layer=None)
self.patch_unembed = PatchUnEmbed(
img_size=img_size, patch_size=patch_size, in_chans=0, embed_dim=dim, norm_layer=None)
def forward(self, x, x_size):
return self.patch_embed(self.conv(self.patch_unembed(self.residual_group(x, x_size), x_size))) + x
class PatchEmbed(nn.Module):
r""" transfer 2D feature map into 1D token sequence
Args:
img_size (int): Image size. Default: None.
patch_size (int): Patch token size. Default: None.
in_chans (int): Number of input image channels. Default: 3.
embed_dim (int): Number of linear projection output channels. Default: 96.
norm_layer (nn.Module, optional): Normalization layer. Default: None
"""
def __init__(self, img_size=224, patch_size=4, in_chans=3, embed_dim=96, norm_layer=None):
super().__init__()
img_size = to_2tuple(img_size)
patch_size = to_2tuple(patch_size)
patches_resolution = [img_size[0] // patch_size[0], img_size[1] // patch_size[1]]
self.img_size = img_size
self.patch_size = patch_size
self.patches_resolution = patches_resolution
self.num_patches = patches_resolution[0] * patches_resolution[1]
self.in_chans = in_chans
self.embed_dim = embed_dim
if norm_layer is not None:
self.norm = norm_layer(embed_dim)
else:
self.norm = None
def forward(self, x):
x = x.flatten(2).transpose(1, 2) # b Ph*Pw c
if self.norm is not None:
x = self.norm(x)
return x
class PatchUnEmbed(nn.Module):
r""" return 2D feature map from 1D token sequence
Args:
img_size (int): Image size. Default: None.
patch_size (int): Patch token size. Default: None.
in_chans (int): Number of input image channels. Default: 3.
embed_dim (int): Number of linear projection output channels. Default: 96.
norm_layer (nn.Module, optional): Normalization layer. Default: None
"""
def __init__(self, img_size=224, patch_size=4, in_chans=3, embed_dim=96, norm_layer=None):
super().__init__()
img_size = to_2tuple(img_size)
patch_size = to_2tuple(patch_size)
patches_resolution = [img_size[0] // patch_size[0], img_size[1] // patch_size[1]]
self.img_size = img_size
self.patch_size = patch_size
self.patches_resolution = patches_resolution
self.num_patches = patches_resolution[0] * patches_resolution[1]
self.in_chans = in_chans
self.embed_dim = embed_dim
def forward(self, x, x_size):
x = x.transpose(1, 2).view(x.shape[0], self.embed_dim, x_size[0], x_size[1]) # b Ph*Pw c
return x
if __name__ == '__main__':
input = torch.randn(1, 64*64, 96) # B N C
# 检查是否有可用的 GPU
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
# 将输入张量移动到 GPU
input = input.to(device)
print(input.shape)
# Initialize ResidualGroup
model = ResidualGroup(dim=96, input_resolution=(64, 64), depth=6, d_state=16, mlp_ratio=4.,
drop_path=0.)
model = model.to(device)
# 使用输入张量调用模型
output = model(input, (64, 64)) print(output.shape)
便捷下载方式
浏览打开网址:https://github.com/ai-dawang/PlugNPlay-Modules
更多分析可见原文