【Pytorch】CNN中的Attention

目录

  • 更大层面上的Attention
    • 在attention中,怎么分区channel-wise还是spatial-wise
    • 举一个Spatial-Channel Attention的例子
  • 使用广泛的Dot-product Attention
    • attention机制中的query,key,value的概念解释
    • Attention的一个例子

更大层面上的Attention

在attention中,怎么分区channel-wise还是spatial-wise

为了更好地理解 “wise”,可以将其看作是一种特定维度或方面的强调。例如:
“time-wise” 表示与时间相关的事物。
在这种用法中,“wise” 帮助明确我们正在讨论的是哪一个特定的维度或方面。因此,当我们谈论 “channel-wise” attention 时,我们的焦点是在于如何以通道为基础进行操作;当我们谈论 “spatial-wise” attention 时,我们的焦点是在于空间位置或区域。

在神经网络中,特别是在处理图像或视频数据时,Attention 机制可以以不同的方式集中于输入数据的不同部分。在这些方法中,“channel-wise” 和 “spatial-wise” attention 是两种常见的方式。下面解释这两种方式:

  1. Channel-wise Attention
  • 含义:在 “channel-wise” attention 中,“wise” 指的是关注操作是针对不同的通道进行的。在图像处理中,通道通常指的是颜色通道(如RGB中的红、绿、蓝),或者在深度学习模型中,通道可以是不同的特征表示。
  • 例子:如果一个图像处理模型正在处理一个具有多个通道的特征图,“channel-wise” attention 将决定哪些通道更重要,可能会增强一些通道的特征而减弱其他通道的特征。

应用:这种类型的attention在处理那些不同通道具有不同语义信息的数据时特别有用。例如,在卷积神经网络中,不同的卷积层可能会学习到代表不同高级特征的通道(如边缘、纹理等)。

  1. Spatial-wise Attention
  • 含义:在 “spatial-wise” attention 中,“wise” 指的是关注操作是针对图像或特征图的不同空间区域进行的。这种方法关注于图像中的不同位置,而不是整个图像作为一个整体。
  • 例子:在对象检测任务中,“spatial-wise” attention 可能会集中于图像中包含重要对象的区域,而忽略其他不相关的区域。

应用:这种类型的attention在图像识别或对象检测等任务中特别有用,因为它可以帮助模型集中于图像中最重要的部分,例如,一个对象可能只占据图像的一小部分。

  1. 如何选择

选择 channel-wise 还是 spatial-wise attention 取决于具体任务和数据的特点。在某些情况下,甚至可以将两者结合起来,以便同时利用通道和空间信息。例如,一些高级神经网络架构在其attention机制中同时考虑了通道和空间维度,从而提高了模型对图像的理解能力。

举一个Spatial-Channel Attention的例子

例子来自于《Cross-Modal Relation-Aware Networks for Audio-Visual Event Localization》中的AGSCA模块,图片和英文部分均为原文

Given audio features a t ∈ R d a \boldsymbol{a}_t \in \mathbb{R}^{d_a} atRda and visual features v t ∈ R d v × ( H ∗ W ) v_t \in \mathbb{R}^{d_v \times(H * W)} vtRdv×(HW) where H H H and W W W are the height and width of feature maps respectively, AGSCA first generates channel-wise attention maps M t c ∈ R d v × 1 \boldsymbol{M}_t^c \in \mathbb{R}^{d_v \times 1} MtcRdv×1 to adaptively emphasize informative features. It then produces spatial attention maps M t s ∈ R 1 × ( H ∗ W ) \boldsymbol{M}_t^s \in \mathbb{R}^{1 \times(H * W)} MtsR1×(HW) for the channelattentive features to highlight sounding regions, yielding channelspatial attentive visual features v t c s v_t^{c s} vtcs, as illustrated in Figure 3. The attention process can be summarized as, v t c s = M t s ⊗ ( v t c ) T , v t c = M t c ⊙ v t , \begin{aligned} & v_t^{c s}=\boldsymbol{M}_t^s \otimes\left(v_t^c\right)^T, \\ & v_t^c=\boldsymbol{M}_t^c \odot v_t, \end{aligned} vtcs=Mts(vtc)T,vtc=Mtcvt,where ⊗ \otimes denotes matrix multiplication, and ⊙ \odot means element-wise multiplication. We next separately introduce the channel-wise attention that generates attention maps M t c \boldsymbol{M}_t^c Mtc and spatial attention that produces attention maps M t s \boldsymbol{M}_t^s Mts.

