Diffusers documentation

Bộ thích ứng cũ

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

Bộ thích ứng cũ

Các phương pháp này vẫn được hỗ trợ, đặc biệt là trên các checkpoint thuộc họ Stable Diffusion. Đối với các công việc mới, hãy ưu tiên sử dụng LoRA, IP-Adapter và ControlNet.

T2I-Adapter

T2I-Adapter là một adapter cho việc tạo ra có kiểm soát. Nó học một ánh xạ từ tín hiệu điều khiển (ví dụ, bản đồ độ sâu hoặc cạnh canny) vào các đặc trưng nội bộ của mô hình được huấn luyện trước, và thêm sự dẫn dắt đó trong quá trình tạo ra. Nó có vai trò tương tự như ControlNet, nhưng là một adapter gắn thêm nhẹ hơn.

Tải một adapter có điều khiển, sau đó tải pipeline bằng from_pretrained() và truyền nó với adapter=.

import torch
from diffusers import T2IAdapter, StableDiffusionXLAdapterPipeline, AutoencoderKL

t2i_adapter = T2IAdapter.from_pretrained(
    "TencentARC/t2i-adapter-canny-sdxl-1.0",
    dtype=torch.float16,
)

Tạo ảnh canny bằng opencv-python.

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)

Truyền ảnh canny vào pipeline để tạo ra một ảnh.

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
ảnh gốc
Control image (Canny edges)
ảnh canny
Generated image (T2I-Adapter + prompt)
hình ảnh được tạo

Xem thêm đào tạo T2I-Adapter.

MultiAdapter

Kết hợp nhiều điều khiển (ví dụ: canny và depth) bằng MultiAdapter. Truyền một danh sách các adapter vào MultiAdapter, sau đó truyền các hình ảnh điều khiển tương ứng dưới dạng danh sách vào pipeline.

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),
    ]
)

Truyền các adapter, prompt và ảnh điều khiển vào StableDiffusionXLAdapterPipeline. Sử dụng tham số adapter_conditioning_scale để xác định mức độ ưu tiên gán cho từng điều khiển.

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)
ảnh canny
Control image (depth map)
bản đồ độ sâu
Generated image (MultiAdapter + prompt)
hình ảnh được tạo

Textual Inversion

Textual Inversion cá nhân hóa một mô hình cho một khái niệm từ 3-5 hình ảnh bằng cách tinh chỉnh các word embedding gắn với một token duy nhất (<sks>). Sau đó, bạn có thể sử dụng token đó trong prompt của mình để tạo ra khái niệm (ví dụ: pixel art).

Trọng số của Textual Inversion thường chỉ vài KB vì chúng chỉ là các word embedding. Hãy tải chúng sau from_pretrained() bằng load_textual_inversion(), và bao gồm token duy nhất trong prompt để kích hoạt quá trình sinh.

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]

Để huấn luyện embeddings, xem Train textual inversion.

Textual Inversion cũng có thể được huấn luyện để học negative embeddings nhằm điều hướng quá trình tạo ảnh tránh xa các đặc điểm không mong muốn như “mờ” hoặc “xấu”, giúp cải thiện chất lượng hình ảnh.

EasyNegative là một embedding âm được sử dụng rộng rãi, chứa nhiều khái niệm âm đã được học. Hãy tải các embedding âm và chỉ định tên tệp cùng token liên quan đến các embedding âm. Truyền token này vào negative_prompt trong pipeline của bạn để kích hoạt nó.

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