Buckets:

mishig's picture
|
download
raw
8.04 kB
# ガイド
[Classifier-free guidance](https://huggingface.co/papers/2207.12598)は、プロンプトによりよく合致したモデルの生成を導き、生成品質・制御性・プロンプトへの忠実性を向上させるために広く用いられます。ガイダンス手法にはさまざまな種類があり、Diffusersではこれらを*guiders*と呼びます。ブロックと同様に、パイプラインを書き換えることなく、用途に応じて異なるguiderを切り替えて使用できます。
このガイドでは、guiderの切り替え、guiderパラメータの調整、およびHubへの読み込みと共有の方法について説明します。
## ガイドの切り替え
[ClassifierFreeGuidance](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/guiders#diffusers.ClassifierFreeGuidance)はデフォルトのguiderであり、[init_pipeline()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_blocks#diffusers.ModularPipelineBlocks.init_pipeline)でパイプラインが初期化された際に作成されます。`from_config`によって作成されるため、モジュラーリポジトリから仕様を読み込む必要はありません。guiderは`modular_model_index.json`にリストされません。
[get_component_spec()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.get_component_spec) を使用して、guider を確認します。
```py
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()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components)に渡すことで、別のguiderに切り替えます。
> [!TIP]
> guiderを変更すると、guiderの種類を変更したことを通知するテキストが返されます。
> ```bash
> ModularPipeline.update_components: adding guider with new type: PerturbedAttentionGuidance, previous type: ClassifierFreeGuidance
> ```
```py
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()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.get_component_spec)を再度使用して、guiderのタイプが異なることを確認してください。
```py
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` コンポーネントとして扱われます。
```json
{
"guider": [
null,
null,
{
"repo": "YiYiXu/modular-loader-t2i-guider",
"revision": null,
"subfolder": "pag_guider",
"type_hint": [
"diffusers",
"PerturbedAttentionGuidance"
],
"variant": null
}
]
}
```
[load_components()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.load_components) の呼び出し後、`modular_model_index.json` に記載された読み込み仕様に基づいて、guider が作成されます。
```py
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()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline_components#diffusers.ComponentSpec.create)メソッドと[update_components()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components)で調整できます。以下の例では、`guidance_scale`の値を変更しています。
```py
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()](/docs/diffusers/v0.43.0/ja/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub) メソッドを呼び出して、Hub に共有します。
```py
guider.push_to_hub("YiYiXu/modular-loader-t2i-guider", subfolder="pag_guider")
```
このguiderをpipelineで利用可能にするには、`modular_model_index.json`ファイルを修正するか、[update_components()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components)メソッドを使用してください。
`modular_model_index.json` ファイルを編集し、guider の設定を含むフォルダを指すようにして、guider の読み込み仕様を追加します。
```json
{
"guider": [
"diffusers",
"PerturbedAttentionGuidance",
{
"repo": "YiYiXu/modular-loader-t2i-guider",
"revision": null,
"subfolder": "pag_guider",
"type_hint": [
"diffusers",
"PerturbedAttentionGuidance"
],
"variant": null
}
],
```
`default_creation_method()`を`from_pretrained`に変更し、[update_components()](/docs/diffusers/v0.43.0/ja/api/modular_diffusers/pipeline#diffusers.ModularPipeline.update_components)を使用して、guiderとコンポーネントの仕様、およびパイプラインの設定を更新します。
> [!TIP]
> 作成方法を変更すると、作成タイプを`from_pretrained`に変更したことを示すテキストが返されます。
> ```bash
> ModularPipeline.update_components: changing the default_creation_method of guider from from_config to from_pretrained.
> ```
```py
guider_spec = t2i_pipeline.get_component_spec("guider")
guider_spec.default_creation_method="from_pretrained"
guider_spec.pretrained_model_name_or_path="YiYiXu/modular-loader-t2i-guider"
guider_spec.subfolder="pag_guider"
pag_guider = guider_spec.load()
t2i_pipeline.update_components(guider=pag_guider)
```
パイプラインのデフォルトのguiderにするには、[push_to_hub()](/docs/diffusers/v0.43.0/ja/api/schedulers/overview#diffusers.utils.PushToHubMixin.push_to_hub)を呼び出します。これは任意の手順であり、ローカルでの実験のみを行う場合は不要です。
```py
t2i_pipeline.push_to_hub("YiYiXu/modular-doc-guider")
```

Xet Storage Details

Size:
8.04 kB
·
Xet hash:
1c4532fbb82850fe9c8e53a584762569f561f01617ad54f20fa2477ee611a231

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.