File size: 17,749 Bytes
55249e4
 
 
 
eae9bb9
f773839
 
73550bc
f773839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55249e4
 
 
 
 
 
b4925d2
f773839
 
 
 
 
 
 
 
 
 
 
 
73550bc
 
 
 
 
 
 
 
 
06aa340
73550bc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2ba4ec8
 
db98a16
f773839
 
0221ae2
f773839
 
 
 
 
 
41d006c
 
 
 
 
 
 
 
 
 
f773839
 
 
2ba4ec8
 
db98a16
55249e4
2ba4ec8
281ac0f
 
 
73550bc
f773839
281ac0f
0221ae2
f773839
 
 
 
55249e4
 
 
 
b4925d2
55249e4
 
 
 
 
 
 
 
 
f773839
 
 
 
 
 
 
c570f20
 
 
7fa46c5
3dd984b
 
c570f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
55249e4
 
 
 
b4925d2
55249e4
 
 
 
 
 
 
 
 
c570f20
 
 
 
73550bc
 
 
 
 
 
 
 
06aa340
 
 
73550bc
06aa340
 
 
 
 
 
 
 
 
 
 
73550bc
 
 
c570f20
 
06aa340
c570f20
 
 
 
 
 
 
 
 
 
 
06aa340
 
 
c570f20
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e53c1e6
3dd984b
 
 
 
 
f773839
 
 
 
 
e8b794f
 
f773839
 
 
 
2ba4ec8
35a87cf
f773839
 
 
2ba4ec8
 
35a87cf
f773839
 
 
79162b8
 
 
 
73550bc
 
55249e4
73550bc
 
 
 
 
 
 
f773839
0221ae2
f773839
0221ae2
f773839
 
 
 
 
 
 
 
 
 
2ed7fa7
 
b9b9e70
 
2ed7fa7
b9b9e70
a0f5007
2ed7fa7
 
f773839
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
06aa340
 
f773839
 
 
 
 
 
 
 
 
 
 
55249e4
f773839
 
 
 
 
 
 
 
c570f20
 
 
 
 
55249e4
 
c570f20
55249e4
 
 
06aa340
c570f20
 
 
 
 
 
3dd984b
55249e4
c570f20
 
55249e4
 
c570f20
 
 
 
 
55249e4
c570f20
 
 
55249e4
c570f20
06aa340
 
 
55249e4
06aa340
 
 
 
55249e4
06aa340
c570f20
06aa340
 
 
79162b8
 
fe8ff41
79162b8
 
 
06aa340
c570f20
f773839
73550bc
 
 
 
55249e4
 
73550bc
55249e4
 
 
73550bc
 
 
 
 
 
55249e4
73550bc
 
 
 
 
 
 
 
 
 
55249e4
73550bc
06aa340
 
 
55249e4
06aa340
 
 
 
55249e4
06aa340
79162b8
 
 
 
fe8ff41
79162b8
 
 
 
f773839
 
 
 
 
 
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
## Some code was modified from Ovseg and OV-Sam.Thanks to their excellent work.
## Ovseg Code:https://github.com/facebookresearch/ov-seg
## OV-Sam Code:https://github.com/HarborYuan/ovsam

import spaces
import multiprocessing as mp
import numpy as np
from PIL import Image,ImageDraw
import torch
try:
    import detectron2
except:
    import os
    os.system('pip install git+https://github.com/facebookresearch/detectron2.git')

from detectron2.config import get_cfg
from detectron2.projects.deeplab import add_deeplab_config
from detectron2.data.detection_utils import read_image
from mask_adapter import add_maskformer2_config, add_fcclip_config, add_mask_adapter_config
from mask_adapter.sam_maskadapter import SAMVisualizationDemo, SAMPointVisualizationDemo
import gradio as gr
import open_clip
from sam2.build_sam import build_sam2
from mask_adapter.modeling.meta_arch.mask_adapter_head import build_mask_adapter
from mask_adapter.data.datasets import openseg_classes

