模型概述
模型特點
模型能力
使用案例
🚀 潛在一致性模型 (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++ 許可證。

