0


【diffusers极速入门(七)】Classifier-Free Guidance (CFG)直观理解以及对应代码

系列文章目录

  • 【diffusers 极速入门(一)】pipeline 实际调用的是什么? call 方法!
  • 【diffusers 极速入门(二)】如何得到扩散去噪的中间结果?Pipeline callbacks 管道回调函数
  • 【diffusers极速入门(三)】生成的图像尺寸与 UNet 和 VAE 之间的关系
  • 【diffusers极速入门(四)】EMA 操作是什么?
  • 【diffusers极速入门(五)】扩散模型中的 Scheduler(noise_scheduler)的作用是什么?
  • 【diffusers极速入门(六)】缓存梯度和自动放缩学习率以及代码详解

  1. 提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档

文章目录


前言

由于 Classifier-Free Guidance (CFG) 相关的理论解释博客已经很多了,本文不涉及理论推导,而侧重直观理解和对应的 diffusers 代码。

一、Classifier-Free Guidance (CFG) 的做法和作用

在生成模型(如扩散模型)中,Classifier-Free Guidance 是一种在不依赖显式分类器的情况下提升生成结果质量的技术。传统上,扩散模型会在噪声和目标分布之间逐步转换,但为了让生成结果更符合特定的条件(如文本描述),引入了 guidance 方法。

做法:

  1. 无条件生成:模型首先生成一个“无条件”(unconditioned)的预测,即在没有任何文本提示或条件的情况下的生成。

  2. 有条件生成:模型再生成一个“有条件”的预测,即在给定文本提示(prompt)的情况下的生成。

  3. 合成结果:最终的生成结果通过将无条件和有条件的预测组合来实现:

    1. 最终生成
    2. =
    3. 无条件预测
    4. +
    5. w
    6. ×
    7. (
    8. 有条件预测
    9. 无条件预测
    10. )
    11. \text{最终生成} = \text{无条件预测} + w \times (\text{有条件预测} - \text{无条件预测})

    最终生成=无条件预测+w×(有条件预测−无条件预测)

其中,( w ) 是

  1. guidance_scale

参数,用于控制生成结果与文本提示的相关性。这个公式的目标是在保持生成结果自然性的前提下,使其更贴合给定的条件(如文本描述)。

作用:

  • guidance_scale(即公式中的 ( w ))越大,生成的图像越贴近文本提示,但这可能会导致图像质量的下降或不自然的细节。
  • guidance_scale 值越小,图像则越自然(真实),但可能与文本提示的相关性较低。

二、对应 diffusers 代码

  1. /path/to/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py

的代码为例, CFG 主要相关的参数是

  1. guidance_scale

  1. do_classifier_free_guidance

  • **guidance_scale**:控制 CFG 的强度,影响生成图像与文本提示的相关性。通过设置 guidance_scale>1 启用 CFG。 guidance_scale 越高、生成的图像与文本 “提示” 的相关性越高,但通常图像质量会有所下降。
  • **do_classifier_free_guidance**:布尔参数,用于启用或禁用 CFG。当启用时,模型会根据上面提到的公式进行预测。
第一处代码(
  1. __call__

函数中)

代码中

  1. noise_pred.chunk(2)

这一行将模型的预测结果一分为二,其中

  1. noise_pred_uncond

是无条件预测,

  1. noise_pred_text

是有条件预测。负向提示嵌入的处理使得在使用 CFG 时,模型能生成更符合用户要求的结果。

  1. # perform guidanceif self.do_classifier_free_guidance:
  2. noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
  3. noise_pred = noise_pred_uncond + self.guidance_scale *(noise_pred_text - noise_pred_uncond)
第二处代码(
  1. encode_prompt

函数中)

**

  1. do_classifier_free_guidance

  1. negative_prompt_embeds

:**

  1. do_classifier_free_guidance

为 True 时,且 “负向提示”(negative prompt)的嵌入(

  1. negative_prompt_embeds

)为 None,才会执行对 negative prompt 的处理。

  1. ...if do_classifier_free_guidance and negative_prompt_embeds isNone:
  2. negative_prompt = negative_prompt or""
  3. negative_prompt_2 = negative_prompt_2 or negative_prompt
  4. negative_prompt_3 = negative_prompt_3 or negative_prompt
  5. # normalize str to list
  6. negative_prompt = batch_size *[negative_prompt]ifisinstance(negative_prompt,str)else negative_prompt
  7. negative_prompt_2 =(
  8. batch_size *[negative_prompt_2]ifisinstance(negative_prompt_2,str)else negative_prompt_2
  9. )
  10. negative_prompt_3 =(
  11. batch_size *[negative_prompt_3]ifisinstance(negative_prompt_3,str)else negative_prompt_3
  12. )...

参考文献

  1. guidance_scale (float, optional, defaults to 5.0): Guidance scale as defined in Classifier-Free Diffusion Guidance.
  2. guidance_scale is defined as w of equation 2. of Imagen Paper.

本文转载自: https://blog.csdn.net/weixin_44212848/article/details/141128157
版权归原作者 多恩Stone 所有, 如有侵权,请联系我们删除。

“【diffusers极速入门(七)】Classifier-Free Guidance (CFG)直观理解以及对应代码”的评论:

还没有评论