🤗 Diffusers 是一个用于生成图像、音频甚至分子 3D 结构的先进预训练扩散模型的首选库。无论您是在寻找简单的推理解决方案,还是想训练自己的扩散模型,🤗 Diffusers 都是一个支持两者的模块化工具箱。我们的库设计理念是:[可用性优于性能](https://huggingface.co/docs/diffusers/conceptual/philosophy#usability-over-performance)、[简洁优于简易](https://huggingface.co/docs/diffusers/conceptual/philosophy#simple-over-easy) 以及 [可定制性优于抽象](https://huggingface.co/docs/diffusers/conceptual/philosophy#tweakable-contributorfriendly-over-abstraction)。 🤗 Diffusers 提供三个核心组件: - 先进的[扩散管道](https://huggingface.co/docs/diffusers/api/pipelines/overview),只需几行代码即可运行推理。 - 可互换的噪声[调度器](https://huggingface.co/docs/diffusers/api/schedulers/overview),用于不同的扩散速度和输出质量。 - 预训练的[模型](https://huggingface.co/docs/diffusers/api/models/overview),可作为构建模块,并与调度器结合,创建您自己的端到端扩散系统。 ## 安装 我们建议在虚拟环境中通过 PyPI 或 Conda 安装 🤗 Diffusers。有关安装 [PyTorch](https://pytorch.org/get-started/locally/) 的更多详细信息,请参阅其官方文档。 ### PyTorch 使用 `pip`(官方包):
pip install --upgrade diffusers[torch]
使用 `conda`(由社区维护):
conda install -c conda-forge diffusers
### Apple Silicon (M1/M2) 支持
请参阅 [如何在 Apple Silicon 上使用 Stable Diffusion](https://huggingface.co/docs/diffusers/optimization/mps) 指南。
## 快速开始
使用 🤗 Diffusers 生成输出非常简单。要从文本生成图像,请使用 `from_pretrained` 方法加载任何预训练的扩散模型(在 [Hub](https://huggingface.co/models?library=diffusers&sort=downloads) 上浏览 30,000 多个检查点):
from diffusers import DiffusionPipeline
import torch
pipeline = DiffusionPipeline.from_pretrained("stable-diffusion-v1-5/stable-diffusion-v1-5", torch_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
查看 [快速开始](https://huggingface.co/docs/diffusers/quicktour) 指南,立即开启您的扩散之旅!
## 如何浏览文档
| **文档** | **我能学到什么?** |
|---------------------------------------------------------------------|-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [教程](https://huggingface.co/docs/diffusers/tutorials/tutorial_overview) | 学习如何使用库中最重要的功能的基础速成课程,例如使用模型和调度器构建自己的扩散系统,以及训练自己的扩散模型。 |
| [加载](https://huggingface.co/docs/diffusers/using-diffusers/loading) | 关于如何加载和配置库的所有组件(管道、模型和调度器)的指南,以及如何使用不同的调度器。 |
| [推理管道](https://huggingface.co/docs/diffusers/using-diffusers/overview_techniques) | 关于如何为不同推理任务使用管道、批量生成、控制生成输出和随机性的指南,以及如何为库贡献管道。 |
| [优化](https://huggingface.co/docs/diffusers/optimization/fp16) | 关于如何优化扩散模型以运行更快、消耗更少内存的指南。 |
| [训练](https://huggingface.co/docs/diffusers/training/overview) | 关于如何使用不同训练技术为不同任务训练扩散模型的指南。 |
## 贡献
我们 ❤️ 来自开源社区的贡献!
如果您想为此库做出贡献,请查看我们的 [贡献指南](https://github.com/huggingface/diffusers/blob/main/CONTRIBUTING.md)。
您可以寻找想要解决的 [问题](https://github.com/huggingface/diffusers/issues) 来为库做贡献。
- 查看 [Good first issues](https://github.com/huggingface/diffusers/issues?q=is%3Aopen+is%3Aissue+label%3A%22good+first+issue%22) 了解一般的贡献机会
- 查看 [New model/pipeline](https://github.com/huggingface/diffusers/issues?q=is%3Aopen+is%3Aissue+label%3A%22New+pipeline%2Fmodel%22) 贡献令人兴奋的新扩散模型 / 扩散管道
- 查看 [New scheduler](https://github.com/huggingface/diffusers/issues?q=is%3Aopen+is%3Aissue+label%3A%22New+scheduler%22)
同时,在我们的公共 Discord 频道中打个招呼 👋 @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}}
}