在这里插入图片描述

  1. 我们先来看如何得到channel-wise attention maps M t c \boldsymbol{M}_t^c Mtc:

Channel-Wise Attention. We explicitly model the dependencies between channels of features with the guidance of audio signals. Specifically, we first project audio and visual features to the same dimension d v d_v dv using fully-connected layers with non-linearity, resulting in audio guidance maps a t m ∈ R d v \boldsymbol{a}_t^m \in \mathbb{R}^{d_v} atmRdv and projected visual features with dimensions of d v × ( H ∗ W ) d_v \times(H * W) dv×(HW). We then leverage the guidance information of a t m \boldsymbol{a}_t^m atm by fusing visual features with a t m \boldsymbol{a}_t^m atm via element-wise multiplication. Following [17], we spatially squeeze the fused features by global average pooling. Last, we forward the fused feature vector through two fully-connected layers with nonlinearity to model the relationships between channels, yielding channel attention maps M t c \boldsymbol{M}_t^c Mtc. We give the details as follows:
M t c = σ ( W 1 U 1 c ( δ a ( U a c a t ⊙ U v c v t ) ) ) , \boldsymbol{M}_{\boldsymbol{t}}^{\boldsymbol{c}}=\sigma\left(\boldsymbol{W}_1 \boldsymbol{U}_1^c\left(\delta_a\left(\boldsymbol{U}_a^c \boldsymbol{a}_t \odot \boldsymbol{U}_v^c \boldsymbol{v}_t\right)\right)\right), Mtc=σ(W1U1c(δa(UacatUvcvt))),
where U a c ∈ R d v × d a , U v c ∈ R d v × d v \boldsymbol{U}_a^c \in \mathbb{R}^{d_v \times d_a}, \boldsymbol{U}_v^c \in \mathbb{R}^{d_v \times d_v} UacRdv×da,UvcRdv×dv, and U 1 c ∈ R d × d v \boldsymbol{U}_1^c \in \mathbb{R}^{d \times d_v} U1cRd×dv are fullyconnected layers with ReLU as an activation function, W 1 ∈ R d v × d \boldsymbol{W}_1 \in \mathbb{R}^{d_v \times d} W1Rdv×d are learnable parameters with d = 256 d=256 d=256 as a hidden dimension, δ a \delta_a δa indicates global average pooling, and σ \sigma σ denotes the sigmoid function. We add a residual connection by adding one to each element of M t c \boldsymbol{M}_t^c Mtc to obtain the final channel attention maps.

代码如下:

        # ============================== Channel Attention ====================================
        audio_query_1 = self.relu(self.affine_audio_1(audio_feature)).unsqueeze(-2)
        video_query_1 = self.relu(self.affine_video_1(visual_feature)).reshape(batch*t_size, h*w, -1)
        audio_video_query_raw = (audio_query_1 * video_query_1).mean(-2)
        audio_video_query = self.relu(self.affine_bottleneck(audio_video_query_raw))
        channel_att_maps = self.affine_v_c_att(audio_video_query).sigmoid().reshape(batch, t_size, -1, v_dim)
        c_att_visual_feat = (raw_visual_feature * (channel_att_maps + 1))

就是说利用音频特征,和视觉特征中的特征图进行相乘,接着对空间特征做一个全局池化,每个通道得到一个值。最终得到 M t c ∈ R d v × 1 \boldsymbol{M}_t^c \in \mathbb{R}^{d_v \times 1} MtcRdv×1代表着操作是针对不同的通道,给每个通道一个attention的分数。

  1. 接着是如何得到Spatial-wise attention maps M t s \boldsymbol{M}_t^s Mts:

