学术
模块更新于 2026-11-21· 浏览 2· 下载 2

MFMSA 多频多尺度注意力

原理创新:双分支注意力串行设计,先通过2D DCT提取多频统计信息做通道校准,抑制噪声通道,再做多尺度空间注意力,聚合不同大小病灶的边界特征,兼顾结构信息和细粒度细节

医学影像医学图像分割注意力机制CVPR计算机视觉域泛化UNet频率域学习多尺度特征

结构图

MFMSA 多频多尺度注意力结构图
MFMSA 多频多尺度注意力结构图

原理创新:双分支注意力串行设计,先通过2D DCT提取多频统计信息做通道校准,抑制噪声通道,再做多尺度空间注意力,聚合不同大小病灶的边界特征,兼顾结构信息和细粒度细节

核心痛点

现有医学图像分割模型要么只做多尺度适配不同病灶大小,要么只做多频率抗噪,忽略两者互补性,跨模态/未知域泛化性极差;多任务深度监督时低分辨率特征大倍率上采样会丢失边界细节,导致分割精度上不去。

模块亮点

  • 原理创新:双分支注意力串行设计,先通过2D DCT提取多频统计信息做通道校准,抑制噪声通道,再做多尺度空间注意力,聚合不同大小病灶的边界特征,兼顾结构信息和细粒度细节
  • 性能拉满:搭配E-SDM模块的MADGNet在6种模态、15个公开数据集上达到SOTA,平均DSC比次优模型高1.1%,跨未知域场景下最高提升7.2%,边界预测更精准
  • 效率友好:仅31M参数量,单张352×352图像推理耗时24ms,计算开销低,易落地

适用场景

CV领域的所有2D医学图像分割任务(超声、CT、皮肤镜、结肠镜、眼底等多模态都适配),兼容UNet、UNet++、ResNet等编码器-解码器架构,能解决跨域泛化差、噪声干扰、小病灶漏检、边界模糊等常见问题。

插入位置建议

  • 推荐放在解码器每一层的特征拼接操作之后(即编码器浅层特征和上一层解码器上采样特征拼接后),替换原有的普通卷积层做特征精炼,也可插入编码器各层输出端增强基础特征表征。

