扩散模型Diffusers Pipeline API使用介绍

这篇具有很好参考价值的文章主要介绍了扩散模型Diffusers Pipeline API使用介绍。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

1 关于Diffusers Pipeline

1.1 简介

大部分扩散模型包含多个独立训练的子模型和组件模块组合而成,例如StableDiffusion 有:

  • 3个独立训练的子模型:Autoencoder、 Conditional Unet、CLIP text encoder
  • 调度器组件scheduler,
  • CLIPImageProcessor,
  • safety checker.

为了让开发者以最简单的方式使用最新最先进的扩散模型,diffusers开发了pipeline管理和使用这些类,使得开发者可以以端对端方式使用扩散模型。

注意:pipeline本身没有提供任何训练相关功能,如果想要实现训练,可以参考官方的训练样例

1.2 官方Pipeline

以下表格是diffusers官方实现的Pipeline,每个Pipeline有对应的论文。

Pipeline Source Tasks
dance diffusion Dance Diffusion Unconditional Audio Generation
ddpm Denoising Diffusion Probabilistic Models Unconditional Image Generation
ddim Denoising Diffusion Implicit Models Unconditional Image Generation
latent_diffusion High-Resolution Image Synthesis with Latent Diffusion Models Text-to-Image Generation
latent_diffusion_uncond High-Resolution Image Synthesis with Latent Diffusion Models Unconditional Image Generation
pndm Pseudo Numerical Methods for Diffusion Models on Manifolds Unconditional Image Generation
score_sde_ve Score-Based Generative Modeling through Stochastic Differential Equations Unconditional Image Generation
score_sde_vp Score-Based Generative Modeling through Stochastic Differential Equations Unconditional Image Generation
stable_diffusion Stable Diffusion Text-to-Image Generation
stable_diffusion Stable Diffusion Image-to-Image Text-Guided Generation
stable_diffusion Stable Diffusion Text-Guided Image Inpainting
stochastic_karras_ve Elucidating the Design Space of Diffusion-Based Generative Models Unconditional Image Generation

2 Pipeline API接口

扩散模型包含多个独立的模型和组件,不同任务中模型独立训练,并且可以用其他模型替换。不同的Pipeline可能包含专有的函数接口,但所有Pipeline都有的共同函数如下:

  • from_pretrained(cls, pretrained_model_name_or_path, **kwargs): 参数pretrained_model_name_or_path可以是 Hugging Face Hub repository的 id, 例如: runwayml/stable-diffusion-v1-5 或本地路径:"./stable-diffusion". 为了确保所有模型和组件能被正确加载,需要提供一个 model_index.json 文件, 例如: runwayml/stable-diffusion-v1-5/model_index.json, 这个文件定义了所有要被加载的组件。其格式如下: <name>: ["<library>", "<class name>"],其中<name> 是类<class name>实例的名称。此类可以在库"<library>"中加载到。
  • save_pretrained(self, save_directory) : 参数save_directory为本地目录路径,例如: ./stable-diffusion ,所有的模型和组件会被保存。每个模型和组件创建一个对应的子目录,子目录名称为模型或组件的属性名称如./stable_diffusion/unet. 此外,还会再根目录创建 model_index.json 文件如:./stable_diffusion/model_index.json
  • to(self, torch_device: Optional[Union[str, torch.device]] = None) 参数torch_device为 stringtorch.device 类型,将所有torch.nn.Module 类型的对象转移到指定的device上,此函数与pytorch的to函数功能一致。
  • __call__函数执行推理,此函数定义了pipeline的推理逻辑,不同的Pipeline对应的推理输入差别很大,例如文生图Pipeline StableDiffusionPipeline 的输入应该是文本prompt,输出是生成的图。 而DDPMPipeline 则无需提供任何输入。因此读者需要根据实际的Pipeline功能以及查看相应的官方文档使用。

注意: 所有的Pipeline的__call__函数会自动调用torch.no_grad函数禁用梯度,因为Pipeline不是用于训练。如果你在前向推理后有保存梯度的需求,可以自定义Pipeline,参考官方示例

3 使用示例

1 扩散模型:文生图

# make sure you're logged in with `huggingface-cli login`
from diffusers import StableDiffusionPipeline, LMSDiscreteScheduler

pipe = StableDiffusionPipeline.from_pretrained("runwayml/stable-diffusion-v1-5")
pipe = pipe.to("cuda")

prompt = "a photo of an astronaut riding a horse on mars"
image = pipe(prompt).images[0]  
    
image.save("astronaut_rides_horse.png")

2 扩散模型:图生图

StableDiffusionImg2ImgPipeline 接受一个文本prompt和初始图片作为条件,指导生成新图。

import requests
from PIL import Image
from io import BytesIO

from diffusers import StableDiffusionImg2ImgPipeline

# load the pipeline
device = "cuda"
pipe = StableDiffusionImg2ImgPipeline.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
).to(device)

# let's download an initial image
url = "https://raw.githubusercontent.com/CompVis/stable-diffusion/main/assets/stable-samples/img2img/sketch-mountains-input.jpg"

response = requests.get(url)
init_image = Image.open(BytesIO(response.content)).convert("RGB")
init_image = init_image.resize((768, 512))

prompt = "A fantasy landscape, trending on artstation"

images = pipe(prompt=prompt, image=init_image, strength=0.75, guidance_scale=7.5).images

images[0].save("fantasy_landscape.png")

可以在colab中直接运行colab

3 扩充模型:In-painting

StableDiffusionInpaintPipeline 接受文本prompt和mask,用于编辑图像指定区域。

import PIL
import requests
import torch
from io import BytesIO

from diffusers import StableDiffusionInpaintPipeline

