diffusers-Load pipelines,models,and schedulers

https://huggingface.co/docs/diffusers/using-diffusers/loadingicon-default.png?t=N7T8https://huggingface.co/docs/diffusers/using-diffusers/loading

有一种简便的方法用于推理是至关重要的。扩散系统通常由多个组件组成,如parameterized model、tokenizers和schedulers,它们以复杂的方式进行交互。这就是为什么我们设计了DiffusionPipeline,将整个扩散系统的复杂性包装成易于使用的API,同时保持足够的灵活性,以适应其他用例,例如将每个组件单独加载作为构建块来组装自己的扩散系统。

1.Diffusion Pipeline

DiffusionPipeline是扩散模型最简单最通用的方法。

from diffusers import DiffusionPipelinerepo_id = "runwayml/stable-diffusion-v1-5"
pipe = DiffusionPipeline.from_pretrained(repo_id, use_safetensors=True)

也可以使用特定的pipeline

from diffusers import StableDiffusionPipelinerepo_id = "runwayml/stable-diffusion-v1-5"
pipe = StableDiffusionPipeline.from_pretrained(repo_id, use_safetensors=True)

Community pipelines是原始实现不同于DiffusionPipeline,例如StableDiffusionControlNetPipeline.

1.1 local pipeline

from diffusers import DiffusionPipelinerepo_id = "./stable-diffusion-v1-5" # local path
stable_diffusion = DiffusionPipeline.from_pretrained(repo_id, use_safetensors=True)

from_pretrained()方法在检测到本地路径时不会下载。

1.2 swap components in a pipeline

可以使用另一个兼容的组件来自定义任何流程的默认组件。定制非常重要,因为:

  1. 更改调度器对于探索生成速度和质量之间的权衡是重要的。
  2. 模型的不同组件通常是独立训练的,您可以用性能更好的组件替换掉现有组件。
  3. 在微调过程中,通常只有一些组件(如UNet或文本编码器)进行训练。
from diffusers import DiffusionPipelinerepo_id = "runwayml/stable-diffusion-v1-5"
stable_diffusion = DiffusionPipeline.from_pretrained(repo_id, use_safetensors=True)
stable_diffusion.scheduler.compatibles
from diffusers import DiffusionPipeline, EulerDiscreteScheduler, DPMSolverMultistepSchedulerrepo_id = "runwayml/stable-diffusion-v1-5"
scheduler = EulerDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
stable_diffusion = DiffusionPipeline.from_pretrained(repo_id, scheduler=scheduler, use_safetensors=True)

可以将PNDMScheduler更换为EulerDiscreteScheduler,在回传到DiffusionPipeline中。

1.3 safety checker

safety checker可以根据已知的NSFW内容检查生成的输出, 

from diffusers import DiffusionPipelinerepo_id = "runwayml/stable-diffusion-v1-5"
stable_diffusion = DiffusionPipeline.from_pretrained(repo_id, safety_checker=None, use_safetensors=True)

1.4 reuse components across pipelines

可以在多个pipeline中可以重复使用相同的组件,以避免将权重加载到RAM中2次

from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipelinemodel_id = "runwayml/stable-diffusion-v1-5"
stable_diffusion_txt2img = StableDiffusionPipeline.from_pretrained(model_id, use_safetensors=True)components = stable_diffusion_txt2img.components

可以将components传递到另一个pipeline中,无需将权重重新加载到RAM中:

stable_diffusion_img2img = StableDiffusionImg2ImgPipeline(**components)

下面的方式更加灵活:

from diffusers import StableDiffusionPipeline, StableDiffusionImg2ImgPipelinemodel_id = "runwayml/stable-diffusion-v1-5"
stable_diffusion_txt2img = StableDiffusionPipeline.from_pretrained(model_id, use_safetensors=True)
stable_diffusion_img2img = StableDiffusionImg2ImgPipeline(vae=stable_diffusion_txt2img.vae,text_encoder=stable_diffusion_txt2img.text_encoder,tokenizer=stable_diffusion_txt2img.tokenizer,unet=stable_diffusion_txt2img.unet,scheduler=stable_diffusion_txt2img.scheduler,safety_checker=None,feature_extractor=None,requires_safety_checker=False,
)

1.5 checkpoint variants

以torch.float16保存,节省一半的内存,但是无法训练,EMA不用于推理,用于微调模型。

2. models

from diffusers import UNet2DConditionModelrepo_id = "runwayml/stable-diffusion-v1-5"
model = UNet2DConditionModel.from_pretrained(repo_id, subfolder="unet", use_safetensors=True)

所有的权重都存储在一个safetensors中, 可以用.from_single_file()来加载模型。safetensors安全且加载速度快。

