Buckets:

mishig's picture
|
download
raw
6.69 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/zh/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 图像传入 pipeline 以生成图像。
```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 图像
生成的图像
另请参阅 [T2I-Adapter 训练](../training/t2i_adapters)。
### MultiAdapter
使用 `MultiAdapter` 组合多个控制(例如 canny 和 depth)。将适配器列表传入 `MultiAdapter`,然后将对应的控制图像以列表形式传入 pipeline。
```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/zh/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 图像
深度图
生成的图像
## 文本反转
[Textual Inversion](https://huggingface.co/papers/2208.01618) 通过微调绑定到唯一标记(`<sks>`)的词嵌入,使模型从 3-5 张图片中学习特定概念。之后,你可以在提示词中使用该标记来生成该概念(例如,像素艺术)。
Textual Inversion 权重通常只有几 KB,因为它们仅包含词嵌入。请在调用 [from_pretrained()](/docs/diffusers/v0.43.0/zh/api/pipelines/overview#diffusers.DiffusionPipeline.from_pretrained) 之后,使用 [load_textual_inversion()](/docs/diffusers/v0.43.0/zh/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:
6.69 kB
·
Xet hash:
cde6d6a34a35ea9bca7bcbc85c6b987823ec2351e2f4f1f469bba56915179336

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