LCM-LoRA模型推理简明教程

潜在一致性模型 (LCM) 通常可以通过 2-4 个步骤生成高质量图像,从而可以在几乎实时的设置中使用扩散模型。

来自官方网站:

LCM 只需 4,000 个训练步骤(约 32 个 A100 GPU 小时)即可从任何预训练的稳定扩散 (SD) 中提取出来,只需 2~4 个步骤甚至一步即可生成高质量的 768 x 768 分辨率图像,从而显着加速文本转换 -图像生成。 我们使用 LCM 在短短 4,000 次训练迭代中提取了 Dreamshaper-V7 版本的 SD。

有关 LCM 的更多技术概述,请参阅论文。

然而,每个模型需要单独蒸馏以进行潜在一致性蒸馏。 LCM-LoRA 的核心思想是只训练几个适配器层,在本例中适配器是 LoRA。 这样,我们就不必训练完整的模型并保持可训练参数的数量可控。 然后,生成的 LoRA 可以应用于模型的任何微调版本,而无需单独蒸馏它们。 此外,LoRA 还可应用于图像到图像、ControlNet/T2I-Adapter、修复、AnimateDiff 等。LCM-LoRA 还可以与其他 LoRA 结合,只需很少的步骤 (4-8) 即可生成样式图像。

NSDT在线工具推荐: Three.js AI纹理开发包 - YOLO合成数据生成器 - GLTF/GLB在线编辑 - 3D模型格式在线转换 - 可编程3D场景编辑器

LCM-LoRA 可用于 stable-diffusion-v1-5、stable-diffusion-xl-base-1.0 和 SSD-1B 模型。 所有的检查点都可以在这个集合中找到。

有关LCM-LoRA的更多详细信息,请参阅技术报告。

本指南展示了如何使用 LCM-LoRA 进行推理:

  • 文本到图像
  • 图像到图像
  • 与风格化的 LoRA 相结合
  • ControlNet/T2I 适配器
  • 图像修复
  • 动画差异

在阅读本指南之前,我们将先了解一下使用 LCM-LoRA 执行推理的一般工作流程。 LCM-LoRA 与其他稳定扩散 LoRA 类似,因此它们可以与任何支持 LoRA 的 DiffusionPipeline 一起使用。

  • 加载任务特定的管道和模型。
  • 将调度程序设置为 LCMScheduler。
  • 加载模型的 LCM-LoRA 权重。
  • 减少 [1.0, 2.0] 之间的guiding_scale,并在 [4, 8] 之间设置 num_inference_steps。
  • 使用常用参数对管道进行推理。

让我们看看如何使用 LCM-LoRA 对不同的任务进行推理。

首先,确保已安装 peft,以获得更好的 LoRA 支持。

pip install -U peft

1、文本转图像

我们将使用 StableDiffusionXLPipeline 和调度程序:LCMScheduler,然后加载 LCM-LoRA。 该管道与 LCM-LoRA 和调度程序一起,可实现快速推理工作流程,克服扩散模型的缓慢迭代特性。

import torch
from diffusers import DiffusionPipeline, LCMSchedulerpipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",variant="fp16",torch_dtype=torch.float16
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LCM-LoRA
pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"generator = torch.manual_seed(42)
image = pipe(prompt=prompt, num_inference_steps=4, generator=generator, guidance_scale=1.0
).images[0]

结果如下:

请注意,我们仅使用 4 个步骤进行生成,这比标准 SDXL 通常使用的步骤要少得多。

你可能已经注意到,我们设置guidance_scale=1.0,这会禁用classifer-free-guidance。 这是因为 LCM-LoRA 是在指导下进行训练的,因此在这种情况下批量大小不必加倍。 这会导致更快的推理时间,但缺点是负面提示对去噪过程没有任何影响。
还可以使用 LCM-LoRA 的指导,但由于训练的性质,模型对guiding_scale 值非常敏感,高值可能会导致生成的图像中出现伪影。 在我们的实验中,我们发现最佳值在 [1.0, 2.0] 范围内。

2、使用微调模型进行推理

如上所述,LCM-LoRA 可以应用于模型的任何微调版本,而无需单独提取它们。 让我们看看如何使用微调模型进行推理。 在此示例中,我们将使用 animagine-xl 模型,它是用于生成动画的 SDXL 模型的微调版本。

