Stable Diffusion with Diffusers 学习笔记: 原理+完整pipeline代码

这篇具有很好参考价值的文章主要介绍了Stable Diffusion with Diffusers 学习笔记: 原理+完整pipeline代码。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

参考链接:
https://huggingface.co/blog/stable_diffusion#how-does-stable-diffusion-work

在这篇文章中,我们想展示如何使用Stable Diffusion with the 🧨 Diffusers library,,解释模型是如何工作的,最后深入探讨扩散器是如何允许自定义图像生成pipeline的。

如果你对扩散模型完全陌生,我们建议你阅读下面的博客文章:

  • The Annotated Diffusion Model
  • Getting started with 🧨 Diffusers

01 使用

首先,你应该安装diffusers==0.10.2:

pip install diffusers==0.10.2 transformers scipy ftfy accelerate

The Stable Diffusion model可以在推理中运行,只需使用StableDiffusionPipeline管道的几行。通过简单的from_pretrained函数调用,pipeline设置了从文本生成图像所需的一切。

from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4")
pipe.to("cuda")

如果你受到GPU内存的限制,并且可用的GPU RAM少于10GB,请确保以float16精度加载StableDiffusionPipeline,而不是如上所述的默认float32精度。

你可以通过从fp16分支加载权重并告诉扩散器期望权重为float16精度来实现:

import torch
from diffusers import StableDiffusionPipeline

pipe = StableDiffusionPipeline.from_pretrained("CompVis/stable-diffusion-v1-4", revision="fp16", torch_dtype=torch.float16)

要运行管道,只需定义prompt和调用pipe

prompt = "a photograph of an astronaut riding a horse"

image = pipe(prompt).images[0]

# you can save the image with
# image.save(f"astronaut_rides_horse.png")

如果您想要确定性输出,您可以设置一个随机种子并将生成器传递给管道。

import torch

generator = torch.Generator("cuda").manual_seed(1024)
image = pipe(prompt, guidance_scale=7.5, generator=generator).images[0]

可以使用num_inference_steps参数更改推理步骤的数量。

接下来,让我们看看如何同时生成相同提示符的多个图像。首先,我们将创建一个image_grid函数,以帮助我们在网格中很好地可视化它们。

from PIL import Image

