更准确地说,
为了开始使用,我们建议你看一下两个笔记本:
diffusers
Stable Diffusion是由CompVis,Stability AI和LAION的研究人员和工程师创建的文本到图像潜在扩散模型。它是在来自LAION-5B数据库子集的512x512图像上训练的。此模型使用冻结的 CLIP ViT-L/14 文本编码器根据文本提示对模型进行调理。凭借其860M UNet和123M文本编码器,该型号相对轻巧,可在具有至少10GB VRAM的GPU上运行。有关详细信息,请参阅型号卡。
你需要先接受模型许可,然后才能下载或使用稳定扩散权重。如果你同意,请访问模型卡,阅读许可证并勾选复选框。你必须是 Hugging Face Hub 中的
# make sure you're logged in with `huggingface-cli login`
from torch import autocast
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler
lms = LMSDiscreteScheduler(
beta_start=0.00085,
beta_end=0.012,
beta_schedule="scaled_linear"
)
pipe = StableDiffusionPipeline.from_pretrained(
"CompVis/stable-diffusion-v1-3",
scheduler=lms,
use_auth_token=True
).to("cuda")
prompt = "a photo of an astronaut riding a horse on mars"
with autocast("cuda"):
image = pipe(prompt)["sample"][0]
image.save("astronaut_rides_horse.png")
如果你想自己
# !pip install diffusers transformers
from diffusers import DiffusionPipeline
model_id = "CompVis/ldm-text2im-large-256"
# load model and scheduler
ldm = DiffusionPipeline.from_pretrained(model_id)
# run pipeline in inference (sample random noise and denoise)
prompt = "A painting of a squirrel eating a burger"
images = ldm([prompt], num_inference_steps=50, eta=0.3, guidance_scale=6)["sample"]
# save images
for idx, image in enumerate(images):
image.save(f"squirrel-{idx}.png")
# !pip install diffusers
from diffusers import DDPMPipeline, DDIMPipeline, PNDMPipeline
model_id = "google/ddpm-celebahq-256"
# load model and scheduler
ddpm = DDPMPipeline.from_pretrained(model_id) # you can replace DDPMPipeline with DDIMPipeline or PNDMPipeline for faster inference
# run pipeline in inference (sample random noise and denoise)
image = ddpm()["sample"]
# save image
image[0].save("ddpm_generated_image.png")
如果你只想玩一些网络演示,可以尝试以下
型 | 拥抱面部空间 |
---|---|
文本到图像潜在扩散 | |
人脸生成器 | |
具有不同调度程序的 DDPM |
模型:神经网络,$p_\theta(\mathbf{x}_{t-1}|\mathbf{x}_t)$(见下图)进行建模,并经过端到端训练以消除图像的噪声输入。示例:UNet、条件 UNet、3D UNet、Transformer UNet
图来自 DDPM 纸张(https://arxiv.org/abs/2006.11239)。
调度程序:用于推理和训练的算法类。该类提供了根据 alpha、beta 计划计算先前图像以及预测训练噪声的功能。示例:DDPM、DDIM、PNDM、DEIS
采样和训练算法。图来自 DDPM 纸张(https://arxiv.org/abs/2006.11239)。
扩散管道:端到端管道,包括多个扩散模型,可能的文本编码器,...示例:滑动、潜扩散、成像、DALL-E 2
图来自ImageGen(https://imagen.research.google/)。
带点
pip install --upgrade diffusers # should install diffusers 0.2.4
与康达
conda install -c conda-forge diffusers
对于第一个版本,
一些管道组件已经在开发中,即:
我们希望扩散器成为一般扩散器模型有用的工具箱;如果你发现自己受到当前 API 的任何限制,或者希望看到其他模型、调度程序或技术,请打开 GitHub 问题,提及你希望看到的内容。
这个库具体化了许多不同作者以前的工作,如果没有他们伟大的研究和实现,这是不可能实现的。我们要特别感谢以下实现,这些实现帮助我们进行开发,没有这些实现,API就不会像今天这样完善:
我们还要感谢@heejkoo对扩散模型的论文、代码和资源的非常有用的概述,以及@crowsonkb和@rromb,以进行有用的讨论和见解。