COCO_CATEGORIES_pan = openseg_classes.get_coco_categories_with_prompt_eng()
stuff_classes = [k["name"] for k in COCO_CATEGORIES_pan]
ADE20K_150_CATEGORIES_ = openseg_classes.get_ade20k_categories_with_prompt_eng()
ade20k_stuff_classes = [k["name"] for k in ADE20K_150_CATEGORIES_]
class_names_coco_ade20k =  stuff_classes + ade20k_stuff_classes


def setup_cfg(config_file):
    cfg = get_cfg()
    add_deeplab_config(cfg)
    add_maskformer2_config(cfg)
    add_fcclip_config(cfg)
    add_mask_adapter_config(cfg)
    cfg.merge_from_file(config_file)
    cfg.freeze()
    return cfg

class IMGState:
    def __init__(self):
        self.img = None
        self.selected_points = []
        self.selected_points_labels = []
        self.selected_bboxes = []

        self.available_to_set = True

    def set_img(self, img):
        self.img = img

        self.available_to_set = False

    def clear(self):
        self.img = None
        self.selected_points = []
        self.selected_points_labels = []
        self.selected_bboxes = []

        self.available_to_set = True

    def clean(self):
        self.selected_points = []
        self.selected_points_labels = []
        self.selected_bboxes = []


    @property
    def available(self):
        return self.available_to_set
    
    
@spaces.GPU
@torch.no_grad()
@torch.autocast(device_type="cuda", dtype=torch.float32)
def inference_automatic(input_img, class_names):
    mp.set_start_method("spawn", force=True)
    config_file = './configs/ground-truth-warmup/mask-adapter/mask_adapter_convnext_large_cocopan_eval_ade20k.yaml'
    cfg = setup_cfg(config_file)
    
    demo = SAMVisualizationDemo(cfg, 0.8, sam2_model, clip_model,mask_adapter)
    
    class_names = class_names.split(',')
    img = read_image(input_img, format="BGR")
    if len(class_names) == 1:
        class_names.append('others')
    txts = [f'a photo of {cls_name}' for cls_name in class_names]
    text = open_clip.tokenize(txts)


    text_features = clip_model.encode_text(text.cuda())
    text_features /= text_features.norm(dim=-1, keepdim=True)
        
    _, visualized_output = demo.run_on_image(img, class_names,text_features)

    return Image.fromarray(np.uint8(visualized_output.get_image())).convert('RGB')

@spaces.GPU
@torch.no_grad()
@torch.autocast(device_type="cuda", dtype=torch.float32)
def inference_point(input_img, img_state,class_names_input):


    mp.set_start_method("spawn", force=True)
    
    points = img_state.selected_points
    print(f"Selected point: {points}")
    
    config_file = './configs/ground-truth-warmup/mask-adapter/mask_adapter_convnext_large_cocopan_eval_ade20k.yaml'
    cfg = setup_cfg(config_file)
    
    demo = SAMPointVisualizationDemo(cfg, 0.8, sam2_model, clip_model,mask_adapter)
    
    if not class_names_input:
        class_names_input = class_names_coco_ade20k
        
    if class_names_input == class_names_coco_ade20k:
        text_features = torch.from_numpy(np.load("./text_embedding/coco_ade20k_text_embedding_new.npy")).cuda()
        _, visualized_output = demo.run_on_image_with_points(img_state.img, points,text_features)
    else:
        class_names_input = class_names_input.split(',')
        txts = [f'a photo of {cls_name}' for cls_name in class_names_input]
        text = open_clip.tokenize(txts)
        text_features = clip_model.encode_text(text.cuda())
        text_features /= text_features.norm(dim=-1, keepdim=True)
        _, visualized_output = demo.run_on_image_with_points(img_state.img, points,text_features,class_names_input)
        
    return visualized_output


sam2_model = None
clip_model = None
mask_adapter = None

