【AIGC】Diffusers:AutoPipeline自动化扩散生图管道

前言

🤗 扩散器能够完成许多不同的任务,并且您通常可以将相同的预训练权重用于多个任务,例如文本到图像、图像到图像和修复。但是,如果您不熟悉库和扩散模型,可能很难知道将哪个管道用于任务。例如,如果您将 runwayml/stable-diffusion-v1-5 模型用于文本到图像,您可能不知道也可以通过分别使用 StableDiffusionImg2ImgPipeline 和 StableDiffusionInpaintPipeline 类加载模型来将其用于图像到图像和修复。

该 AutoPipeline 类旨在简化扩散器中🤗管道的多样性。它是一个通用的、任务优先的管道,可让你专注于任务。它 AutoPipeline 会自动检测要使用的正确管道类,这样可以更轻松地加载任务的检查点,而无需知道特定的管道类名称。

AutoPipeline支持以下任务

  • Stable Diffusion
  • ControlNet 
  • Stable Diffusion XL (SDXL)
  • DeepFloyd IF DeepFloyd 中频
  • Kandinsky 2.1
  • Kandinsky 2.2 

AutoPipelineForText2Image 

首先选择一个模型。例如,如果您对使用 runwayml/stable-diffusion-v1-5 模型的文本到图像感兴趣,请使用 AutoPipelineForText2Image: 

from diffusers import AutoPipelineForText2Image
import torch

pipeline = AutoPipelineForText2Image.from_pretrained(
    "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True
).to("cuda")
prompt = "peasant and dragon combat, wood cutting style, viking era, bevel with rune"

image = pipeline(prompt, num_inference_steps=25).images[0]
image

 在后台,AutoPipelineForText2Image:

自动从 model_index.json 文件中检测 "stable-diffusion" 类 

根据 "stable-diffusion" 类名加载相应的文本到图像 StableDiffusionPipeline

AutoPipelineForImage2Image

同样,对于图生图,AutoPipelineForImage2Image 会从 model_index.json 文件中检测到检查 "stable-diffusion" 点,并在后台加载相应的 StableDiffusionImg2ImgPipeline。还可以传递特定于管道类的任何其他参数,例如 strength ,它确定添加到输入图像的噪声或变化量:

from diffusers import AutoPipelineForImage2Image
import torch
import requests
from PIL import Image
from io import BytesIO

pipeline = AutoPipelineForImage2Image.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
    use_safetensors=True,
).to("cuda")
prompt = "a portrait of a dog wearing a pearl earring"

url = "https://upload.wikimedia.org/wikipedia/commons/thumb/0/0f/1665_Girl_with_a_Pearl_Earring.jpg/800px-1665_Girl_with_a_Pearl_Earring.jpg"

response = requests.get(url)
image = Image.open(BytesIO(response.content)).convert("RGB")
image.thumbnail((768, 768))

image = pipeline(prompt, image, num_inference_steps=200, strength=0.75, guidance_scale=10.5).images[0]
image

AutoPipelineForInpainting

如果要进行修复,则 AutoPipelineForInpainting 会以相同的方式加载基础 StableDiffusionInpaintPipeline 类: 

from diffusers import AutoPipelineForInpainting
from diffusers.utils import load_image
import torch

pipeline = AutoPipelineForInpainting.from_pretrained(
    "stabilityai/stable-diffusion-xl-base-1.0", torch_dtype=torch.float16, use_safetensors=True
).to("cuda")

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 = load_image(img_url).convert("RGB")
mask_image = load_image(mask_url).convert("RGB")

prompt = "A majestic tiger sitting on a bench"
image = pipeline(prompt, image=init_image, mask_image=mask_image, num_inference_steps=50, strength=0.80).images[0]
image

 

如果您尝试加载不受支持的检查点,它将引发错误:

from diffusers import AutoPipelineForImage2Image
import torch

pipeline = AutoPipelineForImage2Image.from_pretrained(
    "openai/shap-e-img2img", torch_dtype=torch.float16, use_safetensors=True
)
"ValueError: AutoPipeline can't find a pipeline linked to ShapEImg2ImgPipeline for None"

重复利用模型于多个管道

 对于某些工作流,或者如果要加载多个管道,则从检查点重用相同的组件比重新加载它们会不必要地消耗额外内存更节省内存。例如,如果您正在将检查点用于文本到图像,并且想要再次将其用于图像到图像,请使用 from_pipe() 方法。此方法从以前加载的管道的组件创建新管道,无需额外的内存成本。

from_pipe() 方法检测原始管道类,并将其映射到与要执行的任务相对应的新管道类。例如,如果加载文本到图像的 "stable-diffusion" 类管道:

from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
import torch

pipeline_text2img = AutoPipelineForText2Image.from_pretrained(
    "runwayml/stable-diffusion-v1-5", torch_dtype=torch.float16, use_safetensors=True
)
print(type(pipeline_text2img))
"<class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion.StableDiffusionPipeline'>"

 然后 from_pipe() 将原始 "stable-diffusion" 管道类映射到StableDiffusionImg2ImgPipeline:

