🚀 SD-XL 1.0 精煉器模型卡片
SD-XL 1.0 精煉器模型是基於文本提示生成和修改圖像的強大工具,通過獨特的潛在擴散管道,能生成高質量的圖像,在圖像生成領域具有重要的研究價值。
🚀 快速開始
安裝依賴
確保將 diffusers
升級到 >= 0.18.0
:
pip install diffusers --upgrade
此外,還需安裝 transformers
、safetensors
、accelerate
以及 invisible_watermark
:
pip install invisible_watermark transformers accelerate safetensors
使用精煉器改進圖像
import torch
from diffusers import StableDiffusionXLImg2ImgPipeline
from diffusers.utils import load_image
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipe = pipe.to("cuda")
url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"
init_image = load_image(url).convert("RGB")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt, image=init_image).images
提升推理速度
當使用 torch >= 2.0
時,可通過 torch.compile
提升 20 - 30% 的推理速度。在運行管道前,用 torch.compile
包裝 unet
:
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
處理 GPU 顯存限制
若受限於 GPU 顯存,可調用 pipe.enable_model_cpu_offload
啟用 CPU 卸載,而非使用 .to("cuda")
:
- pipe.to("cuda")
+ pipe.enable_model_cpu_offload()
更多高級用例,請查看 文檔。
✨ 主要特性
- 集成專家管道:SDXL 採用 集成專家 管道進行潛在擴散。先由基礎模型生成(有噪聲的)潛在變量,再由精煉模型進行最終去噪處理。基礎模型也可單獨使用。
- 兩階段管道可選:可先使用基礎模型生成所需輸出大小的潛在變量,再使用專門的高分辨率模型和 SDEdit 技術(https://arxiv.org/abs/2108.01073,即“img2img”)對潛在變量進行處理。
📦 安裝指南
升級 diffusers
pip install diffusers --upgrade
安裝其他依賴
pip install invisible_watermark transformers accelerate safetensors
💻 使用示例
基礎用法
import torch
from diffusers import StableDiffusionXLImg2ImgPipeline
from diffusers.utils import load_image
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipe = pipe.to("cuda")
url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"
init_image = load_image(url).convert("RGB")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt, image=init_image).images
高級用法
當使用 torch >= 2.0
時,可通過 torch.compile
提升推理速度:
import torch
from diffusers import StableDiffusionXLImg2ImgPipeline
from diffusers.utils import load_image
pipe = StableDiffusionXLImg2ImgPipeline.from_pretrained(
"stabilityai/stable-diffusion-xl-refiner-1.0", torch_dtype=torch.float16, variant="fp16", use_safetensors=True
)
pipe.unet = torch.compile(pipe.unet, mode="reduce-overhead", fullgraph=True)
pipe = pipe.to("cuda")
url = "https://huggingface.co/datasets/patrickvonplaten/images/resolve/main/aa_xl/000000009.png"
init_image = load_image(url).convert("RGB")
prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt, image=init_image).images
📚 詳細文檔
模型

SDXL 由用於潛在擴散的 集成專家 管道組成:
第一步,使用基礎模型(可在 https://huggingface.co/stabilityai/stable-diffusion-xl-base-1.0 獲取)生成(有噪聲的)潛在變量,然後使用專門的精煉模型對其進行最終去噪處理。需注意,基礎模型可單獨使用。
另外,也可使用兩階段管道:
首先,使用基礎模型生成所需輸出大小的潛在變量。第二步,使用專門的高分辨率模型,並對第一步生成的潛在變量應用 SDEdit 技術(https://arxiv.org/abs/2108.01073,也稱為“img2img”),使用相同的提示。此技術比第一種稍慢,因為需要更多的函數評估。
源代碼可在 https://github.com/Stability-AI/generative-models 獲取。
模型描述
模型來源
出於研究目的,推薦使用我們的 generative-models
GitHub 倉庫(https://github.com/Stability-AI/generative-models),該倉庫實現了最流行的擴散框架(包括訓練和推理),並會逐步添加新功能,如蒸餾。
Clipdrop 提供免費的 SDXL 推理。
- 倉庫:https://github.com/Stability-AI/generative-models
- 演示:https://clipdrop.co/stable-diffusion
評估
上圖評估了用戶對 SDXL(有和無精煉)相對於 SDXL 0.9 以及 Stable Diffusion 1.5 和 2.1 的偏好。SDXL 基礎模型的性能明顯優於之前的版本,結合精煉模塊的模型整體性能最佳。
用途
直接使用
該模型僅用於研究目的。可能的研究領域和任務包括:
- 藝術品生成,以及在設計和其他藝術過程中的應用。
- 教育或創意工具中的應用。
- 生成模型的研究。
- 具有生成有害內容潛力的模型的安全部署。
- 探究和理解生成模型的侷限性和偏差。
超出範圍的使用
該模型並非用於生成真實或準確反映人物或事件的內容,因此使用該模型生成此類內容超出了其能力範圍。
侷限性和偏差
侷限性
- 模型無法實現完美的照片級真實感。
- 模型無法渲染清晰可讀的文本。
- 模型在處理涉及組合性的複雜任務時存在困難,例如渲染與“紅色立方體放在藍色球體上”對應的圖像。
- 面部和人物的生成可能不夠理想。
- 模型的自動編碼部分存在信息損失。
偏差
雖然圖像生成模型的能力令人印象深刻,但它們也可能強化或加劇社會偏差。
📄 許可證
該模型使用 CreativeML Open RAIL++ - M 許可證。