Buckets:

mishig's picture
|
download
raw
7.64 kB
# レガシーアダプター
これらのメソッドは引き続きサポートされています。特に、Stable Diffusion系のチェックポイントでは使用できます。新しい開発では、[LoRA](../tutorials/using_peft_for_inference)、[IP-Adapter](./ip_adapter)、[ControlNet](./controlnet)の使用を推奨します。
## T2I-Adapter
[T2I-Adapter](https://huggingface.co/papers/2302.08453)は、制御可能な生成のためのアダプターです。制御信号(深度マップやCannyエッジなど)から事前学習済みモデルの内部特徴量へのマッピングを学習し、生成時にそのガイダンスを追加します。[ControlNet](./controlnet)と同様の役割を持ちますが、より軽量なプラグイン型アダプターです。
制御条件付きアダプターを読み込み、[from_pretrained()](/docs/diffusers/v0.43.0/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) でパイプラインを読み込んだ後、`adapter=` を通して渡します。
```py
import torch
from diffusers import T2IAdapter, StableDiffusionXLAdapterPipeline, AutoencoderKL
t2i_adapter = T2IAdapter.from_pretrained(
"TencentARC/t2i-adapter-canny-sdxl-1.0",
dtype=torch.float16,
)
```
[opencv-python](https://github.com/opencv/opencv-python)を使用してCannyエッジ画像を生成します。
```py
import cv2
import numpy as np
from PIL import Image
from diffusers.utils import load_image
original_image = load_image(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/non-enhanced-prompt.png"
)
image = np.array(original_image)
low_threshold = 100
high_threshold = 200
image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
canny_image = Image.fromarray(image)
```
Canny画像をパイプラインに渡して、画像を生成します。
```py
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", dtype=torch.float16)
pipeline = StableDiffusionXLAdapterPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
adapter=t2i_adapter,
vae=vae,
dtype=torch.float16,
).to("cuda") # or "mps", "xpu", "cpu"
prompt = """
A photorealistic overhead image of a cat reclining sideways in a flamingo pool floatie holding a margarita.
The cat is floating leisurely in the pool and completely relaxed and happy.
"""
pipeline(
prompt,
image=canny_image,
num_inference_steps=100,
guidance_scale=10,
).images[0]
```
元の画像
canny image
生成された画像
また、[T2I-Adapterの学習](../training/t2i_adapters)も参照してください。
### MultiAdapter
複数のコントロール(canny や depth など)を`MultiAdapter`で組み合わせます。`MultiAdapter`にアダプターのリストを渡し、その後、対応するコントロール画像をリストとしてパイプラインに渡します。
```py
import torch
from diffusers.utils import load_image
from diffusers import StableDiffusionXLAdapterPipeline, AutoencoderKL, MultiAdapter, T2IAdapter
canny_image = load_image(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/canny-cat.png"
)
depth_image = load_image(
"https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/sdxl_depth_image.png"
)
controls = [canny_image, depth_image]
prompt = ["""
a relaxed rabbit sitting on a striped towel next to a pool with a tropical drink nearby,
bright sunny day, vacation scene, 35mm photograph, film, professional, 4k, highly detailed
"""]
adapters = MultiAdapter(
[
T2IAdapter.from_pretrained("TencentARC/t2i-adapter-canny-sdxl-1.0", dtype=torch.float16),
T2IAdapter.from_pretrained("TencentARC/t2i-adapter-depth-midas-sdxl-1.0", dtype=torch.float16),
]
)
```
アダプター、プロンプト、コントロール画像を[StableDiffusionXLAdapterPipeline](/docs/diffusers/v0.43.0/ja/api/pipelines/stable_diffusion/adapter#diffusers.StableDiffusionXLAdapterPipeline)に渡します。各コントロールに割り当てる重みを決定するには、`adapter_conditioning_scale`パラメーターを使用します。
```py
vae = AutoencoderKL.from_pretrained("madebyollin/sdxl-vae-fp16-fix", dtype=torch.float16)
pipeline = StableDiffusionXLAdapterPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-base-1.0",
dtype=torch.float16,
vae=vae,
adapter=adapters,
).to("cuda") # or "mps", "xpu", "cpu"
pipeline(
prompt,
image=controls,
height=1024,
width=1024,
adapter_conditioning_scale=[0.7, 0.7]
).images[0]
```
canny image
深度マップ
生成された画像
## Textual Inversion
[Textual Inversion](https://huggingface.co/papers/2208.01618)は、3〜5枚の画像から得られる概念にモデルを適応させるために、一意のトークン(`<sks>`)に紐付けられた単語埋め込みをファインチューニングします。その後、プロンプト内でそのトークンを使用することで、その概念(例えば、ピクセルアート)を生成できます。
Textual Inversionの重みは、単語埋め込みのみであるため、通常は数KB程度です。[from_pretrained()](/docs/diffusers/v0.43.0/ja/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained)の後に[load_textual_inversion()](/docs/diffusers/v0.43.0/ja/api/loaders/textual_inversion#diffusers.loaders.TextualInversionLoaderMixin.load_textual_inversion)で読み込み、プロンプトに固有トークンを含めることで生成をトリガーします。
```py
import torch
from diffusers import AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5",
dtype=torch.float16
).to("cuda") # or "mps", "xpu", "cpu"
```
```py
pipeline.load_textual_inversion("sd-concepts-library/gta5-artwork")
prompt = "A cute brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration, <gta5-artwork> style"
pipeline(prompt).images[0]
```
埋め込みの学習については、[テキストインバージョンの学習](../training/text_inversion)を参照してください。
Textual Inversion は、*ネガティブ埋め込み*を学習して「ぼやけた」や「醜い」などの望ましくない特徴から生成を遠ざけるようにすることもでき、画像品質の向上に役立ちます。
EasyNegative は、複数の学習済みネガティブ概念を含む、広く使用されているネガティブ埋め込みです。ネガティブ埋め込みを読み込み、ネガティブ埋め込みに関連付けられたファイル名とトークンを指定します。このトークンをパイプラインの `negative_prompt` に渡すことで、これを有効にします。
```py
import torch
from diffusers import AutoPipelineForText2Image
pipeline = AutoPipelineForText2Image.from_pretrained(
"stable-diffusion-v1-5/stable-diffusion-v1-5",
dtype=torch.float16
).to("cuda") # or "mps", "xpu", "cpu"
pipeline.load_textual_inversion(
"EvilEngine/easynegative",
weight_name="easynegative.safetensors",
token="easynegative"
)
prompt = "A cute brown bear eating a slice of pizza, stunning color scheme, masterpiece, illustration"
negative_prompt = "easynegative"
pipeline(prompt, negative_prompt=negative_prompt).images[0]
```

Xet Storage Details

Size:
7.64 kB
·
Xet hash:
1d82d79b6cfb9d744261d703c78d4d61f0778612f710dda74ef9428b9a93ac3f

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