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

FFM 特征融合模块

先对齐再减冗:1×1 卷积把 CNN 特征图与 Transformer token 序列对齐到同一维度,再用通道注意力(CA)压低冗余通道、放大互补信息

计算机视觉计算机视觉语义分割FFM裂缝检测特征融合

结构图

FFM 特征融合模块结构图
FFM 特征融合模块结构图

先对齐再减冗:1×1 卷积把 CNN 特征图与 Transformer token 序列对齐到同一维度,再用通道注意力(CA)压低冗余通道、放大互补信息

论文信息

  • 论文:A dual encoder crack segmentation network with Haar wavelet-based high–low frequency attention
  • 出处:Expert Systems With Applications 2024

核心痛点

裂缝细长、占比小、背景复杂,纯 CNN 丢细节、建不了长程关系,纯 Transformer 又被大片背景 patch 干扰;双编码器融合时大多只是简单拼接或相加,两路特征缺乏深层交互,互补优势难以发挥。

模块亮点

  • 先对齐再减冗:1×1 卷积把 CNN 特征图与 Transformer token 序列对齐到同一维度,再用通道注意力(CA)压低冗余通道、放大互补信息
  • 跨域双向交互:CFB 让 CNN 特征的 Q 与 Transformer 特征的 K/V 交叉做多头注意力,反向再来一遍,双向拼接降维;Q/K/V 全用深度可分离卷积生成,更省参数
  • 相乘增强再聚合:两路特征相乘(CE)增强显著信息、抑制弱特征,拼接后经 FFB 聚合去冗;DECS-Net 在 DeepCrack 上 Re 92.70%、F1 87.51%,Crack3238 上 Re 79.02%、F1 78.64%,均列十个对比 SOTA 之首

适用场景

领域为裂缝分割 / 结构健康监测(道路、桥梁等);原文在 DeepCrack 与 Crack3238 上验证;可迁移到医学图像分割、遥感线状目标提取等双分支网络。

插入位置建议

  • 放在双编码器对应尺度层之后、送入解码器之前,替代简单相加或拼接
  • 替换 TransFuse、FAT-Net 等混合分割网络中原有的特征融合模块
  • 用在 U 型网络跳跃连接处,融合编码器与解码器特征

适用任务

  • 裂缝分割
  • 结构健康监测

完整代码

ffm.py
import torch
import torch.nn as nn


from einops.einops import rearrange

def to_3d(x):
    return rearrange(x, 'b c h w -> b (h w) c')

def to_4d(x, h, w):
    return rearrange(x, 'b (h w) c -> b c h w', h=h, w=w)


class DSC(nn.Module):
    def __init__(self, c_in, c_out, k_size=3, stride=1, padding=1):
        super(DSC, self).__init__()
        self.c_in = c_in
        self.c_out = c_out
        self.dw = nn.Conv2d(c_in, c_in, k_size, stride, padding, groups=c_in)
        self.pw = nn.Conv2d(c_in, c_out, 1, 1)

    def forward(self, x):
        out = self.dw(x)
        out = self.pw(out)
        return out

class IDSC(nn.Module):
    def __init__(self, c_in, c_out, k_size=3, stride=1, padding=1):
        super(IDSC, self).__init__()
        self.c_in = c_in
        self.c_out = c_out
        self.dw = nn.Conv2d(c_out, c_out, k_size, stride, padding, groups=c_out)
        self.pw = nn.Conv2d(c_in, c_out, 1, 1)

    def forward(self, x):
        out = self.pw(x)
        out = self.dw(out)
        return out

