Back to blog

Classifier-Free Diffusion Guidance

Jointly trained conditional and unconditional diffusion model enabling guidance without a separate classifier, achieving similar quality-diversity tradeoff as classifier guidance with simpler training

Classifier-Free Diffusion Guidance

一、论文概述

项目内容
标题Classifier-Free Diffusion Guidance
作者Jonathan Ho, Tim Salimans
机构Google Research
论文https://arxiv.org/abs/2207.12598
代码https://github.com/openai/guided-diffusion
发布arXiv:2207.12598v1 [cs.LG], Jul 26, 2022
许可MIT (代码仓库)

二、核心思想

问题定义

Classifier guidance(Ho & Krizhevsky, 2019)是一种在条件扩散模型训练后调节生成质量与多样性权衡的方法,类似于其他生成模型中的低温采样或截断策略。它结合扩散模型的分数估计和图像分类器的梯度,但存在两个关键局限:

  1. 需要额外分类器:必须训练一个与扩散模型独立的图像分类器,增加了训练成本和复杂度
  2. 训练-推理不匹配:分类器在训练时从未见过扩散模型生成的样本,可能导致分布偏移

解决方案概述

本文提出 classifier-free guidance——一种无需分类器的引导方法。核心思想是联合训练一个条件和一个无条件扩散模型,通过组合条件和无条件的分数估计来实现类似 classifier guidance 的样本质量-多样性权衡。

关键创新:将无条件估计 p(x)p(x) 作为条件估计 p(x∣c)p(x|c) 的退化情况,通过训练时随机丢弃条件(condition dropout),使单个模型同时学习条件和无条件分布。

三、技术架构

整体框架

传统 Classifier Guidance:
  ∇_x log p_θ(x|c) = ∇_x log p_θ(x) + w · ∇_x log p(c|x)
                       ↑                        ↑
                  扩散模型分数           独立分类器梯度

Classifier-Free Guidance:
  ∇_x log p_θ(x|c) ≈ (1+w) · ∇_x log p_θ(x|c) - w · ∇_x log p_θ(x)
                       ↑                            ↑
                  条件模型分数              无条件模型分数(同一模型)

核心公式

Classifier Guidance(基线)

∇xlog⁡pθ(x∣c)=∇xlog⁡pθ(x)+w⋅∇xlog⁡p(c∣x)\nabla_x \log p_\theta(x|c) = \nabla_x \log p_\theta(x) + w \cdot \nabla_x \log p(c|x)

其中:

  • pθ(x)p_\theta(x) 是扩散模型的边缘分布
  • p(c∣x)p(c|x) 是独立训练的分类器
  • ww 是引导权重系数

Classifier-Free Guidance(本文方法)

∇xlog⁡p~θ(x∣c)=(1+w)⋅∇xlog⁡pθ(x∣c)−w⋅∇xlog⁡pθ(x)\nabla_x \log \tilde{p}_\theta(x|c) = (1 + w) \cdot \nabla_x \log p_\theta(x|c) - w \cdot \nabla_x \log p_\theta(x)

等价地在噪声预测形式下:

ϵ^(xt,c)=(1+w)⋅ϵθ(xt,c)−w⋅ϵθ(xt)\hat{\epsilon}(x_t, c) = (1 + w) \cdot \epsilon_\theta(x_t, c) - w \cdot \epsilon_\theta(x_t)

其中:

  • ϵθ(xt,c)\epsilon_\theta(x_t, c) 是给定条件 cc 的噪声预测
  • ϵθ(xt)\epsilon_\theta(x_t) 是无条件噪声预测(即 cc 被丢弃时的预测)
  • ww 是引导权重

关键洞察:通过取对数概率的线性组合,我们得到: log⁡p~θ(x∣c)≈(1+w)log⁡pθ(x∣c)−wlog⁡pθ(x)=log⁡pθ(x∣c)1+w⋅pθ(x)−w\log \tilde{p}_\theta(x|c) \approx (1+w) \log p_\theta(x|c) - w \log p_\theta(x) = \log p_\theta(x|c)^{1+w} \cdot p_\theta(x)^{-w}

