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

MCAM 多模态交叉注意力模块

二阶交叉注意力:对光学和 SAR 特征各用 1×1 卷积生成 V、Q、K,先分别算自注意力图,再用 Hadamard 积交叉得到联合注意力图,挖掘两模态间的隐式交互

多模态遥感图像语义分割MCAM多模态融合注意力机制

结构图

MCAM 多模态交叉注意力模块结构图
MCAM 多模态交叉注意力模块结构图

二阶交叉注意力:对光学和 SAR 特征各用 1×1 卷积生成 V、Q、K,先分别算自注意力图,再用 Hadamard 积交叉得到联合注意力图,挖掘两模态间的隐式交互

论文信息

  • 论文:MCANet: A joint semantic segmentation framework of optical and SAR images for land use classification
  • 出处:International Journal of Applied Earth Observation and Geoinformation 2022

核心痛点

光学影像光谱纹理丰富但易受天气影响,SAR 全天候成像、几何信息强,两者高度互补;但现有融合方法多停留在相加、拼接层面,模态间缺乏深度交互,导致水体漏提、城乡混淆频发。

模块亮点

  • 二阶交叉注意力:对光学和 SAR 特征各用 1×1 卷积生成 V、Q、K,先分别算自注意力图,再用 Hadamard 积交叉得到联合注意力图,挖掘两模态间的隐式交互
  • 全局长依赖建模:把整幅图像当作观测范围,建立像素间中长程依赖,对道路、水体等狭长地物改善尤其明显
  • 整体模型涨点:论文未给 MCAM 单独消融,MCANet 在 WHU-OPT-SAR 上 OA 0.829、Kappa 0.737 均为最高,比 MBFNet 分别高 4.6%、7.9%;城市类精度 0.622 约为 MRSDC(0.131)的五倍

适用场景

领域为多模态遥感语义分割 / 土地利用分类;原文在 WHU-OPT-SAR 上验证了农田、城市、水体、道路等地物分类;可迁移到多源遥感融合、RGB-红外检测分割、多模态医学影像等任务。

插入位置建议

  • 双分支骨干提取完两种模态特征之后、送入解码器之前,替换简单的拼接或相加
  • 低层和高层特征上各插一个,原文低层与高层特征流都用了 MCAM
  • 参照 non-local 块的插法放进双流网络的特征交互处,输出经零初始化的 1×1 卷积,与原特征残差相加

适用任务

  • 多模态遥感语义分割
  • 土地利用分类

完整代码

mcam.py
import torch
from torch import nn
from torch.nn import functional as F

"""
图像特征融合模块
它通过采用跨模态注意力机制,帮助深度学习模型关注重要特征.
MCAM模块实现了SAR(合成孔径雷达)和Optical图像(通过光学传感器捕获的图像)特征之间的有效融合,利用跨模态注意力机制提取和利用两种模态之间的互补信息,这对于提高多模态数据处理任务的性能至关重要。
这种机制不仅增强了模型对特征的表达能力,还增强了模型在复杂环境下的适应性和鲁棒性。
"""

