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

MSC 多尺度稀疏交叉注意力

多尺度线索抓全:对低层特征并行做 3、5、7 三种窗口的平均池化后相加,小窗口保纹理细节、大窗口抓整体结构

计算机视觉遥感图像场景分类MSC注意力机制多尺度融合

结构图

MSC 多尺度稀疏交叉注意力结构图
MSC 多尺度稀疏交叉注意力结构图

多尺度线索抓全:对低层特征并行做 3、5、7 三种窗口的平均池化后相加,小窗口保纹理细节、大窗口抓整体结构

论文信息

  • 论文:Multiscale Sparse Cross-Attention Network for Remote Sensing Scene Classification
  • 出处:IEEE TGRS 2025

核心痛点

遥感场景分类里,CNN 层数一深小目标就逐渐"消失",高层特征语义强、低层特征细节全;但现有融合多用加法、拼接简单组合,不区分各层贡献;即便上交叉注意力也对所有 token 一视同仁,全量融合连干扰一起聚合,拖累分类。

模块亮点

  • 多尺度线索抓全:对低层特征并行做 3、5、7 三种窗口的平均池化后相加,小窗口保纹理细节、大窗口抓整体结构
  • 稀疏去干扰:对注意力图每行做两次不同保留比例的 TopK 稀疏,无关位置 softmax 后归零,两路结果由可学习权重 α、β 自动配比(最优保留比例 1/2 与 1/6,NWPU 上为 1/8 与 1/6)
  • 整体涨点明显:作为 MSCN 核心融合模块,在 UCM、AID 所有训练比例下 OA 均最高:AID 20% 比例 OA 达 95.86%,比 TCNN 高 1.31%、比 EMTCAL 高 1.17%;UCM 50% / 80% 下比 EFCOMFF-Net 高 0.15% / 0.09%

适用场景

领域为遥感场景分类,原文在 UCM(21 类)、AID(30 类)、NWPU(45 类)上验证;也适合 FPN 跨层融合、U-Net 跳连增强等任务。

插入位置建议

  • 接在 CNN 骨干(如 ResNet50)之后:高层特征作 Q、各低层特征作 K/V 逐个融合再拼接
  • 替换 FPN 等跨层融合里的加法 / 拼接,做一次 MSC 交叉融合
  • 放在 U-Net 类网络的跳连处:深层特征作 Q、浅层跳连特征作 K/V

适用任务

  • 遥感场景分类
  • 原文在 UCM

完整代码

msc.py
import torch
import torch.nn as nn
from einops import rearrange
from math import sqrt


class MSC(nn.Module):
    def __init__(self, dim, num_heads=8, topk=True, kernel=[3, 5, 7], s=[1, 1, 1], pad=[1, 2, 3],
                 qkv_bias=False, qk_scale=None, attn_drop_ratio=0., proj_drop_ratio=0., k1=2, k2=3):
        super(MSC, self).__init__()
        self.num_heads = num_heads
        head_dim = dim // num_heads
        self.scale = qk_scale or head_dim ** -0.5

        self.q = nn.Linear(dim, dim, bias=qkv_bias)
        self.kv = nn.Linear(dim, dim * 2, bias=qkv_bias)
        self.attn_drop = nn.Dropout(attn_drop_ratio)
        self.proj = nn.Linear(dim, dim)
        self.proj_drop = nn.Dropout(proj_drop_ratio)
        self.k1 = k1
        self.k2 = k2

        self.attn1 = torch.nn.Parameter(torch.tensor([0.5]), requires_grad=True)
        self.attn2 = torch.nn.Parameter(torch.tensor([0.5]), requires_grad=True)
        # self.attn3 = torch.nn.Parameter(torch.tensor([0.3]), requires_grad=True)

        self.avgpool1 = nn.AvgPool2d(kernel_size=kernel[0], stride=s[0], padding=pad[0])
        self.avgpool2 = nn.AvgPool2d(kernel_size=kernel[1], stride=s[1], padding=pad[1])
        self.avgpool3 = nn.AvgPool2d(kernel_size=kernel[2], stride=s[2], padding=pad[2])

        self.layer_norm = nn.LayerNorm(dim)

        self.topk = topk  # False True

    def forward(self, x, y):
        # x0 = x
        y1 = self.avgpool1(y)
        y2 = self.avgpool2(y)
        y3 = self.avgpool3(y)
        # y = torch.cat([y1.flatten(-2,-1),y2.flatten(-2,-1),y3.flatten(-2,-1)],dim = -1)
        y = y1 + y2 + y3
        y = y.flatten(-2, -1)

        y = y.transpose(1, 2)
        y = self.layer_norm(y)
        x = rearrange(x, 'b c h w -> b (h w) c')
        # y = rearrange(y,'b c h w -> b (h w) c')
        B, N1, C = y.shape
        # print(y.shape)
        kv = self.kv(y).reshape(B, N1, 2, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
        k, v = kv[0], kv[1]
        # qkv = self.qkv(x).reshape(B, N, 3, self.num_heads, C // self.num_heads).permute(2, 0, 3, 1, 4)
        B, N, C = x.shape
        q = self.q(x).reshape(B, N, self.num_heads, C // self.num_heads).permute(0, 2, 1, 3)

        attn = (q @ k.transpose(-2, -1)) * self.scale

        # print(self.k1,self.k2)    
        mask1 = torch.zeros(B, self.num_heads, N, N1, device=x.device, requires_grad=False)
        index = torch.topk(attn, k=int(N1 / self.k1), dim=-1, largest=True)[1]
        # print(index[0,:,48])
        mask1.scatter_(-1, index, 1.)
        attn1 = torch.where(mask1 > 0, attn, torch.full_like(attn, float('-inf')))
        attn1 = attn1.softmax(dim=-1)
        attn1 = self.attn_drop(attn1)
        out1 = (attn1 @ v)

        mask2 = torch.zeros(B, self.num_heads, N, N1, device=x.device, requires_grad=False)
        index = torch.topk(attn, k=int(N1 / self.k2), dim=-1, largest=True)[1]
        # print(index[0,:,48])
        mask2.scatter_(-1, index, 1.)
        attn2 = torch.where(mask2 > 0, attn, torch.full_like(attn, float('-inf')))
        attn2 = attn2.softmax(dim=-1)
        attn2 = self.attn_drop(attn2)
        out2 = (attn2 @ v)

        out = out1 * self.attn1 + out2 * self.attn2  # + out3 * self.attn3
        # out = out1 * self.attn1 + out2 * self.attn2

        x = out.transpose(1, 2).reshape(B, N, C)

        x = self.proj(x)
        x = self.proj_drop(x)
        hw = int(sqrt(N))
        x = rearrange(x, 'b (h w) c -> b c h w', h=hw, w=hw)
        # x = x + x0
        return x


if __name__ == '__main__':
    block = MSC(dim=64).to('cuda')

    x = torch.rand(2, 64, 32, 32).to('cuda')

    y = torch.rand(2, 64, 32, 32).to('cuda')

    output = block(x, y)

    # Print the input and output shapes
    print(f"Input x size: {x.size()}")
    print(f"Input y size: {y.size()}")
    print(f"Output size: {output.size()}")

环境与依赖

Python 3.9+

继续浏览

下一篇MSF 多频谱融合模块中频引导提纯:拿域稳定的中频特征当 Query、高/低频特征当 Key/Value 做交叉注意力,从域特定频段里捞回域不变信息,保留全部频段而不是直接丢弃

相关资源