File size: 3,407 Bytes
2c50deb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import os
import torch
import numpy as np

import gradio as gr

from segment_anything import build_sam, SamAutomaticMaskGenerator

os.system(r'python -m wget https://dl.fbaipublicfiles.com/segment_anything/sam_vit_h_4b8939.pth')

hourglass_args = {
    "baseline": {},
    "1.2x faster": {
        "use_hourglass": True,
        "hourglass_clustering_location": 14,
        "hourglass_num_cluster": 100,
    },
    "1.5x faster": {
        "use_hourglass": True,
        "hourglass_clustering_location": 6,
        "hourglass_num_cluster": 81,
    },
}

def predict(image, speed_mode):
    mask_generator = SamAutomaticMaskGenerator(build_sam(checkpoint="sam_vit_h_4b8939.pth", hourglass_kwargs=hourglass_args[speed_mode]))
    masks = mask_generator.generate(image)

    if len(masks) == 0:
        return image
    sorted_masks = sorted(masks, key=(lambda x: x['area']), reverse=True)
    img = np.ones(image.shape)
    for mask in sorted_masks:
        m = mask['segmentation']
        color_mask = np.random.random((1, 1, 3))
        img = img * (1 - m[..., None]) + color_mask * m[..., None]

    image = ((image + img * 255) / 2).astype(np.uint8)
    return image

description = """
#  <center>Expedit-SAM (Expedite Segment Anything Model without any training)</center>
Github link: [Link](https://github.com/Expedit-LargeScale-Vision-Transformer/Expedit-SAM)
You can select the speed mode you want to use from the "Speed Mode" dropdown menu and click "Run" to segment the image you uploaded to the "Input Image" box.
"""
if (SPACE_ID := os.getenv('SPACE_ID')) is not None:
    description += f'\n<p>For faster inference without waiting in queue, you may duplicate the space and upgrade to GPU in settings. <a href="https://huggingface.co/spaces/{SPACE_ID}?duplicate=true"><img style="display: inline; margin-top: 0em; margin-bottom: 0em" src="https://bit.ly/3gLdBN6" alt="Duplicate Space" /></a></p>'


def main():
    with gr.Blocks() as demo:
        gr.Markdown(description)
        with gr.Column():
            with gr.Row():
                with gr.Column():
                    input_image = gr.Image(label="Input Image")
                    speed_mode = gr.Dropdown(
                        choices=list(hourglass_args.keys()),
                        value="baseline", 
                        label="Speed Mode",
                        multiselect=False,
                    )
                    with gr.Row():
                        run_btn = gr.Button(label="Run", id="run", value="Run")
                        clear_btn = gr.Button(label="Clear", id="clear", value="Clear")
                output_image = gr.Image(label="Output Image")
            gr.Examples(
                examples=[
                    ["./notebooks/images/dog.jpg"],
                    ["notebooks/images/groceries.jpg"],
                    ["notebooks/images/truck.jpg"],
                ],
                inputs=[input_image],
                outputs=[output_image],
                fn=predict,
            )
        
        run_btn.click(
            fn=predict, 
            inputs=[input_image, speed_mode], 
            outputs=output_image
        )
        clear_btn.click(
            fn=lambda: [None, None], 
            inputs=None, 
            outputs=[input_image, output_image], 
            queue=False,
        )

    demo.queue()
    demo.launch()

if __name__ == "__main__":
    main()