每日Attention学习5——Multi-Scale Channel Attention Module

模块出处

[link] [code] [WACV 21] Attentional Feature Fusion


模块名称

Multi-Scale Channel Attention Module (MS-CAM)


模块作用

通道注意力


模块结构

在这里插入图片描述


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


class MS_CAM(nn.Module):

    def __init__(self, channels=64, r=4):
        super(MS_CAM, 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.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):
        xl = self.local_att(x)
        xg = self.global_att(x)
        xlg = xl + xg
        wei = self.sigmoid(xlg)
        return x * wei

    
if __name__ == '__main__':
    x = torch.randn([3, 256, 16, 16])
    ms_sam = MS_CAM(channels=256)
    out = ms_sam(x)
    print(out.shape)  # 3, 256, 16, 16

原文表述

MS-CAM的核心思想在于,通过改变空间池化的大小,可以在多个尺度上实现通道注意力。为了尽可能保持轻量级,我们只是在注意力模块内将局部上下文添加到全局上下文中。我们选择点卷积(PointWise Conv)作为局部通道上下文融合器,它只利用每个空间位置的点级通道交互。

  • 6
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值