Diffusers documentation

Stable Diffusion 2

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 2

Stable Diffusion 2 adalah model latent diffusion teks-ke-gambar yang dibangun di atas karya Stable Diffusion asli, dan dipimpin oleh Robin Rombach dan Katherine Crowson dari Stability AI dan LAION.

Rilis Stable Diffusion 2.0 mencakup model teks-ke-gambar yang robust, dilatih menggunakan encoder teks baru (OpenCLIP) yang dikembangkan oleh LAION dengan dukungan dari Stability AI, yang sangat meningkatkan kualitas gambar yang dihasilkan dibandingkan rilis V1 sebelumnya. Model teks-ke-gambar dalam rilis ini dapat menghasilkan gambar dengan resolusi default 512x512 piksel dan 768x768 piksel. Model-model ini dilatih pada subset estetika dari dataset LAION-5B yang dibuat oleh tim DeepFloyd di Stability AI, yang kemudian difilter lebih lanjut untuk menghapus konten dewasa menggunakan filter NSFW LAION.

Untuk detail lebih lanjut tentang cara kerja Stable Diffusion 2 dan perbedaannya dengan Stable Diffusion asli, silakan merujuk pada posting pengumuman resmi.

Arsitektur Stable Diffusion 2 lebih kurang identik dengan model Stable Diffusion asli, jadi silakan lihat dokumentasi API-nya untuk cara menggunakan Stable Diffusion 2. Kami merekomendasikan penggunaan DPMSolverMultistepScheduler karena memberikan trade-off kecepatan/kualitas yang wajar dan dapat dijalankan dengan hanya 20 langkah.

Stable Diffusion 2 tersedia untuk tugas seperti teks-ke-gambar, inpainting, super-resolusi, dan kedalaman-ke-gambar:

TugasRepository
teks-ke-gambar (512x512)stabilityai/stable-diffusion-2-base
teks-ke-gambar (768x768)stabilityai/stable-diffusion-2
inpaintingstabilityai/stable-diffusion-2-inpainting
super-resolutionstable-diffusion-x4-upscaler
depth-to-imagestabilityai/stable-diffusion-2-depth

Berikut adalah beberapa contoh cara menggunakan Stable Diffusion 2 untuk setiap tugas:

Pastikan untuk melihat bagian Tips Stable Diffusion untuk mempelajari cara mengeksplorasi tradeoff antara kecepatan dan kualitas scheduler, serta cara menggunakan ulang komponen pipeline secara efisien!

Jika Anda tertarik menggunakan salah satu checkpoint resmi untuk tugas tertentu, jelajahi organisasi Hub CompVis dan Stability AI!

Teks-ke-gambar

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

Inpainting

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)

Super-resolusi

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)

Depth-to-image

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