-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathnodes.py
475 lines (390 loc) · 15.2 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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
from __future__ import annotations
from typing import Any
import numpy as np
import torch
import torch.jit
import torch.nn.functional as F
from torch import Tensor
from tqdm import trange
from comfy.utils import ProgressBar
from comfy.model_patcher import ModelPatcher
from comfy.model_base import BaseModel
from comfy.model_management import cast_to_device, get_torch_device
import comfy.utils
import comfy.lora
import folder_paths
import nodes
from . import mat
from .util import (
gaussian_blur,
binary_erosion,
binary_dilation,
make_odd,
mask_floor,
mask_unsqueeze,
to_torch,
to_comfy,
resize_square,
undo_resize_square,
)
class InpaintHead(torch.nn.Module):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.head = torch.nn.Parameter(torch.empty(size=(320, 5, 3, 3), device="cpu"))
def __call__(self, x):
x = F.pad(x, (1, 1, 1, 1), "replicate")
return F.conv2d(x, weight=self.head)
def load_fooocus_patch(lora: dict, to_load: dict):
patch_dict = {}
loaded_keys = set()
for key in to_load.values():
if value := lora.get(key, None):
patch_dict[key] = ("fooocus", value)
loaded_keys.add(key)
not_loaded = sum(1 for x in lora if x not in loaded_keys)
if not_loaded > 0:
print(
f"[ApplyFooocusInpaint] {len(loaded_keys)} Lora keys loaded, {not_loaded} remaining keys not found in model."
)
return patch_dict
if not hasattr(comfy.lora, "calculate_weight") and hasattr(ModelPatcher, "calculate_weight"):
too_old_msg = "comfyui-inpaint-nodes requires a newer version of ComfyUI (v0.1.1 or later), please update!"
raise RuntimeError(too_old_msg)
original_calculate_weight = comfy.lora.calculate_weight
injected_model_patcher_calculate_weight = False
def calculate_weight_patched(patches, weight, key, intermediate_dtype=torch.float32):
remaining = []
for p in patches:
alpha = p[0]
v = p[1]
is_fooocus_patch = isinstance(v, tuple) and len(v) == 2 and v[0] == "fooocus"
if not is_fooocus_patch:
remaining.append(p)
continue
if alpha != 0.0:
v = v[1]
w1 = cast_to_device(v[0], weight.device, torch.float32)
if w1.shape == weight.shape:
w_min = cast_to_device(v[1], weight.device, torch.float32)
w_max = cast_to_device(v[2], weight.device, torch.float32)
w1 = (w1 / 255.0) * (w_max - w_min) + w_min
weight += alpha * cast_to_device(w1, weight.device, weight.dtype)
else:
print(
f"[ApplyFooocusInpaint] Shape mismatch {key}, weight not merged ({w1.shape} != {weight.shape})"
)
if len(remaining) > 0:
return original_calculate_weight(remaining, weight, key, intermediate_dtype)
return weight
def inject_patched_calculate_weight():
global injected_model_patcher_calculate_weight
if not injected_model_patcher_calculate_weight:
print(
"[comfyui-inpaint-nodes] Injecting patched comfy.model_patcher.ModelPatcher.calculate_weight"
)
comfy.lora.calculate_weight = calculate_weight_patched
injected_model_patcher_calculate_weight = True
class LoadFooocusInpaint:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"head": (folder_paths.get_filename_list("inpaint"),),
"patch": (folder_paths.get_filename_list("inpaint"),),
}
}
RETURN_TYPES = ("INPAINT_PATCH",)
CATEGORY = "inpaint"
FUNCTION = "load"
def load(self, head: str, patch: str):
head_file = folder_paths.get_full_path("inpaint", head)
inpaint_head_model = InpaintHead()
sd = torch.load(head_file, map_location="cpu", weights_only=True)
inpaint_head_model.load_state_dict(sd)
patch_file = folder_paths.get_full_path("inpaint", patch)
inpaint_lora = comfy.utils.load_torch_file(patch_file, safe_load=True)
return ((inpaint_head_model, inpaint_lora),)
class ApplyFooocusInpaint:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model": ("MODEL",),
"patch": ("INPAINT_PATCH",),
"latent": ("LATENT",),
}
}
RETURN_TYPES = ("MODEL",)
CATEGORY = "inpaint"
FUNCTION = "patch"
_inpaint_head_feature: Tensor | None = None
_inpaint_block: Tensor | None = None
def patch(
self,
model: ModelPatcher,
patch: tuple[InpaintHead, dict[str, Tensor]],
latent: dict[str, Any],
):
base_model: BaseModel = model.model
latent_pixels = base_model.process_latent_in(latent["samples"])
noise_mask = latent["noise_mask"].round()
latent_mask = F.max_pool2d(noise_mask, (8, 8)).round().to(latent_pixels)
inpaint_head_model, inpaint_lora = patch
feed = torch.cat([latent_mask, latent_pixels], dim=1)
inpaint_head_model.to(device=feed.device, dtype=feed.dtype)
self._inpaint_head_feature = inpaint_head_model(feed)
self._inpaint_block = None
lora_keys = comfy.lora.model_lora_keys_unet(model.model, {})
lora_keys.update({x: x for x in base_model.state_dict().keys()})
loaded_lora = load_fooocus_patch(inpaint_lora, lora_keys)
m = model.clone()
m.set_model_input_block_patch(self._input_block_patch)
patched = m.add_patches(loaded_lora, 1.0)
not_patched_count = sum(1 for x in loaded_lora if x not in patched)
if not_patched_count > 0:
print(f"[ApplyFooocusInpaint] Failed to patch {not_patched_count} keys")
inject_patched_calculate_weight()
return (m,)
def _input_block_patch(self, h: Tensor, transformer_options: dict):
if transformer_options["block"][1] == 0:
if self._inpaint_block is None or self._inpaint_block.shape != h.shape:
assert self._inpaint_head_feature is not None
batch = h.shape[0] // self._inpaint_head_feature.shape[0]
self._inpaint_block = self._inpaint_head_feature.to(h).repeat(batch, 1, 1, 1)
h = h + self._inpaint_block
return h
class VAEEncodeInpaintConditioning:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"positive": ("CONDITIONING",),
"negative": ("CONDITIONING",),
"vae": ("VAE",),
"pixels": ("IMAGE",),
"mask": ("MASK",),
}
}
RETURN_TYPES = ("CONDITIONING", "CONDITIONING", "LATENT", "LATENT")
RETURN_NAMES = ("positive", "negative", "latent_inpaint", "latent_samples")
FUNCTION = "encode"
CATEGORY = "inpaint"
def encode(self, positive, negative, vae, pixels, mask):
try:
positive, negative, latent = nodes.InpaintModelConditioning().encode(
positive, negative, pixels, vae, mask, noise_mask=True
)
except TypeError: # ComfyUI versions older than 2024-11-19
positive, negative, latent = nodes.InpaintModelConditioning().encode(
positive, negative, pixels, vae, mask
)
latent_inpaint = dict(
samples=positive[0][1]["concat_latent_image"],
noise_mask=latent["noise_mask"].round(),
)
return (positive, negative, latent_inpaint, latent)
class MaskedFill:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"mask": ("MASK",),
"fill": (["neutral", "telea", "navier-stokes"],),
"falloff": ("INT", {"default": 0, "min": 0, "max": 8191, "step": 1}),
}
}
RETURN_TYPES = ("IMAGE",)
CATEGORY = "inpaint"
FUNCTION = "fill"
def fill(self, image: Tensor, mask: Tensor, fill: str, falloff: int):
image = image.detach().clone()
alpha = mask_unsqueeze(mask_floor(mask))
assert alpha.shape[0] == image.shape[0], "Image and mask batch size does not match"
falloff = make_odd(falloff)
if falloff > 0:
erosion = binary_erosion(alpha, falloff)
alpha = alpha * gaussian_blur(erosion, falloff)
if fill == "neutral":
m = (1.0 - alpha).squeeze(1)
for i in range(3):
image[:, :, :, i] -= 0.5
image[:, :, :, i] *= m
image[:, :, :, i] += 0.5
else:
import cv2
method = cv2.INPAINT_TELEA if fill == "telea" else cv2.INPAINT_NS
for slice, alpha_slice in zip(image, alpha):
alpha_np = alpha_slice.squeeze().cpu().numpy()
alpha_bc = alpha_np.reshape(*alpha_np.shape, 1)
image_np = slice.cpu().numpy()
filled_np = cv2.inpaint(
(255.0 * image_np).astype(np.uint8),
(255.0 * alpha_np).astype(np.uint8),
3,
method,
)
filled_np = filled_np.astype(np.float32) / 255.0
filled_np = image_np * (1.0 - alpha_bc) + filled_np * alpha_bc
slice.copy_(torch.from_numpy(filled_np))
return (image,)
class MaskedBlur:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"image": ("IMAGE",),
"mask": ("MASK",),
"blur": ("INT", {"default": 255, "min": 3, "max": 8191, "step": 1}),
"falloff": ("INT", {"default": 0, "min": 0, "max": 8191, "step": 1}),
}
}
RETURN_TYPES = ("IMAGE",)
CATEGORY = "inpaint"
FUNCTION = "fill"
def fill(self, image: Tensor, mask: Tensor, blur: int, falloff: int):
blur = make_odd(blur)
falloff = min(make_odd(falloff), blur - 2)
image, mask = to_torch(image, mask)
original = image.clone()
alpha = mask_floor(mask)
if falloff > 0:
erosion = binary_erosion(alpha, falloff)
alpha = alpha * gaussian_blur(erosion, falloff)
alpha = alpha.expand(-1, 3, -1, -1)
image = gaussian_blur(image, blur)
image = original + (image - original) * alpha
return (to_comfy(image),)
class LoadInpaintModel:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"model_name": (folder_paths.get_filename_list("inpaint"),),
}
}
RETURN_TYPES = ("INPAINT_MODEL",)
CATEGORY = "inpaint"
FUNCTION = "load"
def load(self, model_name: str):
from spandrel import ModelLoader
model_file = folder_paths.get_full_path("inpaint", model_name)
if model_file is None:
raise RuntimeError(f"Model file not found: {model_name}")
if model_file.endswith(".pt"):
sd = torch.jit.load(model_file, map_location="cpu").state_dict()
else:
sd = comfy.utils.load_torch_file(model_file, safe_load=True)
if "synthesis.first_stage.conv_first.conv.resample_filter" in sd: # MAT
model = mat.load(sd)
else:
model = ModelLoader().load_from_state_dict(sd)
model = model.eval()
return (model,)
class InpaintWithModel:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"inpaint_model": ("INPAINT_MODEL",),
"image": ("IMAGE",),
"mask": ("MASK",),
"seed": ("INT", {"default": 0, "min": 0, "max": 0xFFFFFFFFFFFFFFFF}),
},
"optional": {
"optional_upscale_model": ("UPSCALE_MODEL",),
},
}
RETURN_TYPES = ("IMAGE",)
CATEGORY = "inpaint"
FUNCTION = "inpaint"
def inpaint(
self,
inpaint_model: mat.MAT | Any,
image: Tensor,
mask: Tensor,
seed: int,
optional_upscale_model=None,
):
if isinstance(inpaint_model, mat.MAT):
required_size = 512
elif inpaint_model.architecture.id == "LaMa":
required_size = 256
else:
raise ValueError(f"Unknown model_arch {type(inpaint_model)}")
if optional_upscale_model != None:
from comfy_extras.nodes_upscale_model import ImageUpscaleWithModel
upscaler = ImageUpscaleWithModel
image, mask = to_torch(image, mask)
batch_size = image.shape[0]
if mask.shape[0] != batch_size:
mask = mask[0].unsqueeze(0).repeat(batch_size, 1, 1, 1)
image_device = image.device
device = get_torch_device()
inpaint_model.to(device)
batch_image = []
pbar = ProgressBar(batch_size)
for i in trange(batch_size):
work_image, work_mask = image[i].unsqueeze(0), mask[i].unsqueeze(0)
work_image, work_mask, original_size = resize_square(
work_image, work_mask, required_size
)
work_mask = mask_floor(work_mask)
torch.manual_seed(seed)
work_image = inpaint_model(work_image.to(device), work_mask.to(device))
if optional_upscale_model != None:
work_image = work_image.movedim(1, -1)
work_image = upscaler.upscale(upscaler, optional_upscale_model, work_image)
work_image = work_image[0].movedim(-1, 1)
work_image.to(image_device)
work_image = undo_resize_square(work_image.to(image_device), original_size)
work_image = image[i] + (work_image - image[i]) * mask_floor(mask[i])
batch_image.append(work_image)
pbar.update(1)
inpaint_model.cpu()
result = torch.cat(batch_image, dim=0)
return (to_comfy(result),)
class DenoiseToCompositingMask:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"mask": ("MASK",),
"offset": (
"FLOAT",
{"default": 0.1, "min": 0.0, "max": 1.0, "step": 0.01},
),
"threshold": (
"FLOAT",
{"default": 0.2, "min": 0.01, "max": 1.0, "step": 0.01},
),
}
}
RETURN_TYPES = ("MASK",)
CATEGORY = "inpaint"
FUNCTION = "convert"
def convert(self, mask: Tensor, offset: float, threshold: float):
assert 0.0 <= offset < threshold <= 1.0, "Threshold must be higher than offset"
mask = (mask - offset) * (1 / (threshold - offset))
mask = mask.clamp(0, 1)
return (mask,)
class ExpandMask:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"mask": ("MASK",),
"grow": ("INT", {"default": 16, "min": 0, "max": 8096, "step": 1}),
"blur": ("INT", {"default": 7, "min": 0, "max": 8096, "step": 1}),
}
}
RETURN_TYPES = ("MASK",)
CATEGORY = "inpaint"
FUNCTION = "expand"
def expand(self, mask: Tensor, grow: int, blur: int):
mask = mask_unsqueeze(mask)
if grow > 0:
mask = binary_dilation(mask, grow)
if blur > 0:
mask = gaussian_blur(mask, make_odd(blur))
return (mask.squeeze(1),)