Diffusers 文档

Stable Diffusion 2

Hugging Face's logo
加入 Hugging Face 社区

并获得增强的文档体验

开始使用

稳定扩散 2

稳定扩散 2 是一个文本到图像的潜在扩散模型,它基于原始的 Stable Diffusion 的工作构建,并由 Stability AI 和 LAION 的 Robin Rombach 和 Katherine Crowson 领导。

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

有关稳定扩散 2 的工作原理以及它与原始 Stable Diffusion 的区别的更多详细信息,请参阅官方公告帖子

稳定扩散 2 的架构与原始的 Stable Diffusion 模型或多或少相同,因此请查看其 API 文档以了解如何使用稳定扩散 2。我们建议使用 DPMSolverMultistepScheduler,因为它提供了合理的速度/质量权衡,并且只需 20 步即可运行。

稳定扩散 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

以下是一些关于如何为每个任务使用稳定扩散 2 的示例

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

如果您有兴趣为某个任务使用官方检查点之一,请浏览 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 上更新