@spaces.GPU
@torch.no_grad()
@torch.autocast(device_type="cuda", dtype=torch.float32)
def inference_box(input_img,  img_state,class_names_input):
    # if len(img_state.selected_bboxes) != 2:
    #     return None
    mp.set_start_method("spawn", force=True)
    
    box_points = img_state.selected_bboxes
    bbox = (
        min(box_points[0][0], box_points[1][0]),
        min(box_points[0][1], box_points[1][1]),
        max(box_points[0][0], box_points[1][0]),
        max(box_points[0][1], box_points[1][1]),
    )
    bbox = np.array(bbox)
    config_file = './configs/ground-truth-warmup/mask-adapter/mask_adapter_convnext_large_cocopan_eval_ade20k.yaml'
    cfg = setup_cfg(config_file)
    
    demo = SAMPointVisualizationDemo(cfg, 0.8, sam2_model, clip_model,mask_adapter)
    
    if not class_names_input:
        class_names_input = class_names_coco_ade20k
        
    if class_names_input == class_names_coco_ade20k:
        text_features = torch.from_numpy(np.load("./text_embedding/coco_ade20k_text_embedding_new.npy")).cuda()
        _, visualized_output = demo.run_on_image_with_boxes(img_state.img, bbox,text_features)
    else:
        class_names_input = class_names_input.split(',')
        txts = [f'a photo of {cls_name}' for cls_name in class_names_input]
        text = open_clip.tokenize(txts)
        text_features = clip_model.encode_text(text.cuda())
        text_features /= text_features.norm(dim=-1, keepdim=True)
        _, visualized_output = demo.run_on_image_with_boxes(img_state.img, bbox,text_features,class_names_input)
        
    return visualized_output



def get_points_with_draw(image, img_state, evt: gr.SelectData):
    label = 'Add Mask'

    x, y = evt.index[0], evt.index[1]
    point_radius, point_color = 10, (97, 217, 54) if label == "Add Mask" else (237, 34, 13)

    img_state.selected_points.append([x, y])
    img_state.selected_points_labels.append(1 if label == "Add Mask" else 0)
    if img_state.img is None:
        img_state.set_img(np.array(image))
        
    draw = ImageDraw.Draw(image)
    draw.polygon(
        [
            (x, y - point_radius),
            (x + point_radius * 0.25, y - point_radius * 0.25),
            (x + point_radius, y),
            (x + point_radius * 0.25, y + point_radius * 0.25),
            (x, y + point_radius),
            (x - point_radius * 0.25, y + point_radius * 0.25),
            (x - point_radius, y),
            (x - point_radius * 0.25, y - point_radius * 0.25)
        ],
        fill=point_color,
    )
    return img_state, image

def get_bbox_with_draw(image, img_state, evt: gr.SelectData):
    
    x, y = evt.index[0], evt.index[1]
    point_radius, point_color, box_outline = 5, (237, 34, 13), 2
    box_color = (237, 34, 13)

    if len(img_state.selected_bboxes) in [0, 1]:
        img_state.selected_bboxes.append([x, y])
    elif len(img_state.selected_bboxes) == 2:
        img_state.selected_bboxes = [[x, y]]
        image = Image.fromarray(img_state.img)
    else:
        raise ValueError(f"Cannot be {len(img_state.selected_bboxes)}")
    
    if img_state.img is None:
        img_state.set_img(np.array(image))

    draw = ImageDraw.Draw(image)
    draw.ellipse(
        [(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)],
        fill=point_color,
    )

    if len(img_state.selected_bboxes) == 2:
        box_points = img_state.selected_bboxes
        bbox = (min(box_points[0][0], box_points[1][0]),
                min(box_points[0][1], box_points[1][1]),
                max(box_points[0][0], box_points[1][0]),
                max(box_points[0][1], box_points[1][1]),
                )
        draw.rectangle(
            bbox,
            outline=box_color,
            width=box_outline
        )
    return img_state, image

def check_and_infer_box(input_image, img_state_bbox,class_names_input_box):
    if len(img_state_bbox.selected_bboxes) == 2:
        return inference_box(input_image, img_state_bbox, class_names_input_box)
    return None