pipeline_img2img = AutoPipelineForImage2Image.from_pipe(pipeline_text2img)
print(type(pipeline_img2img))
"<class 'diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_img2img.StableDiffusionImg2ImgPipeline'>"

如果将可选参数(如禁用安全检查器)传递到原始管道,则此参数也会传递到新管道:

from diffusers import AutoPipelineForText2Image, AutoPipelineForImage2Image
import torch

pipeline_text2img = AutoPipelineForText2Image.from_pretrained(
    "runwayml/stable-diffusion-v1-5",
    torch_dtype=torch.float16,
    use_safetensors=True,
    requires_safety_checker=False,
).to("cuda")

pipeline_img2img = AutoPipelineForImage2Image.from_pipe(pipeline_text2img)
print(pipeline_img2img.config.requires_safety_checker)
"False"

 如果要更改新管道的行为,可以覆盖原始管道中的任何参数,甚至可以覆盖配置。例如,要重新打开安全检查器并添加 strength 参数,请执行以下操作:

pipeline_img2img = AutoPipelineForImage2Image.from_pipe(pipeline_text2img, requires_safety_checker=True, strength=0.3)
print(pipeline_img2img.config.requires_safety_checker)
"True"

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

如若内容造成侵权/违法违规/事实不符,请联系我们进行投诉反馈qq邮箱809451989@qq.com,一经查实,立即删除!

相关文章

新闻界的AI革命:Newspager GPT 全面解析

简介有没有想过一家报社是如何运作的&#xff1f;传统的报社要有策划、采编、编辑、美工、审校等等角色&#xff0c;而现在借助 AI&#xff0c;很多事情可以由 AI 代替了&#xff01;Newspager GPT 就是这样一个由多智能体组成的 AI 系统&#xff0c;你只要输入几个你感兴趣的主…

Javaweb之SpringBootWeb案例之阿里云OSS服务入门的详细解析

2.3.2 入门 阿里云oss 对象存储服务的准备工作我们已经完成了&#xff0c;接下来我们就来完成第二步操作&#xff1a;参照官方所提供的sdk示例来编写入门程序。 首先我们需要来打开阿里云OSS的官方文档&#xff0c;在官方文档中找到 SDK 的示例代码&#xff1a; 参照官方提供…

基于 Gurobi 的纸浆运载船顺序装卸决策建模求解|Gurobi优化应用

Pulp-Carrier-Loading-Optimization-with-Gurobi 基于 Gurobi 的纸浆运载船顺序装卸决策建模求解。中山大学智能工程学院《运筹学》课程期末建模课程设计。优化工具&#xff1a;Python的Gurobi 项目仓库 Github: Pulp-Carrier-Loading-Optimization-with-Gurobi 摘要 本研究…

E4 基于Mysql的游标定义和应用

一、实验目的: 熟练使用MySQL游标的定义和应用。 二、实验要求: 1、基本硬件配置:英特尔Pentium III 以上,大于4G内存&#xff1b; 2、软件要求:Mysql&#xff1b; 3、时间:1小时&#xff1b; 4、撰写实验报告并按时提交。 三、实验内容: 问题1&#xff1a;请写一个存储…

快速打通 Vue 3(五):详解 Vue 中的路由

08. 路由 很激动进入了 Vue 3 的学习&#xff0c;作为一个已经上线了三年多的框架&#xff0c;很多项目都开始使用 Vue 3 来编写了 这一组文章主要聚焦于 Vue 3 的新技术和新特性 如果想要学习基础的 Vue 语法可以看我专栏中的其他博客 Vue&#xff08;一&#xff09;&#xff…

蓝桥杯备战——5.动态数码管扫描

1.分析原理图 经查阅说明书得知数码管为共阳极&#xff0c;共阳端口接到了U8,而段码接到了U7。 如果需要选中U8,我们只需要将P250;P261;P271; 如果需要选中U7,我们只需要将P251;P261;P271; 2.代码示例 void Delay1ms() //12.000MHz {unsigned char data i, j;i 12;j 169;…

贪心算法-01:跳跃游戏

关于贪心算法 贪心算法是动态规划的一个特例&#xff0c;相对于动态规划&#xff0c;使用贪心算法需要满足更多条件&#xff0c;但是效率比动态规划要高。 贪心选择的性质就是&#xff1a;每一步都做出一个局部最优解&#xff0c;最终的结果就是全局最优。不过这是一种特殊性…

uniapp组件库中Collapse 折叠面板 的使用方法

目录 #平台差异说明 #基本使用 #控制面板的初始状态&#xff0c;以及是否可以操作 #自定义样式 #1. 如果修改展开后的内容&#xff1f; #2. 如何自定义标题的样式&#xff1f; #3. 如何修改整个Item的样式&#xff1f; #API #Collapse Props #Collapse Item Props #…