def download_image(url):
    response = requests.get(url)
    return PIL.Image.open(BytesIO(response.content)).convert("RGB")

img_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo.png"
mask_url = "https://raw.githubusercontent.com/CompVis/latent-diffusion/main/data/inpainting_examples/overture-creations-5sI6fQgYIuo_mask.png"

init_image = download_image(img_url).resize((512, 512))
mask_image = download_image(mask_url).resize((512, 512))

pipe = StableDiffusionInpaintPipeline.from_pretrained(
    "runwayml/stable-diffusion-inpainting",
    torch_dtype=torch.float16,
)
pipe = pipe.to("cuda")

prompt = "Face of a yellow cat, high resolution, sitting on a park bench"
image = pipe(prompt=prompt, image=init_image, mask_image=mask_image).images[0]

可以在colab中直接运行 colab文章来源地址https://www.toymoban.com/news/detail-721241.html

到了这里,关于扩散模型Diffusers Pipeline API使用介绍的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

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

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

相关文章

  • 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日
    浏览(62)
  • 番外篇Diffusion&Stable Diffusion扩散模型与稳定扩散模型

    本篇文章为阅读笔记,,主要内容围绕扩散模型和稳定扩散模型展开,介绍了kl loss、vae模型的损失函数以及变分下限作为扩展部分。扩散模型是一种生成模型,定义了一个逐渐扩散的马尔科夫链,逐渐项数据添加噪声,然后学习逆扩散过程,从噪声中构建所需的数据样本。稳

    2024年02月03日
    浏览(41)
  • 扩散模型 - Stable Diffusion

    ​ Stable Diffusion 是由 Stability AI 开发的 开源 扩散模型。Stable Diffusion 可以完成多模态任务,包括:文字生成图像(text2img)、图像生成图像(img2img)等。 4.1 Stable Diffusion 的组成部分 ​ Stable Diffusion 由两部分 组成 : 文本编码器:提取文本 prompt 的信息 图像生成器:根据文本

    2024年02月11日
    浏览(46)
  • Stable diffusion扩散模型相关

    时隔两年半(2年4个月),我又回来研究生成技术了。以前学习研究GAN没结果,不管是技术上,还是应用产品上,结果就放弃了,现在基于diffusion的技术又把生成技术带上了一个新的高度。现在自己又来研究学习这方面的东西了。现在看来,以前还是自己自我定位不清晰,想

    2024年01月17日
    浏览(42)
  • 【CV】稳定扩散模型(Stable Diffusion)

      🔎大家好,我是Sonhhxg_柒,希望你看完之后,能对你有所帮助,不足请指正!共同学习交流🔎 📝个人主页-Sonhhxg_柒的博客_CSDN博客 📃 🎁欢迎各位→点赞👍 + 收藏⭐️ + 留言📝​ 📣系列专栏 - 机器学习【ML】 自然语言处理【NLP】  深度学习【DL】 ​​  🖍foreword

    2024年02月09日
    浏览(43)
  • Stable Diffusion扩散模型 + Consistency一致性模型

    通过估计数据分布梯度进行生成建模 一文解释 Diffusion Model (一) DDPM 理论推导 随着人工智能在图像生成,文本生成以及多模态生成等 生成领域 的技术不断累积,生成对抗网络(GAN)、变微分自动编码器(VAE)、normalizing flow models、自回归模型(AR)、energy-based models以及近年来

    2024年02月09日
    浏览(55)
  • Stable Diffusion生成式扩散模型代码实现原理

    Stable Diffusion可以使用PyTorch或TensorFlow等深度学习框架来实现。这些框架提供了一系列的工具和函数,使得开发者可以更方便地构建、训练和部署深度学习模型。因此可以使用PyTorch或TensorFlow来实现Stable Diffusion模型。 安装PyTorch:确保您已经安装了PyTorch,并具备基本的PyTorch使用

    2024年03月13日
    浏览(35)
  • 扩散模型实战(十):Stable Diffusion文本条件生成图像大模型

     扩散模型实战(一):基本原理介绍 扩散模型实战(二):扩散模型的发展 扩散模型实战(三):扩散模型的应用 扩散模型实战(四):从零构建扩散模型 扩散模型实战(五):采样过程 扩散模型实战(六):Diffusers DDPM初探 扩散模型实战(七):Diffusers蝴蝶图像生成实

    2024年02月03日
    浏览(42)
  • 文字转图片生成系统-Stable diffusion稳定扩散模型

    二话不说先上效果图:(附带代码和模型资源文件)  让它画一个超级汽车在海边。。  近期百度推出了文言一心, 一个能回答问题,能根据文字描述绘制图片的服务,前期可能不太完善出现了一些失误,不过这个idea还是相当不错的   这个东西挺好哈,作为文学创作,生成

    2024年02月09日
    浏览(40)
  • Stable Diffusion扩散模型推导公式的基础知识

    A 和 B 是两个独立事件: ⇒ Rightarrow ⇒ P ( A ∣ B ) = P ( A ) P(A|B)=P(A) P ( A ∣ B ) = P ( A ) , P ( B ∣ A ) = P ( B ) P(B|A)=P(B) P ( B ∣ A ) = P ( B ) , ⇒ Rightarrow ⇒ P ( A , B ∣ C ) = P ( A ∣ C ) P ( B ∣ C ) P(A,B|C)=P(A|C)P(B|C) P ( A , B ∣ C ) = P ( A ∣ C ) P ( B ∣ C ) 贝叶斯公式: P ( A ∣ B ) = P ( B ∣

    2024年04月10日
    浏览(42)

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

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

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

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

二维码1

领取红包

二维码2

领红包