def initialize_models(sam_path, adapter_pth, model_cfg, cfg):
    cfg = setup_cfg(cfg)
    global sam2_model, clip_model, mask_adapter

    if sam2_model is None:
        sam2_model = build_sam2(model_cfg, sam_path, device="cpu", apply_postprocessing=False)
        sam2_model = sam2_model.to("cuda")
        print("SAM2 model initialized.")
    
    if clip_model is None:
        clip_model, _, _ = open_clip.create_model_and_transforms("convnext_large_d_320", pretrained="laion2b_s29b_b131k_ft_soup")
        clip_model = clip_model.eval()
        clip_model = clip_model.to("cuda")
        print("CLIP model initialized.")
    
    if mask_adapter is None:
        mask_adapter = build_mask_adapter(cfg, "MASKAdapterHead").to("cuda")
        mask_adapter = mask_adapter.eval()
        adapter_state_dict = torch.load(adapter_pth)
        mask_adapter.load_state_dict(adapter_state_dict)
        print("Mask Adapter model initialized.")

def preprocess_example(input_img, img_state):
    img_state.clear()
    return img_state,None

def clear_everything(img_state):
    img_state.clear()
    return img_state, None, None, gr.Textbox(value='',lines=1, placeholder=class_names_coco_ade20k, label='Class Names')


def clean_prompts(img_state):
    img_state.clean()
    return img_state, Image.fromarray(img_state.img), None

# 初始化配置和模型
model_cfg = "configs/sam2.1/sam2.1_hiera_l.yaml"
sam_path = './sam2.1_hiera_large.pt'
adapter_pth = './model_0279999_with_sem_new.pth'
cfg = './configs/ground-truth-warmup/mask-adapter/mask_adapter_convnext_large_cocopan_eval_ade20k.yaml'

initialize_models(sam_path, adapter_pth, model_cfg, cfg)

# Examples for testing
examples = [
    ['./demo/images/000000001025.jpg', 'dog, beach, trees, sea, sky, snow, person, rocks, buildings, birds, beach umbrella, beach chair'],
    ['./demo/images/ADE_val_00000979.jpg', 'sky,sea,mountain,pier,beach,island,,landscape,horizon'],
    ['./demo/images/ADE_val_00001200.jpg', 'bridge, mountains, trees, water, sky, buildings, boats, animals, flowers, waterfalls, grasslands, rocks'],
]

examples_point = [
    ['./demo/images/ADE_val_00000739.jpg'],
    ['./demo/images/000000052462.jpg'],
    ['./demo/images/000000081766.jpg'],
    ['./demo/images/ADE_val_00000001.jpg'],
    ['./demo/images/000000033707.jpg'],
    ['./demo/images/ADE_val_00000572.jpg']
]

output_labels = ['segmentation map']

title = '<center><h2>Mask-Adapter + Segment Anything-2</h2></center>'

description = """
<b>Mask-Adapter: The Devil is in the Masks for Open-Vocabulary Segmentation</b><br>
Mask-Adapter effectively extends to SAM or SAM-2 without additional training, achieving impressive results across multiple open-vocabulary segmentation benchmarks.<br>
<div style="display: flex; gap: 20px;">
    <a href="https://arxiv.org/abs/2406.20076">
        <img src="https://img.shields.io/badge/arXiv-Paper-red" alt="arXiv Paper">
    </a>
    <a href="https://github.com/hustvl/MaskAdapter">
        <img src="https://img.shields.io/badge/GitHub-Code-blue" alt="GitHub Code">
    </a>
</div>
"""

