🤗 Diffusers 是 Hugging Face 開發的最先進的擴散模型庫,專門用於圖像、音訊甚至分子3D結構的生成。無論你是尋找簡單的推理解決方案還是訓練自己的擴散模型,🤗 Diffusers 都是一個支持兩者的模組化工具箱。
項目地址: https://github.com/huggingface/diffusers
最先進的擴散管道 (Diffusion Pipelines)
可互換的噪聲調度器 (Noise Schedulers)
預訓練模型 (Pretrained Models)
# 官方包
pip install --upgrade diffusers[torch]
# 社區維護的 conda 版本
conda install -c conda-forge diffusers
pip install --upgrade diffusers[flax]
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
任務 | 管道 | 推薦模型 |
---|---|---|
無條件圖像生成 | DDPMPipeline | google/ddpm-ema-church-256 |
文本到圖像 | StableDiffusionPipeline | stable-diffusion-v1-5/stable-diffusion-v1-5 |
文本到圖像 (unCLIP) | UnCLIPPipeline | kakaobrain/karlo-v1-alpha |
文本到圖像 (DeepFloyd IF) | IFPipeline | DeepFloyd/IF-I-XL-v1.0 |
文本到圖像 (Kandinsky) | KandinskyPipeline | kandinsky-community/kandinsky-2-2-decoder |
可控生成 | StableDiffusionControlNetPipeline | lllyasviel/sd-controlnet-canny |
圖像編輯 | StableDiffusionInstructPix2PixPipeline | timbrooks/instruct-pix2pix |
圖像到圖像 | StableDiffusionImg2ImgPipeline | stable-diffusion-v1-5/stable-diffusion-v1-5 |
圖像修復 | StableDiffusionInpaintPipeline | runwayml/stable-diffusion-inpainting |
圖像變體 | StableDiffusionImageVariationPipeline | lambdalabs/sd-image-variations-diffusers |
圖像超分辨率 | StableDiffusionUpscalePipeline | stabilityai/stable-diffusion-x4-upscaler |
潛在空間超分 | StableDiffusionLatentUpscalePipeline | stabilityai/sd-x2-latent-upscaler |
文檔類型 | 學習內容 |
---|---|
Tutorial | 學習庫的基本技能,如使用模型和調度器構建擴散系統,訓練自己的擴散模型 |
Loading | 如何加載和配置庫的所有組件(管道、模型和調度器),以及如何使用不同的調度器 |
Pipelines for inference | 如何使用管道進行不同的推理任務、批量生成、控制生成輸出和隨機性 |
Optimization | 如何優化管道以在記憶體受限的硬體上運行,並加速推理 |
Training | 如何訓練自己的擴散模型以進行不同任務 |
🤗 Diffusers 是目前最完整、最易用的擴散模型庫之一。它不僅提供了豐富的預訓練模型和管道,還支持自定義訓練和優化。無論是AI研究者、開發者還是創作者,都能在這個庫中找到所需的工具來實現各種生成式AI應用。