Diffusers documentation
Stable Diffusion 2
Stable Diffusion 2
Stable Diffusion 2は、元のStable Diffusionの研究を基盤として構築された、テキストから画像を生成する潜在拡散モデルです。Stability AIのRobin Rombach氏とLAIONのKatherine Crowson氏が主導しました。
Stable Diffusion 2.0 のリリースには、LAION が Stability AI の支援を得て開発した新しいテキストエンコーダー(OpenCLIP)を使用して学習された、堅牢なテキストから画像生成モデルが含まれています。これにより、以前の V1 リリースと比較して、生成される画像の品質が大幅に向上しています。このリリースのテキストから画像生成モデルは、デフォルトの解像度として 512x512 ピクセルと 768x768 ピクセルの両方で画像を生成できます。 これらのモデルは、Stability AI の DeepFloyd チームが作成した LAION-5B データセット の美学的なサブセットで学習されており、その後 LAION の NSFW フィルター を使用して成人向けコンテンツを除去するためにさらにフィルタリングされています。
Stable Diffusion 2 の動作原理や、元の Stable Diffusion との違いに関する詳細については、公式のアナウンスメントを参照してください。
Stable Diffusion 2 のアーキテクチャは、元の Stable Diffusion モデル とほぼ同一です。Stable Diffusion 2 の使用方法については、その API ドキュメントを参照してください。DPMSolverMultistepScheduler の使用を推奨します。これは速度と品質のバランスが適切であり、わずか 20 ステップで実行できます。
Stable Diffusion 2 は、テキストから画像生成、インペインティング、スーパーレゾリューション、深度から画像生成などのタスクで利用できます:
| タスク | リポジトリ |
|---|---|
| テキストから画像への変換 (512x512) | stabilityai/stable-diffusion-2-base |
| テキストから画像への変換 (768x768) | stabilityai/stable-diffusion-2 |
| inpainting | stabilityai/stable-diffusion-2-inpainting |
| 超解像 | stable-diffusion-x4-upscaler |
| 深度から画像 | stabilityai/stable-diffusion-2-depth |
各タスクにおけるStable Diffusion 2の使用方法の例を以下に示します。
スケジューラーの速度と品質のトレードオフをどのように探索するか、またパイプラインのコンポーネントを効率的に再利用する方法を学ぶために、Stable DiffusionのTipsセクションをぜひご確認ください。
タスクに公式チェックポイントのいずれかを使用する場合は、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)