每日Attention学习25——Multi-Scale Attention Fusion

模块出处

[TCSVT 24] [link] [code] DSNet: A Novel Way to Use Atrous Convolutions in Semantic Segmentation


模块名称

Multi-Scale Attention Fusion (MSAF)


模块作用

双级特征融合


模块结构

在这里插入图片描述


模块思想

MSAF的主要思想是让网络根据损失学习特征权重,允许模型选择性地融合来自不同尺度的信息。


模块代码
import torch
import torch.nn as nn
import torch.nn.functional as F


class MSAF(nn.Module):
    def __init__(self, channels=64, r=4):
        super(MSAF, self).__init__()
        inter_channels = int(channels // r)

        self.local_att = nn.Sequential(
            nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(inter_channels),
            nn.ReLU(inplace=True),
            nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(channels),
        )

        self.context1 = nn.Sequential(
            nn.AdaptiveAvgPool2d((4, 4)),
            nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(inter_channels),
            nn.ReLU(inplace=True),
            nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(channels)
        )

        self.context2 = nn.Sequential(
            nn.AdaptiveAvgPool2d((8, 8)),
            nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(inter_channels),
            nn.ReLU(inplace=True),
            nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(channels)
        )

        self.global_att = nn.Sequential(
            nn.AdaptiveAvgPool2d(1),
            nn.Conv2d(channels, inter_channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(inter_channels),
            nn.ReLU(inplace=True),
            nn.Conv2d(inter_channels, channels, kernel_size=1, stride=1, padding=0),
            nn.BatchNorm2d(channels),
        )

        self.sigmoid = nn.Sigmoid()

    def forward(self, x, residual):
        h, w = x.shape[2], x.shape[3]
        xa = x + residual
        xl = self.local_att(xa)
        c1 = self.context1(xa)
        c2 = self.context2(xa)
        xg = self.global_att(xa)
        c1 = F.interpolate(c1, size=[h, w], mode='nearest')
        c2 = F.interpolate(c2, size=[h, w], mode='nearest')
        xlg = xl + xg + c1 + c2 
        wei = self.sigmoid(xlg)
        xo = 2 * x * wei + 2 * residual * (1 - wei)
        return xo
    

if __name__ == '__main__':
    msaf = MSAF()
    x1 = torch.randn([2, 64, 16, 16])
    x2 = torch.randn([2, 64, 16, 16])
    out = msaf(x1, x2)  
    print(out.shape)  # 2, 64, 16, 16

### 基于多尺度特征融合的桥梁表面图像缺陷分割方法 为了实现高精度的桥梁表面图像缺陷分割,可以采用基于卷积神经网络(CNN)的方法来提取多尺度特征并进行融合。这种方法的核心在于通过不同层次的特征图捕获局部细节和全局上下文信息,从而提高模型对复杂背景和微小缺陷区域的识别能力。 #### 多尺度特征融合的作用 多尺度特征融合能够有效解决单一尺度特征无法全面描述目标对象的问题。具体来说,在深层网络中,浅层特征通常包含更多的纹理和边缘信息,而深层特征则更注重语义理解[^1]。因此,将两者结合起来可以在保持空间分辨率的同时增强对物体边界的刻画能力。 #### 实现过程中的关键技术点 以下是几个重要的技术环节: - **编码器解码器结构** 使用U-Net架构作为基础框架,其中编码部分负责逐级下采样获取抽象化特征;解码阶段则是逐步上采样的过程,恢复原始输入尺寸,并在此过程中引入跳跃连接以保留更多细粒度的空间位置关系[^2]。 - **注意力机制的应用** 引入通道注意模块(Channel Attention Module,CAM)或者空间注意模块(Spatial Attention Module,SAM),使得网络更加关注那些对于当前任务至关重要的区域或通道响应强度较大的地方[^3]。 - **损失函数设计** 结合交叉熵损失(Cross Entropy Loss)与Dice系数等指标共同指导训练方向,确保像素级别分类准确性以及连通域完整性得到兼顾。 ```python import torch.nn as nn class MultiScaleFusionModule(nn.Module): def __init__(self, in_channels, out_channels): super(MultiScaleFusionModule, self).__init__() # 定义不同的分支操作 self.branch_1x1 = nn.Conv2d(in_channels, out_channels//4, kernel_size=1) self.branch_3x3_reduce = nn.Conv2d(in_channels, out_channels//4, kernel_size=1) self.branch_3x3 = nn.Conv2d(out_channels//4, out_channels//4, kernel_size=3, padding=1) def forward(self, x): branch_1x1_output = self.branch_1x1(x) branch_3x3_output = self.branch_3x3(self.branch_3x3_reduce(x)) fused_feature_map = torch.cat([branch_1x1
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值