Prompt-to-Prompt:基于 cross-attention 控制的图像编辑技术

Paper: Hertz A, Mokady R, Tenenbaum J, et al. Prompt-to-prompt image editing with cross attention control[J]. arXiv preprint arXiv:2208.01626, 2022.
Introduction: https://prompt-to-prompt.github.io/
Code: https://github.com/google/prompt-to-prompt

Prompt-to-Prompt 是 Google 提出的一种全新的图像编辑方法,不同于任何传统方法需要用户指定编辑区域,Prompt-to-Prompt 只需要用户对 prompt 文本进行编辑,就能获得图像编辑的直观体验。并且 Prompt-to-Prompt 基于预训练的 Latent Diffusion 或 Stable Diffusion,不需要任何训练或微调,可以直接拿来做编辑任务。

Prompt-to-Prompt 所做的任务是根据编辑指令进行图像编辑,保证了编辑不对图像产生大的变动。具体方法是通过在 diffusion 模型的扩散过程中注入 cross-attention maps,来控制 pixels 在扩散过程中关注 prompt 文本的 tokens,从而编辑图像。Cross-attention 层是联系图像空间布局、几何形状和 prompt 文本中 tokens 的关键,这也是本文的一个重要创新。

一. 研究思路

在图像编辑方法出来之前,想要修改图像都是通过 ⌈ \lceil 遮盖 + 修改 ⌋ \rfloor 的方法进行的,这种局部修改方法比较慢,而且遮盖还会丢失关键信息。但要想直接对图像进行编辑,又有一个相当棘手的问题,就是任何输入文本的微小变动都可能使模型生成的图像发生巨大的改变,而图像编辑需要尽可能保持原有图像。

于是文中提出了一种 Prompt-to-Prompt 方法,使用基于文本的预训练 diffusion 模型,通过调整模型生成图像过程中的 cross-attention maps,从而保证在尽可能保持原有图像的空间布局和几何外观的情况下实现图像编辑。

Prompt-to-Prompt 主要适用于以下三种任务:

  • Word Swap:替换文本中的某个单词,也称为 Replacement;
  • Adding a New Phrase:增加一段新的描述以改变图像风格或增加图像细节,也称为 Refinement;
  • Attention Re–weighting:增强或减弱某个单词(一般是形容词)在图像中的作用效果,也称为 Re-weight;

在这里插入图片描述

二. Cross-Attention 控制

cross-attention 层是控制图像空间布局 (spatial layout) 和 prompt 中分词 (token) 关系的关键,高维张量 cross-attention maps 可以在 pixels 和 tokens 间建立连接。以 text-conditioned diffusion 的一次图像生成过程为例,pixels 和 tokens 间的联系如下:
在这里插入图片描述

所以 Prompt-to-Prompt 通过在扩散过程向预训练模型中注入特定的 cross-attention maps,能够使得一些 pixels 去匹配对应的 tokens。为了维持原始图像的空间布局与几何形状,可以在生成编辑图像的过程中向 cross-attention maps 中注入原始图像的特定 cross-attention maps。

在这里插入图片描述

1. Replacement

当想要替换文本中的某个单词时,用原始图像的 cross-attention maps M t M_t Mt 替换目标图像的 cross-attention maps M t ∗ M_t^* Mt,这样就可以在维持原始图像空间布局的情况下表示新的语义。

2. Refinement

当想要增加描述时,就将原始文本中没有改变的 token 对应的 M t M_t Mt 部分注入 M t ∗ M_t^* Mt,这样就可以在维持原始图像空间布局的情况下编辑图像细节或风格。

3. Re-weight

当想要增强或减弱某个单词在图像中的作用效果时,只需要调整 cross-attention maps 中 token 对应部分的权重即可。

三. 编辑图像生成

Prompt-to-Prompt 使用基于文本的预训练 diffusion 模型,通过调整模型生成图像过程中的 cross-attention maps,以实现图像编辑。所以 Prompt-to-Prompt 不需要对模型进行训练或微调,也不需要任何训练数据,用户只需要编辑 prompt 文本,就可以直观得体验图像编辑。

