部署LCM(Latent Consistency Models)实现快速出图

LCM 的全称是 Latent Consistency Models(潜在一致性模型),由清华大学交叉信息研究院的研究者们构建。通过一些创新性的方法,LCM 只用少数的几步推理就能生成高分辨率图像,将主流文生图模型的效率提高 5-10 倍,所以能呈现出实时的效果。

目前LCM只可以使用一个模型Dreamshaper_v7,基于SD版本Dreamshaper微调而来的。

LCM模型下载:

https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7icon-default.png?t=N7T8https://huggingface.co/SimianLuo/LCM_Dreamshaper_v7

项目源码:

GitHub - luosiallen/latent-consistency-model: Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step InferenceLatent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inference - GitHub - luosiallen/latent-consistency-model: Latent Consistency Models: Synthesizing High-Resolution Images with Few-Step Inferenceicon-default.png?t=N7T8https://github.com/luosiallen/latent-consistency-model

安装依赖库

pip install --upgrade diffusers  # make sure to use at least diffusers >= 0.22
pip install transformers accelerate

运行模型

文生图

from diffusers import DiffusionPipeline

import torch

pipe = DiffusionPipeline.from_pretrained("SimianLuo/LCM_Dreamshaper_v7")

# To save GPU memory, torch.float16 can be used, but it may compromise image quality.
pipe.to(torch_device="cuda", torch_dtype=torch.float32)

prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"

# Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps.
num_inference_steps = 4
images = pipe(prompt=prompt, num_inference_steps=num_inference_steps, guidance_scale=8.0, lcm_origin_steps=50, output_type="pil").images
images[0].save("image.png")

图生图

from diffusers import AutoPipelineForImage2Image
import torch
import PIL

pipe = AutoPipelineForImage2Image.from_pretrained("SimianLuo/LCM_Dreamshaper_v7")
# To save GPU memory, torch.float16 can be used, but it may compromise image quality.
pipe.to(torch_device="cuda", torch_dtype=torch.float32)

prompt = "High altitude snowy mountains"
image = PIL.Image.open("./snowy_mountains.png")

# Can be set to 1~50 steps. LCM support fast inference even <= 4 steps. Recommend: 1~8 steps.
num_inference_steps = 4
images = pipe(prompt=prompt, image=image, num_inference_steps=num_inference_steps, guidance_scale=8.0).images

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

在线demo

https://huggingface.co/spaces/SimianLuo/Latent_Consistency_Modelicon-default.png?t=N7T8https://huggingface.co/spaces/SimianLuo/Latent_Consistency_Model

OpenXLab浦源 - 应用中心应用中心提供应用托管的服务,用户只需遵循平台规范,通过简单的前端封装组件(Gradio)即可构建模型推理应用演示demo,应用中心提供免费应用部署的能力,普通用户也可在应用中心中交互式体验模型的能力,更好帮助用户寻找想要的学术模型或应用服务。通过前端封装组件和平台的SDK工具,帮助AI开发者简单快速构建人工智能应用。icon-default.png?t=N7T8https://openxlab.org.cn/apps/detail/Latent-Consistency-Model/Latent-Consistency-Model

将LCM集成到Stable Diffusion WebUI中

SD WebUI 的 LCM 插件源码

GitHub - 0xbitches/sd-webui-lcm: Latent Consistency Model for AUTOMATIC1111 Stable Diffusion WebUILatent Consistency Model for AUTOMATIC1111 Stable Diffusion WebUI - GitHub - 0xbitches/sd-webui-lcm: Latent Consistency Model for AUTOMATIC1111 Stable Diffusion WebUIicon-default.png?t=N7T8https://github.com/0xbitches/sd-webui-lcm选择“Extensions”->“Install from URL”,安装LCM插件。

生成的图片将会保存到outputs/txt2img-images/LCM

LCM插件提供了txt2img、img2img、vid2vid三个选项卡。

Img2Img和Vid2Vid的输出高度和宽度将与输入相同,目前不可更改。

生成的视频将保存到outputs/LCM-vid2vid


​​​​​​​

LCM-LoRA

LCM每个模型都需要单独蒸馏,以获得潜在的一致性蒸馏。LCM-LoRA的核心思想是只训练几个适配器层,在这种情况下,适配器是LoRA。这样,我们不必训练完整模型,并保持可训练参数数量的管理。然后,由此产生的LoRA可以应用于模型的任何微调版本,而无需单独蒸馏它们。此外,LoRA可以应用于img2img, ControlNet/T2I-Adapter, inpainting, AnimateDiff等。LCM-LoRA还可以与其他LoRA相结合,只需几个步骤(4-8)即可生成样式图像。

Lora模型下载

LCM-LoRAs 可用于 stable-diffusion-v1-5, stable-diffusion-xl-base-1.0, 和 SSD-1B 模型。

https://huggingface.co/latent-consistency/lcm-lora-sdv1-5​​​​​​​

https://huggingface.co/latent-consistency/lcm-lora-sdxl

https://huggingface.co/latent-consistency/lcm-lora-ssd-1b

使用时我们可以将推理步骤的数量减少到仅2到8个步骤,将CFG Scale值介于1.0和2.0之间。调整Lora的权重,使其更接近原本的效果。

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
引用\[1\]: Stable Diffusion是一个基于Latent Diffusion Models(潜在扩散模型,LDMs)的文图生成(text-to-image)模型。Stable Diffusion在LAION-5B的一个子集上训练了一个Latent Diffusion Models,该模型专门用于文图生成。\[1\]引用\[2\]: 在潜在表示空间上做diffusion操作其主要过程和标准的扩散模型没有太大的区别,所用到的扩散模型的具体实现为time-conditional UNet。\[2\]引用\[3\]: 高分辨率图像合成的普及diffusion models属于基于似然的模型类,其模式覆盖行为使它们容易花费过多的容量(从而计算资源)来建模数据的难以察觉的细节。尽管重加权变分目标旨在通过对初始去噪步骤进行欠采样来解决这一问题,但diffusion models仍然需要计算量,因为训练和评估这样的模型需要在RGB图像的高维空间中反复进行函数评估(和梯度计算)。\[3\] 问题: Latent Diffusion Models是什么? 回答: Latent Diffusion Models(潜在扩散模型,LDMs)是一种用于文图生成的模型。在潜在表示空间上进行diffusion操作,其过程与标准的扩散模型类似。具体实现中使用了time-conditional UNet。这种模型的训练和评估需要在RGB图像的高维空间中进行函数评估和梯度计算,因此需要大量的计算资源。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* *2* *3* [Latent Diffusion Models](https://blog.csdn.net/weixin_43135178/article/details/127972532)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值