Diffusers 文档

Stable Diffusion 2

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

Stable Diffusion 2

Stable Diffusion 2 是一个文本到图像的*潜在扩散*模型,它建立在原始的 Stable Diffusion 的工作基础上,由 Stability AILAION 的 Robin Rombach 和 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
图像修复 stabilityai/stable-diffusion-2-inpainting
超分辨率 stable-diffusion-x4-upscaler
深度到图像 stabilityai/stable-diffusion-2-depth

以下是一些如何为每个任务使用 Stable Diffusion 2 的示例

务必查看 Stable Diffusion 的 提示 部分,了解如何探索调度器速度和质量之间的权衡,以及如何高效地重用管道组件!

如果您有兴趣将其中一个官方检查点用于某个任务,请探索 CompVisRunwayStability AI Hub 组织!

文本到图像

from diffusers import DiffusionPipeline, DPMSolverMultistepScheduler
import torch

repo_id = "stabilityai/stable-diffusion-2-base"
pipe = DiffusionPipeline.from_pretrained(repo_id, torch_dtype=torch.float16, variant="fp16")

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")

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, torch_dtype=torch.float16, variant="fp16")

pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
pipe = pipe.to("cuda")

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, torch_dtype=torch.float16)
pipeline = pipeline.to("cuda")

# 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",
    torch_dtype=torch.float16,
).to("cuda")


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)
< > 在 GitHub 上更新