2.1 load different stable diffusion formats

.ckpt也可以用from_single_file(),但最好转成hf格式,可以使用diffusers官方提供的服务转:https://huggingface.co/spaces/diffusers/sd-to-diffusers

也可以使用脚本转:https://github.com/huggingface/diffusers/blob/main/scripts/convert_original_stable_diffusion_to_diffusers.py

python ../diffusers/scripts/convert_original_stable_diffusion_to_diffusers.py --checkpoint_path temporalnetv3.ckpt --original_config_file cldm_v15.yaml --dump_path ./ --controlnet

 A1111 Lora文件,diffusers可以使用load_lora_weights()加载lora模型:

from diffusers import DiffusionPipeline, UniPCMultistepScheduler
import torchpipeline = DiffusionPipeline.from_pretrained("andite/anything-v4.0", torch_dtype=torch.float16, safety_checker=None
).to("cuda")
pipeline.scheduler = UniPCMultistepScheduler.from_config(pipeline.scheduler.config)# uncomment to download the safetensor weights
#!wget https://civitai.com/api/download/models/19998 -O howls_moving_castle.safetensorspipeline.load_lora_weights(".", weight_name="howls_moving_castle.safetensors")prompt = "masterpiece, illustration, ultra-detailed, cityscape, san francisco, golden gate bridge, california, bay area, in the snow, beautiful detailed starry sky"
negative_prompt = "lowres, cropped, worst quality, low quality, normal quality, artifacts, signature, watermark, username, blurry, more than one bridge, bad architecture"images = pipeline(prompt=prompt,negative_prompt=negative_prompt,width=512,height=512,num_inference_steps=25,num_images_per_prompt=4,generator=torch.manual_seed(0),
).imagesfrom diffusers.utils import make_image_gridmake_image_grid(images, 2, 2)

3.scheduler

scheduler没有参数化或训练;由配置文件定义。加载scheduler不会消耗大的内存,并且相同的配置文件可以用于各种不同的scheduler,比如下面的scheduler均可与StableDiffusionPipline兼容。

Diffusion流程本质上是由扩散模型和scheduler组成的集合,它们在一定程度上彼此独立。这意味着可以替换流程的某些部分,其中最好的例子就是scheduler。扩散模型通常只定义从噪声到较少噪声样本的前向传递过程,而调度器定义了整个去噪过程,包括:

去噪步骤是多少?随机的还是确定性的?用什么算法找到去噪样本? 调度器可以非常复杂,并且经常在去噪速度和去噪质量之间进行权衡。

from diffusers import StableDiffusionPipeline
from diffusers import (DDPMScheduler,DDIMScheduler,PNDMScheduler,LMSDiscreteScheduler,EulerDiscreteScheduler,EulerAncestralDiscreteScheduler,DPMSolverMultistepScheduler,
)repo_id = "runwayml/stable-diffusion-v1-5"ddpm = DDPMScheduler.from_pretrained(repo_id, subfolder="scheduler")
ddim = DDIMScheduler.from_pretrained(repo_id, subfolder="scheduler")
pndm = PNDMScheduler.from_pretrained(repo_id, subfolder="scheduler")
lms = LMSDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
euler_anc = EulerAncestralDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
euler = EulerDiscreteScheduler.from_pretrained(repo_id, subfolder="scheduler")
dpm = DPMSolverMultistepScheduler.from_pretrained(repo_id, subfolder="scheduler")# replace `dpm` with any of `ddpm`, `ddim`, `pndm`, `lms`, `euler_anc`, `euler`
pipeline = StableDiffusionPipeline.from_pretrained(repo_id, scheduler=dpm, use_safetensors=True)

4.DiffusionPipline explained

作为一个类方法,DiffusionPipeline.from_pretrained()做两件事,1.下载推理所需的权重并缓存,一般存在在.cache文件中,2.将缓存文件中的model_index.json进行实例化。

feature_extractor--CLIPFeatureExtractor(transformers);scheduler--PNDMScheduler;text_encoder--CLIPTextModel(transformers);tokenizer--CLIPTokenizer(transformers);unet--UNet2DConditionModel;vae--AutoencoderKL

{"_class_name": "StableDiffusionPipeline","_diffusers_version": "0.6.0","feature_extractor": ["transformers","CLIPImageProcessor"],"safety_checker": ["stable_diffusion","StableDiffusionSafetyChecker"],"scheduler": ["diffusers","PNDMScheduler"],"text_encoder": ["transformers","CLIPTextModel"],"tokenizer": ["transformers","CLIPTokenizer"],"unet": ["diffusers","UNet2DConditionModel"],"vae": ["diffusers","AutoencoderKL"]
}

