# Stable Diffusion 2

Stable Diffusion 2は、元の[Stable Diffusion](https://stability.ai/blog/stable-diffusion-public-release)の研究を基盤として構築された、テキストから画像を生成する_潜在拡散_モデルです。[Stability AI](https://stability.ai/)のRobin Rombach氏と[LAION](https://laion.ai/)のKatherine Crowson氏が主導しました。

*Stable Diffusion 2.0 のリリースには、LAION が Stability AI の支援を得て開発した新しいテキストエンコーダー（OpenCLIP）を使用して学習された、堅牢なテキストから画像への生成モデルが含まれています。これにより、以前の V1 リリースと比較して、生成される画像の品質が大幅に向上しています。このリリースのテキストから画像への生成モデルは、デフォルトの解像度として 512x512 ピクセルと 768x768 ピクセルの両方で画像を生成できます。これらのモデルは、Stability AI の DeepFloyd チームが作成した [LAION-5B データセット](https://laion.ai/blog/laion-5b/) の美学的なサブセットで学習されており、その後 [LAION の NSFW フィルター](https://openreview.net/forum?id=M3Y74vmsMcY) を使用して成人向けコンテンツが除去されるようさらにフィルタリングされています。*

Stable Diffusion 2 の動作原理や、元の Stable Diffusion との違いに関する詳細については、公式の[アナウンスメント](https://stability.ai/blog/stable-diffusion-v2-release)を参照してください。

Stable Diffusion 2 のアーキテクチャは、元の [Stable Diffusion モデル](./text2img) とほぼ同一です。Stable Diffusion 2 の使用方法については、その API ドキュメントを参照してください。[DPMSolverMultistepScheduler](/docs/diffusers/main/ja/api/schedulers/multistep_dpm_solver#diffusers.DPMSolverMultistepScheduler) の使用を推奨します。これは速度と品質のバランスが適切であり、わずか 20 ステップで実行できます。

Stable Diffusion 2 は、テキストから画像生成、インペインティング、スーパーレゾリューション、深度から画像生成などのタスクで利用できます:

| タスク                    | リポジトリ                                                                                                    |
|-------------------------|---------------------------------------------------------------------------------------------------------------|
| テキストから画像への変換 (512x512) | [stabilityai/stable-diffusion-2-base](https://huggingface.co/stabilityai/stable-diffusion-2-base)             |
| テキストから画像への変換 (768x768) | [stabilityai/stable-diffusion-2](https://huggingface.co/stabilityai/stable-diffusion-2)                       |
| inpainting              | [stabilityai/stable-diffusion-2-inpainting](https://huggingface.co/stabilityai/stable-diffusion-2-inpainting) |
| 超解像        | [stable-diffusion-x4-upscaler](https://huggingface.co/stabilityai/stable-diffusion-x4-upscaler)               |
| 深度から画像          | [stabilityai/stable-diffusion-2-depth](https://huggingface.co/stabilityai/stable-diffusion-2-depth)           |

各タスクにおけるStable Diffusion 2の使用方法の例を以下に示します。

> [!TIP]
> スケジューラーの速度と品質のトレードオフをどのように探索するか、またパイプラインのコンポーネントを効率的に再利用する方法を学ぶために、Stable Diffusionの[Tips](overview#tips)セクションをぜひご確認ください。
>
> タスクに公式チェックポイントのいずれかを使用する場合は、[CompVis](https://huggingface.co/CompVis) と [Stability AI](https://huggingface.co/stabilityai) の Hub 組織を探索してください!

## テキストから画像へ

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

## インペインティング

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

## 超解像

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

## 深度から画像へ

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