记原始图像为 I \mathcal I I,原始 prompt 文本为 P \mathcal P P,编辑后图像为 I ∗ \mathcal I^* I,编辑后 prompt 文本为 P ∗ \mathcal P^* P,随机种子为 s s s D M ( z t , P , t , s ) DM(z_t, \mathcal P, t,s) DM(zt,P,t,s) 表示 t t t 时刻的逆扩散过程,输出隐空间的噪声图像 z t − 1 z_{t-1} zt1 和 cross-attention map M t M_t Mt D M ( z t , P , t , s ) { M ← M ^ } DM(z_t, \mathcal P, t,s)\{M \leftarrow \widehat M\} DM(zt,P,t,s){MM } 表示替换该步骤中的 M M M M ^ \widehat M M E d i t ( M t , M t ∗ , t ) Edit(M_t, M_t^*, t) Edit(Mt,Mt,t) 表示 t t t 时刻的 cross-attention map 是 M t M_t Mt M t ∗ M_t^* Mt

在这里插入图片描述

去噪过程中,分别使用当前时间步的原始隐变量 z t z_{t} zt 和目标隐变量 z t ∗ z_{t}^* zt P \mathcal P P P ∗ \mathcal P^* P 计算 M t M_t Mt M t ∗ M_t^* Mt,然后将 M t M_t Mt 注入 M t ∗ M_t^* Mt 得到新的 cross-attention map M ^ \widehat M M ,最后再用 M ^ \widehat M M P ∗ \mathcal P^* P 计算 cross-attention 生成新的 z t − 1 ∗ z_{t-1}^* zt1 用于下一轮迭代:
在这里插入图片描述

1. Replacement

