开源 Python 库---Diffusers 库的安装及运用(自留)
Diffusers 库是 Hugging Face 官方开发的 开源 Python 库,专门用于简化扩散模型(Diffusion Models)的部署与应用。
一、安装Diffusers库
1. 激活 Nimiconda 环境
conda activate your_env_name
2. 安装 Diffusers 及其依赖
Diffusers 需要 PyTorch 或 TensorFlow 作为后端,推荐使用 PyTorch:
# 安装 PyTorch (根据您的 CUDA 版本选择合适的命令)
conda install pytorch torchvision torchaudio pytorch-cuda=11.7 -c pytorch -c nvidia# 安装 Diffusers 和 Transformers
pip install diffusers transformers
3. 可选依赖安装
根据您的使用场景,可能需要安装额外依赖
# 如果需要使用 Accelerate 进行分布式训练
pip install accelerate# 如果需要图像处理功能
pip install pillow# 如果需要使用 Flax (JAX) 后端
pip install flax jax
验证安装
创建一个简单的 Python 脚本验证安装:
from diffusers import DiffusionPipeline# 加载预训练模型
pipeline = DiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")# 使用 GPU (如果可用)
pipeline.to("cuda")# 生成图像
image = pipeline("An astronaut riding a horse on mars").images[0]
image.save("astronaut_rides_horse.png")
二、文件存放规范
项目根目录/
│
├── 你的脚本.py # Python 主程序
├── generated_images/ # 自定义输出目录(建议)
│ ├── astronaut_rides_horse.png
│ ├── cute_cat_on_beach.png
│ └── ...
└── .cache/ # 自动创建的缓存目录(默认位置)
└── huggingface/
└── hub/
├── models--runwayml--stable-diffusion-v1-5
└── ...其他模型...
1. 生成的图像文件(如 output.png
)
默认情况下,文件会保存在当前工作目录(即运行 Python 脚本的目录)。建议明确指定输出路径:
import os# 方法1:固定输出目录(推荐)
output_dir = "./generated_images"
os.makedirs(output_dir, exist_ok=True) # 自动创建目录
output_path = os.path.join(output_dir, "output.png")# 方法2:让用户自定义路径
output_path = input("Enter save path (default: ./output.png): ") or "output.png"# 生成并保存图像
image.save(output_path)
2. 下载的模型文件
Diffusers 会自动从 Hugging Face Hub 下载模型,默认保存位置为:
-
Linux/Mac:
~/.cache/huggingface/hub
-
Windows:
C:\Users\<username>\.cache\huggingface\hub
自定义模型缓存路径:
from diffusers import StableDiffusionPipeline
import os# 设置环境变量(在加载模型前)
os.environ["HF_HOME"] = "/path/to/your/custom/cache"# 然后正常加载模型
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
4. 最佳实践建议
-
输出目录:
-
为生成的图像创建专用目录(如
./generated_images
) -
文件名建议包含提示词或时间戳(避免覆盖)
-
-
模型缓存:
-
大模型建议固定缓存路径(通过
HF_HOME
) -
服务器环境可设置为共享目录(如
/shared/huggingface_cache
)
-
-
权限问题:
如果遇到权限错误,尝试:
os.makedirs(output_dir, exist_ok=True, mode=0o755) # 确保可写
路径兼容性:
from diffusers import StableDiffusionPipeline# 打印实际模型缓存路径
pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
print(f"Model cached at: {pipe.config._name_or_path}")
三、模型文件和布局
1. Safetensors 库
Safetensors 是一种安全快速的文件格式,用于安全地存储和加载张量。Safetensors 限制 header 大小以限制某些类型的攻击,支持延迟加载(对分布式设置很有用),并且通常具有更快的加载速度。
2.LoRA 文件
LoRA 是一种轻量级适配器,训练快速且易于,因此在以某种方式或样式生成图像方面特别受欢迎。这些适配器通常存储在 safetensors 文件中,并且在 civitai 等模型共享平台上广泛流行。LoRA 使用 load_lora_weights() 方法加载到基础模型中。
from diffusers import StableDiffusionXLPipeline
import torch# base model
pipeline = StableDiffusionXLPipeline.from_pretrained("Lykon/dreamshaper-xl-1-0", torch_dtype=torch.float16, variant="fp16"
).to("cuda")# download LoRA weights
!wget https://civitai.com/api/download/models/168776 -O blueprintify.safetensors# load LoRA weights
pipeline.load_lora_weights(".", weight_name="blueprintify.safetensors")
prompt = "bl3uprint, a highly detailed blueprint of the empire state building, explaining how to build all parts, many txt, blueprint grid backdrop"
negative_prompt = "lowres, cropped, worst quality, low quality, normal quality, artifacts, signature, watermark, username, blurry, more than one bridge, bad architecture"image = pipeline(prompt=prompt,negative_prompt=negative_prompt,generator=torch.manual_seed(0),
).images[0]
image
3.CKPT
PyTorch 的 torch.save 函数使用 Python 的 pickle 实用程序来序列化和保存模型。这些文件保存为 ckpt 文件,并且包含整个模型的权重。
使用 from_single_file() 方法直接加载 ckpt 文件。
from diffusers import StableDiffusionPipelinepipeline = StableDiffusionPipeline.from_single_file("https://huggingface.co/stable-diffusion-v1-5/stable-diffusion-v1-5/blob/main/v1-5-pruned.ckpt"
)