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 是一个基于原始 Stable Diffusion 工作的文本到图像 潜在扩散 模型,由来自 Stability AI 的 Robin Rombach 和来自 LAION 的 Katherine Crowson 主导开发。

Stable Diffusion 2.0 版本包含使用全新文本编码器(OpenCLIP)训练的稳健文本到图像模型,该编码器由 LAION 开发并得到 Stability AI 的支持,与早期的 V1 版本相比,大幅提升了生成图像的质量。此版本中的文本到图像模型可以生成默认分辨率为 512x512 像素和 768x768 像素的图像。 这些模型是在 Stability AI 的 DeepFloyd 团队创建的 LAION-5B 数据集 的美学子集上训练的,随后使用 LAION 的 NSFW 过滤器 进一步过滤以移除成人内容。

有关 Stable Diffusion 2 的工作原理及其与原始 Stable Diffusion 的区别的更多详细信息,请参阅官方公告。

Stable Diffusion 2 的架构与原始的 Stable Diffusion 模型 基本相同,因此请参阅其 API 文档以了解如何使用 Stable Diffusion 2。我们建议使用 DPMSolverMultistepScheduler,因为它在速度和质量之间提供了合理的平衡,并且可以仅使用 20 步进行运行。

Stable Diffusion 2 可用于文本生成图像、图像修复、超分辨率和深度图生成图像等任务:

任务仓库
文本到图像(512x512)stabilityai/stable-diffusion-2-base
文本到图像 (768x768)stabilityai/stable-diffusion-2
inpaintingstabilityai/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