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

レガシーアダプター

これらのメソッドは引き続きサポートされています。特に、Stable Diffusion系のチェックポイントでは使用できます。新しい開発では、LoRA、IP-Adapter、ControlNetの使用を推奨します。

T2I-Adapter

T2I-Adapterは、制御可能な生成のためのアダプターです。制御信号(深度マップやCannyエッジなど)から事前学習済みモデルの内部特徴量へのマッピングを学習し、生成時にそのガイダンスを追加します。ControlNetと同様の役割を持ちますが、より軽量なプラグイン型アダプターです。

制御条件付きアダプターを読み込み、from_pretrained() でパイプラインを読み込んだ後、adapter= を通して渡します。

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を使用してCannyエッジ画像を生成します。

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画像をパイプラインに渡して、画像を生成します。

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]
Original image
元の画像
Control image (Canny edges)
canny image
Generated image (T2I-Adapter + prompt)
生成された画像

また、T2I-Adapterの学習も参照してください。

MultiAdapter

複数のコントロール(canny や depth など)をMultiAdapterで組み合わせます。MultiAdapterにアダプターのリストを渡し、その後、対応するコントロール画像をリストとしてパイプラインに渡します。

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に渡します。各コントロールに割り当てる重みを決定するには、adapter_conditioning_scaleパラメーターを使用します。

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]
Control image (Canny edges)
canny image
Control image (depth map)
深度マップ
Generated image (MultiAdapter + prompt)
生成された画像

Textual Inversion

Textual Inversionは、3〜5枚の画像から得られる概念にモデルを適応させるために、一意のトークン(<sks>)に紐付けられた単語埋め込みをファインチューニングします。その後、プロンプト内でそのトークンを使用することで、その概念(例えば、ピクセルアート)を生成できます。

Textual Inversionの重みは、単語埋め込みのみであるため、通常は数KB程度です。from_pretrained()の後にload_textual_inversion()で読み込み、プロンプトに固有トークンを含めることで生成をトリガーします。

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("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]

埋め込みの学習については、テキストインバージョンの学習を参照してください。

Textual Inversionは、ネガティブ埋め込みを学習して生成を「ぼやけ」や「醜さ」などの望ましくない特徴から遠ざけるように制御することもでき、画像品質の向上に役立ちます。

EasyNegative は、複数の学習済みネガティブ概念を含む、広く使用されているネガティブ埋め込みです。ネガティブ埋め込みを読み込み、ネガティブ埋め込みに関連付けられたファイル名とトークンを指定します。このトークンをパイプラインの negative_prompt に渡すことで、これを有効にします。

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]
Update on GitHub