File size: 1,185 Bytes
8d9a1a3
 
94a2d08
713f483
8d9a1a3
 
 
 
 
 
2893544
 
 
 
 
 
 
 
 
095021a
 
5d37dd0
8d9a1a3
 
 
2f1fa52
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import torch
from controlnet_aux import LineartDetector
from diffusers import ControlNetModel,UniPCMultistepScheduler,StableDiffusionControlNetPipeline
from PIL import Image

device= "cuda" if torch.cuda.is_available() else "cpu"
print("Using device for I2I_2:", device)


def I2I_2(image, prompt,size,num_inference_steps):
    processor = LineartDetector.from_pretrained("lllyasviel/Annotators")

    checkpoint = "ControlNet-1-1-preview/control_v11p_sd15_lineart"
    controlnet = ControlNetModel.from_pretrained(checkpoint, torch_dtype=torch.float16).to(device)
    pipe = StableDiffusionControlNetPipeline.from_pretrained(
        "radames/stable-diffusion-v1-5-img2img", controlnet=controlnet, torch_dtype=torch.float16
    ).to(device)
    pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
    pipe.enable_model_cpu_offload()
    if not isinstance(image, Image.Image):
        image = Image.fromarray(image)
    image.resize((size,size))
    image=processor(image)
    generator = torch.Generator(device=device).manual_seed(0)
    image = pipe(prompt, num_inference_steps=num_inference_steps, generator=generator, image=image).images[0]
    return image