-
Notifications
You must be signed in to change notification settings - Fork 2
/
stable_makeup_nodes.py
233 lines (207 loc) · 8.81 KB
/
stable_makeup_nodes.py
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
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import folder_paths
import os
import torch
import diffusers
dif_version = str(diffusers.__version__)
dif_version_int = int(dif_version.split(".")[1])
from diffusers import (DDIMScheduler, ControlNetModel,
KDPM2AncestralDiscreteScheduler, LMSDiscreteScheduler, DPMSolverMultistepScheduler,
DPMSolverSinglestepScheduler,
EulerDiscreteScheduler, HeunDiscreteScheduler, KDPM2DiscreteScheduler,
EulerAncestralDiscreteScheduler, UniPCMultistepScheduler,
DDPMScheduler, LCMScheduler, StableDiffusionPipeline, )
from .pipeline_sd15 import StableDiffusionControlNetPipeline
from .detail_encoder.encoder_plus import detail_encoder
from .import spiga_draw
from PIL import Image
from .facelib import FaceDetector
from comfy.utils import common_upscale
import numpy as np
makeup_current_path = os.path.dirname(os.path.abspath(__file__))
weigths_current_path = os.path.join(folder_paths.models_dir, "stable_makeup")
if not os.path.exists(weigths_current_path):
os.makedirs(weigths_current_path)
scheduler_list = ["DDIM",
"Euler",
"Euler a",
"DDPM",
"DPM++ 2M",
"DPM++ 2M Karras",
"DPM++ 2M SDE",
"DPM++ 2M SDE Karras",
"DPM++ SDE",
"DPM++ SDE Karras",
"DPM2",
"DPM2 Karras",
"DPM2 a",
"DPM2 a Karras",
"Heun",
"LCM",
"LMS",
"LMS Karras",
"UniPC",
]
def get_sheduler(name):
scheduler = False
if name == "Euler":
scheduler = EulerDiscreteScheduler()
elif name == "Euler a":
scheduler = EulerAncestralDiscreteScheduler()
elif name == "DDIM":
scheduler = DDIMScheduler()
elif name == "DDPM":
scheduler = DDPMScheduler()
elif name == "DPM++ 2M":
scheduler = DPMSolverMultistepScheduler()
elif name == "DPM++ 2M Karras":
scheduler = DPMSolverMultistepScheduler(use_karras_sigmas=True)
elif name == "DPM++ 2M SDE":
scheduler = DPMSolverMultistepScheduler(algorithm_type="sde-dpmsolver++")
elif name == "DPM++ 2M SDE Karras":
scheduler = DPMSolverMultistepScheduler(use_karras_sigmas=True, algorithm_type="sde-dpmsolver++")
elif name == "DPM++ SDE":
scheduler = DPMSolverSinglestepScheduler()
elif name == "DPM++ SDE Karras":
scheduler = DPMSolverSinglestepScheduler(use_karras_sigmas=True)
elif name == "DPM2":
scheduler = KDPM2DiscreteScheduler()
elif name == "DPM2 Karras":
scheduler = KDPM2DiscreteScheduler(use_karras_sigmas=True)
elif name == "DPM2 a":
scheduler = KDPM2AncestralDiscreteScheduler()
elif name == "DPM2 a Karras":
scheduler = KDPM2AncestralDiscreteScheduler(use_karras_sigmas=True)
elif name == "Heun":
scheduler = HeunDiscreteScheduler()
elif name == "LCM":
scheduler = LCMScheduler()
elif name == "LMS":
scheduler = LMSDiscreteScheduler()
elif name == "LMS Karras":
scheduler = LMSDiscreteScheduler(use_karras_sigmas=True)
elif name == "UniPC":
scheduler = UniPCMultistepScheduler()
return scheduler
def tensor_to_pil(tensor):
image_np = tensor.squeeze().mul(255).clamp(0, 255).byte().numpy()
image = Image.fromarray(image_np, mode='RGB')
return image
def pil2narry(img):
img = torch.from_numpy(np.array(img).astype(np.float32) / 255.0).unsqueeze(0)
return img
def nomarl_upscale(img_tensor, width, height):
samples = img_tensor.movedim(-1, 1)
img = common_upscale(samples, width, height, "nearest-exact", "center")
samples = img.movedim(1, -1)
img_pil = tensor_to_pil(samples)
return img_pil
class StableMakeup_LoadModel:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"ckpt_name": (folder_paths.get_filename_list("checkpoints"),),
"clip":("STRING", {"default": "openai/clip-vit-large-patch14"}),
"scheduler": (scheduler_list,),
}
}
RETURN_TYPES = ("MODEL","MODEL",)
RETURN_NAMES = ("model","makeup_encoder",)
FUNCTION = "main_loader"
CATEGORY = "Stable_Makeup"
def main_loader(self,ckpt_name,clip,scheduler):
ckpt_path = folder_paths.get_full_path("checkpoints", ckpt_name)
scheduler_used = get_sheduler(scheduler)
makeup_encoder_path = os.path.join(weigths_current_path,"pytorch_model.bin")
id_encoder_path = os.path.join(weigths_current_path,"pytorch_model_1.bin")
pose_encoder_path = os.path.join(weigths_current_path,"pytorch_model_2.bin")
original_config_file=os.path.join(folder_paths.models_dir,"configs","v1-inference.yaml")
sd15_config=os.path.join(makeup_current_path,"sd15_config")
if dif_version_int >= 28:
pipe = StableDiffusionPipeline.from_single_file(
ckpt_path,config=sd15_config, original_config=original_config_file)
else:
pipe = StableDiffusionPipeline.from_single_file(
ckpt_path, config=sd15_config,original_config_file=original_config_file)
pipe.to("cuda")
Unet= pipe.unet
vae=pipe.vae
text_encoder = pipe.text_encoder
id_encoder = ControlNetModel.from_unet(Unet)
pose_encoder = ControlNetModel.from_unet(Unet)
makeup_encoder = detail_encoder(Unet, clip, "cuda", dtype=torch.float32)
makeup_state_dict = torch.load(makeup_encoder_path)
id_state_dict = torch.load(id_encoder_path)
id_encoder.load_state_dict(id_state_dict, strict=False)
pose_state_dict = torch.load(pose_encoder_path)
pose_encoder.load_state_dict(pose_state_dict, strict=False)
makeup_encoder.load_state_dict(makeup_state_dict, strict=False)
id_encoder.to("cuda")
pose_encoder.to("cuda")
makeup_encoder.to("cuda")
pipe = StableDiffusionControlNetPipeline.from_pretrained(
sd15_config,
safety_checker=None,
unet=Unet,
vae=vae,
text_encoder=text_encoder,
controlnet=[id_encoder, pose_encoder],
torch_dtype=torch.float32).to("cuda")
pipe.scheduler = scheduler_used.from_config(pipe.scheduler.config)
return (pipe,makeup_encoder)
class StableMakeup_Sampler:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"id_image": ("IMAGE",),
"makeup_image": ("IMAGE",),
"pipe": ("MODEL",),
"makeup_encoder": ("MODEL",),
"facedetector": (["mobilenet","resnet"],),
"dataname": (["300wpublic", "300wprivate","merlrav","wflw"],),
"cfg": ("FLOAT", {"default": 1.6, "min": 0.0, "max": 30.0, "step": 0.1, "round": 0.01}),
"steps": ("INT", {"default": 30, "min": 1, "max": 10000}),
"width": ("INT", {"default": 512, "min": 256, "max": 768, "step": 64, "display": "number"}),
"height": ("INT", {"default": 512, "min": 256, "max": 768, "step": 64, "display": "number"}),
}
}
RETURN_TYPES = ("IMAGE",)
RETURN_NAMES = ("image",)
FUNCTION = "makeup_main"
CATEGORY = "Stable_Makeup"
def makeup_main(self, id_image, makeup_image, pipe, makeup_encoder,facedetector,dataname,cfg, steps,
width, height ):
if facedetector=="mobilenet":
weight_path=os.path.join(weigths_current_path, "mobilenet0.25_Final.pth")
else:
weight_path=os.path.join(weigths_current_path, "resnet50.pth")
detector = FaceDetector(name=facedetector,weight_path=weight_path)
def get_draw(pil_img, size,dataname):
spigas = spiga_draw.spiga_process(pil_img, detector,dataname)
if spigas == False:
width, height = pil_img.size
black_image_pil = Image.new('RGB', (width, height), color=(0, 0, 0))
return black_image_pil
else:
spigas_faces = spiga_draw.spiga_segmentation(spigas, size=size)
return spigas_faces
id_image=nomarl_upscale(id_image, width, height)
makeup_image = nomarl_upscale(makeup_image, width, height)
pose_image = get_draw(id_image, size=width,dataname=dataname)
result_img = makeup_encoder.generate(id_image=[id_image, pose_image], makeup_image=makeup_image,num_inference_steps=steps,
pipe=pipe, guidance_scale=cfg)
image=pil2narry(result_img)
return (image,)
NODE_CLASS_MAPPINGS = {
"StableMakeup_LoadModel":StableMakeup_LoadModel,
"StableMakeup_Sampler": StableMakeup_Sampler
}
NODE_DISPLAY_NAME_MAPPINGS = {
"StableMakeup_LoadModel":"StableMakeup_LoadModel",
"StableMakeup_Sampler": "StableMakeup_Sampler",
}