class FFM(nn.Module):
    def __init__(self, dim1, dim2, res):
        super().__init__()
        self.trans_c = nn.Conv2d(dim1, dim2, 1)
        self.avg = nn.AdaptiveAvgPool2d(1)
        self.li1 = nn.Linear(dim2, dim2)
        self.li2 = nn.Linear(dim2, dim2)

        self.qx = DSC(dim2, dim2)
        self.kx = DSC(dim2, dim2)
        self.vx = DSC(dim2, dim2)
        self.projx = DSC(dim2, dim2)

        self.qy = DSC(dim2, dim2)
        self.ky = DSC(dim2, dim2)
        self.vy = DSC(dim2, dim2)
        self.projy = DSC(dim2, dim2)

        self.concat = nn.Conv2d(dim2*2, dim2, 1)

        self.fusion = nn.Sequential(IDSC(dim2*4, dim2),
                                    nn.BatchNorm2d(dim2),
                                    nn.GELU(),
                                    DSC(dim2, dim2),
                                    nn.BatchNorm2d(dim2),
                                    nn.GELU(),
                                    nn.Conv2d(dim2, dim2, 1),
                                    nn.BatchNorm2d(dim2),
                                    nn.GELU())


    def forward(self, x, y):
        b, c, h, w = x.shape
        B, N, C = y.shape
        H = W = int(N**0.5)

        x = self.trans_c(x)
        y = y.reshape(B, H, W, C).permute(0, 3, 1, 2)

        avg_x = self.avg(x).permute(0, 2, 3, 1)
        avg_y = self.avg(y).permute(0, 2, 3, 1)
        x_weight = self.li1(avg_x)
        y_weight = self.li2(avg_y)
        x = x.permute(0, 2, 3, 1) * x_weight
        y = y.permute(0, 2, 3, 1) * y_weight

        out1 = x * y
        out1 = out1.permute(0, 3, 1, 2)

        x = x.permute(0, 3, 1, 2)
        y = y.permute(0, 3, 1, 2)

        qy = self.qy(y).reshape(B, 8, C//8, H//4, 4, W//4, 4).permute(0, 3, 5, 1, 4, 6, 2).reshape(B, N//16, 8, 16, C//8)
        kx = self.kx(x).reshape(B, 8, C//8, H//4, 4, W//4, 4).permute(0, 3, 5, 1, 4, 6, 2).reshape(B, N//16, 8, 16, C//8)
        vx = self.vx(x).reshape(B, 8, C//8, H//4, 4, W//4, 4).permute(0, 3, 5, 1, 4, 6, 2).reshape(B, N//16, 8, 16, C//8)

        attnx = (qy @ kx.transpose(-2, -1)) * (C**-0.5)
        attnx = attnx.softmax(dim=-1)
        attnx = (attnx @ vx).transpose(2, 3).reshape(B, H//4, w//4, 4, 4, C)
        attnx = attnx.transpose(2, 3).reshape(B,  H, W, C).permute(0, 3, 1, 2)
        attnx = self.projx(attnx)


        qx = self.qx(x).reshape(B, 8, C//8, H//4, 4, W//4, 4).permute(0, 3, 5, 1, 4, 6, 2).reshape(B, N//16, 8, 16, C//8)
        ky = self.ky(y).reshape(B, 8, C//8, H//4, 4, W//4, 4).permute(0, 3, 5, 1, 4, 6, 2).reshape(B, N//16, 8, 16, C//8)
        vy = self.vy(y).reshape(B, 8, C//8, H//4, 4, W//4, 4).permute(0, 3, 5, 1, 4, 6, 2).reshape(B, N//16, 8, 16, C//8)

        attny = (qx @ ky.transpose(-2, -1)) * (C**-0.5)
        attny = attny.softmax(dim=-1)
        attny = (attny @ vy).transpose(2, 3).reshape(B, H//4, w//4, 4, 4, C)
        attny = attny.transpose(2, 3).reshape(B, H, W, C).permute(0, 3, 1, 2)
        attny = self.projy(attny)

        out2 = torch.cat([attnx, attny], dim=1)
        out2 = self.concat(out2)

        out = torch.cat([x, y, out1, out2], dim=1)

        out = self.fusion(out)
        return out


if __name__ == '__main__':

    batch_size = 2
    c_in = 64  #
    c_out = 64
    height = 32
    width = 32
    N = height * width

    x_input = torch.rand(batch_size, c_in, height, width).to('cuda')  # For x input
    y_input = torch.rand(batch_size, N, c_in).to('cuda')  # For y input

    block = FFM(dim1=c_in, dim2=c_out, res=height).to('cuda')

    output = block(x_input, y_input)

    print("x_input size:", x_input.size())
    print("y_input size:", y_input.size())
    print("Output size:", output.size())

环境与依赖

Python 3.9+

继续浏览

下一篇FITS 频率插值时序分析复数频域插值:把预测 / 重构表述为复频域插值,核心只是一层复数线性层,用复数乘法同时学幅值缩放与相位偏移,rFFT 进、irFFT 出,本质仍是时域模型,预测、异常检测等任务都能接

相关资源