Views
No views yet
stable-diffusion-xl-base-1.0 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 peftstabilityai/stable-diffusion-xl-base-1.0. 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 = "stabilityai/stable-diffusion-xl-base-1.0"
5adapter_id = "latent-consistency/lcm-lora-sdxl"
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
15prompt = "Self-portrait oil painting, a beautiful cyborg with golden hair, 8k"
16
17# disable guidance_scale by passing 0
18image = pipe(prompt=prompt, num_inference_steps=4, guidance_scale=0).images[0]
1import torch
2from diffusers import AutoPipelineForInpainting, LCMScheduler
3from diffusers.utils import load_image, make_image_grid
4
5pipe = AutoPipelineForInpainting.from_pretrained(
6 "diffusers/stable-diffusion-xl-1.0-inpainting-0.1",
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-sdxl")
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").resize((1024, 1024))
20mask_image = load_image("https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/diffusers/inpaint_mask.png").resize((1024, 1024))
21
22prompt = "a castle on top of a mountain, highly detailed, 8k"
23generator = torch.manual_seed(42)
24image = pipe(
25 prompt=prompt,
26 image=init_image,
27 mask_image=mask_image,
28 generator=generator,
29 num_inference_steps=5,
30 guidance_scale=4,
31).images[0]
32make_image_grid([init_image, mask_image, image], rows=1, cols=3)
1import torch
2from diffusers import DiffusionPipeline, LCMScheduler
3
4pipe = DiffusionPipeline.from_pretrained(
5 "stabilityai/stable-diffusion-xl-base-1.0",
6 variant="fp16",
7 torch_dtype=torch.float16
8).to("cuda")
9
10# set scheduler
11pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
12
13# load LoRAs
14pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl", adapter_name="lcm")
15pipe.load_lora_weights("TheLastBen/Papercut_SDXL", weight_name="papercut.safetensors", adapter_name="papercut")
16
17# Combine LoRAs
18pipe.set_adapters(["lcm", "papercut"], adapter_weights=[1.0, 0.8])
19
20prompt = "papercut, a cute fox"
21generator = torch.manual_seed(0)
22image = pipe(prompt, num_inference_steps=4, guidance_scale=1, generator=generator).images[0]
23image
1import torch
2import cv2
3import numpy as np
4from PIL import Image
5
6from diffusers import StableDiffusionXLControlNetPipeline, 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((1024, 1024))
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("diffusers/controlnet-canny-sdxl-1.0-small", torch_dtype=torch.float16, variant="fp16")
24pipe = StableDiffusionXLControlNetPipeline.from_pretrained(
25 "stabilityai/stable-diffusion-xl-base-1.0",
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-sdxl")
37pipe.fuse_lora()
38
39generator = torch.manual_seed(0)
40image = pipe(
41 "picture of the mona lisa",
42 image=canny_image,
43 num_inference_steps=5,
44 guidance_scale=1.5,
45 controlnet_conditioning_scale=0.5,
46 cross_attention_kwargs={"scale": 1},
47 generator=generator,
48).images[0]
49make_image_grid([canny_image, image], rows=1, cols=2)
1import torch
2import cv2
3import numpy as np
4from PIL import Image
5
6from diffusers import StableDiffusionXLAdapterPipeline, T2IAdapter, LCMScheduler
7from diffusers.utils import load_image, make_image_grid
8
9# Prepare image
10# Detect the canny map in low resolution to avoid high-frequency details
11image = load_image(
12 "https://huggingface.co/Adapter/t2iadapter/resolve/main/figs_SDXLV1.0/org_canny.jpg"
13).resize((384, 384))
14
15image = np.array(image)
16
17low_threshold = 100
18high_threshold = 200
19
20image = cv2.Canny(image, low_threshold, high_threshold)
21image = image[:, :, None]
22image = np.concatenate([image, image, image], axis=2)
23canny_image = Image.fromarray(image).resize((1024, 1024))
24
25# load adapter
26adapter = T2IAdapter.from_pretrained("TencentARC/t2i-adapter-canny-sdxl-1.0", torch_dtype=torch.float16, varient="fp16").to("cuda")
27
28pipe = StableDiffusionXLAdapterPipeline.from_pretrained(
29 "stabilityai/stable-diffusion-xl-base-1.0",
30 adapter=adapter,
31 torch_dtype=torch.float16,
32 variant="fp16",
33).to("cuda")
34
35# set scheduler
36pipe.scheduler = LCMScheduler.from_config(pipe.scheduler.config)
37
38# load LCM-LoRA
39pipe.load_lora_weights("latent-consistency/lcm-lora-sdxl")
40
41prompt = "Mystical fairy in real, magic, 4k picture, high quality"
42negative_prompt = "extra digit, fewer digits, cropped, worst quality, low quality, glitch, deformed, mutated, ugly, disfigured"
43
44generator = torch.manual_seed(0)
45image = pipe(
46 prompt=prompt,
47 negative_prompt=negative_prompt,
48 image=canny_image,
49 num_inference_steps=4,
50 guidance_scale=1.5,
51 adapter_conditioning_scale=0.8,
52 adapter_conditioning_factor=1,
53 generator=generator,
54).images[0]
55make_image_grid([canny_image, image], rows=1, cols=2)