Spatial Attention. We also leverage the guidance capability of audio signals to guide visual spatial attention. Spatial attention follows a similar pattern to the aforementioned channel-wise attention. Note that the input visual features v t c v_t^c vtc are channel attentive. We formulate the process of spatial attention as follows:
M t s = Softmax ⁡ ( x t s ) , x t s = δ ( W 2 ( ( U a s a t ) ⊙ ( U v s v t c ) ) ) , \begin{gathered} \boldsymbol{M}_t^s=\operatorname{Softmax}\left(x_t^s\right), \\ x_t^s=\delta\left(\boldsymbol{W}_2\left(\left(\boldsymbol{U}_a^s \boldsymbol{a}_t\right) \odot\left(\boldsymbol{U}_v^s v_t^c\right)\right)\right), \end{gathered} Mts=Softmax(xts),xts=δ(W2((Uasat)(Uvsvtc))),
where U a s ∈ R d × d a , U v s ∈ R d × d v \boldsymbol{U}_a^s \in \mathbb{R}^{d \times d_a}, \boldsymbol{U}_v^s \in \mathbb{R}^{d \times d_v} UasRd×da,UvsRd×dv are fully-connected layers with ReLU as an activation function, W 2 ∈ R 1 × d \boldsymbol{W}_2 \in \mathbb{R}^{1 \times d} W2R1×d are learnable parameters with d = 256 d=256 d=256 as a hidden dimension, and δ \delta δ denotes the hyperbolic tangent function. With the spatial attention maps M t s \boldsymbol{M}_t^s Mts at hand, we perform weighted summation over v t c v_t^c vtc according to M t s \boldsymbol{M}_t^s Mts to highlight informative regions and shrink spatial dimensions, yielding a channel-spatial attentive visual feature vector v t c s ∈ R d v v_t^{c s} \in \mathbb{R}^{d_v} vtcsRdv as output.

代码如下:

        # ============================== Spatial Attention =====================================
        # channel attended visual feature: [batch * 10, 49, v_dim]
        c_att_visual_feat = c_att_visual_feat.reshape(batch*t_size, -1, v_dim)
        c_att_visual_query = self.relu(self.affine_video_2(c_att_visual_feat))
        audio_query_2 = self.relu(self.affine_audio_2(audio_feature)).unsqueeze(-2)
        audio_video_query_2 = c_att_visual_query * audio_query_2
        spatial_att_maps = self.softmax(self.tanh(self.affine_v_s_att(audio_video_query_2)).transpose(2, 1))
        c_s_att_visual_feat = torch.bmm(spatial_att_maps, c_att_visual_feat).squeeze().reshape(batch, t_size, v_dim)

就是说利用音频特征,和视觉特征中的特征图进行相乘,接着对空间特征不做全局池化了,而是做一个softmax,这样遍得到了空间特征的特征图,最终将通道变为1,得到 M t s ∈ R 1 × ( H ∗ W ) \boldsymbol{M}_t^s \in \mathbb{R}^{1 \times(H * W)} MtsR1×(HW)代表着操作是针对不同的空间,给特征图中每个pixel一个attention的分数,然后每个pixel都乘以这个分数。

使用广泛的Dot-product Attention

第一节介绍了attention的一些理解,事实上,给特征不同的关注度,即通过自己设计的方法,算出attention map再乘到或者作用到特征上,是更早期的attention。目前大家更多的在使用的是dot-product attention.

attention机制中的query,key,value的概念解释

这篇文章给出很好的解释:attention机制中的query,key,value的概念解释,以下直接引用原文
在这里插入图片描述
在这里插入图片描述

Attention的一个例子

