【深度学习】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:通道注意力模块_第1张图片

总结

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

你可能感兴趣的:(各种深度学习模块,深度学习,人工智能)