Diffusers documentation

ガイド

You are viewing main version, which requires installation from source. If you'd like regular pip install, checkout the latest stable version (v0.42.1).
Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

ガイド

Classifier-free guidanceは、プロンプトによりよく合致したモデルの生成を導き、生成品質・制御性・プロンプトへの忠実性を向上させるために広く用いられます。ガイダンス手法にはさまざまな種類があり、Diffusersではこれらをguidersと呼びます。ブロックと同様に、パイプラインを書き換えることなく、用途に応じて異なるguiderを切り替えて使用できます。

このガイドでは、guiderの切り替え、guiderパラメータの調整、およびHubへの読み込みと共有の方法について説明します。

ガイドの切り替え

ClassifierFreeGuidanceはデフォルトのguiderであり、init_pipeline()でパイプラインが初期化された際に作成されます。from_configによって作成されるため、モジュラーリポジトリから仕様を読み込む必要はありません。guiderはmodular_model_index.jsonにリストされません。

get_component_spec() を使用して、guider を確認します。

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')

新しいguiderをupdate_components()に渡すことで、別のguiderに切り替えます。

guiderを変更すると、guiderの種類を変更したことを通知するテキストが返されます。

ModularPipeline.update_components: adding guider with new type: PerturbedAttentionGuidance, previous type: 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()を再度使用して、guiderのタイプが異なることを確認してください。

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')

カスタムガイドラーの読み込み

modular_model_index.json ファイルが保存されている Hub 上の Guider は、from_config コンポーネントではなく、from_pretrained コンポーネントとして扱われます。

{
  "guider": [
    null,
    null,
    {
      "repo": "YiYiXu/modular-loader-t2i-guider",
      "revision": null,
      "subfolder": "pag_guider",
      "type_hint": [
        "diffusers",
        "PerturbedAttentionGuidance"
      ],
      "variant": null
    }
  ]
}

load_components() の呼び出し後、modular_model_index.json に記載された読み込み仕様に基づいて、guider が作成されます。

t2i_pipeline = t2i_blocks.init_pipeline("YiYiXu/modular-doc-guider")
# not created during init
assert t2i_pipeline.guider is None
t2i_pipeline.load_components()
# loaded as PAG guider
t2i_pipeline.guider

guiderパラメータの変更

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)

カスタムガイドラーのアップロード

カスタム guider の push_to_hub() メソッドを呼び出して、Hub に共有します。

guider.push_to_hub("YiYiXu/modular-loader-t2i-guider", subfolder="pag_guider")

このguiderをpipelineで利用可能にするには、modular_model_index.jsonファイルを修正するか、update_components()メソッドを使用してください。

modular_model_index.json
update_components

modular_model_index.json ファイルを編集し、guider の設定を含むフォルダを指すようにして、guider の読み込み仕様を追加します。

{
  "guider": [
    "diffusers",
    "PerturbedAttentionGuidance",
    {
      "repo": "YiYiXu/modular-loader-t2i-guider",
      "revision": null,
      "subfolder": "pag_guider",
      "type_hint": [
        "diffusers",
        "PerturbedAttentionGuidance"
      ],
      "variant": null
    }
  ],
Update on GitHub