OA0
OA0 是一个探索 AI 的社区
现在注册
已注册用户请  登录
OA0  ›  代码  ›  Diffusers — 文生图扩散模型库

Diffusers — 文生图扩散模型库

 
  omega ·  2025-11-24 14:11:32 · 30 次点击  · 0 条评论  



GitHub GitHub release GitHub release Contributor Covenant X account

🤗 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 频道中打个招呼 👋 Join us on Discord。我们讨论关于扩散模型的最新趋势,在贡献、个人项目上互相帮助,或者只是闲聊 ☕。 ## 热门任务与管道
任务 管道 🤗 Hub
无条件图像生成 DDPM google/ddpm-ema-church-256
文本到图像 Stable Diffusion 文本到图像 stable-diffusion-v1-5/stable-diffusion-v1-5
文本到图像 unCLIP kakaobrain/karlo-v1-alpha
文本到图像 DeepFloyd IF DeepFloyd/IF-I-XL-v1.0
文本到图像 Kandinsky kandinsky-community/kandinsky-2-2-decoder
文本引导的图像到图像 ControlNet lllyasviel/sd-controlnet-canny
文本引导的图像到图像 InstructPix2Pix timbrooks/instruct-pix2pix
文本引导的图像到图像 Stable Diffusion 图像到图像 stable-diffusion-v1-5/stable-diffusion-v1-5
文本引导的图像修复 Stable Diffusion 修复 stable-diffusion-v1-5/stable-diffusion-inpainting
图像变体 Stable Diffusion 图像变体 lambdalabs/sd-image-variations-diffusers
超分辨率 Stable Diffusion 放大 stabilityai/stable-diffusion-x4-upscaler
超分辨率 Stable Diffusion 潜在放大 stabilityai/sd-x2-latent-upscaler
## 使用 🧨 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 的潜在扩散模型库,可在 [这里](https://github.com/CompVis/latent-diffusion) 获取 - @hojonathanho 的原始 DDPM 实现,可在 [这里](https://github.com/hojonathanho/diffusion) 获取,以及由 @pesser 翻译成 PyTorch 的极其有用的版本,可在 [这里](https://github.com/pesser/pytorch_diffusion) 获取 - @ermongroup 的 DDIM 实现,可在 [这里](https://github.com/ermongroup/ddim) 获取 - @yang-song 的 Score-VE 和 Score-VP 实现,可在 [这里](https://github.com/yang-song/score_sde_pytorch) 获取 我们还要感谢 @heejkoo 提供的关于扩散模型的论文、代码和资源的非常有用的概述,可在 [这里](https://github.com/heejkoo/Awesome-Diffusion-Models) 获取,以及 @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}}
}
30 次点击  ∙  0 人收藏  
登录后收藏  
0 条回复
关于 ·  帮助 ·  PING ·  隐私 ·  条款   
OA0 - Omni AI 0 一个探索 AI 的社区
沪ICP备2024103595号-2
耗时 33 ms
Developed with Cursor