模型简介
模型特点
模型能力
使用案例
🚀 Stable Diffusion v1 - 4 模型卡片
Stable Diffusion 是一种潜在文本到图像的扩散模型,能够根据任何文本输入生成逼真的图像。欲了解 Stable Diffusion 的工作原理,请查看 🤗 的 Stable Diffusion 与 🧨Diffusers 博客。
Stable - Diffusion - v1 - 4 检查点使用 [Stable - Diffusion - v1 - 2](https:/steps/huggingface.co/CompVis/stable - diffusion - v1 - 2) 检查点的权重进行初始化,随后在分辨率为 512x512 的情况下对 225k 步进行微调,使用的数据集为 “laion - aesthetics v2 5+”,并以 10% 的概率丢弃文本条件以改进 无分类器引导采样。
此处的权重旨在与 🧨 Diffusers 库一起使用。如果您正在寻找要加载到 CompVis Stable Diffusion 代码库中的权重,请 [点击此处](https://huggingface.co/CompVis/stable - diffusion - v - 1 - 4 - original)。
🚀 快速开始
我们推荐使用 🤗 的 Diffusers 库 来运行 Stable Diffusion。
PyTorch
pip install --upgrade diffusers transformers scipy
使用默认的 PNDM 调度器运行管道:
import torch
from diffusers import StableDiffusionPipeline
model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to(device)
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
image.save("astronaut_rides_horse.png")
⚠️ 重要提示
如果您的 GPU 内存有限,可用的 GPU RAM 少于 4GB,请确保以 float16 精度加载 StableDiffusionPipeline,而不是像上面那样使用默认的 float32 精度。您可以通过告诉 diffusers 期望权重为 float16 精度来实现这一点:
import torch
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to(device)
pipe.enable_attention_slicing()
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
image.save("astronaut_rides_horse.png")
要更换噪声调度器,请将其传递给 from_pretrained
:
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
model_id = "CompVis/stable-diffusion-v1-4"
# 这里使用 Euler 调度器
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
image.save("astronaut_rides_horse.png")
JAX/Flax
要在 TPU 和 GPU 上使用 StableDiffusion 以实现更快的推理,您可以利用 JAX/Flax。
使用默认的 PNDMScheduler 运行管道:
import jax
import numpy as np
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from diffusers import FlaxStableDiffusionPipeline
pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", revision="flax", dtype=jax.numpy.bfloat16
)
prompt = "a photo of an astronaut riding a horse on mars"
prng_seed = jax.random.PRNGKey(0)
num_inference_steps = 50
num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)
# 分片输入和随机数生成器
params = replicate(params)
prng_seed = jax.random.split(prng_seed, num_samples)
prompt_ids = shard(prompt_ids)
images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=True).images
images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
⚠️ 重要提示
如果您的 TPU 内存有限,请确保以
bfloat16
精度加载FlaxStableDiffusionPipeline
,而不是像上面那样使用默认的float32
精度。您可以通过告诉 diffusers 从 “bf16” 分支加载权重来实现这一点。
import jax
import numpy as np
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from diffusers import FlaxStableDiffusionPipeline
pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", revision="bf16", dtype=jax.numpy.bfloat16
)
prompt = "a photo of an astronaut riding a horse on mars"
prng_seed = jax.random.PRNGKey(0)
num_inference_steps = 50
num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)
# 分片输入和随机数生成器
params = replicate(params)
prng_seed = jax.random.split(prng_seed, num_samples)
prompt_ids = shard(prompt_ids)
images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=True).images
images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
✨ 主要特性
Stable Diffusion 是一种潜在文本到图像的扩散模型,能够根据任何文本输入生成逼真的图像。
📦 安装指南
PyTorch
pip install --upgrade diffusers transformers scipy
💻 使用示例
基础用法
import torch
from diffusers import StableDiffusionPipeline
model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to(device)
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
image.save("astronaut_rides_horse.png")
高级用法
更换噪声调度器
from diffusers import StableDiffusionPipeline, EulerDiscreteScheduler
model_id = "CompVis/stable-diffusion-v1-4"
# 这里使用 Euler 调度器
scheduler = EulerDiscreteScheduler.from_pretrained(model_id, subfolder="scheduler")
pipe = StableDiffusionPipeline.from_pretrained(model_id, scheduler=scheduler, torch_dtype=torch.float16)
pipe = pipe.to("cuda")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]
image.save("astronaut_rides_horse.png")
JAX/Flax 加速推理
import jax
import numpy as np
from flax.jax_utils import replicate
from flax.training.common_utils import shard
from diffusers import FlaxStableDiffusionPipeline
pipeline, params = FlaxStableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-4", revision="flax", dtype=jax.numpy.bfloat16
)
prompt = "a photo of an astronaut riding a horse on mars"
prng_seed = jax.random.PRNGKey(0)
num_inference_steps = 50
num_samples = jax.device_count()
prompt = num_samples * [prompt]
prompt_ids = pipeline.prepare_inputs(prompt)
# 分片输入和随机数生成器
params = replicate(params)
prng_seed = jax.random.split(prng_seed, num_samples)
prompt_ids = shard(prompt_ids)
images = pipeline(prompt_ids, params, prng_seed, num_inference_steps, jit=True).images
images = pipeline.numpy_to_pil(np.asarray(images.reshape((num_samples,) + images.shape[-3:])))
📚 详细文档
模型详情
属性 | 详情 |
---|---|
开发者 | Robin Rombach, Patrick Esser |
模型类型 | 基于扩散的文本到图像生成模型 |
语言 | 英语 |
许可证 | [CreativeML OpenRAIL M 许可证](https://huggingface.co/spaces/CompVis/stable - diffusion - license) 是一种 [Open RAIL M 许可证](https://www.licenses.ai/blog/2022/8/18/naming - convention - of - responsible - ai - licenses),改编自 BigScience 和 RAIL Initiative 在负责任的 AI 许可领域的联合工作。有关我们许可证所基于的 [BLOOM Open RAIL 许可证的文章](https://bigscience.huggingface.co/blog/the - bigscience - rail - license) |
模型描述 | 这是一个可用于根据文本提示生成和修改图像的模型。它是一个 潜在扩散模型,使用固定的预训练文本编码器 (CLIP ViT - L/14),如 Imagen 论文 中所建议 |
更多信息资源 | [GitHub 仓库](https://github.com/CompVis/stable - diffusion),论文 |
引用方式 | bibtex<br> @InProceedings{Rombach_2022_CVPR,<br> author = {Rombach, Robin and Blattmann, Andreas and Lorenz, Dominik and Esser, Patrick and Ommer, Bj\"orn},<br> title = {High - Resolution Image Synthesis With Latent Diffusion Models},<br> booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},<br> month = {June},<br> year = {2022},<br> pages = {10684 - 10695}<br> }<br> |
使用方式
直接使用
该模型仅用于研究目的。可能的研究领域和任务包括:
- 安全部署有可能生成有害内容的模型。
- 探索和理解生成模型的局限性和偏差。
- 艺术品生成以及在设计和其他艺术过程中的应用。
- 在教育或创意工具中的应用。
- 生成模型的研究。
排除的使用方式如下所述。
滥用、恶意使用和超出范围的使用
注意:本节内容摘自 [DALLE - MINI 模型卡片](https://huggingface.co/dalle - mini/dalle - mini),但同样适用于 Stable Diffusion v1。
该模型不应被用于故意创建或传播为人们创造敌对或疏离环境的图像。这包括生成人们可预见会感到不安、痛苦或冒犯的图像;或传播历史或当前刻板印象的内容。
超出范围的使用
该模型并非用于生成对人或事件的真实或准确表示,因此使用该模型生成此类内容超出了该模型的能力范围。
滥用和恶意使用
使用该模型生成对个人残酷的内容是对该模型的滥用。这包括但不限于:
- 生成贬低、非人化或以其他方式伤害人们或其环境、文化、宗教等的表示。
- 故意推广或传播歧视性内容或有害刻板印象。
- 在未经个人同意的情况下冒充个人。
- 在可能看到的人未同意的情况下生成性内容。
- 错误信息和虚假信息。
- 令人震惊的暴力和血腥场景的表示。
- 违反版权或许可材料使用条款的共享。
- 违反版权或许可材料使用条款的对其进行修改后的内容共享。
限制和偏差
限制
- 模型无法实现完美的照片级逼真度。
- 模型无法渲染清晰可读的文本。
- 模型在涉及组合性的更困难任务上表现不佳,例如渲染与 “A red cube on top of a blue sphere” 对应的图像。
- 面部和人物一般可能无法正确生成。
- 模型主要使用英语字幕进行训练,在其他语言中的效果不佳。
- 模型的自动编码部分存在信息损失。
- 模型在大规模数据集 [LAION - 5B](https://laion.ai/blog/laion - 5b/) 上进行训练,该数据集包含成人内容,如果没有额外的安全机制和考虑,不适合用于产品。
- 未采取额外措施对数据集进行去重。因此,我们观察到对于训练数据中重复的图像存在一定程度的记忆现象。可以在 [https://rom1504.github.io/clip - retrieval/](https://rom1504.github.io/clip - retrieval/) 上搜索训练数据,以帮助检测记忆的图像。
偏差
虽然图像生成模型的能力令人印象深刻,但它们也可能强化或加剧社会偏差。Stable Diffusion v1 在 [LAION - 2B(en)](https://laion.ai/blog/laion - 5b/) 的子集上进行训练,该子集主要由英语描述的图像组成。来自使用其他语言的社区和文化的文本和图像可能未得到充分考虑。这影响了模型的整体输出,因为白人和西方文化通常被设定为默认。此外,模型使用非英语提示生成内容的能力明显低于使用英语提示的能力。
安全模块
该模型的预期用途是与 Diffusers 中的 安全检查器 一起使用。该检查器通过将模型输出与已知的硬编码 NSFW 概念进行比较来工作。这些概念被故意隐藏,以降低对该过滤器进行逆向工程的可能性。具体来说,检查器在图像生成后,在 CLIPTextModel
的嵌入空间中比较有害概念的类概率。这些概念与生成的图像一起传入模型,并与每个 NSFW 概念的手工设计权重进行比较。
训练
训练数据
模型开发者使用以下数据集对模型进行训练:
- LAION - 2B (en) 及其子集(见下一节)
训练过程
Stable Diffusion v1 - 4 是一个潜在扩散模型,它将自动编码器与在自动编码器的潜在空间中训练的扩散模型相结合。在训练过程中:
- 图像通过编码器进行编码,将图像转换为潜在表示。自动编码器使用相对下采样因子 8,将形状为 H x W x 3 的图像映射到形状为 H/f x W/f x 4 的潜在表示。
- 文本提示通过 ViT - L/14 文本编码器进行编码。
- 文本编码器的非池化输出通过交叉注意力输入到潜在扩散模型的 UNet 主干中。
- 损失是添加到潜在表示中的噪声与 UNet 预测之间的重建目标。
我们目前提供四个检查点,训练方式如下:
- [
stable - diffusion - v1 - 1
](https://huggingface.co/CompVis/stable - diffusion - v1 - 1):在 [laion2B - en](https://huggingface.co/datasets/laion/laion2B - en) 上以分辨率256x256
进行 237,000 步训练。在 [laion - high - resolution](https://huggingface.co/datasets/laion/laion - high - resolution)(来自 LAION - 5B 的 170M 个分辨率>= 1024x1024
的示例)上以分辨率512x512
进行 194,000 步训练。 - [
stable - diffusion - v1 - 2
](https://huggingface.co/CompVis/stable - diffusion - v1 - 2):从stable - diffusion - v1 - 1
继续训练。在 “laion - improved - aesthetics”(laion2B - en 的一个子集,过滤为原始大小>= 512x512
、估计美学分数> 5.0
且估计水印概率< 0.5
的图像。水印估计来自 LAION - 5B 元数据,美学分数使用 [改进的美学估计器](https://github.com/christophschuhmann/improved - aesthetic - predictor) 进行估计)上以分辨率512x512
进行 515,000 步训练。 - [
stable - diffusion - v1 - 3
](https://huggingface.co/CompVis/stable - diffusion - v1 - 3):从stable - diffusion - v1 - 2
继续训练。在 “laion - improved - aesthetics” 上以分辨率512x512
进行 195,000 步训练,并以 10% 的概率丢弃文本条件以改进 无分类器引导采样。 - [
stable - diffusion - v1 - 4
](https://huggingface.co/CompVis/stable - diffusion - v1 - 4):从stable - diffusion - v1 - 2
继续训练。在 “laion - aesthetics v2 5+” 上以分辨率512x512
进行 225,000 步训练,并以 10% 的概率丢弃文本条件以改进 无分类器引导采样。
训练参数
- 硬件:32 x 8 x A100 GPU
- 优化器:AdamW
- 梯度累积:2
- 批次大小:32 x 8 x 2 x 4 = 2048
- 学习率:在 10,000 步内热身到 0.0001,然后保持不变
评估结果
使用不同的无分类器引导尺度(1.5, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0)和 50 个 PLMS 采样步骤进行评估,显示了检查点的相对改进:

使用 50 个 PLMS 步骤和来自 COCO2017 验证集的 10000 个随机提示进行评估,在 512x512 分辨率下进行评估。未针对 FID 分数进行优化。
环境影响
Stable Diffusion v1 估计排放量
基于这些信息,我们使用 Lacoste 等人 (2019) 中提出的 机器学习影响计算器 估计了以下 CO2 排放量。利用硬件、运行时间、云提供商和计算区域来估计碳影响。
- 硬件类型:A100 PCIe 40GB
- 使用时长:150000 小时
- 云提供商:AWS
- 计算区域:美国东部
- 碳排放(功耗 x 时间 x 基于电网位置产生的碳):11250 kg CO2 当量
引用
@InProceedings{Rombach_2022_CVPR,
author = {Rombach, Robin and Blattmann, Andreas and Lorenz, Dominik and Esser, Patrick and Ommer, Bj\"orn},
title = {High-Resolution Image Synthesis With Latent Diffusion Models},
booktitle = {Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition (CVPR)},
month = {June},
year = {2022},
pages = {10684-10695}
}
本模型卡片由 Robin Rombach 和 Patrick Esser 编写,基于 [DALL - E Mini 模型卡片](https://huggingface.co/dalle - mini/dalle - mini)。
📄 许可证
请阅读 [CreativeML OpenRAIL M 许可证](https://huggingface.co/spaces/CompVis/stable - diffusion - license) 以访问此模型。
该模型是开放访问的,可供所有人使用,CreativeML OpenRAIL - M 许可证进一步规定了权利和使用方式。CreativeML OpenRAIL 许可证规定:
- 您不能使用该模型故意生成或共享非法或有害的输出或内容。
- 作者对您生成的输出不主张任何权利,您可以自由使用它们,并对其使用负责,其使用不得违反许可证中规定的条款。
- 您可以重新分发权重,并将模型用于商业用途和/或作为服务使用。如果您这样做,请注意您必须包含与许可证中相同的使用限制,并向所有用户共享一份 CreativeML OpenRAIL - M 许可证(请完整仔细阅读许可证)。
请在此处仔细阅读完整的许可证:[https://huggingface.co/spaces/CompVis/stable - diffusion - license](https://huggingface.co/spaces/CompVis/stable - diffusion - license)

