模型概述
模型特點
模型能力
使用案例
🚀 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)