from diffusers import DiffusionPipeline, LCMSchedulerpipe = DiffusionPipeline.from_pretrained("Linaqruf/animagine-xl",variant="fp16",torch_dtype=torch.float16
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LCM-LoRA
pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")prompt = "face focus, cute, masterpiece, best quality, 1girl, green hair, sweater, looking at viewer, upper body, beanie, outdoors, night, turtleneck"generator = torch.manual_seed(0)
image = pipe(prompt=prompt, num_inference_steps=4, generator=generator, guidance_scale=1.0
).images[0]

结果如下:

3、图像到图像

LCM-LoRA 也可以应用于图像到图像的任务。 让我们看看如何使用 LCM 执行图像到图像的生成。 在本例中,我们将使用 dreamshaper-7 模型和 LCM-LoRA 来实现 stable-diffusion-v1-5 。

import torch
from diffusers import AutoPipelineForImage2Image, LCMScheduler
from diffusers.utils import make_image_grid, load_imagepipe = AutoPipelineForImage2Image.from_pretrained("Lykon/dreamshaper-7",torch_dtype=torch.float16,variant="fp16",
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LCM-LoRA
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")# prepare image
url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
init_image = load_image(url)
prompt = "Astronauts in a jungle, cold color palette, muted colors, detailed, 8k"# pass prompt and image to pipeline
generator = torch.manual_seed(0)
image = pipe(prompt,image=init_image,num_inference_steps=4,guidance_scale=1,strength=0.6,generator=generator
).images[0]
make_image_grid([init_image, image], rows=1, cols=2)

结果如下:

你可以根据提示和提供的图像获得不同的结果。 为了获得最佳结果,我们建议尝试 num_inference_steps、strength 和guiding_scale 参数的不同值并选择最佳值。

4、与风格化的 LoRA 结合

LCM-LoRA 可以与其他 LoRA 结合使用,只需很少的步骤即可生成样式图像 (4-8)。 在下面的示例中,我们将使用 LCM-LoRA 和剪纸 LoRA。 要了解有关如何组合 LoRA 的更多信息,请参阅这个指南。

import torch
from diffusers import DiffusionPipeline, LCMSchedulerpipe = DiffusionPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0",variant="fp16",torch_dtype=torch.float16
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LoRAs
pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl", adapter_name="lcm")
pipe.load_lora_weights("TheLastBen/Papercut_SDXL", weight_name="papercut.safetensors", adapter_name="papercut")# Combine LoRAs
pipe.set_adapters(["lcm", "papercut"], adapter_weights=[1.0, 0.8])prompt = "papercut, a cute fox"
generator = torch.manual_seed(0)
image = pipe(prompt, num_inference_steps=4, guidance_scale=1, generator=generator).images[0]
image

结果如下:

5、ControlNet/T2I 适配器

让我们看看如何使用 ControlNet/T2I-Adapter 和 LCM-LoRA 进行推理。

在本例中,我们将使用 SD-v1-5 模型和 SD-v1-5 的 LCM-LoRA 以及 canny ControlNet。

import torch
import cv2
import numpy as np
from PIL import Imagefrom diffusers import StableDiffusionControlNetPipeline, ControlNetModel, LCMScheduler
from diffusers.utils import load_imageimage = load_image("https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png"
).resize((512, 512))image = np.array(image)low_threshold = 100
high_threshold = 200image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
canny_image = Image.fromarray(image)controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
pipe = StableDiffusionControlNetPipeline.from_pretrained("runwayml/stable-diffusion-v1-5",controlnet=controlnet,torch_dtype=torch.float16,safety_checker=None,variant="fp16"
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LCM-LoRA
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")generator = torch.manual_seed(0)
image = pipe("the mona lisa",image=canny_image,num_inference_steps=4,guidance_scale=1.5,controlnet_conditioning_scale=0.8,cross_attention_kwargs={"scale": 1},generator=generator,
).images[0]
make_image_grid([canny_image, image], rows=1, cols=2)

结果如下:

本示例中的推理参数可能不适用于所有示例,因此我们建议你尝试“num_inference_steps”、“guidance_scale”、“controlnet_conditioning_scale”和“cross_attention_kwargs”参数的不同值,并选择最佳的值。

下面的示例展示了如何将 LCM-LoRA 与 Canny T2I 适配器和 SDXL 结合使用。

import torch
import cv2
import numpy as np
from PIL import Imagefrom diffusers import StableDiffusionXLAdapterPipeline, T2IAdapter, LCMScheduler
from diffusers.utils import load_image, make_image_grid# Prepare image
# Detect the canny map in low resolution to avoid high-frequency details
image = load_image("https://huggingface.co/Adapter/t2iadapter/resolve/main/figs_SDXLV1.0/org_canny.jpg"
).resize((384, 384))image = np.array(image)low_threshold = 100
high_threshold = 200image = cv2.Canny(image, low_threshold, high_threshold)
image = image[:, :, None]
image = np.concatenate([image, image, image], axis=2)
canny_image = Image.fromarray(image).resize((1024, 1024))# load adapter
adapter = T2IAdapter.from_pretrained("TencentARC/t2i-adapter-canny-sdxl-1.0", torch_dtype=torch.float16, varient="fp16").to("cuda")pipe = StableDiffusionXLAdapterPipeline.from_pretrained("stabilityai/stable-diffusion-xl-base-1.0", adapter=adapter,torch_dtype=torch.float16,variant="fp16", 
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LCM-LoRA
pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")prompt = "Mystical fairy in real, magic, 4k picture, high quality"
negative_prompt = "extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured"generator = torch.manual_seed(0)
image = pipe(prompt=prompt,negative_prompt=negative_prompt,image=canny_image,num_inference_steps=4,guidance_scale=1.5, adapter_conditioning_scale=0.8, adapter_conditioning_factor=1,generator=generator,
).images[0]
make_image_grid([canny_image, image], rows=1, cols=2)

结果如下:

6、图像修复

LCM-LoRA 也可用于修复。

import torch
from diffusers import AutoPipelineForInpainting, LCMScheduler
from diffusers.utils import load_image, make_image_gridpipe = AutoPipelineForInpainting.from_pretrained("runwayml/stable-diffusion-inpainting",torch_dtype=torch.float16,variant="fp16",
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LCM-LoRA
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")# load base and mask image
init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint.png")
mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png")# generator = torch.Generator("cuda").manual_seed(92)
prompt = "concept art digital painting of an elven castle, inspired by lord of the rings, highly detailed, 8k"
generator = torch.manual_seed(0)
image = pipe(prompt=prompt,image=init_image,mask_image=mask_image,generator=generator,num_inference_steps=4,guidance_scale=4, 
).images[0]
make_image_grid([init_image, mask_image, image], rows=1, cols=3)

结果如下:

7、动画差异

AnimateDiff 允许你使用稳定扩散模型对图像进行动画处理。 为了获得好的结果,我们需要生成多个帧(16-24),而使用标准 SD 模型执行此操作可能会非常慢。 LCM-LoRA 可用于显着加快该过程,因为你只需为每一帧执行 4-8 个步骤。 让我们看看如何使用 LCM-LoRA 和 AnimateDiff 执行动画。

import torch
from diffusers import MotionAdapter, AnimateDiffPipeline, DDIMScheduler, LCMScheduler
from diffusers.utils import export_to_gifadapter = MotionAdapter.from_pretrained("diffusers/animatediff-motion-adapter-v1-5")
pipe = AnimateDiffPipeline.from_pretrained("frankjoshua/toonyou_beta6",motion_adapter=adapter,
).to("cuda")# set scheduler
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)# load LCM-LoRA
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5", adapter_name="lcm")
pipe.load_lora_weights("guoyww/animatediff-motion-lora-zoom-in", weight_name="diffusion_pytorch_model.safetensors", adapter_name="motion-lora")pipe.set_adapters(["lcm", "motion-lora"], adapter_weights=[0.55, 1.2])prompt = "best quality, masterpiece, 1girl, looking at viewer, blurry background, upper body, contemporary, dress"
generator = torch.manual_seed(0)
frames = pipe(prompt=prompt,num_inference_steps=5,guidance_scale=1.25,cross_attention_kwargs={"scale": 1},num_frames=24,generator=generator
).frames[0]
export_to_gif(frames, "animation.gif")

结果如下:


原文链接:LCM-LoRA推理简明教程 - BimAnt

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

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

相关文章

sed文本 免交互

目录 什么是sed 概念 格式 基本用法 命令的选项 打印第三行 打印日志文件 打印奇数行 打印偶数行 第三行退出 删除第三行 sed在不打开文件的情况下修改文件内容 在后面添加 选项a 在字符中间添加 \n 实现追加换行 全部追加 在前面插入 选项i 替换 选项c …

Java精品项目源码基于SpringBoot的智慧园区管理系统(v67)

Java精品项目源码基于SpringBoot的智慧园区管理系统(v67) 大家好,小辰今天给大家介绍一个智慧园区管理系统,演示视频公众号(小辰哥的Java)对号查询观看即可 文章目录 Java精品项目源码基于SpringBoot的智慧园区管理系统(v67)难度…

【图像分割】【深度学习】PFNet官方Pytorch代码-PFNet网络损失函数模块解析

【图像分割】【深度学习】PFNet官方Pytorch代码-PFNet网络损失函数模块解析 文章目录 【图像分割】【深度学习】PFNet官方Pytorch代码-PFNet网络损失函数模块解析前言PM定位模块损失函数FM聚焦模块损失函数总结 前言 在详细解析PFNet代码之前,首要任务是成功运行PF…

压缩包文件丢失?4个正确找回方法分享!

“我有一个很重要的压缩包保存在电脑上,但是不知道为什么里面有些文件丢失了。有什么方法可以快速找回压缩文件?请大家给我支支招吧!” 如果我们的文件太多,将它们放在压缩包中不仅能让文件更有序,还能更合理的节省电脑…

LD_PRELOAD劫持

LD_PRELOAD劫持 <1> LD_PRELOAD简介 LD_PRELOAD 是linux下的一个环境变量。用于动态链接库的加载&#xff0c;在动态链接库的过程中他的优先级是最高的。类似于 .user.ini 中的 auto_prepend_file&#xff0c;那么我们就可以在自己定义的动态链接库中装入恶意函数。 也…

tp8 使用rabbitMQ(3)发布/订阅

发布/订阅 当我们想把一个消息&#xff0c;发送给 多个消费者的时候&#xff0c;我们把这种模式叫做发布/订阅模式&#xff0c;比如我们做两个消费者&#xff0c;其中一个消费者把消息写入磁盘中&#xff0c;别一个消费者把消息结果输出到屏幕上&#xff0c;就要用到发布订阅模…

产品化和商品化

我们经常会在IT产业听过以下岗位&#xff1a; 1、产品序列&#xff1a;产品行销经理 2、产品序列&#xff1a;产品经理、需求分析师、产品详细设计工程师、UIUE设计师 3、产品序列&#xff1a;业务架构师、应用架构师、数据架构师、技术架构师 4、研发序列&#xff1a;创新原型…

Java 图片验证码需求分析

&#x1f497;wei_shuo的个人主页 &#x1f4ab;wei_shuo的学习社区 &#x1f310;Hello World &#xff01; 图片验证码 需求分析 连续因输错密码而登录失败时&#xff0c;记录其连续输错密码的累加次数&#xff1b;若在次数小于5时&#xff0c;用户输入正确的密码并成功登录…

前K个高频单词(Java详解)

一、题目描述 给定一个单词列表 words 和一个整数 k &#xff0c;返回前 k 个出现次数最多的单词。 返回的答案应该按单词出现频率由高到低排序。如果不同的单词有相同出现频率&#xff0c; 按字典顺序 排序。 示例1&#xff1a; 输入: words ["i", "love&…

浅谈硬件连通性测试几大优势

硬件连通性测试是确保硬件系统正常运行、提高系统可靠性和降低生产成本的关键步骤。在现代工程和制造中&#xff0c;将连通性测试纳入生产流程是一个明智的选择&#xff0c;有助于确保硬件产品的质量和性能达到最优水平。本文将介绍硬件连通性测试的主要优势有哪些! 一、提高系…

游戏测试和软件测试有什么区别

针对手游而言&#xff0c;游戏测试的本质是APP&#xff0c;所以不少手游的测试方式与APP测试异曲同工&#xff0c;然而也有所不同。APP更多的是具有一种工具&#xff0c;一款APP好不好用不重要&#xff0c;关键点在于实用。而游戏则具有一种玩具属性&#xff0c;它并不见得实用…

基于Python+requests编写的自动化测试项目-实现流程化的接口串联

框架产生目的&#xff1a;公司走的是敏捷开发模式&#xff0c;编写这种框架是为了能够满足当前这种发展模式&#xff0c;用于前后端联调之前&#xff08;后端开发完接口&#xff0c;前端还没有将业务处理完毕的时候&#xff09;以及日后回归阶段&#xff0c;方便为自己腾出学(m…

图像异常检测研究现状综述

论文标题&#xff1a;图像异常检测研究现状综述 作者&#xff1a;吕承侃 1, 2 沈 飞 1, 2, 3 张正涛 1, 2, 3 张 峰 1, 2, 3 发表日期&#xff1a;2022年6月 阅读日期 &#xff1a;2023年11月28 研究背景&#xff1a; 图像异常检测是计算机视觉领域的一个热门研究课题, 其目…

leetCode 39.组合总和 + 回溯算法 + 剪枝 + 图解 + 笔记

39. 组合总和 - 力扣&#xff08;LeetCode&#xff09; 给你一个 无重复元素 的整数数组 candidates 和一个目标整数 target &#xff0c;找出 candidates 中可以使数字和为目标数 target 的 所有 不同组合 &#xff0c;并以列表形式返回。你可以按 任意顺序 返回这些组合 can…

2015年五一杯数学建模A题不确定性条件下的最优路径问题解题全过程文档及程序

2015年五一杯数学建模 A题 不确定性条件下的最优路径问题 原题再现 目前&#xff0c;交通拥挤和事故正越来越严重的困扰着城市交通。随着我国交通运输事业的迅速发展&#xff0c;交通“拥塞”已经成为很多城市的“痼疾”。在复杂的交通环境下&#xff0c;如何寻找一条可靠、快…

HarmonyOS 数据持久化 Preferences 如何在页面中对数据进行读写

背景介绍 最近在了解并跟着官方文档尝试做一个鸿蒙app 小demo的过程中对在app中保存数据遇到些问题 特此记录下来 这里的数据持久化以 Preferences为例子展开 废话不多说 这里直接上节目(官方提供的文档示例:) 以Stage模型为例 1.明确preferences的类型 import data_prefer…

印刷企业建设数字工厂管理系统的工作内容有哪些

随着科技的不断进步&#xff0c;数字工厂管理系统在印刷企业中的应用越来越广泛。这种系统可以有效地整合企业内外资源&#xff0c;提高生产效率&#xff0c;降低生产成本&#xff0c;并为印刷企业提供更好的业务运营与管理模式。本文将从以下几个方面探讨印刷企业建设数字工厂…

如何用postman实现接口自动化测试

postman使用 开发中经常用postman来测试接口&#xff0c;一个简单的注册接口用postman测试&#xff1a; 接口正常工作只是最基本的要求&#xff0c;经常要评估接口性能&#xff0c;进行压力测试。 postman进行简单压力测试 下面是压测数据源&#xff0c;支持json和csv两个格…

Kibana部署

服务器 安装软件主机名IP地址系统版本配置KibanaElk10.3.145.14centos7.5.18042核4G软件版本&#xff1a;nginx-1.14.2、kibana-7.13.2-linux-x86_64.tar.gz 1. 安装配置Kibana &#xff08;1&#xff09;安装 [rootelk ~]# tar zxf kibana-7.13.2-linux-x86_64.tar.gz -C…

easyExcel 注解开发 快速以及简单上手 以及包含工具类

easyExcel 简单快速使用 1. mevan 这里版本我这里选的是 poi 4.1.2和 ali的easyexcel 的 3.3.1。 因为阿里easy是根据poi的依赖开发的有关系&#xff0c;两者需要对应要不然就会有很多bug和错误在运行时发生。需要版本对应&#xff0c;然而就是easy的代码也会有bug这个版本是比…