ORM-06-jooq 入门介绍

拓展阅读 The jdbc pool for java.(java 手写 jdbc 数据库连接池实现) The simple mybatis.&#xff08;手写简易版 mybatis&#xff09; JOOQ JOOQ 可以通过数据库直接生成 java 代码&#xff0c;通过 flent-api 进行数据库操作。 SQL builder JOOQ 非常的灵活和强大。你可…

深入理解旅游网站开发:Java+SpringBoot+Vue+MySQL的实战经验

✍✍计算机编程指导师 ⭐⭐个人介绍&#xff1a;自己非常喜欢研究技术问题&#xff01;专业做Java、Python、微信小程序、安卓、大数据、爬虫、Golang、大屏等实战项目。 ⛽⛽实战项目&#xff1a;有源码或者技术上的问题欢迎在评论区一起讨论交流&#xff01; ⚡⚡ Java实战 |…

flink内存管理(三):MemorySegment内存使用场景:托管内存与网络内存

文章目录 一.ManagedMemory&#xff08;算子&#xff09;内存的申请与使用1. tm内存申请与使用大致流程2. 创建MemoryManager实例3. 算子使用通过MemoryManager使用内存4. ManagedMemory内存空间申请流程 二.NetworkBuffer内存申请与使用1. NetworkBuffer构造器 在Flink内存模型…

Windows11 Copilot助手开启教程(免费GPT-4)

Windows11上开启Copilot助手教程踩坑指南 Copilot介绍Copilot开启步骤1、更新系统2、更改语言和区域3、下载 ViVeTool 工具4、开启Copilot 使用 Copilot介绍 Windows Copilot 是 Windows 11 中的一个新功能&#xff0c;它可以让你与一个智能助理进行对话&#xff0c;获取信息&…

树莓派无显示屏连接

终端命令控制树莓派关机 1&#xff1a;用网线连接树莓派 按照正常的步骤 &#xff0c;搜索控制面板&#xff0c;网络和internet&#xff0c;网络和共享中心&#xff0c;更改适配器设置&#xff0c;右键WIFI&#xff0c;点击属性&#xff0c;点击共享&#xff0c;打勾允许即可&…

5G安卓手机定制_基于天玑900的安卓主板方案

5G安卓手机方案是一款采用联发科MT6877(天玑900)平台的高性能、可运行安卓操作系统的5G智能模块。该手机采用台积电6纳米低功耗工艺&#xff0c;主频高达2.4GHz&#xff0c;内存支持LPDDR5&#xff0c;并支持5G Sub-6GHz全频段和5G双载波聚合技术等多种制式。同时&#xff0c;该…

Typora1.7.6安装、激活、图床设置和使用

1.安装Typora 双击”typora-setup-x64-1.7.6.exe“安装包。 如果之前安装过先卸载&#xff0c;删除原文件夹。 Typora 1.7.6下载 提取码&#xff1a;ix2b 选择“Install for all users”。 图1-1 选择安装模式 选择安装目录&#xff0c;然后选择“Next”。 图1-2 选择安装路…

23111 C++ day2

思维导图 自己封装一个矩形类(Rect)&#xff0c;拥有私有属性:宽度(width)、高度(height),定义公有成员函数: 初始化函数:void init(int w, int h)更改宽度的函数:set_w(int w)更改高度的函数:set_h(int h) 输出该矩形的周长和面积函数:void show() #include <iostream&g…

web安全学习笔记【10】——数据包分析

基础[1] [2] [3] [4] 入门-HTTP数据包&Postman构造&请求方法&请求头修改&状态码判断[5] [6] [7] #知识点&#xff1a; 1、Web常规-系统&中间件&数据库&源码等 2、Web其他-前后端&软件&Docker&分配站等 3、Web拓展-CDN&WAF&OS…

go语言(十三)-----interface

一、Interface 通用万能类型 空接口int&#xff0c;string&#xff0c;float&#xff0c;struct都实现了interface都可以用interface{}类型,引用任意的数据类型 package mainimport "fmt"//interface()是万能数据类型 func myFunc(arg interface{}) {fmt.Println(&…

pycharm中无法使用anaconda虚拟环境

anaconda里创建了虚拟环境&#xff0c;然后在虚拟环境中明明安装了TensorFlow1.12&#xff0c;但是到pycharm中使用anaconda的虚拟环境时&#xff0c;就是没有TensorFlow1.12&#xff0c;注意下面这幅图 里面有一个选项“use conda package manager”&#xff0c;这个默认是勾…

聊聊 程序员裁员潮:技术变革下的职业危机

前几天一则新闻爆料&#xff1a;一对来自中国的工程师夫妻在美身亡&#xff0c;疑因谷歌裁员致悲剧发生。看到后深感可惜&#xff0c;鲜活的生命就因为裁员殒落了&#xff1b;同时我也深有感触&#xff0c;有幸经历过裁员&#xff0c;体会过那一段低迷不振的日子。 但是回首当下…
最新文章