下面是runway/stable-diffusion-v1-5的文件夹结构:

.
├── feature_extractor
│   └── preprocessor_config.json
├── model_index.json
├── safety_checker
│   ├── config.json
│   └── pytorch_model.bin
├── scheduler
│   └── scheduler_config.json
├── text_encoder
│   ├── config.json
│   └── pytorch_model.bin
├── tokenizer
│   ├── merges.txt
│   ├── special_tokens_map.json
│   ├── tokenizer_config.json
│   └── vocab.json
├── unet
│   ├── config.json
│   ├── diffusion_pytorch_model.bin
└── vae├── config.json├── diffusion_pytorch_model.bin

可以查看组件的属性和配置:

pipeline.tokenizer
CLIPTokenizer(name_or_path="/root/.cache/huggingface/hub/models--runwayml--stable-diffusion-v1-5/snapshots/39593d5650112b4cc580433f6b0435385882d819/tokenizer",vocab_size=49408,model_max_length=77,is_fast=False,padding_side="right",truncation_side="right",special_tokens={"bos_token": AddedToken("<|startoftext|>", rstrip=False, lstrip=False, single_word=False, normalized=True),"eos_token": AddedToken("<|endoftext|>", rstrip=False, lstrip=False, single_word=False, normalized=True),"unk_token": AddedToken("<|endoftext|>", rstrip=False, lstrip=False, single_word=False, normalized=True),"pad_token": "<|endoftext|>",},
)

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/130501.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Spring-Spring 之底层架构核心概念解析

BeanDefinition BeanDefinition表示Bean定义&#xff0c;BeanDefinition中存在很多属性用来描述一个Bean的特点。比如&#xff1a; class&#xff0c;表示Bean类型scope&#xff0c;表示Bean作用域&#xff0c;单例或原型等lazyInit&#xff1a;表示Bean是否是懒加载initMeth…

LeetCode 421. 数组中两个数的最大异或值

原题链接&#xff1a;https://leetcode.cn/problems/maximum-xor-of-two-numbers-in-an-array/description/?envTypedaily-question&envId2023-11-04 题目分析 异或且时间复杂度在nlogn内第一反应想到字典树&#xff0c;扫一遍存进字典树&#xff0c;然后遍历每个数&…

【Git企业开发】第四节.Git的分支管理策略和bug分支

文章目录 前言一、Git的分支管理策略 1.1 Fast forward 模式和--no-ff 模式 1.2 企业分支管理策略二、bug分支三、删除临时分支四、总结总结 前言 一、Git的分支管理策略 1.1 Fast forward 模式和--no-ff 模式 通常合并分支时&#xff0c;如果可能&#xff0c;Git 会…

AI:51-基于深度学习的电影评价

🚀 本文选自专栏:AI领域专栏 从基础到实践,深入了解算法、案例和最新趋势。无论你是初学者还是经验丰富的数据科学家,通过案例和项目实践,掌握核心概念和实用技能。每篇案例都包含代码实例,详细讲解供大家学习。 📌📌📌本专栏包含以下学习方向: 机器学习、深度学…

【CSS】div 盒子居中的常用方法

<body><div class"main"><div class"box"></div></div> </body>绝对定位加 margin: auto; &#xff1a; <style>* {padding: 0;margin: 0;}.main {width: 400px;height: 400px;border: 2px solid #000;positio…

C++prime之输入输出文件

作为一种优秀的语言&#xff0c;C必然是能操作文件的&#xff0c;但是我们要知道&#xff0c;C是不直接处理输入输出的&#xff0c;而是通过一族定义在标准库中的类型来处理IO的。 ‘流’和‘缓冲区’ ‘流’和‘缓冲区’ C程序把输入输出看作字节流&#xff0c;并且其只检查…

Microsoft Dynamics 365 CE 扩展定制 - 5. 外部集成

本章内容包括: 使用.NET从其他系统连接到Dynamics 365使用OData(Java)从其他系统连接到Dynamics 365使用外部库从外部源检索数据使用web应用程序连接到Dynamics 365运行Azure计划任务设置Azure Service Bus终结点与Azure Service Bus构建近乎实时的集成使用来自Azure服务总线…

接口测试入门,如何划分接口文档

1.首先最主要的就是要分析接口测试文档&#xff0c;每一个公司的测试文档都是不一样的。具体的就要根据自己公司的接口而定&#xff0c;里面缺少的内容自己需要与开发进行确认。 我认为一针对于测试而言的主要的接口测试文档应该包含的内容分为以下几个方面。 a.具体的一个业…

selenium自动化测试入门 —— 设置等待时间

