forked from storyicon/comfyui_segment_anything
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathnode.py
More file actions
638 lines (540 loc) · 20.2 KB
/
node.py
File metadata and controls
638 lines (540 loc) · 20.2 KB
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
import os
import sys
import cv2
sys.path.append(
os.path.dirname(os.path.abspath(__file__))
)
import copy
import torch
import numpy as np
from PIL import Image,ImageDraw
import logging
from torch.hub import download_url_to_file
from urllib.parse import urlparse
import folder_paths
import comfy.model_management
from sam_hq.predictor import SamPredictorHQ
from sam_hq.build_sam_hq import sam_model_registry
from local_groundingdino.datasets import transforms as T
from local_groundingdino.util.utils import clean_state_dict as local_groundingdino_clean_state_dict
from local_groundingdino.util.slconfig import SLConfig as local_groundingdino_SLConfig
from local_groundingdino.models import build_model as local_groundingdino_build_model
import glob
import folder_paths
from transformers import VitMatteImageProcessor, VitMatteForImageMatting
logger = logging.getLogger('comfyui_segment_anything')
sam_model_dir_name = "sams"
sam_model_list = {
"sam_vit_h (2.56GB)": {
"model_url": "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth"
},
"sam_vit_l (1.25GB)": {
"model_url": "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_l_0b3195.pth"
},
"sam_vit_b (375MB)": {
"model_url": "https://dl.fbaipublicfiles.com/segment_anything/sam_vit_b_01ec64.pth"
},
"sam_hq_vit_h (2.57GB)": {
"model_url": "https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_h.pth"
},
"sam_hq_vit_l (1.25GB)": {
"model_url": "https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_l.pth"
},
"sam_hq_vit_b (379MB)": {
"model_url": "https://huggingface.co/lkeab/hq-sam/resolve/main/sam_hq_vit_b.pth"
},
"mobile_sam(39MB)": {
"model_url": "https://github.com/ChaoningZhang/MobileSAM/blob/master/weights/mobile_sam.pt"
}
}
groundingdino_model_dir_name = "grounding-dino"
groundingdino_model_list = {
"GroundingDINO_SwinT_OGC (694MB)": {
"config_url": "https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/GroundingDINO_SwinT_OGC.cfg.py",
"model_url": "https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swint_ogc.pth",
},
"GroundingDINO_SwinB (938MB)": {
"config_url": "https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/GroundingDINO_SwinB.cfg.py",
"model_url": "https://huggingface.co/ShilongLiu/GroundingDINO/resolve/main/groundingdino_swinb_cogcoor.pth"
},
}
def get_bert_base_uncased_model_path():
comfy_bert_model_base = os.path.join(folder_paths.models_dir, 'bert-base-uncased')
if glob.glob(os.path.join(comfy_bert_model_base, '**/model.safetensors'), recursive=True):
print('grounding-dino is using models/bert-base-uncased')
return comfy_bert_model_base
return 'bert-base-uncased'
def list_files(dirpath, extensions=[]):
return [f for f in os.listdir(dirpath) if os.path.isfile(os.path.join(dirpath, f)) and f.split('.')[-1] in extensions]
def list_sam_model():
return list(sam_model_list.keys())
def load_sam_model(model_name):
sam_checkpoint_path = get_local_filepath(
sam_model_list[model_name]["model_url"], sam_model_dir_name)
model_file_name = os.path.basename(sam_checkpoint_path)
model_type = model_file_name.split('.')[0]
if 'hq' not in model_type and 'mobile' not in model_type:
model_type = '_'.join(model_type.split('_')[:-1])
sam = sam_model_registry[model_type](checkpoint=sam_checkpoint_path)
sam_device = comfy.model_management.get_torch_device()
sam.to(device=sam_device)
sam.eval()
sam.model_name = model_file_name
return sam
def get_local_filepath(url, dirname, local_file_name=None):
if not local_file_name:
parsed_url = urlparse(url)
local_file_name = os.path.basename(parsed_url.path)
destination = folder_paths.get_full_path(dirname, local_file_name)
if destination:
logger.warn(f'using extra model: {destination}')
return destination
folder = os.path.join(folder_paths.models_dir, dirname)
if not os.path.exists(folder):
os.makedirs(folder)
destination = os.path.join(folder, local_file_name)
if not os.path.exists(destination):
logger.warn(f'downloading {url} to {destination}')
download_url_to_file(url, destination)
return destination
def load_groundingdino_model(model_name):
dino_model_args = local_groundingdino_SLConfig.fromfile(
get_local_filepath(
groundingdino_model_list[model_name]["config_url"],
groundingdino_model_dir_name
),
)
if dino_model_args.text_encoder_type == 'bert-base-uncased':
dino_model_args.text_encoder_type = get_bert_base_uncased_model_path()
dino = local_groundingdino_build_model(dino_model_args)
checkpoint = torch.load(
get_local_filepath(
groundingdino_model_list[model_name]["model_url"],
groundingdino_model_dir_name,
),
)
dino.load_state_dict(local_groundingdino_clean_state_dict(
checkpoint['model']), strict=False)
device = comfy.model_management.get_torch_device()
dino.to(device=device)
dino.eval()
return dino
def list_groundingdino_model():
return list(groundingdino_model_list.keys())
def groundingdino_predict(
dino_model,
image,
prompt,
threshold
):
def load_dino_image(image_pil):
transform = T.Compose(
[
T.RandomResize([800], max_size=1333),
T.ToTensor(),
T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),
]
)
image, _ = transform(image_pil, None) # 3, h, w
return image
def get_grounding_output(model, image, caption, box_threshold):
caption = caption.lower()
caption = caption.strip()
if not caption.endswith("."):
caption = caption + "."
device = comfy.model_management.get_torch_device()
image = image.to(device)
with torch.no_grad():
outputs = model(image[None], captions=[caption])
logits = outputs["pred_logits"].sigmoid()[0] # (nq, 256)
boxes = outputs["pred_boxes"][0] # (nq, 4)
# filter output
logits_filt = logits.clone()
boxes_filt = boxes.clone()
filt_mask = logits_filt.max(dim=1)[0] > box_threshold
logits_filt = logits_filt[filt_mask] # num_filt, 256
boxes_filt = boxes_filt[filt_mask] # num_filt, 4
return boxes_filt.cpu()
dino_image = load_dino_image(image.convert("RGB"))
boxes_filt = get_grounding_output(
dino_model, dino_image, prompt, threshold
)
H, W = image.size[1], image.size[0]
for i in range(boxes_filt.size(0)):
boxes_filt[i] = boxes_filt[i] * torch.Tensor([W, H, W, H])
boxes_filt[i][:2] -= boxes_filt[i][2:] / 2
boxes_filt[i][2:] += boxes_filt[i][:2]
return boxes_filt
def create_pil_output(image_np, masks, boxes_filt):
output_masks, output_images = [], []
boxes_filt = boxes_filt.numpy().astype(int) if boxes_filt is not None else None
for mask in masks:
output_masks.append(Image.fromarray(np.any(mask, axis=0)))
image_np_copy = copy.deepcopy(image_np)
image_np_copy[~np.any(mask, axis=0)] = np.array([0, 0, 0, 0])
output_images.append(Image.fromarray(image_np_copy))
return output_images, output_masks
def create_tensor_output(image_np, masks, boxes_filt):
output_masks, output_images = [], []
boxes_filt = boxes_filt.numpy().astype(int) if boxes_filt is not None else None
for mask in masks:
image_np_copy = copy.deepcopy(image_np)
image_np_copy[~np.any(mask, axis=0)] = np.array([0, 0, 0, 0])
output_image, output_mask = split_image_mask(
Image.fromarray(image_np_copy))
output_masks.append(output_mask)
output_images.append(output_image)
return (output_images, output_masks)
def split_image_mask(image):
image_rgb = image.convert("RGB")
image_rgb = np.array(image_rgb).astype(np.float32) / 255.0
image_rgb = torch.from_numpy(image_rgb)[None,]
if 'A' in image.getbands():
mask = np.array(image.getchannel('A')).astype(np.float32) / 255.0
mask = torch.from_numpy(mask)[None,]
else:
mask = torch.zeros((64, 64), dtype=torch.float32, device="cpu")
return (image_rgb, mask)
def sam_segment(
sam_model,
image,
boxes
):
if boxes.shape[0] == 0:
return None
sam_is_hq = False
# TODO: more elegant
if hasattr(sam_model, 'model_name') and 'hq' in sam_model.model_name:
sam_is_hq = True
predictor = SamPredictorHQ(sam_model, sam_is_hq)
image_np = np.array(image)
image_np_rgb = image_np[..., :3]
predictor.set_image(image_np_rgb)
transformed_boxes = predictor.transform.apply_boxes_torch(
boxes, image_np.shape[:2])
sam_device = comfy.model_management.get_torch_device()
masks, _, _ = predictor.predict_torch(
point_coords=None,
point_labels=None,
boxes=transformed_boxes.to(sam_device),
multimask_output=False)
masks = masks.permute(1, 0, 2, 3).cpu().numpy()
return create_tensor_output(image_np, masks, boxes)
class SAMModelLoader:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_name": (list_sam_model(), ),
}
}
CATEGORY = "segment_anything"
FUNCTION = "main"
RETURN_TYPES = ("SAM_MODEL", )
def main(self, model_name):
sam_model = load_sam_model(model_name)
return (sam_model, )
class GroundingDinoModelLoader:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_name": (list_groundingdino_model(), ),
}
}
CATEGORY = "segment_anything"
FUNCTION = "main"
RETURN_TYPES = ("GROUNDING_DINO_MODEL", )
def main(self, model_name):
dino_model = load_groundingdino_model(model_name)
return (dino_model, )
class GroundingDinoSAMSegment:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"sam_model": ('SAM_MODEL', {}),
"grounding_dino_model": ('GROUNDING_DINO_MODEL', {}),
"image": ('IMAGE', {}),
"prompt": ("STRING", {}),
"threshold": ("FLOAT", {
"default": 0.3,
"min": 0,
"max": 1.0,
"step": 0.01
}),
}
}
CATEGORY = "segment_anything"
FUNCTION = "main"
RETURN_TYPES = ("IMAGE", "MASK")
def main(self, grounding_dino_model, sam_model, image, prompt, threshold):
res_images = []
res_masks = []
for item in image:
item = Image.fromarray(
np.clip(255. * item.cpu().numpy(), 0, 255).astype(np.uint8)).convert('RGBA')
boxes = groundingdino_predict(
grounding_dino_model,
item,
prompt,
threshold
)
if boxes.shape[0] == 0:
break
(images, masks) = sam_segment(
sam_model,
item,
boxes
)
res_images.extend(images)
res_masks.extend(masks)
if len(res_images) == 0:
_, height, width, _ = image.size()
empty_mask = torch.zeros((1, height, width), dtype=torch.uint8, device="cpu")
return (empty_mask, empty_mask)
return (torch.cat(res_images, dim=0), torch.cat(res_masks, dim=0))
class InvertMask:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"mask": ("MASK",),
}
}
CATEGORY = "segment_anything"
FUNCTION = "main"
RETURN_TYPES = ("MASK",)
def main(self, mask):
out = 1.0 - mask
return (out,)
class IsMaskEmptyNode:
@classmethod
def INPUT_TYPES(s):
return {
"required": {
"mask": ("MASK",),
},
}
RETURN_TYPES = ["NUMBER"]
RETURN_NAMES = ["boolean_number"]
FUNCTION = "main"
CATEGORY = "segment_anything"
def main(self, mask):
return (torch.all(mask == 0).int().item(), )
def tensor2pil(image: torch.Tensor) -> Image.Image:
return Image.fromarray(np.clip(255. * image.cpu().numpy().squeeze(), 0, 255).astype(np.uint8))
def pil2tensor(image: Image.Image) -> torch.Tensor:
return torch.from_numpy(np.array(image).astype(np.float32) / 255.0).unsqueeze(0)
class VITMatteModel:
def __init__(self,model,processor):
self.model = model
self.processor = processor
class VITMatteTransformersModelLoader:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"model_name": ("STRING",{"default":"hustvl/vitmatte-small-composition-1k"}),
}
}
RETURN_TYPES = ("VIT_MATTE_MODEL",)
FUNCTION = "load_model"
CATEGORY = "segment_anything"
def load_model(self, model_name):
model = VitMatteForImageMatting.from_pretrained(model_name)
processor = VitMatteImageProcessor.from_pretrained(model_name)
vitmatte = VITMatteModel(
model,
processor,
)
return (vitmatte,)
class GenerateVITMatte:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"image": ("IMAGE", {}),
"trimap": ("TRIMAP", {}),
"vit_matte_model": ("VIT_MATTE_MODEL", {}),
}
}
RETURN_TYPES = ("IMAGE","MASK")
FUNCTION = "generate_matte"
CATEGORY = "Matte Anything"
def generate_matte(self, image, trimap, vit_matte_model):
image = tensor2pil(image)
trimap = tensor2pil(trimap).convert("L")
# prepare image + trimap for the model
inputs = vit_matte_model.processor(images=image, trimaps=trimap, return_tensors="pt")
with torch.no_grad():
predictions = vit_matte_model.model(**inputs).alphas
mask = tensor2pil(predictions).convert('L')
mask = mask.crop((0,0,image.width,image.height)) # remove padding that the prediction appends (works in 32px tiles)
image.putalpha(mask)
image = pil2tensor(image)
mask = pil2tensor(mask)
return (image,mask,)
class MaskToTrimap:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"mask": ("MASK", {}),
"erode_kernel_size": ("INT", {"default":10,"min": 1, "step": 1}),
"dilate_kernel_size": ("INT", {"default":10,"min": 1, "step": 1}),
}
}
RETURN_TYPES = ("TRIMAP",)
FUNCTION = "get_trimap"
CATEGORY = "segment_anything"
def get_trimap(self, mask: torch.Tensor, erode_kernel_size: int, dilate_kernel_size: int):
mask = mask.squeeze(0).cpu().detach().numpy().astype(np.uint8)*255
trimap = self.generate_trimap(mask, erode_kernel_size, dilate_kernel_size).astype(np.float32)
trimap[trimap==128] = 0.5
trimap[trimap==255] = 1
trimap = torch.from_numpy(trimap).unsqueeze(0)
return (trimap,)
def generate_trimap(self,mask, erode_kernel_size=10, dilate_kernel_size=10):
erode_kernel = np.ones((erode_kernel_size, erode_kernel_size), np.uint8)
dilate_kernel = np.ones((dilate_kernel_size, dilate_kernel_size), np.uint8)
eroded = cv2.erode(mask, erode_kernel, iterations=5)
dilated = cv2.dilate(mask, dilate_kernel, iterations=5)
trimap = np.zeros_like(mask)
trimap[dilated==255] = 128
trimap[eroded==255] = 255
return trimap
class TrimapToMask:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"trimap": ("TRIMAP", {}),
}
}
RETURN_TYPES = ("MASK",)
FUNCTION = "to_mask"
CATEGORY = "segment_anything"
def to_mask(self, trimap: torch.Tensor):
return (trimap,)
class MaskToBoundingBox:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"mask": ("MASK", {}),
}
}
CATEGORY = "mask"
FUNCTION = "main"
RETURN_TYPES = ("BOUNDING_BOX",)
def main(self, mask):
mask_np = np.clip(255. * mask.cpu().numpy().squeeze(), 0, 255).astype(np.uint8)
axis = np.where(mask_np != 0)
rmin = np.min(axis[0])
rmax = np.max(axis[0])
cmin = np.min(axis[1])
cmax = np.max(axis[1])
return (torch.FloatTensor([cmin,rmin,cmax,rmax]),)
class BoundingBox:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"left": ("INT", {"default":0,"step":1}),
"top": ("INT", {"default":0,"step":1}),
"right": ("INT", {"default":0,"step":1}),
"bottom": ("INT", {"default":0,"step":1}),
}
}
CATEGORY = "mask"
FUNCTION = "util"
RETURN_TYPES = ("BOUNDING_BOX",)
def main(self, left,top,right,bottom,):
return (torch.FloatTensor([left,top,right,bottom]),)
class BoundingBoxSAMSegment:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"sam_model": ('SAM_MODEL', {}),
"image": ('IMAGE', {}),
"bounding_box": ("BOUNDING_BOX", {}),
}
}
CATEGORY = "segment_anything"
FUNCTION = "main"
RETURN_TYPES = ("IMAGE", "MASK",)
def main(self, sam_model, image, bounding_box):
res_images = []
res_masks = []
for item in image:
item = Image.fromarray(
np.clip(255. * item.cpu().numpy(), 0, 255).astype(np.uint8)).convert('RGBA')
(images, masks) = sam_segment(
sam_model,
item,
bounding_box
)
res_images.extend(images)
res_masks.extend(masks)
if len(res_images) == 0:
_, height, width, _ = image.size()
empty_mask = torch.zeros((1, height, width), dtype=torch.uint8, device="cpu")
return (empty_mask, empty_mask)
return (torch.cat(res_images, dim=0), torch.cat(res_masks, dim=0))
class GroundingDinoBoundingBoxes:
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"grounding_dino_model": ('GROUNDING_DINO_MODEL', {}),
"image": ('IMAGE', {}),
"prompt": ("STRING", {}),
"threshold": ("FLOAT", {
"default": 0.3,
"min": 0,
"max": 1.0,
"step": 0.01
}),
}
}
CATEGORY = "segment_anything"
FUNCTION = "main"
MULTIPE_OUTPUTS=True
RETURN_TYPES = ("IMAGE", "MASK")
RETURN_NAMES = ("IMAGE", "MASK")
def main(self, grounding_dino_model, image, prompt, threshold):
res_images = []
res_masks = []
for item in image:
item = Image.fromarray(
np.clip(255. * item.cpu().numpy(), 0, 255).astype(np.uint8)).convert('RGBA')
boxes = groundingdino_predict(
grounding_dino_model,
item,
prompt,
threshold
)
if boxes.shape[0] == 0:
break
# (images, masks) = sam_segment(
# sam_model,
# item,
# boxes
# )
for box in boxes:
shape = ((box[0],box[1]),(box[2],box[3]))
mask = Image.new("RGB",item.size,"#000000")
drawer = ImageDraw.Draw(mask)
drawer.rectangle(shape, fill ="#ffffff")
res_images.append(pil2tensor(mask))
res_masks.append(pil2tensor(mask))
# res_images.extend(boxes)
# res_masks.extend(boxes)
print(res_images)
if len(res_images) == 0:
_, height, width, _ = image.size()
empty_mask = torch.zeros((1, height, width), dtype=torch.uint8, device="cpu")
return (empty_mask, empty_mask)
return (torch.cat(res_images), torch.cat(res_masks))