-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDiffree_node.py
116 lines (99 loc) · 4.41 KB
/
Diffree_node.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
# !/usr/bin/env python
# -*- coding: UTF-8 -*-
import os
import numpy as np
import torch
from omegaconf import OmegaConf
from .utils import load_model_from_config,CompVisDenoiser,CFGDenoiser,nomarl_upscale_to_pil,generate,load_images
import folder_paths
cur_path = os.path.dirname(os.path.abspath(__file__))
MAX_SEED = np.iinfo(np.int32).max
device = (
"cuda"
if torch.cuda.is_available()
else "mps" if torch.backends.mps.is_available() else "cpu"
)
dtype = torch.float16 if device == "cuda" else torch.float
class Diffree_Model_Loader:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"ckpt_name": (["none"] + folder_paths.get_filename_list("checkpoints"),),
"vae": (["none"] + folder_paths.get_filename_list("vae"),),
}
}
RETURN_TYPES = ("DIFFREE_MODEL", )
RETURN_NAMES = ("pipe",)
FUNCTION = "main_loader"
CATEGORY = "Diffree"
def main_loader(self, ckpt_name, vae,):
if vae != "none":
vae_ckpt = folder_paths.get_full_path("vae", vae)
else:
vae_ckpt = None
ckpt = folder_paths.get_full_path("checkpoints", ckpt_name)
config_path = os.path.join(cur_path, "config", "generate.yaml")
config = OmegaConf.load(config_path)
model = load_model_from_config(config, ckpt, vae_ckpt)
model.eval()
model_wrap = CompVisDenoiser(model)
model_wrap_cfg = CFGDenoiser(model_wrap)
pipe={"model":model,"model_wrap":model_wrap,"model_wrap_cfg":model_wrap_cfg}
return (pipe,)
class Diffree_Sampler:
def __init__(self):
pass
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE",),
"pipe": ("DIFFREE_MODEL",),
"prompt": ("STRING", {"multiline": True, "default": "reflective sunglasses"}),
"randomize_seed":("BOOLEAN", {"default": False},),
"seed": ("INT", {"default": 100, "min": 0, "max": MAX_SEED}),
"steps": ("INT", {"default": 20, "min": 1, "max": 100}),
"randomize_cfg": ("BOOLEAN", {"default": False},),
"text_cfg": ("FLOAT", {"default": 7.5, "min": 0.1, "max": 10.0, "step": 0.1, "round": 0.01}),
"img_cfg": ("FLOAT", {"default": 1.5, "min": 0.1, "max": 10.0, "step": 0.1, "round": 0.01}),
"width": ("INT", {"default": 512, "min": 256, "max": 2048, "step": 64, "display": "number"}),
"height": ("INT", {"default": 512, "min": 256, "max": 2048, "step": 64, "display": "number"}),
"rgb_mode":(["red","green","blue",],),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 100, "step": 1, }),}
}
RETURN_TYPES = ("IMAGE","IMAGE","IMAGE",)
RETURN_NAMES = ("image","mask","rgb_mask",)
FUNCTION = "df_sampler"
CATEGORY = "Diffree"
def df_sampler(self, image, pipe,prompt, randomize_seed,seed, steps,randomize_cfg, text_cfg,img_cfg,width,height,rgb_mode,batch_size,):
model=pipe.get("model")
model_wrap=pipe.get("model_wrap")
model_wrap_cfg=pipe.get("model_wrap_cfg")
model.cuda() if model is not None else model
model_wrap_cfg.cuda() if model_wrap_cfg is not None else model_wrap_cfg
image=nomarl_upscale_to_pil(image,width,height)
mix_image_list,mask_img_list,mix_result_with_rgb_mask_list=[],[],[]
for i in range(batch_size):
mix_image,mask_img,mix_result_with_rgb_mask=generate(model,model_wrap,model_wrap_cfg,image,prompt,width,height,steps,randomize_seed,seed,randomize_cfg,text_cfg,img_cfg,rgb_mode,dtype)
mix_image_list.append(mix_image)
mask_img_list.append(mask_img)
mix_result_with_rgb_mask_list.append(mix_result_with_rgb_mask)
image=load_images(mix_image_list)
mask = load_images(mask_img_list)
rgb_mask=load_images(mix_result_with_rgb_mask_list)
model.cpu()
model_wrap.cpu()
model_wrap_cfg.inner_model.cpu()
torch.cuda.empty_cache()
return (image,mask,rgb_mask)
NODE_CLASS_MAPPINGS = {
"Diffree_Model_Loader": Diffree_Model_Loader,
"Diffree_Sampler": Diffree_Sampler,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"Diffree_Model_Loader": "Diffree_Model_Loader",
"Diffree_Sampler": "Diffree_Sampler",
}