def image_grid(imgs, rows, cols):
    assert len(imgs) == rows*cols

    w, h = imgs[0].size
    grid = Image.new('RGB', size=(cols*w, rows*h))
    grid_w, grid_h = grid.size
    
    for i, img in enumerate(imgs):
        grid.paste(img, box=(i%cols*w, i//cols*h))
    return grid

我们可以通过简单地使用具有重复多次的相同提示符的列表来为同一个提示符生成多个图像。我们将把列表发送到管道,而不是之前使用的字符串。

num_images = 3
prompt = ["a photograph of an astronaut riding a horse"] * num_images

images = pipe(prompt).images

grid = image_grid(images, rows=1, cols=3)

# you can save the grid with
# grid.save(f"astronaut_rides_horse.png")

02 Stable Diffusion 的工作原理

Latent diffusion可以通过在较低维隐空间上应用扩散过程来减少内存和计算复杂度,而不是使用实际的像素空间。这是standard diffusion和Latent diffusion模型之间的关键区别:在Latent diffusion中,模型被训练成生成图像的latent(压缩)表示。

latent Diffusion model 三个主要组成部分

  • An autoencoder (VAE).
  • A U-Net.
  • A text-encoder, e.g. CLIP’s Text Encoder.

The autoencoder (VAE)

VAE模型有两个部分,一个编码器和一个解码器。编码器用于将图像转换为低维潜在表示,这将作为U-Net模型的输入。相反,解码器将潜在的表征转换回图像。

The U-Net

U-Net有一个编码器部分和一个解码器部分,两者都由ResNet块组成。编码器将图像表示压缩为较低分辨率的图像表示,解码器将较低分辨率的图像表示解码回假定噪声较小的原始较高分辨率的图像表示。更具体地说,U-Net输出预测噪声残差,可用于计算预测的去噪图像表示。

为了防止U-Net在downsampling时丢失重要信息,通常在编码器的 downsampling resnet和解码器的upsampling resnet之间添加捷径连接。此外,Stable Diffusion U-Net能够通过交叉注意层调节文本嵌入的输出。交叉注意层通常在ResNet块之间添加到U-Net的编码器和解码器部分。

The Text-encoder

文本编码器负责转换输入提示符,例如:"An astronaut riding a horse"进入U-Net可以理解的嵌入空间。它通常是一个简单的基于转换器的编码器,它将一系列输入标记映射到一系列潜在的文本嵌入。

在训练期间,Stable Diffusion不训练文本编码器,而只是使用CLIP已经训练好的文本编码器CLIPTextModel。

Latent Diffusion 又快又高效的原因

  • 在低纬空间操作,减少内存和计算需求。

Stable Diffusion 的推断过程

Stable Diffusion with  Diffusers 学习笔记: 原理+完整pipeline代码,Diffusion,stable diffusion,Diffusers,LDM,sdxl,AIGC,Text-to-image

  • 以latent seed 和 text prompt 为输入
  • U-Net 迭代去噪
  • U-Netde 输出称为 noise residual,用来计算 denoised latent image representation.

对于 Stable Diffusion,我们推荐:

  • PNDM scheduler (used by default)
  • DDIM scheduler
  • K-LMS scheduler

03 编写你自己的inference pipeline

from transformers import CLIPTextModel, CLIPTokenizer
from diffusers import AutoencoderKL, UNet2DConditionModel, PNDMScheduler

# 1. Load the autoencoder model which will be used to decode the latents into image space. 
vae = AutoencoderKL.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="vae")

# 2. Load the tokenizer and text encoder to tokenize and encode the text. 
tokenizer = CLIPTokenizer.from_pretrained("openai/clip-vit-large-patch14")
text_encoder = CLIPTextModel.from_pretrained("openai/clip-vit-large-patch14")

# 3. The UNet model for generating the latents.
unet = UNet2DConditionModel.from_pretrained("CompVis/stable-diffusion-v1-4", subfolder="unet")


# 4. load the K-LMS scheduler with some fitting parameters
from diffusers import LMSDiscreteScheduler

scheduler = LMSDiscreteScheduler(beta_start=0.00085, beta_end=0.012, beta_schedule="scaled_linear", num_train_timesteps=1000)

# 5. move the models to GPU
torch_device = "cuda"
vae.to(torch_device)
text_encoder.to(torch_device)
unet.to(torch_device) 


# 6. set parameters
prompt = ["a photograph of an astronaut riding a horse"]

height = 512                        # default height of Stable Diffusion
width = 512                         # default width of Stable Diffusion

num_inference_steps = 100           # Number of denoising steps

guidance_scale = 7.5                # Scale for classifier-free guidance

generator = torch.manual_seed(0)    # Seed generator to create the inital latent noise

batch_size = len(prompt)


# 7. get the text_embeddings for the passed prompt
text_input = tokenizer(prompt, padding="max_length", max_length=tokenizer.model_max_length, truncation=True, return_tensors="pt")

text_embeddings = text_encoder(text_input.input_ids.to(torch_device))[0]


# 8. get the unconditional text embeddings for classifier-free guidance
# They need to have the same shape as the conditional text_embeddings (batch_size and seq_length)
max_length = text_input.input_ids.shape[-1]
uncond_input = tokenizer(
    [""] * batch_size, padding="max_length", max_length=max_length, return_tensors="pt"
)
uncond_embeddings = text_encoder(uncond_input.input_ids.to(torch_device))[0]


# 9. concatenate both text_embeddings and uncond_embeddings into a single batch to avoid doing two forward passes
text_embeddings = torch.cat([uncond_embeddings, text_embeddings])


# 10. generate the initial random noise
latents = torch.randn(
    (batch_size, unet.in_channels, height // 8, width // 8),
    generator=generator,
)
latents = latents.to(torch_device)

# 11. initialize the scheduler with our chosen num_inference_steps. 
scheduler.set_timesteps(num_inference_steps)

# 12. The K-LMS scheduler needs to multiply the latents by its sigma values. Let's do this here:
latents = latents * scheduler.init_noise_sigma


# 13. write the denoising loop
from tqdm.auto import tqdm

scheduler.set_timesteps(num_inference_steps)

for t in tqdm(scheduler.timesteps):
    # expand the latents if we are doing classifier-free guidance to avoid doing two forward passes.
    latent_model_input = torch.cat([latents] * 2)

    latent_model_input = scheduler.scale_model_input(latent_model_input, timestep=t)

    # predict the noise residual
    with torch.no_grad():
        noise_pred = unet(latent_model_input, t, encoder_hidden_states=text_embeddings).sample

    # perform guidance
    noise_pred_uncond, noise_pred_text = noise_pred.chunk(2)
    noise_pred = noise_pred_uncond + guidance_scale * (noise_pred_text - noise_pred_uncond)

    # compute the previous noisy sample x_t -> x_t-1
    latents = scheduler.step(noise_pred, t, latents).prev_sample


# 14. use the vae to decode the generated latents back into the image
latents = 1 / 0.18215 * latents
with torch.no_grad():
    image = vae.decode(latents).sample

# 15. convert the image to PIL so we can display or save it
image = (image / 2 + 0.5).clamp(0, 1)
image = image.detach().cpu().permute(0, 2, 3, 1).numpy()
images = (image * 255).round().astype("uint8")
pil_images = [Image.fromarray(image) for image in images]

Citation:

@article{patil2022stable,
author = {Patil, Suraj and Cuenca, Pedro and Lambert, Nathan and von Platen, Patrick},
title = {Stable Diffusion with 🧨 Diffusers},
journal = {Hugging Face Blog},
year = {2022},
note = {https://huggingface.co/blog/rlhf},
}文章来源地址https://www.toymoban.com/news/detail-755078.html

到了这里,关于Stable Diffusion with Diffusers 学习笔记: 原理+完整pipeline代码的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • huggingface的diffusers训练stable diffusion记录

    目录 1.原理                 扩散模型的目的是什么?                         扩散模型是怎么做的?                         前向过程在干啥?                 反向过程在干啥? 2.安装环境 3.lora 训练 4.推理 5.源代码​         

    2024年04月26日
    浏览(25)
  • 代码笔记:Generate lmages with Stable Diffusion

    目录 1、conda环境 2、使用过程中遇到的问题 (1)环境名称:ldm_py38 (2)pip -e git+https://github.com/CompVis/taming-transformers.git@master#egg=taming-transformers (以及pip -e git+https://github.com/openai/CLIP.git@main#egg=clip)报错:ERROR: Command errored out with exit status 128 报错原因:服务器的SSL证书没有经

    2024年02月10日
    浏览(25)
  • 【Stable Diffusion XL】huggingface diffusers 官方教程解读

    相关链接: GitHub: https://github.com/huggingface/diffusers 官方教程:https://huggingface.co/docs/diffusers/tutorials/tutorial_overview StableDiffuson: https://huggingface.co/blog/stable_diffusion#how-does-stable-diffusion-work Diffusers被设计成一个用户友好和灵活的工具箱,用于构建适合您用例的扩散系统。工具箱的核

    2024年02月06日
    浏览(41)
  • Diffusers 0.9.0 正式发布,支持 Stable Diffusion 2!

    扩散模型 (Diffusion models) 是「生成模型」算法领域中的一个相对较新的算法。生成模型通过学习大量训练样本(例如图像或者音频),创建一些与训练数据神似,但又不是完全相同的多样化输出。 Diffusers 库是一个操作扩散模型的工具箱,由 Hugging Face 团队发布,它可以非常方

    2024年02月09日
    浏览(27)
  • 如何将 Stable Diffusion PT+YAML 转换成 diffusers 格式

    Huggingface 的 diffusers 格式是初学者最爱的格式,只需要简单几行代码,就可以下载模型,执行 文字到图片 转换等常用功能 而有时候在网上淘模型的时候,经常会遇到原版 Stable Diffusion 格式,只有一个 .pt 文件和一个 .yaml 配置文件 ,为了方便管理和加载,可以把原版格式转换

    2024年02月16日
    浏览(28)
  • diffusers加速文生图速度;stable-diffusion、PixArt-α模型

    参考: https://pytorch.org/blog/accelerating-generative-ai-3/ https://colab.research.google.com/drive/1jZ5UZXk7tcpTfVwnX33dDuefNMcnW9ME?usp=sharing#scrollTo=jueYhY5YMe22 大概GPU资源8G-16G;另外模型资源下载慢可以在国内镜像:https://aifasthub.com/ 1、加速代码 能加速到2秒左右

    2024年04月23日
    浏览(60)
  • 使用 Docker 和 Diffusers 快速上手 Stable Video Diffusion 图生视频大模型

    本篇文章聊聊,如何快速上手 Stable Video Diffusion (SVD) 图生视频大模型。 月底计划在机器之心的“AI技术论坛”做关于使用开源模型 “Stable Diffusion 模型” 做有趣视频的实战分享。 因为会议分享时间有限,和之前一样,比较简单的部分,就用博客文章的形式来做补充分享吧。

    2024年01月24日
    浏览(53)
  • Hugging Face使用Stable diffusion Diffusers Transformers Accelerate Pipelines VAE

    A library that offers an implementation of various diffusion models, including text-to-image models. 提供不同扩散模型的实现的库,代码上最简洁,国内的问题是 huggingface 需要翻墙。 A Hugging Face library that provides pre-trained deep learning models for natural language processing tasks. 提供了预训练深度学习模型,

    2024年02月07日
    浏览(39)
  • 《满怀美梦的小崽子是pycharm主义者》之服务器部署stable diffusion /diffusers教程

    距离上一次教大家在本地部署sd已经过去了........俺也不知道多久了,相信大家现在应该都已经很熟悉了吧,估计大家也发现了一个问题,就是本地的配置跑sd,一个是对配置要求太高了,现在的模型都特别大,没有一张3090根本玩不了,一个是内存啥的根本不够用模型加上各种

    2024年02月04日
    浏览(34)
  • Stable Diffusion学习笔记

    人工智能真是厉害。。。 我也不能落后 虽然前面pytorch还没有学完,但是热点总是在变的嘛,现在大模型和生成式AI这么火,我也来蹭蹭热度。 就从学习怎么用AIGC工具生成 老婆 纸片人开始吧 ❤ 2023.6.3 ❤ 首先找个教程,就在B站搜一下找播放量最高的学吧 好 决定就是你了

    2024年02月10日
    浏览(26)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包