适用任务

  • CV领域的所有2D医学图像分割任务(超声
  • CT
  • 皮肤镜

完整代码

mfmsa.py
import math
import torch
import torch.nn as nn
import torch.nn.functional as F


"""《Modality-agnostic Domain Generalizable Medical Image Segmentation by Multi-Frequency in Multi-Scale Attention》CVPR2024
深度神经网络的泛化能力在医学图像分割中起着关键作用。然而,基于深度学习的医学图像分析往往忽视了频率方差的重要性,而频率方差是实现模态无关且领域可泛化的模型的关键要素。
此外,各种模型都未能考虑到深度监督下多任务学习可能产生的潜在信息丢失,这一因素可能会损害模型的表示能力。为了应对这些挑战,我们提出了一种用于医学图像分割的模态无关领域可泛化网络 (MADGNet),它包含两个关键组件:
多频多尺度注意 (MFMSA) 块和集成子解码模块 (E-SDM)。MFMSA 块通过结合多频率和多尺度特征来改进空间特征提取的过程,特别是在捕获边界特征方面,从而为组织轮廓和解剖结构提供信息线索。
此外,我们提出了 E-SDM 来减轻深度监督下多任务学习中的信息丢失,尤其是在从低分辨率进行大量上采样期间。
我们评估了 MADGNet 在六种模式和十五个数据集中的分割性能。
通过大量实验,我们证明 MADGNet 在各种模式下始终优于最先进的模型,展现出卓越的分割性能。这证实了 MADGNet 是一种强大的医学图像分割解决方案,在各种成像场景中都表现出色。
"""

def get_freq_indices(method):
    assert method in ['top1','top2','top4','top8','top16','top32',
                      'bot1','bot2','bot4','bot8','bot16','bot32',
                      'low1','low2','low4','low8','low16','low32']
    num_freq = int(method[3:])
    if 'top' in method:
        all_top_indices_x = [0,0,6,0,0,1,1,4,5,1,3,0,0,0,3,2,4,6,3,5,5,2,6,5,5,3,3,4,2,2,6,1]
        all_top_indices_y = [0,1,0,5,2,0,2,0,0,6,0,4,6,3,5,2,6,3,3,3,5,1,1,2,4,2,1,1,3,0,5,3]
        mapper_x = all_top_indices_x[:num_freq]
        mapper_y = all_top_indices_y[:num_freq]
    elif 'low' in method:
        all_low_indices_x = [0,0,1,1,0,2,2,1,2,0,3,4,0,1,3,0,1,2,3,4,5,0,1,2,3,4,5,6,1,2,3,4]
        all_low_indices_y = [0,1,0,1,2,0,1,2,2,3,0,0,4,3,1,5,4,3,2,1,0,6,5,4,3,2,1,0,6,5,4,3]
        mapper_x = all_low_indices_x[:num_freq]
        mapper_y = all_low_indices_y[:num_freq]
    elif 'bot' in method:
        all_bot_indices_x = [6,1,3,3,2,4,1,2,4,4,5,1,4,6,2,5,6,1,6,2,2,4,3,3,5,5,6,2,5,5,3,6]
        all_bot_indices_y = [6,4,4,6,6,3,1,4,4,5,6,5,2,2,5,1,4,3,5,0,3,1,1,2,4,2,1,1,5,3,3,3]
        mapper_x = all_bot_indices_x[:num_freq]
        mapper_y = all_bot_indices_y[:num_freq]
    else:
        raise NotImplementedError
    return mapper_x, mapper_y

class MultiFrequencyChannelAttention(nn.Module):
    def __init__(self,
                 in_channels,
                 dct_h, dct_w,
                 frequency_branches=16,
                 frequency_selection='top',
                 reduction=16):
        super(MultiFrequencyChannelAttention, self).__init__()

        assert frequency_branches in [1, 2, 4, 8, 16, 32]
        frequency_selection = frequency_selection + str(frequency_branches)

        self.num_freq = frequency_branches
        self.dct_h = dct_h
        self.dct_w = dct_w

        mapper_x, mapper_y = get_freq_indices(frequency_selection)
        self.num_split = len(mapper_x)
        mapper_x = [temp_x * (dct_h // 7) for temp_x in mapper_x]
        mapper_y = [temp_y * (dct_w // 7) for temp_y in mapper_y]

        assert len(mapper_x) == len(mapper_y)

        # fixed DCT init
        for freq_idx in range(frequency_branches):
            self.register_buffer('dct_weight_{}'.format(freq_idx), self.get_dct_filter(dct_h, dct_w, mapper_x[freq_idx], mapper_y[freq_idx], in_channels))

        self.fc = nn.Sequential(
            nn.Conv2d(in_channels, in_channels // reduction, kernel_size=1, stride=1, padding=0, bias=False),
            nn.ReLU(inplace=True),
            nn.Conv2d(in_channels // reduction, in_channels, kernel_size=1, stride=1, padding=0, bias=False))

        self.average_channel_pooling = nn.AdaptiveAvgPool2d(1)
        self.max_channel_pooling = nn.AdaptiveMaxPool2d(1)

    def forward(self, x):
        batch_size, C, H, W = x.shape

        x_pooled = x

        if H != self.dct_h or W != self.dct_w:
            x_pooled = torch.nn.functional.adaptive_avg_pool2d(x, (self.dct_h, self.dct_w))

        multi_spectral_feature_avg, multi_spectral_feature_max, multi_spectral_feature_min = 0, 0, 0
        for name, params in self.state_dict().items():
            if 'dct_weight' in name:
                x_pooled_spectral = x_pooled * params
                multi_spectral_feature_avg += self.average_channel_pooling(x_pooled_spectral)
                multi_spectral_feature_max += self.max_channel_pooling(x_pooled_spectral)
                multi_spectral_feature_min += -self.max_channel_pooling(-x_pooled_spectral)
        multi_spectral_feature_avg = multi_spectral_feature_avg / self.num_freq
        multi_spectral_feature_max = multi_spectral_feature_max / self.num_freq
        multi_spectral_feature_min = multi_spectral_feature_min / self.num_freq


        multi_spectral_avg_map = self.fc(multi_spectral_feature_avg).view(batch_size, C, 1, 1)
        multi_spectral_max_map = self.fc(multi_spectral_feature_max).view(batch_size, C, 1, 1)
        multi_spectral_min_map = self.fc(multi_spectral_feature_min).view(batch_size, C, 1, 1)

        multi_spectral_attention_map = F.sigmoid(multi_spectral_avg_map + multi_spectral_max_map + multi_spectral_min_map)

        return x * multi_spectral_attention_map.expand_as(x)

    def get_dct_filter(self, tile_size_x, tile_size_y, mapper_x, mapper_y, in_channels):
        dct_filter = torch.zeros(in_channels, tile_size_x, tile_size_y)

        for t_x in range(tile_size_x):
            for t_y in range(tile_size_y):
                dct_filter[:, t_x, t_y] = self.build_filter(t_x, mapper_x, tile_size_x) * self.build_filter(t_y, mapper_y, tile_size_y)

        return dct_filter

    def build_filter(self, pos, freq, POS):
        result = math.cos(math.pi * freq * (pos + 0.5) / POS) / math.sqrt(POS)
        if freq == 0:
            return result
        else:
            return result * math.sqrt(2)

class MFMSAttentionBlock(nn.Module):
    def __init__(self,
                 in_channels,
                 scale_branches=2,
                 frequency_branches=16,
                 frequency_selection='top',
                 block_repetition=1,
                 min_channel=64,
                 min_resolution=8,
                 groups=32):
        super(MFMSAttentionBlock, self).__init__()

        self.scale_branches = scale_branches
        self.frequency_branches = frequency_branches
        self.block_repetition = block_repetition
        self.min_channel = min_channel
        self.min_resolution = min_resolution

        self.multi_scale_branches = nn.ModuleList([])
        for scale_idx in range(scale_branches):
            inter_channel = in_channels // 2**scale_idx
            if inter_channel < self.min_channel: inter_channel = self.min_channel

            self.multi_scale_branches.append(nn.Sequential(
                nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1 + scale_idx, dilation=1 + scale_idx, groups=groups, bias=False),
                nn.BatchNorm2d(in_channels), nn.ReLU(inplace=True),
                nn.Conv2d(in_channels, inter_channel, kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(inter_channel), nn.ReLU(inplace=True)
            ))

        c2wh = dict([(32, 112), (64, 56), (128, 28), (256, 14), (512, 7)])
        self.multi_frequency_branches = nn.ModuleList([])
        self.multi_frequency_branches_conv1 = nn.ModuleList([])
        self.multi_frequency_branches_conv2 = nn.ModuleList([])
        self.alpha_list = nn.ParameterList([nn.Parameter(torch.ones(1)) for _ in range(scale_branches)])
        self.beta_list = nn.ParameterList([nn.Parameter(torch.ones(1)) for _ in range(scale_branches)])

        for scale_idx in range(scale_branches):
            inter_channel = in_channels // 2**scale_idx
            if inter_channel < self.min_channel: inter_channel = self.min_channel

            if frequency_branches > 0:
                self.multi_frequency_branches.append(
                    nn.Sequential(
                        MultiFrequencyChannelAttention(inter_channel, c2wh[in_channels], c2wh[in_channels], frequency_branches, frequency_selection)))
            self.multi_frequency_branches_conv1.append(
                nn.Sequential(
                    nn.Conv2d(inter_channel, 1, kernel_size=1, stride=1, padding=0, bias=False),
                    nn.Sigmoid()))
            self.multi_frequency_branches_conv2.append(
                nn.Sequential(
                    nn.Conv2d(inter_channel, in_channels, kernel_size=3, stride=1, padding=1, bias=False),
                    nn.BatchNorm2d(in_channels), nn.ReLU(inplace=True)))

    def forward(self, x):
        feature_aggregation = 0
        for scale_idx in range(self.scale_branches):
            feature = F.avg_pool2d(x, kernel_size=2 ** scale_idx, stride=2 ** scale_idx, padding=0) if int(x.shape[2] // 2 ** scale_idx) >= self.min_resolution else x
            feature = self.multi_scale_branches[scale_idx](feature)
            if self.frequency_branches > 0:
                feature = self.multi_frequency_branches[scale_idx](feature)
            spatial_attention_map = self.multi_frequency_branches_conv1[scale_idx](feature)
            feature = self.multi_frequency_branches_conv2[scale_idx](feature * (1 - spatial_attention_map) * self.alpha_list[scale_idx] + feature * spatial_attention_map * self.beta_list[scale_idx])
            feature_aggregation += F.interpolate(feature, size=None, scale_factor=2**scale_idx, mode='bilinear', align_corners=None) if (x.shape[2] != feature.shape[2]) or (x.shape[3] != feature.shape[3]) else feature
        feature_aggregation /= self.scale_branches
        feature_aggregation += x

        return feature_aggregation


if __name__ == '__main__':
    input_tensor = torch.rand(1, 64, 32, 32)

    block = MFMSAttentionBlock(in_channels=64, scale_branches=2, frequency_branches=16, frequency_selection='top', block_repetition=1)

    output = block(input_tensor)

    print("Input size: ", input_tensor.size())
    print("Output size: ", output.size())

环境与依赖

Python 3.9+

继续浏览

下一篇MINT 随机特征注意力多中心高斯混合覆盖分离的贡献区域,论文通过完整混合密度修正采样,并用有限预算输出风险校准权重。所附代码提供冻结特征库和指数映射接口,完整校准需另接。

相关资源