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

68 篇文章 8 订阅
33 篇文章 1 订阅

系列文章目录


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


前言

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

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

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

做法:

  1. 无条件生成:模型首先生成一个“无条件”(unconditioned)的预测,即在没有任何文本提示或条件的情况下的生成。
  2. 有条件生成:模型再生成一个“有条件”的预测,即在给定文本提示(prompt)的情况下的生成。
  3. 合成结果:最终的生成结果通过将无条件和有条件的预测组合来实现:

最终生成 = 无条件预测 + w × ( 有条件预测 − 无条件预测 ) \text{最终生成} = \text{无条件预测} + w \times (\text{有条件预测} - \text{无条件预测}) 最终生成=无条件预测+w×(有条件预测无条件预测)

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

作用:

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

二、对应 diffusers 代码

/path/to/diffusers/pipelines/stable_diffusion_3/pipeline_stable_diffusion_3.py 的代码为例, CFG 主要相关的参数是 guidance_scaledo_classifier_free_guidance

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

代码中 noise_pred.chunk(2) 这一行将模型的预测结果一分为二,其中 noise_pred_uncond 是无条件预测,noise_pred_text 是有条件预测。负向提示嵌入的处理使得在使用 CFG 时,模型能生成更符合用户要求的结果。

  # perform guidance
  if self.do_classifier_free_guidance:
      noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
      noise_pred = noise_pred_uncond + self.guidance_scale * (noise_pred_text - noise_pred_uncond)
第二处代码(encode_prompt 函数中)

do_classifier_free_guidancenegative_prompt_embeds

do_classifier_free_guidance 为 True 时,且 “负向提示”(negative prompt)的嵌入(negative_prompt_embeds)为 None,才会执行对 negative prompt 的处理。

... 
if do_classifier_free_guidance and negative_prompt_embeds is None:
            negative_prompt = negative_prompt or ""
            negative_prompt_2 = negative_prompt_2 or negative_prompt
            negative_prompt_3 = negative_prompt_3 or negative_prompt

            # normalize str to list
            negative_prompt = batch_size * [negative_prompt] if isinstance(negative_prompt, str) else negative_prompt
            negative_prompt_2 = (
                batch_size * [negative_prompt_2] if isinstance(negative_prompt_2, str) else negative_prompt_2
            )
            negative_prompt_3 = (
                batch_size * [negative_prompt_3] if isinstance(negative_prompt_3, str) else negative_prompt_3
            )
...

参考文献

  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.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值