交叉注意力控制



在这里插入图片描述

Cross Attention Control

我们可以通过在扩散过程中注入交叉注意力映射来编辑图像,控制哪个像素在哪个扩散步骤中关注提示文本的哪个标记。使用源图像的注意图来控制生成图像的空间布局和几何形状。当在提示符中交换一个单词时,我们注入源图像映射Mt,覆盖目标图像映射M∗t,以保持空间布局。在添加新短语的情况下,我们只注入与提示符未更改部分对应的映射。通过调整注意图的权重来放大或减弱单词的语义效果。

在这里插入图片描述

生成图像的文本中每个词对应的平均注意力掩码:
在这里插入图片描述
bear, bird在各个扩散步数中对应的注意力图:
在这里插入图片描述

为了将我们的方法应用于各种创造性编辑应用,我们展示了几种通过简单和语义接口控制交叉注意力映射的方法:

Replacement

第一种方法是在固定交叉注意力映射的同时,改变提示符中的单个标记值(例如,“狗”改为“猫”),以保留场景组成。

In this case, the user swaps tokens of the original prompt with others, e.g., the editing the prompt “A painting of a squirrel eating a burger” to “A painting of a squirrel eating a lasagna” or “A painting of a lion eating a burger”. For this we define the class AttentionReplace.

Refinement

第二种是全局编辑图像,例如,通过在提示符中添加新词并冻结对先前标记的注意力,同时允许新的注意力流向新的标记来更改样式。

In this case, the user adds new tokens to the prompt, e.g., editing the prompt “A painting of a squirrel eating a burger” to “A watercolor painting of a squirrel eating a burger”. For this we define the class AttentionEditRefine.

Re-weight

第三是在生成的图像中放大或减弱单词的语义效果

In this case, the user changes the weight of certain tokens in the prompt, e.g., for the prompt “A photo of a poppy field at night”,strengthen or weaken the extent to which the word night affects the resulting image. For this we define the class AttentionReweight.

在这里插入图片描述

Attention Control Options

  • cross_replace_steps: specifies the fraction of steps to edit the cross attention maps. Can also be set to a dictionary [str:float] which specifies fractions for different words in the prompt.
  • self_replace_steps: specifies the fraction of steps to replace the self attention maps.
  • local_blend (optional): LocalBlend object which is used to make local edits. LocalBlend is initialized with the words from each prompt that correspond with the region in the image we want to edit.
  • equalizer: used for attention Re-weighting only. A vector of coefficients to multiply each cross-attention weight
### 十字交叉注意力机制(Criss-Cross Attention, CCA) #### 什么是十字交叉注意力机制? 十字交叉注意力机制是一种用于捕捉全局上下文信息的有效方法,特别适用于语义分割和目标检测等计算机视觉任务。CCA 的核心思想是在不增加过多计算开销的情况下,让每个像素能够感知到整幅图像的信息[^1]。 具体来说,CCA 利用了两个方向上的交互路径来传播信息:水平方向和垂直方向。这种设计使得模型能够在较少的迭代次数内完成全局信息的传递。通过递归应用该模块,可以进一步增强其捕获长距离依赖的能力,而不会显著增加内存消耗或计算复杂度[^2]。 --- #### 数学描述与理论基础 假设输入特征图 \( X \in \mathbb{R}^{H \times W \times C} \),其中 \( H \) 和 \( W \) 表示高度和宽度,\( C \) 是通道数。CCA 首先定义了一个双向传播过程: - **水平传播**: 对于每一行中的像素位置 \( p_i \),它会与其所在行的所有其他像素建立联系。 - **垂直传播**: 同样地,对于每一列中的像素位置 \( q_j \),它也会与其他列中的对应像素形成关联。 这两个方向的操作可以通过矩阵乘法高效实现,并且它们的结果会被组合起来作为最终输出[^3]。 为了减少冗余并提高效率,实际实现中通常会对原始形式做一定简化处理,比如引入卷积核或其他轻量化策略。此外,在某些变体版本里还加入了残差连接以促进梯度流动以及稳定训练过程[^4]。 --- #### PyTorch 实现代码 以下是基于 PyTorch 的简单版 Criss-Cross Attention 模块实现: ```python import torch import torch.nn as nn import torch.nn.functional as F class CrissCrossAttention(nn.Module): """Criss-Cross Attention Module""" def __init__(self, in_dim): super(CrissCrossAttention, self).__init__() self.query_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1) self.key_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim//8, kernel_size=1) self.value_conv = nn.Conv2d(in_channels=in_dim, out_channels=in_dim, kernel_size=1) self.gamma = nn.Parameter(torch.zeros(1)) def forward(self, x): m_batchsize, _, height, width = x.size() proj_query = self.query_conv(x).view(m_batchsize, -1, width*height).permute(0, 2, 1) proj_key = self.key_conv(x).view(m_batchsize, -1, width*height) energy = torch.bmm(proj_query, proj_key) attention = F.softmax(energy, dim=-1) proj_value = self.value_conv(x).view(m_batchsize, -1, width*height) out = torch.bmm(proj_value, attention.permute(0, 2, 1)) out = out.view(m_batchsize, -1, height, width) out = self.gamma * out + x return out ``` 上述代码片段展示了如何构建一个基本的 CCA 层。这里的关键部分包括查询、键值映射及其后续操作步骤;同时注意加入可学习参数 `gamma` 来控制新生成特征的重要性程度[^4]。 --- #### 应用场景分析 由于其出色的性能表现及较低资源需求特性,Criss-Cross Attention 已被广泛应用于多个领域之中,主要包括但不限于以下几个方面: 1. **语义分割**:通过更精确地描绘边界区域细节提升整体精度; 2. **实例分割**:有助于区分紧密相邻的不同对象个体; 3. **目标检测**:改善小物体识别效果的同时保持较快推理速度; 4. **视频理解**:利用时间维度扩展后的版本建模帧间动态变化规律。 这些优势共同构成了 CCNet 成功的基础之一^. --- 问题
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值