🚀 潜在整合性モデル (LCM) LoRA: SDv1-5
潜在整合性モデル (LCM) LoRA は、Simian Luo, Yiqin Tan, Suraj Patil, Daniel Gu らによる LCM-LoRA: A universal Stable-Diffusion Acceleration Module で提案されました。
これは、runwayml/stable-diffusion-v1-5
用の蒸留整合性アダプターで、推論ステップ数をわずか 2 - 8 ステップに削減できます。
🚀 クイックスタート
LCM-LoRA は 🤗 Hugging Face Diffusers ライブラリのバージョン v0.23.0 以降でサポートされています。モデルを実行するには、まず Diffusers ライブラリの最新バージョンと、peft
、accelerate
、transformers
をインストールします。
Hugging Face Hub からのオーディオデータセット:
pip install --upgrade pip
pip install --upgrade diffusers transformers accelerate peft
⚠️ 重要提示
詳細な使用例については、公式の LCM-LoRA ドキュメント をご確認ください。
✨ 主な機能
- 推論ステップ数を大幅に削減できるため、高速な画像生成が可能です。
- 複数のタスク(テキストから画像、画像から画像、インペインティング、ControlNet)に対応しています。
📦 インストール
pip install --upgrade pip
pip install --upgrade diffusers transformers accelerate peft
💻 使用例
基本的な使用法
テキストから画像
アダプターは SDv1-5 またはその派生モデルでロードできます。ここでは、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")
pipe.load_lora_weights(adapter_id)
pipe.fuse_lora()
prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"
image = pipe(prompt=prompt, num_inference_steps=4, guidance_scale=0).images[0]

画像から画像
LCM-LoRA は画像から画像のタスクにも適用できます。ここでは、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")
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
pipe.fuse_lora()
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"
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")
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
pipe.fuse_lora()
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")
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")
pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
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++