forked from spacepxl/ComfyUI-StyleGan
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnodes.py
341 lines (291 loc) · 12 KB
/
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
import os
import sys
import numpy as np
import pickle
import torch
import torch.nn.functional as F
from tqdm import trange
from .slerp import slerp
from .str_utils import str2num
from . import dnnlib
from . import torch_utils
# from . import legacy
sys.modules["dnnlib"] = dnnlib
sys.modules["torch_utils"] = torch_utils
# sys.modules["legacy"] = legacy
import folder_paths
from comfy.utils import PROGRESS_BAR_ENABLED, ProgressBar
from comfy.model_management import get_torch_device
# set the models directory
if "stylegan" not in folder_paths.folder_names_and_paths:
current_paths = [os.path.join(folder_paths.models_dir, "stylegan")]
else:
current_paths, _ = folder_paths.folder_names_and_paths["stylegan"]
folder_paths.folder_names_and_paths["stylegan"] = (current_paths, folder_paths.supported_pt_extensions)
# TODO: example workflows
# - Generating images from random latent vectors
# - Generating variants of an image by moving the latent vector in a random direction
# - Interpolating between two images by averaging their latent vectors
# - !!Completing "image analogies" like A:B::C:D (the latent vector of D is calculated as C+B-A)
class LoadStyleGANLatentImg:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_image": ("IMAGE",)
# "stylegan_image": (folder_paths.get_filename_list("output"), ),
},
}
RETURN_TYPES = ("STYLEGAN_LATENT",)
FUNCTION = "load_latent_image"
CATEGORY = "StyleGAN"
def load_latent_image( self, stylegan_image ):
# load file from drag and drop
# get latent zip from metadata
# extract latent from zip
# latent =
return (latent, )
class SaveStyleGANLatentImg:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_image": ("IMAGE",),
"stylegan_latent": ("STYLEGAN_LATENT",),
},
}
RETURN_TYPES = (None,)
FUNCTION = "save_latent_image"
CATEGORY = "StyleGAN"
def save_latent_image( self, stylegan_latent, stylegan_image ):
# encode latent to zip
# add zip to image metadata
# save image as jpeg
return (None, )
class LoadStyleGAN:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_file": (folder_paths.get_filename_list("stylegan"), ),
},
}
RETURN_TYPES = ("STYLEGAN",)
FUNCTION = "load_stylegan"
CATEGORY = "StyleGAN"
def load_stylegan(self, stylegan_file):
with open(folder_paths.get_full_path("stylegan", stylegan_file), 'rb') as f:
G = pickle.load(f)['G_ema'].to(get_torch_device())
return (G,)
class GenerateStyleGANLatent:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_model": ("STYLEGAN", ),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
},
"optional": {
# "class_label": ("INT", {"default": -1, "min": -1}),
"batch_size": ("INT", {"default": 1, "min": 1, "max": 1024}),
"psi": ("FLOAT", {"default": 0.7, "min": -1.0, "max": 1.0, "step": 0.05}),
}
}
RETURN_TYPES = ("STYLEGAN_LATENT",)
FUNCTION = "generate_latent"
CATEGORY = "StyleGAN"
def generate_latent(self, stylegan_model, seed, batch_size, psi):
if seed < 0xffffffff:
# legacy seed compatible with sd-webui-gan-generator
z = np.random.RandomState(seed).randn(batch_size, stylegan_model.z_dim)
z = torch.tensor(z, dtype=torch.float32).to(get_torch_device())
else:
torch.manual_seed(seed)
z = torch.randn([batch_size, stylegan_model.z_dim]).to(get_torch_device())
w = []
w_avg = stylegan_model.mapping.w_avg
_w = stylegan_model.mapping(z, None)
_w = w_avg + (_w - w_avg) * psi
w.append(_w)
return (torch.cat(w, dim=0), )
class StyleGANSampler:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_model": ("STYLEGAN", ),
"stylegan_latent": ("STYLEGAN_LATENT", ),
},
"optional": {
"noise_mode": (["const", "random",], {"default": "const"}),
},
}
RETURN_TYPES = ("IMAGE","STYLEGAN_LATENT",)
FUNCTION = "generate_image"
CATEGORY = "StyleGAN"
def generate_image(self, stylegan_model, stylegan_latent, noise_mode):
imgs = []
batch_size = stylegan_latent.size(0)
pbar = None
if PROGRESS_BAR_ENABLED and batch_size > 1:
pbar = ProgressBar(batch_size)
for i in trange(batch_size):
img = stylegan_model.synthesis(stylegan_latent[i].unsqueeze(0), noise_mode=noise_mode)
img = torch.permute(img, (0, 2, 3, 1)) # BCHW -> BHWC
img = torch.clip(img / 2 + 0.5, 0, 1) # [-1, 1] -> [0, 1]
imgs.append(img)
if pbar is not None:
pbar.update(1)
imgs = torch.cat(imgs, dim=0)
return (imgs, stylegan_latent, )
class StyleGANInversion:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_model": ("STYLEGAN", ),
"image": ("IMAGE", ),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xffffffffffffffff}),
"num_steps": ("INT", {"default": 1000, "min": 1}),
"w_avg_samples": ("INT", {"default": 10000, "min": 1, "max": 100000}),
"initial_learning_rate": ("FLOAT", {"default": 0.1, "min": 0.00001, "max": 1.0, "step": 0.00001}),
"initial_noise_factor": ("FLOAT", {"default": 0.05, "min": 0.0, "max": 1.0, "step": 0.001}),
"lr_rampdown_length": ("FLOAT", {"default": 0.25, "min": 0.0, "max": 1.0, "step": 0.01}),
"lr_rampup_length": ("FLOAT", {"default": 0.05, "min": 0.0, "max": 1.0, "step": 0.01}),
"noise_ramp_length": ("FLOAT", {"default": 0.75, "min": 0.0, "max": 1.0, "step": 0.01}),
"regularize_noise_weight": ("FLOAT", {"default": 1e5, "min": 0.0, "max": 1e7}),
},
}
RETURN_TYPES = ("STYLEGAN_LATENT", "STYLEGAN_LATENT")
RETURN_NAMES = ("training_latents", "final_latent")
FUNCTION = "train_inversion"
CATEGORY = "StyleGAN"
def train_inversion(
self,
stylegan_model,
image,
seed,
num_steps,
w_avg_samples,
initial_learning_rate,
initial_noise_factor,
lr_rampdown_length,
lr_rampup_length,
noise_ramp_length,
regularize_noise_weight
):
device = get_torch_device()
img_resolution = stylegan_model.img_resolution
target_image = torch.permute(image[...,:3], (0, 3, 1, 2)) # BHWC -> BCHW, RGB only
if target_image.shape != (stylegan_model.img_channels, img_resolution, img_resolution):
target_image = F.interpolate(target_image, size=(img_resolution, img_resolution), mode='area')
target_image = target_image[0] * 255
from .projector import project
projected_w_steps = project(
stylegan_model,
target_image,
num_steps = num_steps,
w_avg_samples = w_avg_samples,
seed = seed,
initial_learning_rate = initial_learning_rate,
initial_noise_factor = initial_noise_factor,
lr_rampdown_length = lr_rampdown_length,
lr_rampup_length = lr_rampup_length,
noise_ramp_length = noise_ramp_length,
regularize_noise_weight = regularize_noise_weight,
device = device,
)
return (projected_w_steps, projected_w_steps[-1].unsqueeze(0))
class BlendStyleGANLatents:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"latent_1": ("STYLEGAN_LATENT", ),
"latent_2": ("STYLEGAN_LATENT", ),
"blend": ("FLOAT", {"default": 0.5, "min": -10.0, "max": 10.0, "step": 0.001}),
"mode": (["slerp", "lerp"],),
"mask": (["total (0xFFFF)", "coarse (0xFF00)", "mid (0x0FF0)", "fine (0x00FF)", "alt1 (0xF0F0)", "alt2 (0x0F0F)", "alt3 (0xF00F)"],)
},
}
RETURN_TYPES = ("STYLEGAN_LATENT",)
FUNCTION = "generate_latent"
CATEGORY = "StyleGAN/extra"
def generate_latent(self, latent_1, latent_2, blend, mode, mask):
if latent_1.shape != latent_2.shape:
raise Exception(f"latent_1 shape {latent_1.shape} and latent_2 shape {latent_2.shape} do not match!")
z = latent_1.clone() # transfer onto L image as default
if mask == 0xFFFF:
blend = self.jmap(blend, -1.0, 1.0, 0.0, 1.0) # make unipolar
else:
if blend > 0: # transfer L onto R
z = latent_2.clone()
else: # transfer R onto L
blend = abs(blend)
latent_1,latent_2 = latent_2,latent_1 # swap L and R
mask = self.num2mask( str2num(mask) )
m = slerp if mode == "slerp" else torch.lerp
z[:,mask,:] = m(latent_1[:,mask,:], latent_2[:,mask,:], blend)
return (z,)
# @classmethod
def jmap(self, sourceValue, sourceRangeMin, sourceRangeMax, targetRangeMin, targetRangeMax) -> float:
if sourceRangeMax == sourceRangeMin:
raise ValueError("mapping from a range of zero will produce NaN!")
return targetRangeMin + ((targetRangeMax - targetRangeMin) * (sourceValue - sourceRangeMin)) / (sourceRangeMax - sourceRangeMin)
# @classmethod
def num2mask(self, num: int) -> np.ndarray:
return np.array([x=='1' for x in bin(num)[2:].zfill(16)], dtype=bool)
class BatchAverageStyleGANLatents:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_latent": ("STYLEGAN_LATENT", ),
},
}
RETURN_TYPES = ("STYLEGAN_LATENT",)
FUNCTION = "generate_latent"
CATEGORY = "StyleGAN/extra"
def generate_latent(self, stylegan_latent):
w = torch.mean(stylegan_latent, dim=0, keepdim=True)
std, mean = torch.std_mean(w)
w = (w - mean) / std
return (w, )
class StyleGANLatentFromBatch:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"stylegan_latent": ("STYLEGAN_LATENT", ),
"index": ("INT", {"default": 0, "min": 0}),
},
}
RETURN_TYPES = ("STYLEGAN_LATENT",)
FUNCTION = "generate_latent"
CATEGORY = "StyleGAN/extra"
def generate_latent(self, stylegan_latent, index):
clipped_index = min(index, stylegan_latent.size(0) - 1)
w = stylegan_latent[clipped_index].unsqueeze(0).detach().clone()
return (w, )
NODE_CLASS_MAPPINGS = {
"LoadStyleGANLatentImg": LoadStyleGANLatentImg,
"SaveStyleGANLatentImg": SaveStyleGANLatentImg,
"LoadStyleGAN": LoadStyleGAN,
"GenerateStyleGANLatent": GenerateStyleGANLatent,
"StyleGANSampler": StyleGANSampler,
"BlendStyleGANLatents": BlendStyleGANLatents,
"BatchAverageStyleGANLatents": BatchAverageStyleGANLatents,
"StyleGANLatentFromBatch": StyleGANLatentFromBatch,
"StyleGANInversion": StyleGANInversion,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"LoadStyleGANLatentImg": "Load StyleGAN Latent from Image Metadata",
"SaveStyleGANLatentImg": "Save StyleGAN Latent to Image Metadata",
"LoadStyleGAN": "Load StyleGAN Model",
"GenerateStyleGANLatent": "Generate StyleGAN Latent",
"StyleGANSampler": "StyleGAN Sampler",
"BlendStyleGANLatents": "Blend StyleGAN Latents (lerp or slerp)",
"BatchAverageStyleGANLatents": "Batch Average StyleGAN Latents",
"StyleGANLatentFromBatch": "StyleGAN Latent From Batch",
"StyleGANInversion": "StyleGAN Inversion",
}