Diffusers documentation
المُكيّفات القديمة
المُكيّفات القديمة
لا تزال هذه الأساليب مدعومة، لا سيما في نقاط التفتيش من عائلة 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.
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)مرّر صورة كانّي إلى خط الأنابيب لتوليد صورة.
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]
انظر أيضًا إلى تدريب 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),
]
)مرّر المُكيّفات (adapters) والموجّه (prompt) وصور التحكم إلى 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]
Textual Inversion
Textual Inversion يُخصّص النموذج لمفهوم من 3 إلى 5 صور من خلال ضبط دقيق لتضمينات الكلمات المرتبطة برمز فريد (<sks>). يمكنك بعد ذلك استخدام ذلك الرمز في طلبك لتوليد المفهوم (على سبيل المثال، فن البكسل).
عادةً ما تكون أوزان Textual Inversion بضعة كيلوبايتات فقط لأنها عبارة عن تمثيلات كلمات (word embeddings) فقط. حمّلها بعد from_pretrained() باستخدام load_textual_inversion()، وشمل الرمز الفريد (unique token) في الأمر (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("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 can also be trained to learn negative embeddings to steer generation away from unwanted characteristics such as “blurry” or “ugly”, making it useful for improving image quality.
يعتبر 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]