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

LEGM 局部特征嵌入全局特征提取模块

局部全局双路并行:卷积分支(3×3 卷积或 5×5 深度卷积)管局部细节,窗口自注意力管长程依赖,两路相加后投影输出

计算机视觉计算机视觉图像去雾LEGMCVPR2024图像复原

结构图

LEGM 局部特征嵌入全局特征提取模块结构图
LEGM 局部特征嵌入全局特征提取模块结构图

局部全局双路并行:卷积分支(3×3 卷积或 5×5 深度卷积)管局部细节,窗口自注意力管长程依赖,两路相加后投影输出

论文信息

  • 论文:Depth Information Assisted Collaborative Mutual Promotion Network for Single Image Dehazing
  • 出处:CVPR 2024

核心痛点

去雾网络的特征提取大多依赖纯卷积,局部信息丰富却缺乏全局视野;而雾的浓度与场景深度直接相关、分布极不均匀,不结合全局结构就难以判断哪里雾重、哪里雾轻;直接引入 softmax 自注意力又受高计算复杂度制约,难以密集堆叠。

模块亮点

  • 局部全局双路并行:卷积分支(3×3 卷积或 5×5 深度卷积)管局部细节,窗口自注意力管长程依赖,两路相加后投影输出
  • 注意力按需配比:用 attn_ratio 和 attn_loc 控制堆叠块中哪些启用注意力、其余只跑卷积,再配合移位窗口与相对位置偏置,全局建模与计算量兼得
  • 涨点幅度最大:SOTS-Indoor 消融中 Baseline 35.69 dB 加 LEGM 后升至 40.41 dB(+4.72 dB),是各组件中单次涨点最大的;整体模型达 42.18 dB、SSIM 0.9967,SSIM、NIQE、PIQE、FADE 均为对比方法中最优

适用场景

领域为底层视觉 / 单图像去雾,原文在 RESIDE 的 SOTS 室内、室外测试集及真实雾图上验证;同样适合去雨、去噪、去模糊、超分等复原网络的基础特征提取块。

插入位置建议

  • 替换 U 型去雾 / 复原网络编码器中的特征提取块,原文编码器就堆叠了 3 个 LEGM
  • 接在 U-Net 等骨干之后当主力特征提取器,解码器可再放 2 个配合特征调制做重建
  • 需要辅助信息引导时,把深度特征等与主特征一起送入第一个 LEGM,原文即以深度信息辅助去雾

适用任务

  • 底层视觉
  • 单图像去雾
  • 原文在 RESIDE 的 SOTS 室内

完整代码

legm.py
import torch
import torch.nn as nn
import torch.nn.functional as F
import math
from torch.nn.init import _calculate_fan_in_and_fan_out
from timm.models.layers import to_2tuple, trunc_normal_


"""《Depth Information Assisted Collaborative Mutual Promotion Network for Single Image Dehazing》 CVPR 2024
从单幅雾霾图像恢复清晰图像是一个开放的逆问题。尽管相关研究取得了显著进展,但大多数现有方法忽略了下游任务对上游去雾过程的促进作用。从雾霾生成机制来看,场景的深度信息与雾霾图像之间存在潜在的关联。
基于此,我们提出了一个双任务协同互促框架来实现单幅图像的去雾。该框架通过双任务交互机制将深度估计和去雾任务融合,实现其性能的相互提升。为了实现两个任务的联合优化,我们开发了一种基于差异感知的替代实现机制。
一方面,我们提出了去雾结果深度图与理想图像之间的差异感知,以促进去雾网络关注去雾过程中非理想区域。另一方面,通过提升雾霾图像中难以恢复区域的深度估计性能,去雾网络可以明确地利用雾霾图像的深度信息来辅助清晰图像的恢复。
为了提升深度估计,我们提出利用去雾图像与真实图像之间的差异来引导深度估计网络聚焦于去雾后不理想的区域。这使得去雾和深度估计能够相互促进,充分发挥各自的优势。实验结果表明,该方法的性能优于目前最先进的方法。
"""

class Mlp(nn.Module):
    def __init__(self, network_depth, in_features, hidden_features=None, out_features=None):
        super().__init__()
        out_features = out_features or in_features
        hidden_features = hidden_features or in_features
        self.network_depth = network_depth
        self.mlp = nn.Sequential(
            nn.Conv2d(in_features, hidden_features, 1),
            nn.ReLU(True),
            nn.Conv2d(hidden_features, out_features, 1)
        )
        self.apply(self._init_weights)

    def _init_weights(self, m):
        if isinstance(m, nn.Conv2d):
            gain = (8 * self.network_depth) ** (-1/4)
            fan_in, fan_out = _calculate_fan_in_and_fan_out(m.weight)
            std = gain * math.sqrt(2.0 / float(fan_in + fan_out))
            trunc_normal_(m.weight, std=std)
            if m.bias is not None:
                nn.init.constant_(m.bias, 0)
    def forward(self, x):
        return self.mlp(x)


