Views
No views yet
runwayml/stable-diffusion-v1-5 that allows
to reduce the number of inference steps to only between 2 - 8 steps.| Model | Params / M |
|---|---|
| lcm-lora-sdv1-5 | 67.5 |
| lcm-lora-ssd-1b | 105 |
| lcm-lora-sdxl | 197M |
peft, accelerate and transformers.
audio dataset from the Hugging Face Hub:1pip install --upgrade pip
2pip install --upgrade diffusers transformers accelerate peftLykon/dreamshaper-7. Next, the scheduler needs to be changed to LCMScheduler and we can reduce the number of inference steps to just 2 to 8 steps.
Please make sure to either disable guidance_scale or use values between 1.0 and 2.0.1import torch
2from diffusers import LCMScheduler, AutoPipelineForText2Image
3
4model_id = "Lykon/dreamshaper-7"
5adapter_id = "latent-consistency/lcm-lora-sdv1-5"
6
7pipe = AutoPipelineForText2Image.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16")
8pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
9pipe.to("cuda")
10
11# load and fuse lcm lora
12pipe.load_lora_weights(adapter_id)
13pipe.fuse_lora()
14
15
16prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"
17
18# disable guidance_scale by passing 0
19image = pipe(prompt=prompt, num_inference_steps=4, guidance_scale=0).images[0]
stable-diffusion-v1-5 .1import torch
2from diffusers import AutoPipelineForImage2Image, LCMScheduler
3from diffusers.utils import make_image_grid, load_image
4
5pipe = AutoPipelineForImage2Image.from_pretrained(
6 "Lykon/dreamshaper-7",
7 torch_dtype=torch.float16,
8 variant="fp16",
9).to("cuda")
10
11# set scheduler
12pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
13
14# load LCM-LoRA
15pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
16pipe.fuse_lora()
17
18# prepare image
19url = "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/img2img-init.png"
20init_image = load_image(url)
21prompt = "Astronauts in a jungle, cold color palette, muted colors, detailed, 8k"
22
23# pass prompt and image to pipeline
24generator = torch.manual_seed(0)
25image = pipe(
26 prompt,
27 image=init_image,
28 num_inference_steps=4,
29 guidance_scale=1,
30 strength=0.6,
31 generator=generator
32).images[0]
33make_image_grid([init_image, image], rows=1, cols=2)
1import torch
2from diffusers import AutoPipelineForInpainting, LCMScheduler
3from diffusers.utils import load_image, make_image_grid
4
5pipe = AutoPipelineForInpainting.from_pretrained(
6 "runwayml/stable-diffusion-inpainting",
7 torch_dtype=torch.float16,
8 variant="fp16",
9).to("cuda")
10
11# set scheduler
12pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
13
14# load LCM-LoRA
15pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
16pipe.fuse_lora()
17
18# load base and mask image
19init_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint.png")
20mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png")
21
22# generator = torch.Generator("cuda").manual_seed(92)
23prompt = "concept art digital painting of an elven castle, inspired by lord of the rings, highly detailed, 8k"
24generator = torch.manual_seed(0)
25image = pipe(
26 prompt=prompt,
27 image=init_image,
28 mask_image=mask_image,
29 generator=generator,
30 num_inference_steps=4,
31 guidance_scale=4,
32).images[0]
33make_image_grid([init_image, mask_image, image], rows=1, cols=3)
1import torch
2import cv2
3import numpy as np
4from PIL import Image
5
6from diffusers import StableDiffusionControlNetPipeline, ControlNetModel, LCMScheduler
7from diffusers.utils import load_image
8
9image = load_image(
10 "https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png"
11).resize((512, 512))
12
13image = np.array(image)
14
15low_threshold = 100
16high_threshold = 200
17
18image = cv2.Canny(image, low_threshold, high_threshold)
19image = image[:, :, None]
20image = np.concatenate([image, image, image], axis=2)
21canny_image = Image.fromarray(image)
22
23controlnet = ControlNetModel.from_pretrained("lllyasviel/sd-controlnet-canny", torch_dtype=torch.float16)
24pipe = StableDiffusionControlNetPipeline.from_pretrained(
25 "runwayml/stable-diffusion-v1-5",
26 controlnet=controlnet,
27 torch_dtype=torch.float16,
28 safety_checker=None,
29 variant="fp16"
30).to("cuda")
31
32# set scheduler
33pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
34
35# load LCM-LoRA
36pipe.load_lora_weights("latent-consistency/lcm-lora-sdv1-5")
37
38generator = torch.manual_seed(0)
39image = pipe(
40 "the mona lisa",
41 image=canny_image,
42 num_inference_steps=4,
43 guidance_scale=1.5,
44 controlnet_conditioning_scale=0.8,
45 cross_attention_kwargs={"scale": 1},
46 generator=generator,
47).images[0]
48make_image_grid([canny_image, image], rows=1, cols=2)