Diffusers documentation

Stable Diffusion 2

Hugging Face's logo
Join the Hugging Face community

and get access to the augmented documentation experience

to get started

Stable Diffusion 2

Stable Diffusion 2 هو نموذج انتشار كامن من النص إلى الصورة مبني على عمل Stable Diffusion الأصلي، وقد قاده روبن رومباخ وكاثيرين كراوسون من Stability AI وLAION.

يتضمن إصدار Stable Diffusion 2.0 نماذج قوية لتحويل النص إلى صورة، تم تدريبها باستخدام مُشفِّر نصوص جديد كليًا (OpenCLIP)، طوّرته LAION بدعم من Stability AI، مما يحسّن بشكل كبير جودة الصور المولّدة مقارنةً بإصدارات V1 السابقة. يمكن لنماذج تحويل النص إلى صورة في هذا الإصدار توليد صور بدقة افتراضية تبلغ 512x512 بكسل و768x768 بكسل. تم تدريب هذه النماذج على مجموعة فرعية جمالية من مجموعة بيانات LAION-5B التي أنشأها فريق DeepFloyd في Stability AI، ثم تم تصفيتها بشكل إضافي لإزالة المحتوى البالغ باستخدام مرشح NSFW الخاص بـ LAION.

لمزيد من التفاصيل حول كيفية عمل Stable Diffusion 2 وكيف يختلف عن Stable Diffusion الأصلي، يُرجى الرجوع إلى منشور الإعلان الرسمي.

تُشبه بنية Stable Diffusion 2 إلى حد كبير بنية نموذج Stable Diffusion الأصلي، لذا راجع وثائق واجهة برمجياته لمعرفة كيفية استخدام Stable Diffusion 2. ننصح باستخدام DPMSolverMultistepScheduler لأنه يوفر توازناً معقولاً بين السرعة والجودة ويمكن تشغيله بعدد قليل من الخطوات يبلغ 20 خطوة فقط.

يتوفر Stable Diffusion 2 لمهام مثل النص إلى صورة، والتعبئة (inpainting)، والدقة الفائقة (super-resolution)، والعمق إلى صورة:

المهمةالمستودع
نص إلى صورة (512x512)stabilityai/stable-diffusion-2-base
نص إلى صورة (768x768)stabilityai/stable-diffusion-2
التلوينstabilityai/stable-diffusion-2-inpainting
دقة فائقةstable-diffusion-x4-upscaler
depth-to-imagestabilityai/stable-diffusion-2-depth

فيما يلي بعض الأمثلة على كيفية استخدام Stable Diffusion 2 لكل مهمة:

تأكد من الاطلاع على قسم نصائح الخاصة بـ Stable Diffusion لمعرفة كيفية استكشاف التوازن بين سرعة المجدول وجودة النتائج، وكيفية إعادة استخدام مكونات خط الأنابيب بكفاءة!

إذا كنت مهتمًا باستخدام أحد نقاط التفتيش الرسمية لمهمة معينة، فاستكشف منظمات CompVis وStability AI على Hub!

النص إلى صورة

from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import torch

repo_id = "stabilityai/stable-diffusion-2-base"
pipe = DiffusionPipeline.from_pretrained(repo_id, dtype=torch.float16, variant="fp16")

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"

prompt = "High quality photo of an astronaut riding a horse in space"
image = pipe(prompt, num_inference_steps=25).images[0]
image

الترميم

import torch
from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
from diffusers.utils import load_image, make_image_grid

img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"

init_image = load_image(img_url).resize((512, 512))
mask_image = load_image(mask_url).resize((512, 512))

repo_id = "stabilityai/stable-diffusion-2-inpainting"
pipe = DiffusionPipeline.from_pretrained(repo_id, dtype=torch.float16, variant="fp16")

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")  # or "mps", "xpu", "cpu"

prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image, num_inference_steps=25).images[0]
make_image_grid([init_image, mask_image, image], rows=1, cols=3)

الرفع الدقيق

from diffusers import StableDiffusionUpscalePipeline
from diffusers.utils import load_image, make_image_grid
import torch

# load model and scheduler
model_id = "stabilityai/stable-diffusion-x4-upscaler"
pipeline = StableDiffusionUpscalePipeline.from_pretrained(model_id, dtype=torch.float16)
pipeline = pipeline.to("cuda")  # or "mps", "xpu", "cpu"

# let's download an  image
url = "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd2-upscale/low_res_cat.png"
low_res_img = load_image(url)
low_res_img = low_res_img.resize((128, 128))
prompt = "a white cat"
upscaled_image = pipeline(prompt=prompt, image=low_res_img).images[0]
make_image_grid([low_res_img.resize((512, 512)), upscaled_image.resize((512, 512))], rows=1, cols=2)

من العمق إلى الصورة

import torch
from diffusers import StableDiffusionDepth2ImgPipeline
from diffusers.utils import load_image, make_image_grid

pipe = StableDiffusionDepth2ImgPipeline.from_pretrained(
    "stabilityai/stable-diffusion-2-depth",
    dtype=torch.float16,
).to("cuda")  # or "mps", "xpu", "cpu"


url = "http://images.cocodataset.org/val2017/000000039769.jpg"
init_image = load_image(url)
prompt = "two tigers"
negative_prompt = "bad, deformed, ugly, bad anotomy"
image = pipe(prompt=prompt, image=init_image, negative_prompt=negative_prompt, strength=0.7).images[0]
make_image_grid([init_image, image], rows=1, cols=2)
Update on GitHub