模型简介
模型特点
模型能力
使用案例
🚀 潜在一致性模型 (LCM) LoRA: SDv1 - 5
潜在一致性模型 (LCM) LoRA 是一种蒸馏一致性适配器,可将推理步骤大幅减少至仅 2 - 8 步,显著提升了图像生成的效率。该模型由 Simian Luo、Yiqin Tan、Suraj Patil、Daniel Gu 等人在论文 LCM - LoRA: A universal Stable - Diffusion Acceleration Module 中提出。
属性 | 详情 |
---|---|
库名称 | diffusers |
基础模型 | runwayml/stable - diffusion - v1 - 5 |
标签 | lora、text - to - image |
许可证 | openrail++ |
推理 | 否 |
模型 | 参数数量 |
---|---|
[lcm - lora - sdv1 - 5](https://huggingface.co/latent - consistency/lcm - lora - sdv1 - 5) | 67.5M |
[lcm - lora - ssd - 1b](https://huggingface.co/latent - consistency/lcm - lora - ssd - 1b) | 105M |
[lcm - lora - sdxl](https://huggingface.co/latent - consistency/lcm - lora - sdxl) | 197M |
🚀 快速开始
LCM - LoRA 自 🤗 Hugging Face Diffusers 库的 v0.23.0 版本起开始支持。若要运行该模型,需先安装最新版本的 Diffusers 库以及 peft
、accelerate
和 transformers
。可使用以下命令进行安装:
pip install --upgrade pip
pip install --upgrade diffusers transformers accelerate peft
⚠️ 重要提示
如需详细的使用示例,建议查看官方 [LCM - LoRA 文档](https://huggingface.co/docs/diffusers/main/en/using - diffusers/inference_with_lcm_lora)。
💻 使用示例
基础用法
文本到图像
适配器可与 SDv1 - 5 或其衍生模型一起加载。以下示例使用 [Lykon/dreamshaper - 7
](https://huggingface.co/Lykon/dreamshaper - 7) 模型。需将调度器更改为 LCMScheduler
,并将推理步骤减少至 2 到 8 步。请确保禁用 guidance_scale
或使用 1.0 到 2.0 之间的值。
import torch
from diffusers import LCMScheduler, AutoPipelineForText2Image
model_id = "Lykon/dreamshaper-7"
adapter_id = "latent-consistency/lcm-lora-sdv1-5"
pipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16")
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
pipe.to("cuda")
# load and fuse lcm lora
pipe.load_lora_weights(adapter_id)
pipe.fuse_lora()
prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"
# disable guidance_scale by passing 0
image = pipe(prompt=prompt, num_inference_steps=4, guidance_scale=0).images[0]
图像到图像
LCM - LoRA 也可应用于图像到图像的任务。以下示例使用 [dreamshaper - 7](https://huggingface.co/Lykon/dreamshaper - 7) 模型和适用于 stable - diffusion - v1 - 5
的 LCM - LoRA。
import torch
from diffusers import AutoPipelineForImage2Image, LCMScheduler
from diffusers.utils import make_image_grid, load_image
pipe = 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")
pipe.fuse_lora()
# 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)
图像修复
LCM - LoRA 还可用于图像修复任务。
import torch
from diffusers import AutoPipelineForInpainting, LCMScheduler
from diffusers.utils import load_image, make_image_grid
pipe = 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")
pipe.fuse_lora()
# 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)
ControlNet
以下示例使用 SD - v1 - 5 模型、适用于 SD - v1 - 5 的 LCM - LoRA 以及 Canny ControlNet。
import torch
import cv2
import numpy as np
from PIL import Image
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, LCMScheduler
from diffusers.utils import load_image
image = 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 = 200
image = 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)
🔧 技术细节
待补充。
📄 许可证
本项目采用 openrail++ 许可证。

