Diffusers documentation
Stable Diffusion 流水线
Stable Diffusion 流水线
Stable Diffusion 是由 CompVis、Stability AI 和 LAION 的研究人员和工程师开发的文本到图像潜在扩散模型。潜在扩散在低维潜在空间中应用扩散过程,以降低内存和计算复杂度。这种特定类型的扩散模型由 Robin Rombach、Andreas Blattmann、Dominik Lorenz、Patrick Esser 和 Björn Ommer 在 High-Resolution Image Synthesis with Latent Diffusion Models 中提出。
Stable Diffusion 是在 LAION-5B 数据集的一个子集上的 512x512 图像上训练的。该模型使用冻结的 CLIP ViT-L/14 文本编码器,以文本提示作为条件来引导模型。凭借其 860M 参数的 UNet 和 123M 参数的文本编码器,该模型相对轻量,可以在消费级 GPU 上运行。
如需了解更多关于 Stable Diffusion 的工作原理及其与基础潜在扩散模型的区别,请参阅 Stability AI 的公告以及我们自己的博客文章以获取更详细的技术信息。
您可以在 CompVis/stable-diffusion 找到 Stable Diffusion v1.0 的原始代码库,在 Stability-AI/stablediffusion 找到 Stable Diffusion v2.0 的原始代码库,以及用于各种任务的原始脚本。不同 Stable Diffusion 版本和任务的额外官方检查点可以在 CompVis 和 Stability AI Hub 组织中查找。请探索这些组织,以找到最适合您使用场景的检查点!
下表总结了可用的 Stable Diffusion 流水线、其支持的任务以及交互式演示:
| Pipeline | 支持的任务 | 🤗 Space |
|---|---|---|
| StableDiffusion | 文本到图像 | |
| StableDiffusionImg2Img | 图像到图像 | |
| StableDiffusionInpaint | inpainting | |
| StableDiffusionDepth2Img | depth-to-image | |
| StableDiffusionImageVariation | 图像变体 | |
| StableDiffusionPipelineSafe(已弃用;无文档页面) | 过滤后的文本到图像 | |
| StableDiffusion2 | 文本到图像、图像修复、深度到图像、超分辨率 | |
| StableDiffusionXL | 文本到图像,图像到图像 | |
| StableDiffusionLatentUpscale | 超分辨率 | |
| StableDiffusionUpscale | 超分辨率 | |
| StableDiffusionLDM3D | 文本到 RGB、文本到深度、文本到全景 | |
| StableDiffusionUpscaleLDM3D | ldm3d 超分辨率 |
提示
为了帮助您充分利用 Stable Diffusion 流水线,这里提供了一些提升性能和易用性的技巧。这些技巧适用于所有 Stable Diffusion 流水线。
探索速度与质量之间的权衡
StableDiffusionPipeline 默认使用 PNDMScheduler,但 🤗 Diffusers 提供了许多其他兼容的调度器(其中一些速度更快或输出质量更高)。例如,如果您想使用 EulerDiscreteScheduler 而不是默认的调度器:
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipeline.scheduler = EulerDiscreteScheduler.from_config(pipeline.scheduler.config)
# or
euler_scheduler = EulerDiscreteScheduler.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="scheduler")
pipeline = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", scheduler=euler_scheduler)复用流水线组件以节省内存
为了节省内存并在多个流水线中复用相同的组件,请使用 .components 方法,以避免将权重多次加载到 RAM 中。
from diffusers import (
StableDiffusionPipeline,
StableDiffusionImg2ImgPipeline,
StableDiffusionInpaintPipeline,
)
text2img = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
img2img = StableDiffusionImg2ImgPipeline(**text2img.components)
inpaint = StableDiffusionInpaintPipeline(**text2img.components)
# now you can use text2img(...), img2img(...), inpaint(...) just like the call methods of each respective pipeline使用 gradio 创建 Web 演示
Stable Diffusion 流水线在 Gradio 中受到自动支持。Gradio 是一个库,可轻松创建美观且用户友好的 Web 机器学习应用。首先,请确保已安装 Gradio:
pip install -U gradio
然后,围绕任意基于 Stable Diffusion 的 pipeline 创建一个 Web 演示。例如,你可以使用 Gradio 的 Interface.from_pipeline 函数,仅用一行代码即可创建一个图像生成 pipeline:
from diffusers import StableDiffusionPipeline
import gradio as gr
pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
gr.Interface.from_pipeline(pipe).launch()它会在您的浏览器中打开一个直观的拖放界面:

类似地,你可以使用以下方式为图像到图像流水线创建一个演示:
from diffusers import StableDiffusionImg2ImgPipeline
import gradio as gr
pipe = StableDiffusionImg2ImgPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5")
gr.Interface.from_pipeline(pipe).launch()默认情况下,Web 演示运行在本地服务器上。如果您想与他人共享,可以在 launch() 中设置 share=True 来生成一个临时的公共链接。或者,您可以将演示托管在 Hugging Face Spaceshttps://huggingface.co/spaces 上,以获取永久链接。