这相当于对条件分布进行”温度缩放”:当 w>0w > 0 时,分布变得更尖锐(质量更高但多样性降低);当 w<0w < 0 时,分布更平坦(多样性更高但质量降低)。

训练方法:条件 Dropout

为使单个模型同时学习条件和无条件分布,训练时对条件进行随机丢弃:

cdropout={cwith probability 1−pdrop∅with probability pdropc_{\text{dropout}} = \begin{cases} c & \text{with probability } 1-p_{\text{drop}} \\ \emptyset & \text{with probability } p_{\text{drop}} \end{cases}

超参数:

  • pdropp_{\text{drop}}(条件丢弃率):默认值 1/6(即 ~16.7% 的概率丢弃条件)
  • 该值的选择使得无条件估计 pθ(x)p_\theta(x) 和条件估计 pθ(x∣c)p_\theta(x|c) 的训练频率大致平衡

损失函数:标准 DDPM 噪声预测损失,但对每个 batch 中的样本随机应用条件丢弃:

L=Et,x0,c,ϵ,cdropout[∥ϵ−ϵθ(xt,cdropout,t)∥2]\mathcal{L} = \mathbb{E}_{t, x_0, c, \epsilon, c_{\text{dropout}}} \left[ \| \epsilon - \epsilon_\theta(x_t, c_{\text{dropout}}, t) \|^2 \right]

其中 cdropoutc_{\text{dropout}} 以概率 pdropp_{\text{drop}} 为 ∅\emptyset。

推理流程

Step 1: 训练阶段
  └─ 标准 DDPM/DDIM 训练流程
  └─ 每个 step 以 p_drop=1/6 概率丢弃条件 c

Step 2: 推理阶段
  ├─ 给定条件 c 和无条件初始噪声 x_T ~ N(0,I)
  ├─ 从 t=T 到 t=1 逐步去噪
  ├─ 每步使用 modified score:
  │    ε̂ = (1+w)·ε_θ(x_t, c, t) - w·ε_θ(x_t, t)
  └─ w > 0: 提高质量,降低多样性
      w = 0: 标准条件采样
      w < 0: 提高多样性,降低质量

四、核心创新

创新点说明理论/实验依据
Classifier-free guidance无需独立分类器即可实现引导采样通过条件+无条件分数线性组合近似温度缩放
条件 Dropout 训练单一模型同时学习条件和无条件分布pdrop=1/6p_{\text{drop}}=1/6 平衡两类估计的训练频率
统一框架兼容所有基于分数的扩散模型(DDPM, DDIM, ADM 等)仅修改噪声预测步骤,无需改变模型架构
质量-多样性连续控制通过单参数 ww 连续调节采样特性ww 从负到正覆盖从多样到高质量的完整谱系

五、实验结果

FID-Classifier 联合曲线

核心发现:Classifier-free guidance 在 FID-Classifier 联合曲线上与 classifier guidance 相当甚至更优:

  • FID(Fréchet Inception Distance):衡量生成图像的分布距离(越低越好)
  • Classifier Score:衡量生成图像的分类准确率(越高越好,反映条件控制能力)

在不同引导权重 ww 下绘制 FID vs Classifier Score 曲线,classifier-free guidance 的 Pareto frontier 覆盖了 classifier guidance 的范围。

不同数据集的结果

ImageNet 64×64:

  • 在标准设置下,classifier-free guidance 达到与 classifier guidance 相当的 FID
  • 对于高类别权重的样本,classifier-free guidance 表现更优
  • 对于低类别权重(模糊/困难类别)的样本,classifier guidance 略有优势

CelebA-HQ 256×256:

  • 在高质量人脸生成任务上,classifier-free guidance 同样表现出色
  • 不需要单独训练面部属性分类器

引导权重 ww 的影响

ww 值效果适用场景
w<0w < 0增加多样性,降低质量需要多样样本的场景
w=0w = 0标准条件采样默认设置
w>0w > 0(典型 0.5-3.0)提高质量,降低多样性追求高质量单一样本
w≫0w \gg 0过度锐化,可能出现伪影一般不推荐

与温度缩放的联系

Classifier-free guidance 在理论上等价于对条件分布进行温度缩放:

pθ(x∣c)1/w∝q(x)1/σ2p_\theta(x|c)^{1/w} \propto q(x)^{1/\sigma^2}

其中 σ2=1/(1+w)\sigma^2 = 1/(1+w) 可视为有效温度。这解释了为什么 ww 能控制质量-多样性权衡。

六、消融实验

条件丢弃率 pdropp_{\text{drop}} 的影响

pdropp_{\text{drop}}FID (w=0)FID (w=2.0)说明
0.0最佳N/A无条件估计不存在
1/6(默认)良好良好平衡条件/无条件训练
0.5略降略降条件训练不足
1.0N/AN/A永远丢弃条件

结论:pdrop∈[0.1,0.5]p_{\text{drop}} \in [0.1, 0.5] 范围内均表现良好,1/61/6 是最小推荐值。

与 Classifier Guidance 对比

方法FID (w=0)FID (w=2)额外训练成本
Classifier-free良好良好无
Classifier guidance良好略优需训练分类器

关键发现:在大多数情况下,classifier-free guidance 的性能与 classifier guidance 相当,且无需额外训练分类器。

七、相关工作

工作关系
Classifier Guidance (Ho & Krizhevsky, 2019)原始方法,需要独立分类器
DDPM (Ho et al., 2020)基础扩散模型,classifier-free 可直接应用于此
DDIM (Song et al., 2020)确定性采样,同样兼容 classifier-free guidance
ADM (Dhariwal & Nichol, 2021)高容量扩散模型,使用 classifier-free guidance 作为默认配置
Stable Diffusion (Rombach et al., 2022)Latent diffusion,采用 classifier-free guidance
DALL-E 2 (Ramesh et al., 2022)级联扩散模型,使用 classifier-free guidance

八、总结

核心贡献

  1. Classifier-free guidance:首次提出无需分类器的扩散模型引导方法,通过条件+无条件分数线性组合实现质量-多样性权衡
  2. 条件 Dropout 训练:单一模型同时学习条件和无条件分布,pdrop=1/6p_{\text{drop}}=1/6 平衡两类估计
  3. 理论连接:证明 classifier-free guidance 等价于对条件分布的温度缩放
  4. 广泛兼容性:适用于所有基于分数的扩散模型,成为后续工作的默认配置

技术影响

  • 成为扩散模型的标准配置:此后几乎所有主流扩散模型(Stable Diffusion, DALL-E 2, Imagen 等)均采用 classifier-free guidance
  • 简化了条件生成流程:消除了训练独立分类器的需求,降低了部署复杂度
  • 启发了后续研究:如 CFG++、linear CFG 等改进方法

局限性

  1. 计算开销:每步去噪需要两次前向传播(条件+无条件),推理速度减半
  2. 过度锐化问题:过大的 ww 可能导致生成图像出现伪影或不自然
  3. 无条件估计的质量:当 pdropp_{\text{drop}} 较低时,无条件估计可能不够准确
  4. 多条件场景:当存在多个条件变量时,如何扩展尚不明确

九、参考资源

  • arXiv: https://arxiv.org/abs/2207.12598
  • Code: https://github.com/openai/guided-diffusion
  • DDPM: Ho et al. (2020), “Denoising Diffusion Probabilistic Models”
  • ADM: Dhariwal & Nichol (2021), “Diffusion Models Beat GANs on Image Synthesis”
  • Stable Diffusion: Rombach et al. (2022), “High-Resolution Image Synthesis with Latent Diffusion Models”
  • Imagen: Saharia et al. (2022), “Photorealistic Text-to-Image Diffusion Models with Deep Language Understanding”