Diffusers documentation
引导器
引导器
Classifier-free guidance 引导模型生成更好地匹配提示,通常用于提高生成质量、控制和提示的遵循度。有不同类型的引导方法,在 Diffusers 中,它们被称为引导器。与块类似,可以轻松切换和使用不同的引导器以适应不同的用例,而无需重写管道。
本指南将向您展示如何切换引导器、调整引导器参数,以及将它们加载并共享到 Hub。
切换引导器
ClassifierFreeGuidance
是默认引导器,在使用 init_pipeline()
初始化管道时创建。它通过 from_config
创建,这意味着它不需要从模块化存储库加载规范。引导器不会列在 modular_model_index.json
中。
使用 get_component_spec()
来检查引导器。
t2i_pipeline.get_component_spec("guider")
ComponentSpec(name='guider', type_hint=<class 'diffusers.guiders.classifier_free_guidance.ClassifierFreeGuidance'>, description=None, config=FrozenDict([('guidance_scale', 7.5), ('guidance_rescale', 0.0), ('use_original_formulation', False), ('start', 0.0), ('stop', 1.0), ('_use_default_values', ['start', 'guidance_rescale', 'stop', 'use_original_formulation'])]), repo=None, subfolder=None, variant=None, revision=None, default_creation_method='from_config')
通过将新引导器传递给 update_components()
来切换到不同的引导器。
更改引导器将返回文本,让您知道您正在更改引导器类型。
ModularPipeline.update_components: 添加具有新类型的引导器: PerturbedAttentionGuidance, 先前类型: ClassifierFreeGuidance
from diffusers import LayerSkipConfig, PerturbedAttentionGuidance
config = LayerSkipConfig(indices=[2, 9], fqn="mid_block.attentions.0.transformer_blocks", skip_attention=False, skip_attention_scores=True, skip_ff=False)
guider = PerturbedAttentionGuidance(
guidance_scale=5.0, perturbed_guidance_scale=2.5, perturbed_guidance_config=config
)
t2i_pipeline.update_components(guider=guider)
再次使用 get_component_spec()
来验证引导器类型是否不同。
t2i_pipeline.get_component_spec("guider")
ComponentSpec(name='guider', type_hint=<class 'diffusers.guiders.perturbed_attention_guidance.PerturbedAttentionGuidance'>, description=None, config=FrozenDict([('guidance_scale', 5.0), ('perturbed_guidance_scale', 2.5), ('perturbed_guidance_start', 0.01), ('perturbed_guidance_stop', 0.2), ('perturbed_guidance_layers', None), ('perturbed_guidance_config', LayerSkipConfig(indices=[2, 9], fqn='mid_block.attentions.0.transformer_blocks', skip_attention=False, skip_attention_scores=True, skip_ff=False, dropout=1.0)), ('guidance_rescale', 0.0), ('use_original_formulation', False), ('start', 0.0), ('stop', 1.0), ('_use_default_values', ['perturbed_guidance_start', 'use_original_formulation', 'perturbed_guidance_layers', 'stop', 'start', 'guidance_rescale', 'perturbed_guidance_stop']), ('_class_name', 'PerturbedAttentionGuidance'), ('_diffusers_version', '0.35.0.dev0')]), repo=None, subfolder=None, variant=None, revision=None, default_creation_method='from_config')
加载自定义引导器
已经在 Hub 上保存并带有 modular_model_index.json
文件的引导器现在被视为 from_pretrained
组件,而不是 from_config
组件。
{
"guider": [
null,
null,
{
"repo": "YiYiXu/modular-loader-t2i-guider",
"revision": null,
"subfolder": "pag_guider",
"type_hint": [
"diffusers",
"PerturbedAttentionGuidance"
],
"variant": null
}
]
}
引导器只有在调用 load_default_components()
之后才会创建,基于 modular_model_index.json
中的加载规范。
t2i_pipeline = t2i_blocks.init_pipeline("YiYiXu/modular-doc-guider")
# 在初始化时未创建
assert t2i_pipeline.guider is None
t2i_pipeline.load_default_components()
# 加载为 PAG 引导器
t2i_pipeline.guider
更改引导器参数
引导器参数可以通过 create()
方法或 update_components()
方法进行调整。下面的示例更改了 guidance_scale
值。
guider_spec = t2i_pipeline.get_component_spec("guider")
guider = guider_spec.create(guidance_scale=10)
t2i_pipeline.update_components(guider=guider)
上传自定义引导器
在自定义引导器上调用 push_to_hub()
方法,将其分享到 Hub。
guider.push_to_hub("YiYiXu/modular-loader-t2i-guider", subfolder="pag_guider")
要使此引导器可用于管道,可以修改 modular_model_index.json
文件或使用 update_components()
方法。
编辑 modular_model_index.json
文件,并添加引导器的加载规范,指向包含引导器配置的文件夹
例如。
{
"guider": [
"diffusers",
"PerturbedAttentionGuidance",
{
"repo": "YiYiXu/modular-loader-t2i-guider",
"revision": null,
"subfolder": "pag_guider",
"type_hint": [
"diffusers",
"PerturbedAttentionGuidance"
],
"variant": null
}
],