Spaces:
Sleeping
Sleeping
chore: adding demo
Browse files- app.py +83 -7
- vintage_bike.jpeg +0 -0
app.py
CHANGED
|
@@ -11,6 +11,7 @@ import torch.nn.functional as F
|
|
| 11 |
import numpy as np
|
| 12 |
from operator import itemgetter
|
| 13 |
import torch
|
|
|
|
| 14 |
import warnings
|
| 15 |
|
| 16 |
warnings.filterwarnings("ignore")
|
|
@@ -19,9 +20,9 @@ initialize(config_path="configs", version_base=None)
|
|
| 19 |
from huggingface_hub import Repository
|
| 20 |
|
| 21 |
repo = Repository(
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
)
|
| 26 |
|
| 27 |
check_path = 'clip-dinoiser/checkpoints/last.pt'
|
|
@@ -38,8 +39,83 @@ model = model.eval()
|
|
| 38 |
|
| 39 |
import gradio as gr
|
| 40 |
|
| 41 |
-
def
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
-
|
| 45 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 11 |
import numpy as np
|
| 12 |
from operator import itemgetter
|
| 13 |
import torch
|
| 14 |
+
import random
|
| 15 |
import warnings
|
| 16 |
|
| 17 |
warnings.filterwarnings("ignore")
|
|
|
|
| 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'
|
|
|
|
| 39 |
|
| 40 |
import gradio as gr
|
| 41 |
|
| 42 |
+
def run_clip_dinoiser(input_image, text_prompts):
|
| 43 |
+
image = input_image.convert("RGB")
|
| 44 |
+
text_prompts = text_prompts.split(",")
|
| 45 |
+
palette = [
|
| 46 |
+
(random.randint(0, 256), random.randint(0, 256), random.randint(0, 256)) for _ in range(len(text_prompts))
|
| 47 |
+
]
|
| 48 |
|
| 49 |
+
model.clip_backbone.decode_head.update_vocab(text_prompts)
|
| 50 |
+
model.to(device)
|
| 51 |
+
model.apply_found = True
|
| 52 |
+
|
| 53 |
+
img_tens = T.PILToTensor()(image).unsqueeze(0).to(device) / 255.
|
| 54 |
+
|
| 55 |
+
h, w = img_tens.shape[-2:]
|
| 56 |
+
output = model(img_tens).cpu()
|
| 57 |
+
output = F.interpolate(output, scale_factor=model.clip_backbone.backbone.patch_size, mode="bilinear",
|
| 58 |
+
align_corners=False)[..., :h, :w]
|
| 59 |
+
output = output[0].argmax(dim=0)
|
| 60 |
+
mask = mask2rgb(output, palette)
|
| 61 |
+
|
| 62 |
+
# fig = plt.figure(figsize=(3, 1))
|
| 63 |
+
# classes = np.unique(output).tolist()
|
| 64 |
+
# plt.imshow(np.array(itemgetter(*classes)(palette)).reshape(1, -1, 3))
|
| 65 |
+
# plt.xticks(np.arange(len(classes)), list(itemgetter(*classes)(text_prompts)), rotation=45)
|
| 66 |
+
# plt.yticks([])
|
| 67 |
+
|
| 68 |
+
|
| 69 |
+
# fig, ax = plt.subplots(nrows=1, ncols=2)
|
| 70 |
+
# alpha=0.5
|
| 71 |
+
# blend = (alpha)*np.array(image)/255. + (1-alpha) * mask/255.
|
| 72 |
+
# ax[0].imshow(blend)
|
| 73 |
+
# ax[1].imshow(mask)
|
| 74 |
+
# ax[0].axis('off')
|
| 75 |
+
# ax[1].axis('off')
|
| 76 |
+
|
| 77 |
+
classes = np.unique(output).tolist()
|
| 78 |
+
palette_array = np.array(itemgetter(*classes)(palette)).reshape(1, -1, 3)
|
| 79 |
+
alpha=0.5
|
| 80 |
+
blend = (alpha)*np.array(image)/255. + (1-alpha) * mask/255.
|
| 81 |
+
return palette_array, blend, mask
|
| 82 |
+
|
| 83 |
+
|
| 84 |
+
if __name__ == "__main__":
|
| 85 |
+
|
| 86 |
+
block = gr.Blocks().queue()
|
| 87 |
+
with block:
|
| 88 |
+
gr.Markdown("<h1><center>CLIP-DINOiser<h1><center>")
|
| 89 |
+
|
| 90 |
+
with gr.Row():
|
| 91 |
+
with gr.Column():
|
| 92 |
+
input_image = gr.Image(source='upload', type="pil")
|
| 93 |
+
text_prompts = gr.Textbox(label="Enter comma-separated prompts")
|
| 94 |
+
run_button = gr.Button(label="Run")
|
| 95 |
+
|
| 96 |
+
with gr.Column():
|
| 97 |
+
palette_array = gr.outputs.Image(
|
| 98 |
+
type="numpy",
|
| 99 |
+
)
|
| 100 |
+
with gr.Row():
|
| 101 |
+
overlay_mask = gr.outputs.Image(
|
| 102 |
+
type="numpy",
|
| 103 |
+
)
|
| 104 |
+
only_mask = gr.outputs.Image(
|
| 105 |
+
type="numpy",
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
run_button.click(
|
| 109 |
+
fn=run_clip_dinoiser,
|
| 110 |
+
inputs=[input_image, text_prompts,],
|
| 111 |
+
outputs=[overlay_mask, only_mask]
|
| 112 |
+
)
|
| 113 |
+
gr.Examples(
|
| 114 |
+
[["vintage_bike.jpeg", "background, vintage bike, leather bag"]],
|
| 115 |
+
inputs = [input_image, text_prompts,],
|
| 116 |
+
outputs = [overlay_mask, only_mask],
|
| 117 |
+
fn=run_clip_dinoiser,
|
| 118 |
+
cache_examples=True,
|
| 119 |
+
label='Try this example input!'
|
| 120 |
+
)
|
| 121 |
+
block.launch(share=False, show_api=False, show_error=True)
|
vintage_bike.jpeg
ADDED
|