【深度学习】CAB:通道注意力模块

@[toc]CAB:通道注意力模块

CAB:通道注意力模块

CAB(Channel Attention Block) 是一种通道注意力模块,通常用于计算机视觉任务中,特别是在图像恢复、超分辨率、去噪等任务中。它的核心思想是通过学习通道之间的依赖关系,自适应地调整每个通道的特征响应,从而增强模型对重要特征的提取能力。

CAB 的核心思想

通道注意力机制:

通过对每个通道的特征进行全局池化,获取全局信息。

使用全连接层(或 1x1 卷积)学习通道之间的依赖关系。

通过 Sigmoid 函数生成通道注意力权重,对原始特征进行加权。

残差连接:

为了保留原始特征信息,通常会在通道注意力模块的输出上添加残差连接。

CAB 的结构

全局平均池化(Global Average Pooling, GAP):

对输入特征图的每个通道进行全局平均池化,得到一个通道描述向量。

全连接层(或 1x1 卷积):

使用全连接层(或 1x1 卷积)学习通道之间的关系。

通常包含一个降维层和一个升维层,以减少计算量。

Sigmoid 激活函数:

对学习到的通道关系进行归一化,生成通道注意力权重。

特征加权:

将生成的通道注意力权重与输入特征图相乘,得到加权后的特征。

残差连接:

将加权后的特征与输入特征相加,保留原始信息。

代码实现

1.使用线性层:

import torch
import torch.nn as nn
import torch.nn.functional as F

class ChannelAttentionBlock(nn.Module):
    def __init__(self, channel, reduction_ratio=16):
        """
        初始化 CAB 模块
        :param channel: 输入特征图的通道数
        :param reduction_ratio: 降维比例,默认为 16
        """
        super(ChannelAttentionBlock, self).__init__()
        self.channel = channel
        self.reduction_ratio = reduction_ratio

        # 全局平均池化
        self.gap = nn.AdaptiveAvgPool2d(1)

        # 全连接层(或 1x1 卷积)
        self.fc = nn.Sequential(
            nn.Linear(channel, channel // reduction_ratio, bias=False),  # 降维
            nn.ReLU(inplace=True),
            nn.Linear(channel // reduction_ratio, channel, bias=False),  # 升维
            nn.Sigmoid()  # 归一化
        )

    def forward(self, x):
        """
        前向传播
        :param x: 输入特征图,形状为 [batch_size, channel, height, width]
        :return: 加权后的特征图
        """
        b, c, h, w = x.size()

        # 全局平均池化
        y = self.gap(x).view(b, c)  # [batch_size, channel]

        # 全连接层
        y = self.fc(y).view(b, c, 1, 1)  # [batch_size, channel, 1, 1]

        # 特征加权
        out = x * y.expand_as(x)  # [batch_size, channel, height, width]

        # 残差连接
        return out + x

2.使用卷积层

class CALayer(nn.Module):
    def __init__(self, channel, reduction=16, bias=False):
        super(CALayer, self).__init__()
        # global average pooling: feature --> point
        self.avg_pool = nn.AdaptiveAvgPool2d(1)
        # feature channel downscale and upscale --> channel weight
        self.conv_du = nn.Sequential(
                nn.Conv2d(channel, channel // reduction, 1, padding=0, bias=bias),
                nn.ReLU(inplace=True),
                nn.Conv2d(channel // reduction, channel, 1, padding=0, bias=bias),
                nn.Sigmoid()
        )
    def forward(self, x):
        y = self.avg_pool(x)
        y = self.conv_du(y)
        return x * y

卷积与线性层对比

在这里插入图片描述

总结

CAB 是一种简单但有效的通道注意力模块,通过学习通道之间的依赖关系,能够自适应地增强重要特征并抑制不重要特征。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

shanks66

你的鼓励是我创作的最大动力!!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值