学术
模块更新于 2026-11-20· 浏览 0· 下载 0

RGA 关系感知全局注意力

关系显式入网:把每个位置与所有位置的双向亲和度堆叠成关系向量,与自身特征拼接,用两层 1×1 卷积 + Sigmoid 学出注意力

计算机视觉计算机视觉行人重识别RGA注意力机制特征提取

结构图

RGA 关系感知全局注意力结构图
RGA 关系感知全局注意力结构图

关系显式入网:把每个位置与所有位置的双向亲和度堆叠成关系向量,与自身特征拼接,用两层 1×1 卷积 + Sigmoid 学出注意力

论文信息

  • 论文:Relation-Aware Global Attention for Person Re-identification
  • 出处:CVPR 2020

核心痛点

行人重识别的主流注意力模块靠局部卷积学权重,感受野有限,难以利用全局结构信息;Non-local 算出两两亲和度却只当加权求和的权重,不挖掘关系里的聚类式结构模式,而行人图像恰恰富含这种全局结构线索。

模块亮点

  • 关系显式入网:把每个位置与所有位置的双向亲和度堆叠成关系向量,与自身特征拼接,用两层 1×1 卷积 + Sigmoid 学出注意力
  • 涨点幅度大:ResNet-50 基线上,CUHK03(L) 的 mAP 从 69.0 提到 77.4;RGA-C 比 SE 高 3.7% mAP,RGA-S 比 CBAM-S、NL 高约 2% mAP
  • 轻量可拆用:全程 1×1 卷积、各位置共享变换,RGA-S 参数量(28.3M)比 Non-local(30.6M)还少;空间版 RGA-S 与通道版 RGA-C 可单独用,也可串联成 RGA-SC(原文最优,CUHK03 上比 RGA-S 再高 2.7% mAP)

适用场景

领域为行人重识别 / 行人检索,原文在 CUHK03、Market1501、MSMT17 上验证,RGA-SC 均达到 SOTA;可迁移到图像检索、细粒度分类、行人属性识别等需要判别性特征的 CNN 任务。

插入位置建议

  • 加在骨干网络各残差块之后,原文在 ResNet-50 的 conv2_x 到 conv5_x 后全加 RGA,效果最好
  • 替换 SE、CBAM 等注意力模块,插在卷积层之后,按需选 RGA-S 或 RGA-C
  • 把空间和通道串联成 RGA-SC(先空间后通道),原文实验该顺序优于并联和反序

适用任务

  • 行人重识别
  • 行人检索
  • 原文在 CUHK03

完整代码

rga.py
import torch
from torch import nn
from torch.nn import functional as F

"""
对于行人重新识别(re-id)来说,注意力机制变得很有吸引力,因为它的目的是加强判别性特征并抑制不相关的特征,这很好地匹配了重新识别的关键,即判别性特征学习。
以前的方法通常使用局部卷积来学习注意力,而忽略了从全局结构模式中挖掘知识。直观地说,特征图中空间位置/节点之间的亲和力提供了类似聚类的信息,有助于推断语义,从而有助于推断注意力,特别是对于可行的人体姿势受到限制的人物图像。
在这项工作中,我们提出了一种有效的关系感知全局注意力(RGA)模块,它捕获全局结构信息以更好地进行注意力学习。
具体来说,对于每个特征位置,为了紧凑地掌握全局范围的结构信息和局部外观信息,我们建议堆叠关系,即其与所有特征位置的成对相关性/亲和力(例如,按光栅扫描顺序) ,和特征本身一起使用浅层卷积模型来学习注意力。
"""