在去噪过程中,早期时间步(高噪声)影响全局风格,后期时间步(低噪声)影响具体形状,因此使用原始图像的 M t M_t Mt 替换目标图像的 M t ∗ M_t^* Mt 必须有所节制,不然会导致全局结构与风格的改变。因此在扩散过程中设置时间节点 τ \tau τ τ \tau τ 之前按 M t M_t Mt 生成编辑图像以维持原有结构和风格,之后再注入 M t ∗ M_t^* Mt 以修改语义,因此有:
E d i t ( M t , M t ∗ , t ) = { M t ∗ t < τ M t o t h e r w i s e Edit(M_t, M_t^*, t)= \begin{cases} M_t^* & t<\tau \\ M_t & otherwise \\ \end{cases} Edit(Mt,Mt,t)={MtMtt<τotherwise

下图是 cross-attention maps 注入程度的对比结果,可以看到,过度注入 cross-attention maps 会限制编辑 P ∗ \mathcal P^* P 的生成(Eg: bicycle -> car):
在这里插入图片描述

2. Refinement

增加一段新的描述时只需要对 P \mathcal P P P ∗ \mathcal P^* P 中共有的 token 进行 cross-attention map 注入。使用对齐函数 A A A 输入 P ∗ \mathcal P^* P 中 token 的索引,输出对应 P \mathcal P P 中 token 的索引;如果没有对应则输出 None。因此有:
( E d i t ( M t , M t ∗ , t ) ) i , j = { ( M t ∗ ) i , j A ( j ) = N o n e ( M t ) i , A ( j ) o t h e r w i s e (Edit(M_t, M_t^*, t))_{i,j}= \begin{cases} (M_t^*)_{i,j} & A(j)=None \\ (M_t)_{i,A(j)} & otherwise \\ \end{cases} (Edit(Mt,Mt,t))i,j={(Mt)i,j(Mt)i,A(j)A(j)=Noneotherwise

Adding a New Phrase 可以增加图像细节或是改变风格:
在这里插入图片描述

3. Re-weight

要想增强或减弱某个单词在图像中的作用效果,只需要给该 token 对应的 cross-attention map 乘上参数 c ∈ [ − 2 , 2 ] c \in [-2,2] c[2,2],其余 token 的 cross-attention map 保持不变。因此有:
( E d i t ( M t , M t ∗ , t ) ) i , j = { c ⋅ ( M t ) i , j j = j ∗ ( M t ) i , j o t h e r w i s e (Edit(M_t, M_t^*, t))_{i,j}= \begin{cases} c \cdot (M_t)_{i,j} & j=j^* \\ (M_t)_{i,j} & otherwise \\ \end{cases} (Edit(Mt,Mt,t))i,j={c(Mt)i,j(Mt)i,jj=jotherwise

Attention Re–weighting 可以控制 prompt 中某些形容词的作用效果:
在这里插入图片描述

四. 应用

【AIGC第六篇】Prompt-to-Prompt:基于cross-attention控制的图像编辑技术

五. 总结

Prompt-to-Prompt 最主要的贡献就是发现了 text-conditioned diffusion 模型中 cross-attention 的强大功能,能够控制图像的空间布局和 prompt 文本中每个 token 之间的交互。通过 cross-attention maps 注入,Prompt-to-Prompt 提出了三种编辑方法:Replacement、Refinement、Re-weight,在实际应用中都实现了令人满意的效果。

六. 复现

Prompt-to-Prompt 基于 Latent Diffusion 或 Stable Diffusion,使用时需要提供 HuggingFace 访问私有模型的身份验证令牌,demo 如下:

### Retrieval-Augmented Generation (RAG) in NLP #### Definition of RAG Retrieval-Augmented Generation combines the strengths of retrieval-based models with generative models to improve conversational systems' performance. Traditional retrieval methods excel at finding relevant information but lack flexibility when generating responses that require synthesis or creativity. Generative models can produce novel text but may suffer from hallucinations—generating content not grounded in factual knowledge. By integrating both approaches, RAG leverages external databases or corpora as a source of evidence during generation, ensuring outputs are more accurate and contextually appropriate while maintaining natural language fluency[^1]. #### Implementation Details The architecture typically consists of two main components: - **Retriever**: Responsible for fetching documents most pertinent to user queries using techniques like dense passage retrieval. ```python class Retriever: def __init__(self): pass def retrieve(self, query): # Implement document search logic here pass ``` - **Generator**: Utilizes retrieved contexts alongside input prompts to craft coherent replies via transformer architectures such as BART or T5. ```python from transformers import AutoModelForSeq2SeqLM, AutoTokenizer class Generator: def __init__(self): self.tokenizer = AutoTokenizer.from_pretrained("facebook/bart-large") self.model = AutoModelForSeq2SeqLM.from_pretrained("facebook/bart-large") def generate(self, prompt, context): inputs = self.tokenizer(prompt + " " + context, return_tensors="pt", max_length=512, truncation=True) output_ids = self.model.generate(inputs["input_ids"]) response = self.tokenizer.decode(output_ids[0], skip_special_tokens=True) return response ``` To enhance traditional RAG further, Graph RAG introduces graph structures into the mix, allowing better representation of relationships between entities within stored knowledge bases compared to vector representations alone[^3]. This approach facilitates richer contextual understanding across diverse domains including healthcare, finance, etc., where interconnected data points play crucial roles. #### Use Cases One prominent application area lies in customer service automation through virtual assistants capable of providing precise answers based on vast amounts of structured/unstructured textual resources without losing personal touch[^4]. Another potential field is legal research assistance; lawyers could benefit greatly by having access to case law summaries generated dynamically according to specific needs rather than manually sifting through countless precedents. --related questions-- 1. How does Cross-Attention mechanism contribute to improving RAG's effectiveness? 2. What challenges might one encounter when implementing custom retrievers tailored towards specialized industries? 3. Can you provide examples illustrating how Graph RAG outperforms conventional RAG implementations regarding entity relationship handling? 4. In what ways has pre-training large-scale language models impacted advancements made within this domain over recent years?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值