在注意力机制的公式 Attention ⁡ ( Q , K , V ) = softmax ⁡ ( Q K T d k ) \operatorname{Attention}(Q, K, V) = \operatorname{softmax}\left(\frac{QK^T}{\sqrt{d_k}}\right) Attention(Q,K,V)=softmax(dk QKT)中,输出维度之所以是 n × d v n \times d_v n×dv,与注意力机制如何操作有关。这里, Q Q Q K K K V V V分别代表查询(Query)、键(Key)和值(Value),其中 n n n是查询的数量, m m m是键和值的数量, d k d_k dk是键和查询的维度,而 d v d_v dv是值的维度。

为什么第一维度是 n n n
第一维度 n n n代表查询的数量。在注意力机制中,每一个查询向量都会独立地与所有键向量进行匹配,计算出一个注意力分布。这个注意力分布然后用来加权对应的值向量,生成一个加权的值向量。因此,每一个查询都会产生一个输出,这就是为什么输出的第一维度是查询的数量 n n n

为什么第二维度是 d v d_v dv
第二维度 d v d_v dv代表值向量的维度。注意力机制的输出是值向量的加权和,因此输出的每个元素都处在值向量所在的空间,即具有相同的维度 d v d_v dv。这表示,尽管注意力分数是基于查询和键的相似度计算的,但最终的输出是对值的加权,反映了值向量的维度而不是查询或键的维度。

物理意义
输出维度的物理意义在于,每个查询都会得到一个对应的输出向量,这个输出向量是基于与该查询相关的所有键的信息加权的值向量。这意味着每个输出向量都是一个上下文化的表示,它综合了所有与该查询相关的值的信息。这使得模型能够根据查询的不同,动态地调整对不同信息的关注程度,从而实现对信息的有效提取和利用。

注意力机制的目的
因此,注意力机制的设计确保了输出不仅仅是简单地复制值向量,而是根据查询和键之间的关系动态生成的上下文化的表示。这种机制使得模型能够在处理复杂的序列或者执行序列到序列的任务时,有效地捕捉和利用输入数据中的复杂关系和模式。

当Q,K,V都是相同内容时,代表着对自己全局特性的信息提取,称为self-attention

以《Cross-Modal Relation-Aware Networks for Audio-Visual Event Localization》 中的cross-modality
relation attention mechanism (CMRA) 模块为例,其目的是利用attention,提高视频和音频处理系统中视觉特征和音频特征之间的相互理解和关联性,以在不同模态(如视觉和音频)之间及其内部探索和利用丰富的关系信息。

        # audio query
        video_key_value_feature = self.video_encoder(visual_feature)
        audio_query_output = self.audio_decoder(audio_feature, video_key_value_feature)  # audio作为查询,输出的还是audio中感兴趣的特征

        # video query
        audio_key_value_feature = self.audio_encoder(audio_feature)
        video_query_output = self.video_decoder(visual_feature, audio_key_value_feature)

其中的一个EncoderLayer实现如下:

class EncoderLayer(Module):
    r"""EncoderLayer is mainly made up of self-attention.

    Args:
        d_model: the number of expected features in the input (required).
        nhead: the number of heads in the multiheadattention models (required).
        dim_feedforward: the dimension of the feedforward network model (default=2048).
        dropout: the dropout value (default=0.1).
        activation: the activation function of intermediate layer, relu or gelu (default=relu).

    """

    def __init__(self, d_model, nhead, dim_feedforward=2048, dropout=0.1, activation="relu"):
        super(EncoderLayer, self).__init__()
        self.self_attn = MultiheadAttention(d_model, nhead, dropout=dropout)
        # Implementation of Feedforward model
        self.linear1 = Linear(d_model, dim_feedforward)
        self.dropout = Dropout(dropout)
        self.linear2 = Linear(dim_feedforward, d_model)

        self.norm1 = LayerNorm(d_model)
        self.norm2 = LayerNorm(d_model)
        self.dropout1 = Dropout(dropout)
        self.dropout2 = Dropout(dropout)

        self.activation = _get_activation_fn(activation)

    def forward(self, src):
        r"""Pass the input through the endocder layer.
        """
        src2 = self.self_attn(src, src, src)[0]
        src = src + self.dropout1(src2)
        src = self.norm1(src)
        if hasattr(self, "activation"):
            src2 = self.linear2(self.dropout(self.activation(self.linear1(src))))
        else:  # for backward compatibility
            src2 = self.linear2(self.dropout(F.relu(self.linear1(src))))
        src = src + self.dropout2(src2)
        src = self.norm2(src)
        return src

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:/a/364331.html

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

