type
status
date
slug
summary
tags
category
icon
password
info
个人博客主页
前置阅读:
Score based generative model:http://myhz0606.com/article/ncsn

引言

在用Stable Diffusion生成图片时,除了输入图片表述文本外(positive prompt),我们还经常会使用negative prompt作为condition来让模型避免生成negative prompt所表述的概念。查阅源码发现stable diffusion中negative prompt的实现机制是将classifier-free guided中替换为,(表示negative prompt)。即:
原生classifier-free guided每一个timestep的噪声估计如下:
当有negative prompt condition时,将上式改为
那么为什么negative prompt能够work呢?

How do negative prompt take effect

为了引出相关推导,先快速回顾一下classifier-guided和classifier-free的motivation。
为了做条件生成(即从条件分布中采样样本),我们可以根据贝叶斯公式进行如下推导:
在classifier-guided任务中,我们已知无条件输入的score based model能够估计出 ,因此,为了得到,我们只需额外训练一个分类器来估计即可。为了控制condition的强度,引入一个guidance scale
对于classifier-free任务中,通过随机drop标签,我们同时训练两个score based model。虽然我们可以通过直接进行条件生成,但为了控制生成时条件的强度,沿用了公式(4) guidance scale的概念。并且,故有:
当有negative prompt作为condition时,此时的condition为两项,一项是: positive prompt condition,另一项为 :negative prompt condition。
只要得到 我们就可以参考之前的采样算法生成样本。重新直接训练一个score based model来估计当然可行,但成本巨大。下面来看如何进行简化[1,2]
由于:
为positive prompt condition的guidance scale,为negative prompt的guidance scale,有
通过式(8)可以得出,我们只需计算三项即可估计出
时,
式(9) 就是stable diffusion源码中实现形式
文献[3]通过“Neutralization Hypothesis”,“Reverse Activation”解释negative prompt conditioning的工作机制,感兴趣的同学可以后续阅读。

When do negative prompt take effect

定性分析

上文我们通过理论推导证明了negative prompt conditioning的可行性。本节将从可视化的角度分析negative prompt conditioning是如何影响图片生成的。主要文献参考[3]
类似Prompt-to-prompt[4]的研究思路,我们可以绘制不同时间步token-wise attention map热力图。从图中发现,negative prompt作用存在一定延迟。positive prompt conditioning在生成的早期(t=0-3)时就关注到对应的区域,而negative prompt conditioning直到t=8-11才能正确关注到对应的区域。
notion image

定量分析

进一步的,为了定量的描述上述机制,文献[3]定义了为negative prompt conditioning的强度
假设:Positive prompt: Pofessional office woman. Negative prompt: Glasses
: 表示negative prompt
: 表示positive prompt
:表示negative prompt第个索引处的token
:表示positive prompt 中与最相关的token。=”Glasses”, 那么=“woman”。
: 在时间步为时,在第层cross-attention处token 对应的attention map。
: 在时间步为时,在第层cross-attention处token 对应的attention map。
越小时,说明negative prompt conditioning的强度越小,反之越大。
选择了10对相应的提示对,10个不同的随机种子上进行实验,并绘制曲线如下:
notion image
从上图不难得出:
  • negative prompt conditioning的强度初始较弱,在时间步为5-10时达到峰值。
  • 当negative prompt 为名词时,呈先增强后降低趋势,这是由于当negative prompt作用后,会移除生成图片中的对应实体,从而让token-wise attention map的响应变弱。
  • 当negative prompt 为形容词时,呈先增强后稳定。
即然negative prompt conditioning存在滞后性,可以在初始阶段(t=0-5)不引入negative prompt conditioning,之后在引入,这能起到类似局部编辑的效果。
notion image

小结

本文相对系统探讨了diffusion model中negative prompt conditioning的工作机理,解释了stable diffusion关于negative prompt conditioning源码实现的合理性(式9),并给出了更一般的形式(式8)。

参考文献

[1] Compositional Visual Generation with Energy Based Models
[2] Compositional Visual Generation with Composable Diffusion Models
[3]Understanding the Impact of Negative Prompts: When and How Do They Take Effect?
 
相关文章
diffusion model(一):DDPM技术小结 (denoising diffusion probabilistic)
Lazy loaded image
diffusion model(二):DDIM技术小结 (denoising diffusion implicit model)
Lazy loaded image
diffusion model(三):classifier guided diffusion model
Lazy loaded image
diffusion model(四):文生图diffusion model (classifier-free guided)
Lazy loaded image
diffusion model(五):LDM: 在隐空间用diffusion model合成高质量图片
Lazy loaded image
diffusion model(六):Dalle2 技术小结
Lazy loaded image
diffusion model(十七)Score视角下的生成模型(Score-Based Generative Models)diffusion model(十九) :SDE视角下的扩散模型
Loading...
莫叶何竹🍀
莫叶何竹🍀
非淡泊无以明志,非宁静无以致远
最新发布
表格结构还原——SLANet
2025-2-27
KV-Cache技术小结(MHA,GQA,MQA,MLA)
2025-2-24
diffusion model(十九) :SDE视角下的扩散模型
2024-12-31
🔥Lit: 进一步提升多模态模型Zero-Shot迁移学习的能力
2024-11-22
RNN并行化——《Were RNNs All We Needed?》论文解读
2024-11-21
Supervised Contrastive Learning
2024-10-12