class MCAM(nn.Module):
    def __init__(self,
                 in_channels,
                 inter_channels=None,
                 dimension=2,
                 sub_sample=True,    # 子采样
                 bn_layer=True):     # 批量归一化
        super(MCAM, self).__init__()

        assert dimension in [1, 2, 3]

        self.dimension = dimension
        self.sub_sample = sub_sample

        self.in_channels = in_channels
        self.inter_channels = inter_channels

        if self.inter_channels is None:
            self.inter_channels = in_channels // 2
            if self.inter_channels == 0:
                self.inter_channels = 1

        if dimension == 3:
            conv_nd = nn.Conv3d
            max_pool_layer = nn.MaxPool3d(kernel_size=(1, 2, 2))
            bn = nn.BatchNorm3d
        elif dimension == 2:
            conv_nd = nn.Conv2d
            max_pool_layer = nn.MaxPool2d(kernel_size=(2, 2))
            bn = nn.BatchNorm2d
        else:
            conv_nd = nn.Conv1d
            max_pool_layer = nn.MaxPool1d(kernel_size=(2))
            bn = nn.BatchNorm1d

        self.g_sar = conv_nd(in_channels=self.in_channels,out_channels=self.inter_channels,kernel_size=1,stride=1,padding=0)

        self.g_opt = conv_nd(in_channels=self.in_channels,
                         out_channels=self.inter_channels,
                         kernel_size=1,
                         stride=1,
                         padding=0)

        if bn_layer:
            self.W = nn.Sequential(
                conv_nd(in_channels=self.inter_channels,
                        out_channels=self.in_channels,
                        kernel_size=1,
                        stride=1,
                        padding=0), bn(self.in_channels))
            nn.init.constant_(self.W[1].weight, 0)
            nn.init.constant_(self.W[1].bias, 0)
        else:
            self.W = conv_nd(in_channels=self.inter_channels,
                             out_channels=self.in_channels,
                             kernel_size=1,
                             stride=1,
                             padding=0)
            nn.init.constant_(self.W.weight, 0)
            nn.init.constant_(self.W.bias, 0)

        self.theta_sar = conv_nd(in_channels=self.in_channels,
                             out_channels=self.inter_channels,
                             kernel_size=1,
                             stride=1,
                             padding=0)
        self.theta_opt = conv_nd(in_channels=self.in_channels,
                             out_channels=self.inter_channels,
                             kernel_size=1,
                             stride=1,
                             padding=0)
        self.phi_sar = conv_nd(in_channels=self.in_channels,
                           out_channels=self.inter_channels,
                           kernel_size=1,
                           stride=1,
                           padding=0)
        self.phi_opt = conv_nd(in_channels=self.in_channels,
                               out_channels=self.inter_channels,
                               kernel_size=1,
                               stride=1,
                               padding=0)

        if sub_sample:
            self.g_sar = nn.Sequential(self.g_sar, max_pool_layer)
            self.g_opt = nn.Sequential(self.g_opt, max_pool_layer)
            self.phi_sar = nn.Sequential(self.phi_sar, max_pool_layer)
            self.phi_opt = nn.Sequential(self.phi_opt, max_pool_layer)

    def forward(self, sar, opt):

        batch_size = sar.size(0)

        g_x = self.g_sar(sar).view(batch_size, self.inter_channels, -1)  # [bs, c, w*h]
        g_x = g_x.permute(0, 2, 1)

        theta_x = self.theta_sar(sar).view(batch_size, self.inter_channels, -1)
        theta_x = theta_x.permute(0, 2, 1)

        phi_x = self.phi_sar(sar).view(batch_size, self.inter_channels, -1)

        f_x = torch.matmul(theta_x, phi_x)
        f_div_C_x = F.softmax(f_x, dim=-1)

        g_y = self.g_opt(opt).view(batch_size, self.inter_channels, -1)  # [bs, c, w*h]
        g_y = g_y.permute(0, 2, 1)

        theta_y = self.theta_opt(opt).view(batch_size, self.inter_channels, -1)
        theta_y = theta_y.permute(0, 2, 1)

        phi_y = self.phi_opt(opt).view(batch_size, self.inter_channels, -1)

        f_y = torch.matmul(theta_y, phi_y)
        f_div_C_y = F.softmax(f_y, dim=-1)
        y = torch.einsum('ijk,ijk->ijk', [f_div_C_x, f_div_C_y])
        y_x = torch.matmul(y, g_x)
        y_y = torch.matmul(y, g_y)
        y = y_x * y_y
        y = y.permute(0, 2, 1).contiguous()
        y = y.view(batch_size, self.inter_channels, *sar.size()[2:])
        y = self.W(y)
        return y

if __name__ == '__main__':
    model = MCAM(in_channels=256).cuda()
    sar = torch.randn(2, 256, 64, 64).cuda()
    opt = torch.randn(2, 256, 64, 64).cuda()
    print("input:", sar.shape, opt.shape)
    print("output:", model(sar, opt).shape)

环境与依赖

Python 3.9+

继续浏览

下一篇MDM 多尺度可分解混合模块先分解、再混合:用平均池化逐级下采样得到多个从细到粗的时间模式,再从最粗尺度向最细尺度做前馈残差混合,细粒度细节与粗粒度趋势互补增强,全程纯 MLP、无自注意力

相关资源