File size: 5,193 Bytes
761be79
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
import os
import torch
import numpy as np
import cv2
from diffusers import DiffusionPipeline, StableDiffusionPipeline
from diffusers import StableDiffusionControlNetPipeline, ControlNetModel
from diffusers import DPMSolverMultistepScheduler, UniPCMultistepScheduler
from diffusers import AutoencoderKL
from PIL import Image

##################################################
def make_canny_condition(image):
    image = np.array(image)
    image = cv2.Canny(image, 100, 200)
    image = image[:, :, None]
    image = np.concatenate([image, image, image], axis=2)
    return image

def make_merged_canny(composition_image, reference_image, alpha):
    if isinstance(composition_image, Image.Image):
        composition_image = np.array(composition_image)
    if isinstance(reference_image, Image.Image):
        reference_image = np.array(reference_image)

    composition_image = cv2.resize(composition_image, reference_image.shape[1::-1])

    composition_canny = make_canny_condition(composition_image)
    reference_canny = make_canny_condition(reference_image)
    control_canny = cv2.addWeighted(composition_canny, alpha, reference_canny, (1.0 - alpha), 0.0)
    return control_canny

##################################################
class SDHelper:
    def __init__(self, config) -> None:
        self.setup_config(config)

    def get_stable_diffusion_models(self):
        # "runwayml/stable-diffusion-v1-5", "stabilityai/stable-diffusion-2-1", "stabilityai/stable-diffusion-xl-base-1.0"
        return {
            '1.5': 'runwayml/stable-diffusion-v1-5',
            '2.1': 'stabilityai/stable-diffusion-2-1',
            'xl': 'stabilityai/stable-diffusion-xl-base-1.0',
        }
    

    # controlnet = ControlNetModel.from_pretrained('lllyasviel/control_v11p_sd15_seg', torch_dtype=torch.float16)
    # pipe = StableDiffusionControlNetPipeline.from_pretrained(config.model_id, controlnet=self.controlnet, torch_dtype=torch.float16)
    # def load_model(self, module, model_id, **kwargs):
    #     local_fn = os.path.join(self.config.model_dir, model_id)
    #     if os.path.exists(local_fn):
    #         controlnet = module.from_pretrained(local_fn, **kwargs)
    #     else:
    #         controlnet = module.from_pretrained(model_id, **kwargs)
    #         controlnet.save_pretrained(local_fn)
    #     return controlnet

    # hugging face
    def load_model(self, module, model_id, **kwargs):
        device = "cuda" if torch.cuda.is_available() else "cpu"

        if torch.cuda.is_available():
            torch.cuda.max_memory_allocated(device=device)
            m = module.from_pretrained(model_id, torch_dtype=torch.float16, variant="fp16", 
                                          **kwargs)
            m.enable_xformers_memory_efficient_attention()
            m = m.to(device)
        else: 
            m = module.from_pretrained(model_id, **kwargs)
            m = m.to(device)

        return m

    def setup_config(self, config):
        self.config = config

        # ae
        if config.get('vae', None) is not None:
            vae = self.load_model(AutoencoderKL, config.vae)
        else:
            vae = None

        # with controlnet
        if config.get('controlnet_id', None) is not None:
            self.controlnet = self.load_model(ControlNetModel, config.controlnet_id)
            self.controlnet_conditioning_scale = config.get('controlnet_conditioning_scale', 1.0)

            pipe = self.load_model(StableDiffusionControlNetPipeline, config.model_id, 
                                   controlnet=self.controlnet)

        # w/o controlnet
        else:
            self.controlnet = None

            # stable diffusion pipeline
            if config.model_id == 'stabilityai/stable-diffusion-xl-base-1.0':
                # sdxl
                pipe = self.load_model(DiffusionPipeline, config.model_id)
            else:
                # sd 1.5, 2.1
                pipe = self.load_model(StableDiffusionPipeline, config.model_id)

        # scheduler
        if config.scheduler == 'DPMSolverMultistepScheduler':
            pipe.scheduler = DPMSolverMultistepScheduler.from_config(pipe.scheduler.config)
        elif config.scheduler == 'UniPCMultistepScheduler':
            pipe.scheduler = UniPCMultistepScheduler.from_config(pipe.scheduler.config)
            
        self.pipe = pipe
        
    def forward(self, prompt, control_image=None):
        if isinstance(control_image, np.ndarray):
            control_image = Image.fromarray(control_image)

        num_images_per_prompt = self.config.get('num_images_per_prompt', 4)

        if control_image is None:
            images = self.pipe(prompt, num_images_per_prompt=num_images_per_prompt).images
        else:
            images = self.pipe(prompt, 
                               num_inference_steps=self.config.get('num_inference_steps', 50),
                               image=control_image, 
                               num_images_per_prompt=num_images_per_prompt,
                               controlnet_conditioning_scale=self.controlnet_conditioning_scale,
                               ).images

        return images


    ##########