def window_partition(x, window_size):
    B, H, W, C = x.shape
    x = x.view(B, H // window_size, window_size, W // window_size, window_size, C)
    windows = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(-1, window_size**2, C)
    return windows


def window_reverse(windows, window_size, H, W):
    B = int(windows.shape[0] / (H * W / window_size / window_size))
    x = windows.view(B, H // window_size, W // window_size, window_size, window_size, -1)
    x = x.permute(0, 1, 3, 2, 4, 5).contiguous().view(B, H, W, -1)
    return x


def get_relative_positions(window_size):
    coords_h = torch.arange(window_size)
    coords_w = torch.arange(window_size)
    # coords = torch.stack(torch.meshgrid(coords_h, coords_w, indexing='xy'))
    coords = torch.stack(torch.meshgrid([coords_h, coords_w]))
    coords_flatten = torch.flatten(coords, 1)
    relative_positions = coords_flatten[:, :, None] - coords_flatten[:, None, :]
    relative_positions = relative_positions.permute(1, 2, 0).contiguous()
    relative_positions_log  = torch.sign(relative_positions) * torch.log(1. + relative_positions.abs())
    return relative_positions_log


class WATT(nn.Module):
    def __init__(self, dim, window_size, num_heads):

        super().__init__()
        self.dim = dim
        self.window_size = window_size
        self.num_heads = num_heads
        head_dim = dim // num_heads
        self.scale = head_dim ** -0.5

        relative_positions = get_relative_positions(self.window_size)
        self.register_buffer("relative_positions", relative_positions)
        self.meta = nn.Sequential(
            nn.Linear(2, 256, bias=True),
            nn.ReLU(True),
            nn.Linear(256, num_heads, bias=True)
        )

        self.softmax = nn.Softmax(dim=-1)

    def forward(self, qkv):
        B_, N, _ = qkv.shape
        qkv = qkv.reshape(B_, N, 3, self.num_heads, self.dim // self.num_heads).permute(2, 0, 3, 1, 4)
        q, k, v = qkv[0], qkv[1], qkv[2]
        q = q * self.scale
        attn = (q @ k.transpose(-2, -1))
        relative_position_bias = self.meta(self.relative_positions)
        relative_position_bias = relative_position_bias.permute(2, 0, 1).contiguous()
        attn = attn + relative_position_bias.unsqueeze(0)
        attn = self.softmax(attn)
        x = (attn @ v).transpose(1, 2).reshape(B_, N, self.dim)
        return x


class Att(nn.Module):
    def __init__(self, network_depth, dim, num_heads, window_size, shift_size, use_attn=False, conv_type=None):
        super().__init__()
        self.dim = dim
        self.head_dim = int(dim // num_heads)
        self.num_heads = num_heads
        self.window_size = window_size
        self.shift_size = shift_size
        self.network_depth = network_depth
        self.use_attn = use_attn
        self.conv_type = conv_type

        if self.conv_type == 'Conv':
            self.conv = nn.Sequential(
                nn.Conv2d(dim, dim, kernel_size=3, padding=1, padding_mode='reflect'),
                nn.ReLU(True),
                nn.Conv2d(dim, dim, kernel_size=3, padding=1, padding_mode='reflect')
            )

        if self.conv_type == 'DWConv':
            self.conv = nn.Conv2d(dim, dim, kernel_size=5, padding=2, groups=dim, padding_mode='reflect')
        if self.conv_type == 'DWConv' or self.use_attn:
            self.V = nn.Conv2d(dim, dim, 1)
            self.proj = nn.Conv2d(dim, dim, 1)
        if self.use_attn:
            self.QK = nn.Conv2d(dim, dim * 2, 1)
            self.attn = WATT(dim, window_size, num_heads)

        self.apply(self._init_weights)

    def _init_weights(self, m):
        if isinstance(m, nn.Conv2d):
            w_shape = m.weight.shape

            if w_shape[0] == self.dim * 2:
                fan_in, fan_out = _calculate_fan_in_and_fan_out(m.weight)
                std = math.sqrt(2.0 / float(fan_in + fan_out))
                trunc_normal_(m.weight, std=std)
            else:
                gain = (8 * self.network_depth) ** (-1/4)
                fan_in, fan_out = _calculate_fan_in_and_fan_out(m.weight)
                std = gain * math.sqrt(2.0 / float(fan_in + fan_out))
                trunc_normal_(m.weight, std=std)

            if m.bias is not None:
                nn.init.constant_(m.bias, 0)

    def check_size(self, x, shift=False):
        _, _, h, w = x.size()
        mod_pad_h = (self.window_size - h % self.window_size) % self.window_size
        mod_pad_w = (self.window_size - w % self.window_size) % self.window_size

        if shift:
            x = F.pad(x, (self.shift_size, (self.window_size-self.shift_size+mod_pad_w) % self.window_size,
                          self.shift_size, (self.window_size-self.shift_size+mod_pad_h) % self.window_size), mode='reflect')
        else:
            x = F.pad(x, (0, mod_pad_w, 0, mod_pad_h), 'reflect')
        return x

    def forward(self, X):
        B, C, H, W = X.shape

        if self.conv_type == 'DWConv' or self.use_attn:
            V = self.V(X)

        if self.use_attn:
            QK = self.QK(X)
            QKV = torch.cat([QK, V], dim=1)

            # shift
            shifted_QKV = self.check_size(QKV, self.shift_size > 0)
            Ht, Wt = shifted_QKV.shape[2:]

            # partition windows
            shifted_QKV = shifted_QKV.permute(0, 2, 3, 1)
            qkv = window_partition(shifted_QKV, self.window_size)  # nW*B, window_size**2, C

            attn_windows = self.attn(qkv)

            # merge windows
            shifted_out = window_reverse(attn_windows, self.window_size, Ht, Wt)  # B H' W' C

            # reverse cyclic shift
            out = shifted_out[:, self.shift_size:(self.shift_size+H), self.shift_size:(self.shift_size+W), :]
            attn_out = out.permute(0, 3, 1, 2)

            if self.conv_type in ['Conv', 'DWConv']:
                conv_out = self.conv(V)
                out = self.proj(conv_out + attn_out)
            else:
                out = self.proj(attn_out)

        else:
            if self.conv_type == 'Conv':
                out = self.conv(X)
            elif self.conv_type == 'DWConv':
                out = self.proj(self.conv(V))

        return out


class TB(nn.Module):
    def __init__(self, network_depth, dim, num_heads, mlp_ratio=4.,
                 norm_layer=nn.LayerNorm, mlp_norm=False,
                 window_size=8, shift_size=0, use_attn=True, conv_type=None):
        super().__init__()
        self.use_attn = use_attn
        self.mlp_norm = mlp_norm

        self.norm1 = norm_layer(dim) if use_attn else nn.Identity()
        self.attn = Att(network_depth, dim, num_heads=num_heads, window_size=window_size,
                              shift_size=shift_size, use_attn=use_attn, conv_type=conv_type)

        self.norm2 = norm_layer(dim) if use_attn and mlp_norm else nn.Identity()
        self.mlp = Mlp(network_depth, dim, hidden_features=int(dim * mlp_ratio))

    def forward(self, x):
        identity = x
        if self.use_attn:
            x = self.norm1(x.permute(0, 2, 3, 1)).permute(0, 3, 1, 2)
        x = self.attn(x)
        x = identity + x

        identity = x
        if self.use_attn and self.mlp_norm:
            x = self.norm2(x.permute(0, 2, 3, 1)).permute(0, 3, 1, 2)
        x = self.mlp(x)
        x = identity + x
        return x


class LEGM(nn.Module):
    def __init__(self, network_depth, dim, depth, num_heads, mlp_ratio=4.,
                 norm_layer=nn.LayerNorm, window_size=8,
                 attn_ratio=0., attn_loc='last', conv_type=None):

        super().__init__()
        self.dim = dim
        self.depth = depth

        attn_depth = attn_ratio * depth

        if attn_loc == 'last':
            use_attns = [i >= depth-attn_depth for i in range(depth)]
        elif attn_loc == 'first':
            use_attns = [i < attn_depth for i in range(depth)]
        elif attn_loc == 'middle':
            use_attns = [i >= (depth-attn_depth)//2 and i < (depth+attn_depth)//2 for i in range(depth)]

        # build blocks
        self.blocks = nn.ModuleList([
            TB(network_depth=network_depth,
                             dim=dim,
                             num_heads=num_heads,
                             mlp_ratio=mlp_ratio,
                             norm_layer=norm_layer,
                             window_size=window_size,
                             shift_size=0 if (i % 2 == 0) else window_size // 2,
                             use_attn=use_attns[i], conv_type=conv_type)
            for i in range(depth)])

    def forward(self, x):
        for blk in self.blocks:
            x = blk(x)
        return x


if __name__ == '__main__':
    # 模型参数配置
    network_depth = 6  # 网络深度
    dim = 64  # 特征维度
    depth = 4  # LEGM模块深度
    num_heads = 4  # 注意力头数
    window_size = 8  # 窗口大小

    # 创建LEGM模型实例
    model = LEGM(
        network_depth=network_depth,
        dim=dim,
        depth=depth,
        num_heads=num_heads,
        window_size=window_size,
        attn_ratio=0.5,  # 50%的块使用注意力机制
        attn_loc='last',  # 注意力放在最后几层
        conv_type='DWConv'  # 使用深度可分离卷积
    ).to('cuda')


    input_tensor = torch.randn(2, dim, 64, 64).to('cuda')


    output_tensor = model(input_tensor)

    # 打印输入输出形状
    print("\n输入张量形状:", input_tensor.shape)
    print("输出张量形状:", output_tensor.shape)

环境与依赖

Python 3.9+

继续浏览

下一篇LFA 局部流注意力显式流相关性建模:由两条交通流公理导出 IO-LFA(Query 取流入、Key/Value 取邻域流出)与 OI-LFA(流出与自身流入逐点交互)两个变体,注意力权重直接反映流量交互强弱,自解释不黑盒

相关资源