🤗 Diffusers 是用于生成图像、音频甚至分子 3D 结构的最先进预训练扩散模型的首选库。无论您是寻找简单的推理解决方案,还是训练自己的扩散模型,🤗 Diffusers 都是一个支持两者的模块化工具箱。我们的库设计重点在于可用性优先于性能、简单优先于容易,以及可定制性优先于抽象。
🤗 Diffusers 提供三个核心组件:
安装
我们建议从 PyPI 或 Conda 在虚拟环境中安装 🤗 Diffusers。有关安装 PyTorch 的更多详细信息,请参阅其官方文档。
PyTorch
使用 pip(官方软件包):
pip install --upgrade diffusers[torch]
使用 conda(由社区维护):
conda install -c conda-forge diffusers
Apple Silicon (M1/M2) 支持
请参阅 How to use Stable Diffusion in Apple Silicon 指南。
快速入门
使用 🤗 Diffusers 生成输出非常简单。要从文本生成图像,请使用 from_pretrained 方法加载任何预训练的扩散模型(在 Hub 中浏览 30,000+ 检查点):
from diffusers import DiffusionPipeline
import torch
pipeline = DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", dtype=torch.float16)
pipeline.to("cuda")
pipeline("An image of a squirrel in Picasso style").images[0]
您还可以深入挖掘模型和调度器工具箱,以构建自己的扩散系统:
from diffusers import DDPMScheduler, UNet2DModel
from PIL import Image
import torch
scheduler = DDPMScheduler.from_pretrained("google/ddpm-cat-256")
model = UNet2DModel.from_pretrained("google/ddpm-cat-256").to("cuda")
scheduler.set_timesteps(50)
sample_size = model.config.sample_size
noise = torch.randn((1, 3, sample_size, sample_size), device="cuda")
input = noise
for t in scheduler.timesteps:
with torch.no_grad():
noisy_residual = model(input, t).sample
prev_noisy_sample = scheduler.step(noisy_residual, t, input).prev_sample
input = prev_noisy_sample
image = (input / 2 + 0.5).clamp(0, 1)
image = image.cpu().permute(0, 2, 3, 1).numpy()[0]
image = Image.fromarray((image * 255).round().astype("uint8"))
image
查看快速入门,今天就开始你的扩散之旅吧!
如何浏览文档
| 文档 | 我能学到什么? |
|---|---|
| 教程 | 学习如何使用该库最重要功能的基础速成课程,例如使用模型和调度器构建自己的扩散系统,以及训练自己的扩散模型。 |
| 加载 | 关于如何加载和配置该库所有组件(管道、模型和调度器)的指南,以及如何使用不同的调度器。 |
| 用于推理的管道 | 关于如何使用管道执行不同推理任务、批量生成、控制生成输出和随机性的指南,以及如何向该库贡献一个管道。 |
| 优化 | 关于如何优化扩散模型以加快运行速度并减少内存消耗的指南。 |
| 训练 | 关于如何使用不同的训练技术为不同任务训练扩散模型的指南。 |
贡献
我们 ❤️ 来自开源社区的贡献!
如果你想为这个库做出贡献,请查看我们的贡献指南。
如果你正在使用 AI 代理,请先将其指向 .ai/ 中的项目约定(运行 make claude 或 make codex)——参见使用 AI 代理进行编码。
你可以关注你愿意着手解决的问题,以向该库做出贡献。
此外,欢迎在我们的公共 Discord 频道 打个招呼 👋。我们讨论关于扩散模型的最新趋势,互相帮助进行贡献、个人项目,或者只是闲聊 ☕。
热门任务与流水线
使用 🧨 Diffusers 的流行库
- https://github.com/microsoft/TaskMatrix
- https://github.com/invoke-ai/InvokeAI
- https://github.com/InstantID/InstantID
- https://github.com/apple/ml-stable-diffusion
- https://github.com/Sanster/lama-cleaner
- https://github.com/IDEA-Research/Grounded-Segment-Anything
- https://github.com/ashawkey/stable-dreamfusion
- https://github.com/deep-floyd/IF
- https://github.com/bentoml/BentoML
- https://github.com/bmaltais/kohya_ss
- 以及另外 14,000 个出色的 GitHub 仓库 💪
感谢您使用我们 ❤️。
致谢
本库具体化了众多不同作者之前的工作,如果没有他们出色的研究和实现,本库将不可能存在。我们特别感谢以下实现,它们在我们的开发过程中提供了帮助,没有它们,今天的 API 不可能如此完善:
- @CompVis 的潜在扩散模型库,可在此处
- @hojonathanho 的原始 DDPM 实现,可在此处,以及 @pesser 极其有用的 PyTorch 翻译版本,可在此处
- @ermongroup 的 DDIM 实现,可在此处
- @yang-song 的 Score-VE 和 Score-VP 实现,可在此处
我们还想感谢 @heejkoo 关于扩散模型论文、代码和资源的非常有益的概述,可在此处,以及 @crowsonkb 和 @rromb 的有益讨论和见解。
引用
@misc{von-platen-etal-2022-diffusers,
author = {Patrick von Platen and Suraj Patil and Anton Lozhkov and Pedro Cuenca and Nathan Lambert and Kashif Rasul and Mishig Davaadorj and Dhruv Nair and Sayak Paul and William Berman and Yiyi Xu and Steven Liu and Thomas Wolf},
title = {Diffusers: State-of-the-art diffusion models},
year = {2022},
publisher = {GitHub},
journal = {GitHub repository},
howpublished = {\url{https://github.com/huggingface/diffusers}}
}