Spaces:
Sleeping
Sleeping
refactor code
Browse files- app.py +3 -2
- app_demo1.py +0 -138
app.py
CHANGED
@@ -101,8 +101,9 @@ def setup_gradio_interface(model, device, colors, color_map):
|
|
101 |
run_button = gr.Button(value="Run")
|
102 |
|
103 |
with gr.Column():
|
104 |
-
|
105 |
-
|
|
|
106 |
h_text = gr.HighlightedText(
|
107 |
label="Labels",
|
108 |
combine_adjacent=False,
|
|
|
101 |
run_button = gr.Button(value="Run")
|
102 |
|
103 |
with gr.Column():
|
104 |
+
with gr.Row():
|
105 |
+
overlay_mask = gr.Image(type="numpy", label="Overlay Mask")
|
106 |
+
only_mask = gr.Image(type="numpy", label="Segmentation Mask")
|
107 |
h_text = gr.HighlightedText(
|
108 |
label="Labels",
|
109 |
combine_adjacent=False,
|
app_demo1.py
DELETED
@@ -1,138 +0,0 @@
|
|
1 |
-
# from models.builder import build_model
|
2 |
-
# from visualization import mask2rgb
|
3 |
-
# from segmentation.datasets import PascalVOCDataset
|
4 |
-
|
5 |
-
# import os
|
6 |
-
# from hydra import compose, initialize
|
7 |
-
# from PIL import Image
|
8 |
-
# import matplotlib.pyplot as plt
|
9 |
-
# from torchvision import transforms as T
|
10 |
-
# import torch.nn.functional as F
|
11 |
-
# import numpy as np
|
12 |
-
from operator import itemgetter
|
13 |
-
# import torch
|
14 |
-
# import random
|
15 |
-
# import warnings
|
16 |
-
|
17 |
-
warnings.filterwarnings("ignore")
|
18 |
-
initialize(config_path="configs", version_base=None)
|
19 |
-
|
20 |
-
# from huggingface_hub import Repository
|
21 |
-
|
22 |
-
repo = Repository(
|
23 |
-
local_dir="clip-dinoiser",
|
24 |
-
clone_from="ariG23498/clip-dinoiser",
|
25 |
-
use_auth_token=os.environ.get("token")
|
26 |
-
)
|
27 |
-
|
28 |
-
check_path = 'clip-dinoiser/checkpoints/last.pt'
|
29 |
-
device = "cuda" if torch.cuda.is_available() else "cpu"
|
30 |
-
|
31 |
-
check = torch.load(check_path, map_location=device)
|
32 |
-
dinoclip_cfg = "clip_dinoiser.yaml"
|
33 |
-
cfg = compose(config_name=dinoclip_cfg)
|
34 |
-
|
35 |
-
model = build_model(cfg.model, class_names=PascalVOCDataset.CLASSES).to(device)
|
36 |
-
model.clip_backbone.decode_head.use_templates=False # switching off the imagenet templates for fast inference
|
37 |
-
model.load_state_dict(check['model_state_dict'], strict=False)
|
38 |
-
model = model.eval()
|
39 |
-
|
40 |
-
# import gradio as gr
|
41 |
-
|
42 |
-
colors = [
|
43 |
-
(0, 255, 0),
|
44 |
-
(0, 0, 255),
|
45 |
-
(255, 255, 0),
|
46 |
-
(255, 0, 255),
|
47 |
-
(0, 255, 255),
|
48 |
-
(114, 128, 250),
|
49 |
-
(0, 165, 255),
|
50 |
-
(0, 128, 0),
|
51 |
-
(144, 238, 144),
|
52 |
-
(238, 238, 175),
|
53 |
-
(255, 191, 0),
|
54 |
-
(0, 128, 0),
|
55 |
-
(226, 43, 138),
|
56 |
-
(255, 0, 255),
|
57 |
-
(0, 215, 255),
|
58 |
-
(255, 0, 0),
|
59 |
-
]
|
60 |
-
|
61 |
-
color_map = {
|
62 |
-
f"{color_id}": f"#{hex(color[0])[2:].zfill(2)}{hex(color[1])[2:].zfill(2)}{hex(color[2])[2:].zfill(2)}" for color_id, color in enumerate(colors)
|
63 |
-
}
|
64 |
-
|
65 |
-
def run_clip_dinoiser(input_image, text_prompts):
|
66 |
-
image = input_image.convert("RGB")
|
67 |
-
text_prompts = text_prompts.split(",")
|
68 |
-
palette = colors[:len(text_prompts)]
|
69 |
-
|
70 |
-
model.clip_backbone.decode_head.update_vocab(text_prompts)
|
71 |
-
model.to(device)
|
72 |
-
model.apply_found = True
|
73 |
-
|
74 |
-
img_tens = T.PILToTensor()(image).unsqueeze(0).to(device) / 255.
|
75 |
-
|
76 |
-
h, w = img_tens.shape[-2:]
|
77 |
-
output = model(img_tens).cpu()
|
78 |
-
output = F.interpolate(output, scale_factor=model.clip_backbone.backbone.patch_size, mode="bilinear",
|
79 |
-
align_corners=False)[..., :h, :w]
|
80 |
-
output = output[0].argmax(dim=0)
|
81 |
-
mask = mask2rgb(output, palette)
|
82 |
-
|
83 |
-
classes = np.unique(output).tolist()
|
84 |
-
palette_array = np.array(itemgetter(*classes)(palette)).reshape(1, -1, 3)
|
85 |
-
alpha=0.5
|
86 |
-
blend = (alpha)*np.array(image)/255. + (1-alpha) * mask/255.
|
87 |
-
|
88 |
-
h_text = list()
|
89 |
-
for idx, text in enumerate(text_prompts):
|
90 |
-
h_text.append((text, f"{idx}"))
|
91 |
-
return blend, mask, h_text
|
92 |
-
|
93 |
-
|
94 |
-
|
95 |
-
|
96 |
-
if __name__ == "__main__":
|
97 |
-
|
98 |
-
block = gr.Blocks().queue()
|
99 |
-
with block:
|
100 |
-
gr.Markdown("<h1><center>CLIP-DINOiser<h1><center>")
|
101 |
-
|
102 |
-
with gr.Row():
|
103 |
-
with gr.Column():
|
104 |
-
input_image = gr.Image(type="pil", label="Input Image")
|
105 |
-
text_prompts = gr.Textbox(label="Enter comma-separated prompts")
|
106 |
-
run_button = gr.Button(value="Run")
|
107 |
-
|
108 |
-
with gr.Column():
|
109 |
-
with gr.Row():
|
110 |
-
overlay_mask = gr.Image(
|
111 |
-
type="numpy",
|
112 |
-
label="Overlay Mask",
|
113 |
-
)
|
114 |
-
only_mask = gr.Image(
|
115 |
-
type="numpy",
|
116 |
-
label="Segmentation Mask"
|
117 |
-
)
|
118 |
-
h_text = gr.HighlightedText(
|
119 |
-
label="Labels",
|
120 |
-
combine_adjacent=False,
|
121 |
-
show_legend=False,
|
122 |
-
color_map=color_map
|
123 |
-
)
|
124 |
-
|
125 |
-
run_button.click(
|
126 |
-
fn=run_clip_dinoiser,
|
127 |
-
inputs=[input_image, text_prompts,],
|
128 |
-
outputs=[overlay_mask, only_mask, h_text]
|
129 |
-
)
|
130 |
-
gr.Examples(
|
131 |
-
[["vintage_bike.jpeg", "background, vintage bike, leather bag"]],
|
132 |
-
inputs = [input_image, text_prompts,],
|
133 |
-
outputs = [overlay_mask, only_mask, h_text],
|
134 |
-
fn=run_clip_dinoiser,
|
135 |
-
cache_examples=True,
|
136 |
-
label='Try this example input!'
|
137 |
-
)
|
138 |
-
block.launch(share=False, show_api=False, show_error=True)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|