time.sleep(3) 固定等待3秒 driver.implicitly_wait(10) 隐性的等待&#xff0c;对应全局 WebDriverWait( driver, timeout).until(‘有返回值的__call__()方法或函数’) 显性的等待&#xff0c;对应到元素 一、time.sleep(seconds) 固定等待 import time time.sleep(3) #…

axios 全局错误处理和请求取消

这两个功能都是用拦截器实现。 前景提要&#xff1a; ts 简易封装 axios&#xff0c;统一 API 实现在 config 中配置开关拦截器 全局错误处理 在构造函数中&#xff0c;添加一个响应拦截器即可。在构造函数中注册拦截器的好处是&#xff0c;无论怎么实例化封装类&#xff0c…

基础课19——客服系统知识库的搭建流程

1.收集整理业务数据 注意&#xff1a;我们在做业务数据收集时&#xff0c;往往是甲方提供给我们的&#xff0c;这时就需要确定一个标准&#xff0c;否则对知识库梳理工作会带来很大的难度&#xff0c;建议和甲方沟通确认一个双方都统一的知识库原材料。 2.创建知识库 在创建知…

Docker基础(简单易懂)

目录 一、docker是什么 核心概念 二、docker安装 1、卸载docker 2、使用yum 安装 三、docker常用命令 1、帮助命令 2、镜像命令 1&#xff09;查看镜像 2&#xff09;查询镜像 3&#xff09;拉取镜像 4&#xff09;删除镜像 3、容器命令 四、容器数据卷 五、Dock…

深度学习之基于Tensorflow人脸面部表情识别系统

欢迎大家点赞、收藏、关注、评论啦 &#xff0c;由于篇幅有限&#xff0c;只展示了部分核心代码。 文章目录 一项目简介 二、功能三、系统四. 总结 一项目简介 基于Tensorflow的人脸面部表情识别系统是一种基于深度学习技术的图像处理应用&#xff0c;该系统主要通过人脸图像数…

CoCa论文笔记

摘要 计算机视觉任务中&#xff0c;探索大规模预训练基础模型具有重要意义&#xff0c;因为这些模型可以可以极快地迁移到下游任务中。本文提出的CoCa&#xff08;Contrastive Captioner&#xff09;&#xff0c;一个极简设计&#xff0c;结合对比损失和captioning损失预训练一…

Zabbix如何监控腾讯云NAT网关

1、NAT网关介绍 NAT 网关&#xff08;NAT Gateway&#xff09;是一种支持 IP 地址转换服务&#xff0c;提供网络地址转换能力&#xff0c;主要包括SNAT&#xff08;Source Network Address Translation&#xff0c;源网络地址转换&#xff09;和DNAT&#xff08;Destination N…

如何使用Python和Matplotlib创建双Y轴动态风格折线图 | 数据可视化教程

前言 我的科研论文中需要绘制一个精美的折线图&#xff0c;我的折线图中有三条曲线&#xff0c;分别表示期望角速度指令信号&#xff0c;和实际的角速度信号&#xff0c;还有实际的航向角信号&#xff0c;现在我已经拥有了数据&#xff0c;使用Python中matplotlib.plt.plot来直…

Java线程的基本概念和五种状态

1. 线程 1.1 创建线程 创建线程通常有以下三种方式&#xff1a; 实现 Runnable 接口&#xff0c;并重写其 run 方法&#xff1a; public class J1_Method01 {public static void main(String[] args) {System.out.println("Main线程的ID为&#xff1a;" Thread.curr…

公安涉密视频会议建设方案

公安涉密视频会议建设方案的制定需要考虑多方面因素。其一般是在复杂涉密网络环境中部署&#xff0c;怎样的内部保密部署方可保障涉密会议最大程度的加密进行呢&#xff1f;以下是从不同维度建设方案&#xff0c;可以根据实际应用场景进行相应的修改以及配置与之匹配的视频会议…

〔001〕虚幻 UE5 发送 get、post 请求、读取 json 文件

✨ 目录 🎈 安装 varest 扩展🎈 开启 varest 扩展🎈 发送 get 请求🎈 发送 post 请求🎈 读取 json 文件🎈 安装 varest 扩展 打开 虚幻商城,搜索 varest 关键字进行检索, varest 是一个 api 调用插件,支持 http/https 请求,也支持 json 文件的读取,最关键是该…

mermaid学习第一天/更改主题颜色和边框颜色/《需求解释流程图》

mermaid 在线官网&#xff1a; https://mermaid-js.github.io/ 在线学习文件&#xff1a; https://mermaid.js.org/syntax/quadrantChart.html 1、今天主要是想做需求解释的流程图&#xff0c;又不想自己画&#xff0c;就用了&#xff0c;框框不能直接进行全局配置&#xff0…