# Stable Diffusion 2

Stable Diffusion 2 adalah model _latent diffusion_ teks-ke-gambar yang dibangun di atas karya [Stable Diffusion](https://stability.ai/blog/stable-diffusion-public-release) asli, dan dipimpin oleh Robin Rombach dan Katherine Crowson dari [Stability AI](https://stability.ai/) dan [LAION](https://laion.ai/).

*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](https://laion.ai/blog/laion-5b/) yang dibuat oleh tim DeepFloyd di Stability AI, yang kemudian difilter lebih lanjut untuk menghapus konten dewasa menggunakan [filter NSFW LAION](https://openreview.net/forum?id=M3Y74vmsMcY).*

Untuk detail lebih lanjut tentang cara kerja Stable Diffusion 2 dan perbedaannya dengan Stable Diffusion asli, silakan merujuk pada [posting pengumuman](https://stability.ai/blog/stable-diffusion-v2-release) resmi.

Arsitektur Stable Diffusion 2 lebih kurang identik dengan model [Stable Diffusion](./text2img) asli, jadi silakan lihat dokumentasi API-nya untuk cara menggunakan Stable Diffusion 2. Kami merekomendasikan penggunaan [DPMSolverMultistepScheduler](/docs/diffusers/main/id/api/schedulers/multistep_dpm_solver#diffusers.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:

| Tugas                    | Repository                                                                                                    |
|-------------------------|---------------------------------------------------------------------------------------------------------------|
| teks-ke-gambar (512x512) | [sd2-community/stable-diffusion-2-base](https://huggingface.co/sd2-community/stable-diffusion-2-base)             |
| teks-ke-gambar (768x768) | [sd2-community/stable-diffusion-2](https://huggingface.co/sd2-community/stable-diffusion-2)                       |
| inpainting              | [sd2-community/stable-diffusion-2-inpainting](https://huggingface.co/sd2-community/stable-diffusion-2-inpainting) |
| super-resolution        | [stable-diffusion-x4-upscaler](https://huggingface.co/stabilityai/stable-diffusion-x4-upscaler)               |
| depth-to-image          | [sd2-community/stable-diffusion-2-depth](https://huggingface.co/sd2-community/stable-diffusion-2-depth)           |

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

> [!TIP]
> Pastikan untuk melihat bagian [Tips](overview#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](https://huggingface.co/CompVis) dan [Stability AI](https://huggingface.co/stabilityai)!

## Teks-ke-gambar

```py
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

```py
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

```py
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

```py
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)
```