class RGA_Module(nn.Module):
    def __init__(self, in_channel, in_spatial, use_spatial=True, use_channel=True,
                 cha_ratio=8, spa_ratio=8, down_ratio=8):
        super(RGA_Module, self).__init__()

        self.in_channel = in_channel
        self.in_spatial = in_spatial

        self.use_spatial = use_spatial
        self.use_channel = use_channel

        print('Use_Spatial_Att: {};\tUse_Channel_Att: {}.'.format(self.use_spatial, self.use_channel))

        # self.inter_channel = in_channel // cha_ratio
        # self.inter_spatial = in_spatial // spa_ratio
        self.inter_channel = max(in_channel // cha_ratio, 1)
        self.inter_spatial = max(in_spatial // spa_ratio, 1)

        # Embedding functions for original features
        if self.use_spatial:
            self.gx_spatial = nn.Sequential(
                nn.Conv2d(in_channels=self.in_channel, out_channels=self.inter_channel,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_channel),
                nn.ReLU()
            )
        if self.use_channel:
            self.gx_channel = nn.Sequential(
                nn.Conv2d(in_channels=self.in_spatial, out_channels=self.inter_spatial,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_spatial),
                nn.ReLU()
            )

        # Embedding functions for relation features
        if self.use_spatial:
            self.gg_spatial = nn.Sequential(
                nn.Conv2d(in_channels=self.in_spatial * 2, out_channels=self.inter_spatial,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_spatial),
                nn.ReLU()
            )
        if self.use_channel:
            self.gg_channel = nn.Sequential(
                nn.Conv2d(in_channels=self.in_channel * 2, out_channels=self.inter_channel,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_channel),
                nn.ReLU()
            )

        # Networks for learning attention weights
        if self.use_spatial:
            num_channel_s = 1 + self.inter_spatial
            self.W_spatial = nn.Sequential(
                nn.Conv2d(in_channels=num_channel_s, out_channels=num_channel_s // down_ratio,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(num_channel_s // down_ratio),
                nn.ReLU(),
                nn.Conv2d(in_channels=num_channel_s // down_ratio, out_channels=1,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(1)
            )
        if self.use_channel:
            num_channel_c = max(1 + self.inter_channel, 1)  # 确保至少为1
            self.W_channel = nn.Sequential(
                nn.Conv2d(in_channels=num_channel_c, out_channels=max(num_channel_c // down_ratio, 1),  # 防止为0
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(max(num_channel_c // down_ratio, 1)),  # 同样防止为0
                nn.ReLU(),
                nn.Conv2d(in_channels=max(num_channel_c // down_ratio, 1), out_channels=1,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(1)
            )

        # Embedding functions for modeling relations
        if self.use_spatial:
            self.theta_spatial = nn.Sequential(
                nn.Conv2d(in_channels=self.in_channel, out_channels=self.inter_channel,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_channel),
                nn.ReLU()
            )
            self.phi_spatial = nn.Sequential(
                nn.Conv2d(in_channels=self.in_channel, out_channels=self.inter_channel,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_channel),
                nn.ReLU()
            )
        if self.use_channel:
            self.theta_channel = nn.Sequential(
                nn.Conv2d(in_channels=self.in_spatial, out_channels=self.inter_spatial,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_spatial),
                nn.ReLU()
            )
            self.phi_channel = nn.Sequential(
                nn.Conv2d(in_channels=self.in_spatial, out_channels=self.inter_spatial,
                          kernel_size=1, stride=1, padding=0, bias=False),
                nn.BatchNorm2d(self.inter_spatial),
                nn.ReLU()
            )

    def forward(self, x):
        b, c, h, w = x.size()

        if self.use_spatial:
            # spatial attention
            theta_xs = self.theta_spatial(x)
            phi_xs = self.phi_spatial(x)
            theta_xs = theta_xs.view(b, self.inter_channel, -1)
            theta_xs = theta_xs.permute(0, 2, 1)
            phi_xs = phi_xs.view(b, self.inter_channel, -1)
            Gs = torch.matmul(theta_xs, phi_xs)
            Gs_in = Gs.permute(0, 2, 1).view(b, h * w, h, w)
            Gs_out = Gs.view(b, h * w, h, w)
            Gs_joint = torch.cat((Gs_in, Gs_out), 1)
            Gs_joint = self.gg_spatial(Gs_joint)

            g_xs = self.gx_spatial(x)
            g_xs = torch.mean(g_xs, dim=1, keepdim=True)
            ys = torch.cat((g_xs, Gs_joint), 1)

            W_ys = self.W_spatial(ys)
            if not self.use_channel:
                out = F.sigmoid(W_ys.expand_as(x)) * x
                return out
            else:
                x = F.sigmoid(W_ys.expand_as(x)) * x

        if self.use_channel:
            # channel attention
            xc = x.view(b, c, -1).permute(0, 2, 1).unsqueeze(-1)
            theta_xc = self.theta_channel(xc).squeeze(-1).permute(0, 2, 1)
            phi_xc = self.phi_channel(xc).squeeze(-1)
            Gc = torch.matmul(theta_xc, phi_xc)
            Gc_in = Gc.permute(0, 2, 1).unsqueeze(-1)
            Gc_out = Gc.unsqueeze(-1)
            Gc_joint = torch.cat((Gc_in, Gc_out), 1)
            Gc_joint = self.gg_channel(Gc_joint)

            g_xc = self.gx_channel(xc)
            g_xc = torch.mean(g_xc, dim=1, keepdim=True)
            yc = torch.cat((g_xc, Gc_joint), 1)

            W_yc = self.W_channel(yc).transpose(1, 2)
            out = F.sigmoid(W_yc) * x

            return out

if __name__ == '__main__':
    block = RGA_Module(in_channel=3, in_spatial=64*64)  # 注意这里in_spatial应当是H*W,如果你希望其代表总的空间维度
    input = torch.rand(32, 3, 64, 64)
    output = block(input)
    print("Input size:", input.size())
    print("Output size:", output.size())

环境与依赖

Python 3.9+

继续浏览

下一篇RSCM 遥感变化时空Mamba空间差异感知:SD-SSM 对展平 token 做前向 + 后向双向 SSM 扫描,全局感受野、线性复杂度;双时相差分特征与双向输出相乘,显式引导空间建模

相关资源