with gr.Blocks() as demo:
    gr.Markdown(title)  
    gr.Markdown(description) 

    with gr.Tabs():
        with gr.TabItem("Automatic Mode"):
            with gr.Row():
                with gr.Column():
                    input_image = gr.Image(type='filepath', label="Input Image")
                    class_names = gr.Textbox(lines=1, placeholder=None, label='Class Names')
                with gr.Column():
                    output_image = gr.Image(type="pil", label='Segmentation Map')

                    # Buttons below segmentation map (now placed under segmentation map)
                    run_button = gr.Button("Run Automatic Segmentation", elem_id="run_button",variant='primary')
                    run_button.click(inference_automatic, inputs=[input_image, class_names], outputs=output_image)
                    
                    clear_button = gr.Button("Clear")
                    clear_button.click(lambda: None, inputs=None, outputs=output_image)
            
            with gr.Row():
                gr.Examples(examples=examples, inputs=[input_image, class_names], outputs=output_image)

        with gr.TabItem("Box Mode"):
            img_state_bbox = gr.State(value=IMGState())
            with gr.Row():  # 水平排列
                with gr.Column(scale=1): 
                    input_image = gr.Image( label="Input Image", type="pil") 
                    class_names_input_box = gr.Textbox(lines=1, placeholder=class_names_coco_ade20k, label='Class Names')
                with gr.Column(scale=1): 
                    output_image_box = gr.Image(type="pil", label='Segmentation Map',interactive=False)  # 输出分割图
                    clear_prompt_button_box = gr.Button("Clean Prompt")
                    clear_button_box = gr.Button("Restart")
                    
            gr.Markdown("Click the top-left and bottom-right corners of the image to select a rectangular area") 

            input_image.select(
                get_bbox_with_draw,
                [input_image, img_state_bbox],
                outputs=[img_state_bbox, input_image]
            ).then(
                check_and_infer_box,
                inputs=[input_image, img_state_bbox,class_names_input_box],
                outputs=[output_image_box]
            )
            
            
            clear_prompt_button_box.click(
                clean_prompts,
                inputs=[img_state_bbox],
                outputs=[img_state_bbox, input_image, output_image_box]
            )
            
            clear_button_box.click(
                clear_everything,
                inputs=[img_state_bbox],
                outputs=[img_state_bbox, input_image, output_image_box,class_names_input_box]
            )
            input_image.clear(
                clear_everything,
                inputs=[img_state_bbox],
                outputs=[img_state_bbox, input_image, output_image_box,class_names_input_box]
            )
            output_image_box.clear(
                clear_everything,
                inputs=[img_state_bbox],
                outputs=[img_state_bbox, input_image, output_image_box,class_names_input_box]
            )

            
            gr.Examples(
                    examples=examples_point, 
                    inputs=[input_image, img_state_bbox], 
                    outputs=[img_state_bbox, output_image_box], 
                    examples_per_page=6,
                    fn=preprocess_example,
                    run_on_click=True,
                    cache_examples=False,
                )
                
        with gr.TabItem("Point Mode"):
            img_state_points = gr.State(value=IMGState())
            with gr.Row():  # 水平排列
                with gr.Column(scale=1): 
                    input_image = gr.Image( label="Input Image", type="pil") 
                    class_names_input_point = gr.Textbox(lines=1, placeholder=class_names_coco_ade20k, label='Class Names')
                with gr.Column(scale=1):  
                    output_image_point = gr.Image(type="pil", label='Segmentation Map',interactive=False)  # 输出分割图
                    clear_prompt_button_point = gr.Button("Clean Prompt")
                    clear_button_point = gr.Button("Restart")
                    
            input_image.select(
                get_points_with_draw,
                [input_image, img_state_points],
                outputs=[img_state_points, input_image]
            ).then(
                inference_point,
                inputs=[input_image, img_state_points,class_names_input_point],
                outputs=[output_image_point]
            )
            clear_prompt_button_point.click(
                clean_prompts,
                inputs=[img_state_points],
                outputs=[img_state_points, input_image, output_image_point]
            )
            clear_button_point.click(
                clear_everything,
                inputs=[img_state_points],
                outputs=[img_state_points, input_image, output_image_point,class_names_input_point]
            )
            input_image.clear(
                clear_everything,
                inputs=[img_state_points],
                outputs=[img_state_points, input_image, output_image_point,class_names_input_point]
            )
            output_image_point.clear(
                clear_everything,
                inputs=[img_state_points],
                outputs=[img_state_points, input_image, output_image_point,class_names_input_point]
            )
            gr.Examples(
                    examples=examples_point, 
                    inputs=[input_image, img_state_points], 
                    outputs=[img_state_points, output_image_point], 
                    examples_per_page=6,
                    fn=preprocess_example,
                    run_on_click=True,
                    cache_examples=False,
                )


            
    # Example images below buttons

    demo.launch()