Views
No views yet




cd in the directory:1git clone https://github.com/HighCWu/control-lora-v3
2cd control-lora-v31# !pip install opencv-python transformers accelerate
2from diffusers import UniPCMultistepScheduler
3from diffusers.utils import load_image
4from model import UNet2DConditionModelEx
5from pipeline import StableDiffusionControlLoraV3Pipeline
6import numpy as np
7import torch
8
9import cv2
10from PIL import Image
11
12# download an image
13image = load_image(
14 "https://hf.co/datasets/huggingface/documentation-images/resolve/main/diffusers/input_image_vermeer.png"
15)
16image = np.array(image)
17
18# get canny image
19image = cv2.Canny(image, 100, 200)
20image = image[:, :, None]
21image = np.concatenate([image, image, image], axis=2)
22canny_image = Image.fromarray(image)
23
24# load stable diffusion v1-5 and control-lora-v3
25unet: UNet2DConditionModelEx = UNet2DConditionModelEx.from_pretrained(
26 "runwayml/stable-diffusion-v1-5", subfolder="unet", torch_dtype=torch.float16
27)
28unet = unet.add_extra_conditions(["canny"])
29pipe = StableDiffusionControlLoraV3Pipeline.from_pretrained(
30 "runwayml/stable-diffusion-v1-5", unet=unet, torch_dtype=torch.float16
31)
32# load attention processors
33pipe.load_lora_weights("HighCWu/sd-control-lora-v3-canny")
34
35# speed up diffusion process with faster scheduler and memory optimization
36pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
37# remove following line if xformers is not installed
38pipe.enable_xformers_memory_efficient_attention()
39
40pipe.enable_model_cpu_offload()
41
42# generate image
43generator = torch.manual_seed(0)
44image = pipe(
45 "futuristic-looking woman", num_inference_steps=20, generator=generator, image=canny_image
46).images[0]
47image.show()