-
Notifications
You must be signed in to change notification settings - Fork 127
/
Copy pathdemo.py
179 lines (137 loc) · 7.11 KB
/
demo.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
# --------------------------------------------------------
# Semantic-SAM: Segment and Recognize Anything at Any Granularity
# Copyright (c) 2023 Microsoft
# Licensed under The MIT License [see LICENSE for details]
# Written by Hao Zhang (hzhangcx@connect.ust.hk)
# --------------------------------------------------------
import gradio as gr
import torch
import argparse
# from gradio import processing_utils
from semantic_sam.BaseModel import BaseModel
from semantic_sam import build_model
from utils.dist import init_distributed_mode
from utils.arguments import load_opt_from_config_file
from utils.constants import COCO_PANOPTIC_CLASSES
from tasks import interactive_infer_image_idino_m2m
def parse_option():
parser = argparse.ArgumentParser('SemanticSAM Demo', add_help=False)
parser.add_argument('--conf_files', default="configs/semantic_sam_only_sa-1b_swinL.yaml", metavar="FILE", help='path to config file', )
parser.add_argument('--ckpt', default="", metavar="FILE", help='path to ckpt', )
args = parser.parse_args()
return args
'''
build args
'''
args = parse_option()
cur_model = 'None'
'''
build model
'''
model=None
model_size=None
ckpt=None
cfgs={'T':"configs/semantic_sam_only_sa-1b_swinT.yaml",
'L':"configs/semantic_sam_only_sa-1b_swinL.yaml"}
# audio = whisper.load_model("base")
sam_cfg=cfgs['L']
opt = load_opt_from_config_file(sam_cfg)
model_sam = BaseModel(opt, build_model(opt)).from_pretrained(args.ckpt).eval().cuda()
@torch.no_grad()
def inference(image,text,text_part,text_thresh,*args, **kwargs):
text_size, hole_scale, island_scale=640,100,100
with torch.autocast(device_type='cuda', dtype=torch.float16):
semantic=False
model=model_sam
a,b= interactive_infer_image_idino_m2m(model, image,text,text_part,text_thresh,text_size,hole_scale,island_scale,semantic, *args, **kwargs)
return a,b
class ImageMask(gr.components.Image):
"""
Sets: source="canvas", tool="sketch"
"""
is_template = True
def __init__(self, **kwargs):
super().__init__(source="upload", tool="sketch", interactive=True, **kwargs)
def preprocess(self, x):
return super().preprocess(x)
'''
launch app
'''
title = "SEMANTIC-SAM: SEGMENT AND RECOGNIZE ANYTHING AT ANY GRANULARITY"
article = "The Demo is Run on SEMANTIC SAM."
from detectron2.data import MetadataCatalog
from utils.constants import COCO_PANOPTIC_CLASSES
from detectron2.data.datasets.builtin_meta import COCO_CATEGORIES
metadata = MetadataCatalog.get('coco_2017_train_panoptic')
all_classes = [name.replace('-other','').replace('-merged','') for name in COCO_PANOPTIC_CLASSES]
all_parts=['arm', 'beak', 'body', 'cap', 'door', 'ear', 'eye', 'foot', 'hair', 'hand', 'handlebar', 'head', 'headlight', 'horn', 'leg', 'license plate', 'mirror', 'mouth', 'muzzle', 'neck', 'nose', 'paw', 'plant', 'pot', 'saddle', 'tail', 'torso', 'wheel', 'window', 'wing']
demo = gr.Blocks()
image=ImageMask(label="Click on Image (Please only click one point, or our model will take the center of all points as the clicked location. Remember to clear the click after each interaction, or we will take the center of the current click and previous ones as the clicked location.)",type="pil",brush_radius=15.0).style(height=512)
gallery_output=gr.Gallery(label="Image Gallery sorted by IoU score.",min_width=1536).style(grid=6)
gallery_output2=gr.Gallery(label="Image Gallery sorted by mask area.",min_width=1536).style(grid=6)
text=gr.components.Textbox(label="Categories. (The default is the categories in COCO panoptic segmentation.)",value=":".join(all_classes),visible=False)
text_part=gr.components.Textbox(label="Part Categories. (The default is the categories in PASCAL Part.)",value=":".join(all_parts),visible=False)
text_res=gr.components.Textbox(label="\"class:part(score)\" of all predictions (seperated by ;): ",visible=True)
text_thresh=gr.components.Textbox(label="The threshold to filter masks with low iou score.",value="0.5",visible=True)
text_size=gr.components.Textbox(label="image size (shortest edge)",value="640",visible=True)
hole_scale=gr.components.Textbox(label="holes scale",value="100",visible=True)
island_scale=gr.components.Textbox(label="island scale",value="100",visible=True)
text_model_size=gr.components.Textbox(label="model size (L or T)",value="L",visible=True)
text_ckpt=gr.components.Textbox(label="ckpt path (relative to /mnt/output/)",value="fengli/joint_part_idino/train_interactive_all_m2m_swinL_bs16_0.1part9_nohash_bs1_resume_all_local_0.15_onlysa_swinL_4node_mnode/model_0099999.pth",visible=True)
text_ckpt_now=gr.components.Textbox(label="current ckpt path (relative to /mnt/output/)",value="",visible=True)
semantic=gr.Checkbox(label="Semantics", info="Do you use semantic? (The semantic model in the demo is trained on SA-1B, COCO and PASCAL Part.)")
title='''
# Semantic-SAM: Segment and Recognize Anything at Any Granularity
# [[Read our arXiv Paper](https://arxiv.org/pdf/2307.04767.pdf)\] \[[Github page](https://github.com/UX-Decoder/Semantic-SAM)\]
# Please only click one point, or our model will take the center of all points as the clicked location. Remember to clear the click after each interaction, or we will take the center of the current click and previous ones as the clicked location.
'''
def change_vocab(choice):
if choice:
return gr.update(visible=True)
else:
return gr.update(visible=False)
with demo:
with gr.Row():
with gr.Column(scale=9.0):
generation_tittle = gr.Markdown(title)
# generation_tittle.render()
with gr.Row(scale=20.0):
image.render()
example = gr.Examples(
examples=[
["examples/tank.png"],
["examples/castle.png"],
["examples/fries1.png"],
["examples/4.png"],
["examples/5.png"],
["examples/corgi2.jpg"],
["examples/minecraft2.png"],
["examples/ref_cat.jpeg"],
["examples/img.png"],
],
inputs=image,
cache_examples=False,
)
with gr.Row(scale=1.0):
with gr.Column():
text_thresh.render()
with gr.Row(scale=2.0):
clearBtn = gr.ClearButton(
components=[image])
runBtn = gr.Button("Run")
with gr.Row(scale=6.0):
text.render()
with gr.Row(scale=1.0):
text_part.render()
gallery_tittle = gr.Markdown("# The masks sorted by IoU scores (masks with low score may have low quality).")
with gr.Row(scale=9.0):
gallery_output.render()
gallery_tittle1 = gr.Markdown("# The masks sorted by mask areas.")
with gr.Row(scale=9.0):
gallery_output2.render()
title = title,
article = article,
allow_flagging = 'never',
runBtn.click(inference, inputs=[image, text, text_part,text_thresh],
outputs = [gallery_output,gallery_output2])
demo.queue().launch(share=True,server_port=6082)