Stable Diffusion XL
简介
Stable Diffusion XL (SDXL) 是目前开源图像生成领域的标杆之作。相比前代,它通过更大的参数量显著提升了画质和对复杂提示词的理解力,尤其是解决了人体结构和文字渲染的痛点。对于开发者和创作者而言,SDXL 的最大价值在于其极强的生态可塑性:你可以通过 LoRA 轻松微调出特定风格,或配合 ControlNet 实现精准的构图控制。它无需依赖昂贵的云端订阅,支持本地部署,是追求极致自定义和私有化部署用户的首选方案。
核心亮点
- 原生支持高分辨率生成,画质细腻且构图自然
- 开源生态强大,支持海量 LoRA 风格插件微调
- 支持本地部署,无需联网即可实现高效出图
- 对复杂提示词理解更精准,大幅降低抽卡成本
使用方法
安装依赖
# 安装 Hugging Face transformers
pip install transformers torch
SDK 使用
# 使用 transformers 加载模型
from transformers import AutoModel, AutoTokenizer
model = AutoModel.from_pretrained("stabilityai/stable-diffusion-xl")
tokenizer = AutoTokenizer.from_pretrained("stabilityai/stable-diffusion-xl")
Hugging Face 下载
我们推荐使用命令行或者 Hugging Face Hub SDK 来进行模型的下载。
操作指引:在下载前,请先通过如下命令安装 huggingface_hub:
操作指引
pip install -U huggingface_hub
命令行下载
下载完整模型库
下载完整模型库
huggingface-cli download stabilityai/stable-diffusion-xl
下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
下载单个文件到指定本地文件夹(以下载 config.json 到当前路径下 ./dir 目录为例)
huggingface-cli download stabilityai/stable-diffusion-xl config.json --local-dir ./dir
SDK 下载
SDK 下载
# 模型下载
from huggingface_hub import snapshot_download
model_dir = snapshot_download('stabilityai/stable-diffusion-xl')
Git 下载
请确保 lfs 已经被正确安装
Git 下载
git lfs install
git clone https://huggingface.co/stabilityai/stable-diffusion-xl
如果您希望跳过 lfs 大文件下载,可以使用如下命令
跳过 LFS
GIT_LFS_SKIP_SMUDGE=1 git clone https://huggingface.co/stabilityai/stable-diffusion-xl
模型文件托管在 Hugging Face Hub,使用 HF CLI / SDK / Git 直接下载,不经过本站。
PyTorch / Transformers 使用
安装 Transformers
安装 Transformers
pip install -U transformers torch
模型加载和推理
模型加载和推理
from transformers import AutoModelForCausalLM, AutoTokenizer
model = AutoModelForCausalLM.from_pretrained('stabilityai/stable-diffusion-xl')
tokenizer = AutoTokenizer.from_pretrained('stabilityai/stable-diffusion-xl')