srikanthp07 commited on
Commit
1528be0
1 Parent(s): c793c0d

Upload 5 files

Browse files
Files changed (5) hide show
  1. README.md +41 -8
  2. app_gradio.py +374 -0
  3. gitattributes.txt +36 -0
  4. gitignore.txt +6 -0
  5. requirements.txt +20 -0
README.md CHANGED
@@ -1,13 +1,46 @@
1
  ---
2
- title: S19erav1
3
- emoji: 📊
4
- colorFrom: green
5
- colorTo: red
6
  sdk: gradio
7
- sdk_version: 3.46.1
8
- app_file: app.py
9
  pinned: false
10
- license: mit
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: FastSAM
3
+ emoji: 🐠
4
+ colorFrom: pink
5
+ colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 3.45.1
8
+ app_file: app_gradio.py
9
  pinned: false
10
+ license: apache-2.0
11
  ---
12
 
13
+ # Fast Segment Anything
14
+
15
+ Official PyTorch Implementation of the <a href="https://github.com/CASIA-IVA-Lab/FastSAM">.
16
+
17
+ The **Fast Segment Anything Model(FastSAM)** is a CNN Segment Anything Model trained by only 2% of the SA-1B dataset published by SAM authors. The FastSAM achieve a comparable performance with
18
+ the SAM method at **50× higher run-time speed**.
19
+
20
+
21
+ ## License
22
+
23
+ The model is licensed under the [Apache 2.0 license](LICENSE).
24
+
25
+
26
+ ## Acknowledgement
27
+
28
+ - [Segment Anything](https://segment-anything.com/) provides the SA-1B dataset and the base codes.
29
+ - [YOLOv8](https://github.com/ultralytics/ultralytics) provides codes and pre-trained models.
30
+ - [YOLACT](https://arxiv.org/abs/2112.10003) provides powerful instance segmentation method.
31
+ - [Grounded-Segment-Anything](https://huggingface.co/spaces/yizhangliu/Grounded-Segment-Anything) provides a useful web demo template.
32
+
33
+ ## Citing FastSAM
34
+
35
+ If you find this project useful for your research, please consider citing the following BibTeX entry.
36
+
37
+ ```
38
+ @misc{zhao2023fast,
39
+ title={Fast Segment Anything},
40
+ author={Xu Zhao and Wenchao Ding and Yongqi An and Yinglong Du and Tao Yu and Min Li and Ming Tang and Jinqiao Wang},
41
+ year={2023},
42
+ eprint={2306.12156},
43
+ archivePrefix={arXiv},
44
+ primaryClass={cs.CV}
45
+ }
46
+ ```
app_gradio.py ADDED
@@ -0,0 +1,374 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from ultralytics import YOLO
2
+ import gradio as gr
3
+ import torch
4
+ from utils.tools_gradio import fast_process
5
+ from utils.tools import format_results, box_prompt, point_prompt, text_prompt
6
+ from PIL import ImageDraw
7
+ import numpy as np
8
+
9
+ # Load the pre-trained model
10
+ model = YOLO('./weights/FastSAM.pt')
11
+
12
+ device = torch.device(
13
+ "cuda"
14
+ if torch.cuda.is_available()
15
+ else "mps"
16
+ if torch.backends.mps.is_available()
17
+ else "cpu"
18
+ )
19
+
20
+ # Description
21
+ title = "<center><strong><font size='8'>🏃 Fast Segment Anything 🤗</font></strong></center>"
22
+
23
+ news = """ # 📖 News
24
+ 🔥 2023/07/14: Add a "wider result" button in text mode (Thanks for [gaoxinge](https://github.com/CASIA-IVA-Lab/FastSAM/pull/95)).
25
+
26
+ 🔥 2023/06/29: Support the text mode (Thanks for [gaoxinge](https://github.com/CASIA-IVA-Lab/FastSAM/pull/47)).
27
+
28
+ 🔥 2023/06/26: Support the points mode. (Better and faster interaction will come soon!)
29
+
30
+ 🔥 2023/06/24: Add the 'Advanced options" in Everything mode to get a more detailed adjustment.
31
+ """
32
+
33
+ description_e = """This is a demo on Github project 🏃 [Fast Segment Anything Model](https://github.com/CASIA-IVA-Lab/FastSAM). Welcome to give a star ⭐️ to it.
34
+
35
+ 🎯 Upload an Image, segment it with Fast Segment Anything (Everything mode). The other modes will come soon.
36
+
37
+ ⌛️ It takes about 6~ seconds to generate segment results. The concurrency_count of queue is 1, please wait for a moment when it is crowded.
38
+
39
+ 🚀 To get faster results, you can use a smaller input size and leave high_visual_quality unchecked.
40
+
41
+ 📣 You can also obtain the segmentation results of any Image through this Colab: [![Open in Colab](https://colab.research.google.com/assets/colab-badge.svg)](https://colab.research.google.com/drive/1oX14f6IneGGw612WgVlAiy91UHwFAvr9?usp=sharing)
42
+
43
+ 😚 A huge thanks goes out to the @HuggingFace Team for supporting us with GPU grant.
44
+
45
+ 🏠 Check out our [Model Card 🏃](https://huggingface.co/An-619/FastSAM)
46
+
47
+ """
48
+
49
+ description_p = """ # 🎯 Instructions for points mode
50
+ This is a demo on Github project 🏃 [Fast Segment Anything Model](https://github.com/CASIA-IVA-Lab/FastSAM). Welcome to give a star ⭐️ to it.
51
+
52
+ 1. Upload an image or choose an example.
53
+
54
+ 2. Choose the point label ('Add mask' means a positive point. 'Remove' Area means a negative point that is not segmented).
55
+
56
+ 3. Add points one by one on the image.
57
+
58
+ 4. Click the 'Segment with points prompt' button to get the segmentation results.
59
+
60
+ **5. If you get Error, click the 'Clear points' button and try again may help.**
61
+
62
+ """
63
+
64
+ examples = [["examples/sa_8776.jpg"], ["examples/sa_414.jpg"], ["examples/sa_1309.jpg"], ["examples/sa_11025.jpg"],
65
+ ["examples/sa_561.jpg"], ["examples/sa_192.jpg"], ["examples/sa_10039.jpg"], ["examples/sa_862.jpg"]]
66
+
67
+ default_example = examples[0]
68
+
69
+ css = "h1 { text-align: center } .about { text-align: justify; padding-left: 10%; padding-right: 10%; }"
70
+
71
+
72
+ def segment_everything(
73
+ input,
74
+ input_size=1024,
75
+ iou_threshold=0.7,
76
+ conf_threshold=0.25,
77
+ better_quality=False,
78
+ withContours=True,
79
+ use_retina=True,
80
+ text="",
81
+ wider=False,
82
+ mask_random_color=True,
83
+ ):
84
+ input_size = int(input_size) # 确保 imgsz 是整数
85
+ # Thanks for the suggestion by hysts in HuggingFace.
86
+ w, h = input.size
87
+ scale = input_size / max(w, h)
88
+ new_w = int(w * scale)
89
+ new_h = int(h * scale)
90
+ input = input.resize((new_w, new_h))
91
+
92
+ results = model(input,
93
+ device=device,
94
+ retina_masks=True,
95
+ iou=iou_threshold,
96
+ conf=conf_threshold,
97
+ imgsz=input_size,)
98
+
99
+ if len(text) > 0:
100
+ results = format_results(results[0], 0)
101
+ annotations, _ = text_prompt(results, text, input, device=device, wider=wider)
102
+ annotations = np.array([annotations])
103
+ else:
104
+ annotations = results[0].masks.data
105
+
106
+ fig = fast_process(annotations=annotations,
107
+ image=input,
108
+ device=device,
109
+ scale=(1024 // input_size),
110
+ better_quality=better_quality,
111
+ mask_random_color=mask_random_color,
112
+ bbox=None,
113
+ use_retina=use_retina,
114
+ withContours=withContours,)
115
+ return fig
116
+
117
+
118
+ def segment_with_points(
119
+ input,
120
+ input_size=1024,
121
+ iou_threshold=0.7,
122
+ conf_threshold=0.25,
123
+ better_quality=False,
124
+ withContours=True,
125
+ use_retina=True,
126
+ mask_random_color=True,
127
+ ):
128
+ global global_points
129
+ global global_point_label
130
+
131
+ input_size = int(input_size) # 确保 imgsz 是整数
132
+ # Thanks for the suggestion by hysts in HuggingFace.
133
+ w, h = input.size
134
+ scale = input_size / max(w, h)
135
+ new_w = int(w * scale)
136
+ new_h = int(h * scale)
137
+ input = input.resize((new_w, new_h))
138
+
139
+ scaled_points = [[int(x * scale) for x in point] for point in global_points]
140
+
141
+ results = model(input,
142
+ device=device,
143
+ retina_masks=True,
144
+ iou=iou_threshold,
145
+ conf=conf_threshold,
146
+ imgsz=input_size,)
147
+
148
+ results = format_results(results[0], 0)
149
+ annotations, _ = point_prompt(results, scaled_points, global_point_label, new_h, new_w)
150
+ annotations = np.array([annotations])
151
+
152
+ fig = fast_process(annotations=annotations,
153
+ image=input,
154
+ device=device,
155
+ scale=(1024 // input_size),
156
+ better_quality=better_quality,
157
+ mask_random_color=mask_random_color,
158
+ bbox=None,
159
+ use_retina=use_retina,
160
+ withContours=withContours,)
161
+
162
+ global_points = []
163
+ global_point_label = []
164
+ return fig, None
165
+
166
+
167
+ def get_points_with_draw(image, label, evt: gr.SelectData):
168
+ global global_points
169
+ global global_point_label
170
+
171
+ x, y = evt.index[0], evt.index[1]
172
+ point_radius, point_color = 15, (255, 255, 0) if label == 'Add Mask' else (255, 0, 255)
173
+ global_points.append([x, y])
174
+ global_point_label.append(1 if label == 'Add Mask' else 0)
175
+
176
+ print(x, y, label == 'Add Mask')
177
+
178
+ # 创建一个可以在图像上绘图的对象
179
+ draw = ImageDraw.Draw(image)
180
+ draw.ellipse([(x - point_radius, y - point_radius), (x + point_radius, y + point_radius)], fill=point_color)
181
+ return image
182
+
183
+
184
+ cond_img_e = gr.Image(label="Input", value=default_example[0], type='pil')
185
+ cond_img_p = gr.Image(label="Input with points", value=default_example[0], type='pil')
186
+ cond_img_t = gr.Image(label="Input with text", value="examples/dogs.jpg", type='pil')
187
+
188
+ segm_img_e = gr.Image(label="Segmented Image", interactive=False, type='pil')
189
+ segm_img_p = gr.Image(label="Segmented Image with points", interactive=False, type='pil')
190
+ segm_img_t = gr.Image(label="Segmented Image with text", interactive=False, type='pil')
191
+
192
+ global_points = []
193
+ global_point_label = []
194
+
195
+ input_size_slider = gr.components.Slider(minimum=512,
196
+ maximum=1024,
197
+ value=1024,
198
+ step=64,
199
+ label='Input_size',
200
+ info='Our model was trained on a size of 1024')
201
+
202
+ with gr.Blocks(css=css, title='Fast Segment Anything') as demo:
203
+ with gr.Row():
204
+ with gr.Column(scale=1):
205
+ # Title
206
+ gr.Markdown(title)
207
+
208
+ with gr.Column(scale=1):
209
+ # News
210
+ gr.Markdown(news)
211
+
212
+ with gr.Tab("Everything mode"):
213
+ # Images
214
+ with gr.Row(variant="panel"):
215
+ with gr.Column(scale=1):
216
+ cond_img_e.render()
217
+
218
+ with gr.Column(scale=1):
219
+ segm_img_e.render()
220
+
221
+ # Submit & Clear
222
+ with gr.Row():
223
+ with gr.Column():
224
+ input_size_slider.render()
225
+
226
+ with gr.Row():
227
+ contour_check = gr.Checkbox(value=True, label='withContours', info='draw the edges of the masks')
228
+
229
+ with gr.Column():
230
+ segment_btn_e = gr.Button("Segment Everything", variant='primary')
231
+ clear_btn_e = gr.Button("Clear", variant="secondary")
232
+
233
+ gr.Markdown("Try some of the examples below ⬇️")
234
+ gr.Examples(examples=examples,
235
+ inputs=[cond_img_e],
236
+ outputs=segm_img_e,
237
+ fn=segment_everything,
238
+ cache_examples=True,
239
+ examples_per_page=4)
240
+
241
+ with gr.Column():
242
+ with gr.Accordion("Advanced options", open=False):
243
+ iou_threshold = gr.Slider(0.1, 0.9, 0.7, step=0.1, label='iou', info='iou threshold for filtering the annotations')
244
+ conf_threshold = gr.Slider(0.1, 0.9, 0.25, step=0.05, label='conf', info='object confidence threshold')
245
+ with gr.Row():
246
+ mor_check = gr.Checkbox(value=False, label='better_visual_quality', info='better quality using morphologyEx')
247
+ with gr.Column():
248
+ retina_check = gr.Checkbox(value=True, label='use_retina', info='draw high-resolution segmentation masks')
249
+
250
+ # Description
251
+ gr.Markdown(description_e)
252
+
253
+ segment_btn_e.click(segment_everything,
254
+ inputs=[
255
+ cond_img_e,
256
+ input_size_slider,
257
+ iou_threshold,
258
+ conf_threshold,
259
+ mor_check,
260
+ contour_check,
261
+ retina_check,
262
+ ],
263
+ outputs=segm_img_e)
264
+
265
+ with gr.Tab("Points mode"):
266
+ # Images
267
+ with gr.Row(variant="panel"):
268
+ with gr.Column(scale=1):
269
+ cond_img_p.render()
270
+
271
+ with gr.Column(scale=1):
272
+ segm_img_p.render()
273
+
274
+ # Submit & Clear
275
+ with gr.Row():
276
+ with gr.Column():
277
+ with gr.Row():
278
+ add_or_remove = gr.Radio(["Add Mask", "Remove Area"], value="Add Mask", label="Point_label (foreground/background)")
279
+
280
+ with gr.Column():
281
+ segment_btn_p = gr.Button("Segment with points prompt", variant='primary')
282
+ clear_btn_p = gr.Button("Clear points", variant='secondary')
283
+
284
+ gr.Markdown("Try some of the examples below ⬇️")
285
+ gr.Examples(examples=examples,
286
+ inputs=[cond_img_p],
287
+ # outputs=segm_img_p,
288
+ # fn=segment_with_points,
289
+ # cache_examples=True,
290
+ examples_per_page=4)
291
+
292
+ with gr.Column():
293
+ # Description
294
+ gr.Markdown(description_p)
295
+
296
+ cond_img_p.select(get_points_with_draw, [cond_img_p, add_or_remove], cond_img_p)
297
+
298
+ segment_btn_p.click(segment_with_points,
299
+ inputs=[cond_img_p],
300
+ outputs=[segm_img_p, cond_img_p])
301
+
302
+ with gr.Tab("Text mode"):
303
+ # Images
304
+ with gr.Row(variant="panel"):
305
+ with gr.Column(scale=1):
306
+ cond_img_t.render()
307
+
308
+ with gr.Column(scale=1):
309
+ segm_img_t.render()
310
+
311
+ # Submit & Clear
312
+ with gr.Row():
313
+ with gr.Column():
314
+ input_size_slider_t = gr.components.Slider(minimum=512,
315
+ maximum=1024,
316
+ value=1024,
317
+ step=64,
318
+ label='Input_size',
319
+ info='Our model was trained on a size of 1024')
320
+ with gr.Row():
321
+ with gr.Column():
322
+ contour_check = gr.Checkbox(value=True, label='withContours', info='draw the edges of the masks')
323
+ text_box = gr.Textbox(label="text prompt", value="a black dog")
324
+
325
+ with gr.Column():
326
+ segment_btn_t = gr.Button("Segment with text", variant='primary')
327
+ clear_btn_t = gr.Button("Clear", variant="secondary")
328
+
329
+ gr.Markdown("Try some of the examples below ⬇️")
330
+ gr.Examples(examples=[["examples/dogs.jpg"], ["examples/fruits.jpg"], ["examples/flowers.jpg"]],
331
+ inputs=[cond_img_t],
332
+ # outputs=segm_img_e,
333
+ # fn=segment_everything,
334
+ # cache_examples=True,
335
+ examples_per_page=4)
336
+
337
+ with gr.Column():
338
+ with gr.Accordion("Advanced options", open=False):
339
+ iou_threshold = gr.Slider(0.1, 0.9, 0.7, step=0.1, label='iou', info='iou threshold for filtering the annotations')
340
+ conf_threshold = gr.Slider(0.1, 0.9, 0.25, step=0.05, label='conf', info='object confidence threshold')
341
+ with gr.Row():
342
+ mor_check = gr.Checkbox(value=False, label='better_visual_quality', info='better quality using morphologyEx')
343
+ retina_check = gr.Checkbox(value=True, label='use_retina', info='draw high-resolution segmentation masks')
344
+ wider_check = gr.Checkbox(value=False, label='wider', info='wider result')
345
+
346
+ # Description
347
+ gr.Markdown(description_e)
348
+
349
+ segment_btn_t.click(segment_everything,
350
+ inputs=[
351
+ cond_img_t,
352
+ input_size_slider_t,
353
+ iou_threshold,
354
+ conf_threshold,
355
+ mor_check,
356
+ contour_check,
357
+ retina_check,
358
+ text_box,
359
+ wider_check,
360
+ ],
361
+ outputs=segm_img_t)
362
+
363
+ def clear():
364
+ return None, None
365
+
366
+ def clear_text():
367
+ return None, None, None
368
+
369
+ clear_btn_e.click(clear, outputs=[cond_img_e, segm_img_e])
370
+ clear_btn_p.click(clear, outputs=[cond_img_p, segm_img_p])
371
+ clear_btn_t.click(clear_text, outputs=[cond_img_p, segm_img_p, text_box])
372
+
373
+ demo.queue()
374
+ demo.launch()
gitattributes.txt ADDED
@@ -0,0 +1,36 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ *.7z filter=lfs diff=lfs merge=lfs -text
2
+ *.arrow filter=lfs diff=lfs merge=lfs -text
3
+ *.bin filter=lfs diff=lfs merge=lfs -text
4
+ *.bz2 filter=lfs diff=lfs merge=lfs -text
5
+ *.ckpt filter=lfs diff=lfs merge=lfs -text
6
+ *.ftz filter=lfs diff=lfs merge=lfs -text
7
+ *.gz filter=lfs diff=lfs merge=lfs -text
8
+ *.h5 filter=lfs diff=lfs merge=lfs -text
9
+ *.joblib filter=lfs diff=lfs merge=lfs -text
10
+ *.lfs.* filter=lfs diff=lfs merge=lfs -text
11
+ *.mlmodel filter=lfs diff=lfs merge=lfs -text
12
+ *.model filter=lfs diff=lfs merge=lfs -text
13
+ *.msgpack filter=lfs diff=lfs merge=lfs -text
14
+ *.npy filter=lfs diff=lfs merge=lfs -text
15
+ *.npz filter=lfs diff=lfs merge=lfs -text
16
+ *.onnx filter=lfs diff=lfs merge=lfs -text
17
+ *.ot filter=lfs diff=lfs merge=lfs -text
18
+ *.parquet filter=lfs diff=lfs merge=lfs -text
19
+ *.pb filter=lfs diff=lfs merge=lfs -text
20
+ *.pickle filter=lfs diff=lfs merge=lfs -text
21
+ *.pkl filter=lfs diff=lfs merge=lfs -text
22
+ *.pt filter=lfs diff=lfs merge=lfs -text
23
+ *.pth filter=lfs diff=lfs merge=lfs -text
24
+ *.rar filter=lfs diff=lfs merge=lfs -text
25
+ *.safetensors filter=lfs diff=lfs merge=lfs -text
26
+ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
27
+ *.tar.* filter=lfs diff=lfs merge=lfs -text
28
+ *.tar filter=lfs diff=lfs merge=lfs -text
29
+ *.tflite filter=lfs diff=lfs merge=lfs -text
30
+ *.tgz filter=lfs diff=lfs merge=lfs -text
31
+ *.wasm filter=lfs diff=lfs merge=lfs -text
32
+ *.xz filter=lfs diff=lfs merge=lfs -text
33
+ *.zip filter=lfs diff=lfs merge=lfs -text
34
+ *.zst filter=lfs diff=lfs merge=lfs -text
35
+ *tfevents* filter=lfs diff=lfs merge=lfs -text
36
+ *.jpg filter=lfs diff=lfs merge=lfs -text
gitignore.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ *.pyc
2
+ *.pyo
3
+ *.pyd
4
+ .DS_Store
5
+ .idea
6
+ gradio_cached_examples
requirements.txt ADDED
@@ -0,0 +1,20 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ # Base-----------------------------------
2
+ matplotlib==3.2.2
3
+ numpy
4
+ opencv-python
5
+
6
+ git+https://github.com/openai/CLIP.git
7
+ # Pillow>=7.1.2
8
+ # PyYAML>=5.3.1
9
+ # requests>=2.23.0
10
+ # scipy>=1.4.1
11
+ # torch
12
+ # torchvision
13
+ # tqdm>=4.64.0
14
+
15
+ # pandas>=1.1.4
16
+ # seaborn>=0.11.0
17
+
18
+ # Ultralytics-----------------------------------
19
+ ultralytics==8.0.121
20
+