GitHub的使用操作

记得看目录哦! 1. 创建仓库2. 下载desktop3. 把创建的库克隆到本地4. 文件拷贝到本地仓库![在这里插入图片描述](https://img-blog.csdnimg.cn/direct/7171ac6c4ca14e3b8d22717121f79c9e.png)5. 在网址后面加/compare进行比较6. 给系统添加功能 1. 创建仓库 2. 下载…

proxmox集群从7.4升级到最新的8.1

关于proxmox,给Zstack的公开建议 十一放假折腾服务器札记 proxmox​​​​​​​专栏上面第一篇文写的时间还是22年6月底,用proxmox搭建的3机集群在合肥光源束测系统后台在线工作快两年了,在没有UPS保护的情况下,经历了实验室的…

CHS_08.2.3.6_1+生产者-消费者问题

CHS_08.2.3.6_1生产者-消费者问题 问题描述问题分析思考:能否改变相邻P、V操作的顺序?知识回顾 在这个小节中 我们会学习一个经典的进程同步互斥的问题 问题描述 并且尝试用上个小节学习的p v操作 也就是信号量机制来解决这个生产者消费者问题 问题的描…

【实战系列----消息队列 数据缓存】rabbitmq 消息队列 搭建和应用

线上运行图,更新不算最新版,但可以使用修改线程等补丁功能,建议使用新版本。 远程服务器配置图: 这个可以更具体情况,因为是缓存队列理所当然 内存越大越好,至于核心4核以上足够使用。4核心一样跑 这里主要是需要配置服…

麒麟系统—— openKylin 安装 Nginx

麒麟系统—— openKylin 安装 Nginx 一、准备工作1. 确保麒麟系统 openKylin 已经安装完毕。 二、下载 nginx三、解压与运行解压检查与编译安装编译运行 四、配置加入到服务中加入环境变量nginx 配置文件 五、常用命令 Nginx 是一款高性能的 HTTP 和反向代理服务器&#xff0c…

Android super.img解包和打包指南(含工具下载lpunpack、lpmake、lpdump)

本文所有命令均需要在linux 上执行 一、解包 1、将Android sparse image格式的super.img转成二进制文件 $ sudo apt install android-sdk-libsparse-utils $ simg2img super.img super.img.bin 2、下载工具lpunpack 和lpmake、lpdump 以及其依赖库 下载地址:https://downl…

C语言实现12种排序算法

1.冒泡排序 思路:比较相邻的两个数字,如果前一个数字大,那么就交换两个数字,直到有序。 时间复杂度:O(n^2),稳定性:这是一种稳定的算法。 代码实现: void bubble_sort(int arr[],…

在Debian11安装Proxmox VE 7及设置GPU工作环境

我们在测试大模型,深度学习的过程中,可能遇到的情况就是不断的清空系统,避免老系统中安装的软件对测试系统造成影响,导致测试结果不准确或莫名报错。今天为小伙伴们介绍使用PVE7做成一个人工智能开发和测试的平台,你可…

Day40 0-1背包问题

0-1背包问题 小明是一位科学家,他需要参加一场重要的国际科学大会,以展示自己的最新研究成果。他需要带一些研究材料,但是他的行李箱空间有限。这些研究材料包括实验设备、文献资料和实验样本等等,它们各自占据不同的空间&#x…

户外没有电源和网络,但需要安装监控系统,怎么办?太阳能智能监控系统给你解决

近期有粉丝给小编求助:需要在没网没电的户外进行智能监控的安装,不知道如何解决。收到粉丝的问题,小编立刻联系了技术人员给出方案。针对野外、户外等场景只需使用太阳能供电模组4G摄像机视频监控EasyCVR平台智能分析网关V4的架构&#xff0c…

【2023地理设计组一等奖】基于机器学习的地下水仿真与时空分析

作品介绍 1 设计思想 1.1 作品背景 华北平原是我国最重要的粮棉产地之一,然而近年来农业的低效用水以及过度压采正逐步加剧其地下水资源的紧张性,为经济可持续发展带来重大风险。而地下水动态变化与人为干预、全球气候波动呈现出高度相关性,因此,地下水的仿真模拟对保障粮…

网络编程套接字(2)

TCP 简单的TCP网络程序服务端创建套接字 服务端绑定服务端监听服务端接收连接测试服务端处理请求客户端创建套接字客户端连接服务器客户端连接服务器单执行流的服务器客户端为什么会显示连接成功? 多进程版的TCP网络程序让孙子进程提供服务 多线程版的TCP网络程序 简…

力扣之2629.复合函数(reduceRight )

/*** param {Function[]} functions* return {Function}*/ var compose function(functions) {return function(x) {return functions.reduceRight((result, func) > func(result), x);} };/*** const fn compose([x > x 1, x > 2 * x])* fn(4) // 9*/ 说明&#x…

Win10的蓝牙和其他设备没有蓝牙开关选项

一、问题背景 虽然蓝牙驱动并没有问题,但是Win10的蓝牙和其他设备却没有显示蓝牙开关的选项。这导致笔记本电脑无法使用蓝牙连接设备。如下图所示: 添加蓝牙或其他设备的标题下面本来应该有一个蓝牙开关,但是笔记本电脑却并没有显示这样的…

k8s安装dashboard报错CrashLoopBackOff

报错信息 使用kubectl get pods -A查看集群,出现错误: kubernetes-dashboard kubernetes-dashboard-xxxxxxxxxx6-2qrst 0/1 CrashLoopBackOff 6 15m查看日志后,发现原因: panic: Get "https://10…

搭建自己的私服 maven 仓库

申明:本文章所使用docker-compose配置文件纯属学习运用,非商用如有雷同请联系本人协调处理。 一、配置docker-compose.yml文件 # 指定docker-compose的版本 version: 3 services: nexus: container_name: nexus_container image: sonatype/nex…

经典mysql实操和行专列操作

1.删除除了学号字段以外,其它字段都相同的冗余记录,只保留一条!(也就是要删除王五和赵六中一条重复数据只留一条) 要求的预期效果: 原始数据创建表结构: CREATE TABLE tb_student (id int(16) NOT NULL,na…

C#验证字符串是否大写、小写,正则表达式vs用Char.IsUpper和Char.IsLower方法遍历字符数组

目录 一、使用的方法 1.正则表达式 2.用Char.IsUpper或Char.IsLower方法 二、源代码 1.源码 2.生成效果 一、使用的方法 1.正则表达式 正则表达式“^[A-Z]$”,其中[A-Z]表示匹配一个到多个大写字母。 正则表达式“^[a-z]$”,其中[a-z]表示匹配一个…

C# wpf 字体图标预览,html字符与unicode转换

在进行wpf 开发工作过程中遇到字体图标无法预览的问题&#xff0c;特此记录。 1、把需要预览的字体文件上传到网站上进行转换 Create Your Own font-face Kits Font Squirrel2、下载文件后进行解压。 3、找到 Glyph Chart 查看字体html字符编码4、在wpf中直接使用即可 <…

【JAVA】单例模式的线程安全性

&#x1f34e;个人博客&#xff1a;个人主页 &#x1f3c6;个人专栏&#xff1a;JAVA ⛳️ 功不唐捐&#xff0c;玉汝于成 目录 正文 我的其他博客 正文 老生常谈的问题了&#xff0c;首先要说的是单例模式的线程安全意味着&#xff1a;某个类的实例在多线程环境 下只会被…