leonelhs commited on
Commit
7842e83
·
1 Parent(s): c4a72e5

make more gradionic

Browse files
Files changed (3) hide show
  1. README.md +19 -3
  2. app.py +72 -62
  3. segformer-b5-finetuned-ade-640-640.onnx +0 -3
README.md CHANGED
@@ -1,17 +1,33 @@
1
  ---
2
- title: SegFormer (ADE20k) in TensorFlow
3
  emoji: 🏃
4
  colorFrom: indigo
5
  colorTo: gray
6
  sdk: gradio
7
- sdk_version: 5.47.1
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
 
11
  ---
12
 
 
 
 
 
 
13
  This space hosts the [SegFormer model](https://arxiv.org/abs/2105.15203) in TensorFlow. This model was fine-tuned on the [ADE20k dataset](http://groups.csail.mit.edu/vision/datasets/ADE20K/). To know more about the checkpoint used in this space, refer to the model card
14
  [here](https://huggingface.co/nvidia/segformer-b5-finetuned-ade-640-640).
15
 
16
  Please note that since the model was fine-tuned on the ADE20k dataset, the model is expected to provide best results for images
17
- belonging to scene categories. For an overview of the dataset, refer to its [homepage](http://groups.csail.mit.edu/vision/datasets/ADE20K/).
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: SegFormer (ADE20k) in ONNX
3
  emoji: 🏃
4
  colorFrom: indigo
5
  colorTo: gray
6
  sdk: gradio
7
+ sdk_version: 5.49.0
8
  app_file: app.py
9
  pinned: false
10
  license: apache-2.0
11
+ short_description: Image segmentation fine-tuned on the ADE20k dataset
12
  ---
13
 
14
+ ## SegFormer (ADE20k) in ONNX
15
+
16
+ This is demo TensorFlow SegFormer from 🤗 `transformers` official package. The pre-trained model was trained to segment scene specific images. We are **currently using ONNX model converted from the TensorFlow based SegFormer to improve the latency**. The average latency of an inference is **21** and **8** seconds for TensorFlow and ONNX converted models respectively (in [Colab](https://github.com/deep-diver/segformer-tf-transformers/blob/main/notebooks/TFSegFormer_ONNX.ipynb)). Check out the [repository](https://github.com/deep-diver/segformer-tf-transformers) to find out how to make inference, finetune the model with custom dataset, and further information.
17
+
18
+
19
  This space hosts the [SegFormer model](https://arxiv.org/abs/2105.15203) in TensorFlow. This model was fine-tuned on the [ADE20k dataset](http://groups.csail.mit.edu/vision/datasets/ADE20K/). To know more about the checkpoint used in this space, refer to the model card
20
  [here](https://huggingface.co/nvidia/segformer-b5-finetuned-ade-640-640).
21
 
22
  Please note that since the model was fine-tuned on the ADE20k dataset, the model is expected to provide best results for images
23
+ belonging to scene categories. For an overview of the dataset, refer to its [homepage](http://groups.csail.mit.edu/vision/datasets/ADE20K/).
24
+
25
+ ## Acknowledgments
26
+ This work integrates code and concepts from several repositories.
27
+ For proper attribution, please refer to the following sources (or notify us if any are missing):
28
+ - [Original space](https://huggingface.co/spaces/chansung/segformer-tf-transformers)
29
+ -
30
+ ## Contact
31
+ For questions, comments, or feedback, please contact:
32
+ 📧 **leonelhs@gmail.com**
33
+
app.py CHANGED
@@ -1,13 +1,46 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  import csv
2
  import os
3
  import sys
 
4
 
5
  import cv2
6
- import gradio as gr
7
- import matplotlib.pyplot as plt
8
  import numpy as np
9
  import onnxruntime as ort
10
- from matplotlib import gridspec
 
 
11
 
12
  ade_palette = []
13
  labels_list = []
@@ -23,14 +56,16 @@ with open(r"ade_palette.txt", "r") as fp:
23
  tmp_list = list(map(int, line[:-1].strip("][").split(", ")))
24
  ade_palette.append(tmp_list)
25
 
26
- colormap = np.asarray(ade_palette)
 
 
 
 
 
27
 
28
- model_filename = "segformer-b5-finetuned-ade-640-640.onnx"
29
  sess_options = ort.SessionOptions()
30
  sess_options.intra_op_num_threads = os.cpu_count()
31
- sess = ort.InferenceSession(
32
- model_filename, sess_options, providers=["CPUExecutionProvider"]
33
- )
34
 
35
 
36
  def label_to_color_image(label):
@@ -42,31 +77,8 @@ def label_to_color_image(label):
42
 
43
  return colormap[label]
44
 
 
45
 
46
- def draw_plot(pred_img, seg):
47
- fig = plt.figure(figsize=(20, 15))
48
-
49
- grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
50
-
51
- plt.subplot(grid_spec[0])
52
- plt.imshow(pred_img)
53
- plt.axis("off")
54
-
55
- LABEL_NAMES = np.asarray(labels_list)
56
- FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
57
- FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
58
-
59
- unique_labels = np.unique(seg)
60
- ax = plt.subplot(grid_spec[1])
61
- plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
62
- ax.yaxis.tick_right()
63
- plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
64
- plt.xticks([], [])
65
- ax.tick_params(width=0.0, labelsize=25)
66
- return fig
67
-
68
-
69
- def sepia(input_img):
70
  img = cv2.imread(input_img)
71
  img = cv2.resize(img, (640, 640)).astype(np.float32)
72
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
@@ -76,42 +88,40 @@ def sepia(input_img):
76
  logits = sess.run(None, {"pixel_values": img_batch})[0]
77
 
78
  logits = np.transpose(logits, (0, 2, 3, 1))
79
- seg = np.argmax(logits, axis=-1)[0].astype("float32")
80
- seg = cv2.resize(seg, (640, 640)).astype("uint8")
81
-
82
- color_seg = np.zeros(
83
- (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
84
- ) # height, width, 3
85
-
86
- for label, color in enumerate(colormap):
87
- color_seg[seg == label, :] = color
88
 
89
- # Convert to BGR
90
- color_seg = color_seg[..., ::-1]
91
 
92
- # Show image + mask
93
- pred_img = img * 0.5 + color_seg * 0.5
94
- pred_img = pred_img.astype(np.uint8)
95
 
96
- fig = draw_plot(pred_img, seg)
97
- return fig
 
 
 
 
98
 
 
99
 
100
- title = "SegFormer(ADE20k) in TensorFlow"
101
- description = """
 
 
 
 
 
 
 
102
 
103
- This is demo TensorFlow SegFormer from 🤗 `transformers` official package. The pre-trained model was trained to segment scene specific images. We are **currently using ONNX model converted from the TensorFlow based SegFormer to improve the latency**. The average latency of an inference is **21** and **8** seconds for TensorFlow and ONNX converted models respectively (in [Colab](https://github.com/deep-diver/segformer-tf-transformers/blob/main/notebooks/TFSegFormer_ONNX.ipynb)). Check out the [repository](https://github.com/deep-diver/segformer-tf-transformers) to find out how to make inference, finetune the model with custom dataset, and further information.
104
 
105
- """
106
 
107
- demo = gr.Interface(
108
- sepia,
109
- gr.Image(type="filepath"),
110
- outputs=["plot"],
111
- examples=["ADE_val_00000001.jpeg"],
112
- allow_flagging="never",
113
- title=title,
114
- description=description,
115
- )
116
 
117
- demo.launch()
 
 
1
+ #######################################################################################
2
+ #
3
+ # MIT License
4
+ #
5
+ # Copyright (c) [2025] [leonelhs@gmail.com]
6
+ #
7
+ # Permission is hereby granted, free of charge, to any person obtaining a copy
8
+ # of this software and associated documentation files (the "Software"), to deal
9
+ # in the Software without restriction, including without limitation the rights
10
+ # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11
+ # copies of the Software, and to permit persons to whom the Software is
12
+ # furnished to do so, subject to the following conditions:
13
+ #
14
+ # The above copyright notice and this permission notice shall be included in all
15
+ # copies or substantial portions of the Software.
16
+ #
17
+ # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
+ # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
+ # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
+ # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
+ # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22
+ # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23
+ # SOFTWARE.
24
+ #
25
+ #######################################################################################
26
+ #
27
+ # This project is one of several repositories exploring image segmentation techniques.
28
+ # All related projects and interactive demos can be found at:
29
+ # https://huggingface.co/spaces/leonelhs/removators
30
+ # huggingface: https://huggingface.co/spaces/leonelhs/segformer-tf-transformers
31
+ #
32
+
33
  import csv
34
  import os
35
  import sys
36
+ from itertools import islice
37
 
38
  import cv2
 
 
39
  import numpy as np
40
  import onnxruntime as ort
41
+ import gradio as gr
42
+ from PIL import Image
43
+ from huggingface_hub import hf_hub_download
44
 
45
  ade_palette = []
46
  labels_list = []
 
56
  tmp_list = list(map(int, line[:-1].strip("][").split(", ")))
57
  ade_palette.append(tmp_list)
58
 
59
+ colormap = np.asarray(ade_palette)
60
+
61
+
62
+ REPO_ID = "leonelhs/segmentators"
63
+ model_path = hf_hub_download(repo_id=REPO_ID, filename="segformer/segformer-b5-finetuned-ade-640-640.onnx")
64
+
65
 
 
66
  sess_options = ort.SessionOptions()
67
  sess_options.intra_op_num_threads = os.cpu_count()
68
+ sess = ort.InferenceSession(model_path, sess_options, providers=["CPUExecutionProvider"])
 
 
69
 
70
 
71
  def label_to_color_image(label):
 
77
 
78
  return colormap[label]
79
 
80
+ def predict(input_img):
81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
82
  img = cv2.imread(input_img)
83
  img = cv2.resize(img, (640, 640)).astype(np.float32)
84
  img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
 
88
  logits = sess.run(None, {"pixel_values": img_batch})[0]
89
 
90
  logits = np.transpose(logits, (0, 2, 3, 1))
91
+ segmented_mask = np.argmax(logits, axis=-1)[0].astype("float32")
92
+ segmented_mask = cv2.resize(segmented_mask, (640, 640)).astype("uint8")
 
 
 
 
 
 
 
93
 
 
 
94
 
95
+ parts = []
96
+ unique_labels = np.unique(segmented_mask)
97
+ label_names = np.asarray(labels_list)
98
 
99
+ for label in unique_labels:
100
+ part = np.where(segmented_mask == label)
101
+ color_seg = np.full((640, 640, 3), 0, dtype=np.uint8)
102
+ color_seg[part[0], part[1], :] = colormap[label]
103
+ color_seg = cv2.cvtColor(color_seg, cv2.COLOR_BGR2GRAY)
104
+ parts.append((color_seg, label_names[label]))
105
 
106
+ return Image.fromarray(img.astype("uint8")), parts
107
 
108
+ with gr.Blocks(title="SegFormer") as app:
109
+ navbar = gr.Navbar(visible=True, main_page_name="Workspace")
110
+ gr.Markdown("## SegFormer(ADE20k) ONNX")
111
+ with gr.Row():
112
+ with gr.Column(scale=1):
113
+ inp = gr.Image(type="filepath", label="Upload Image")
114
+ btn_predict = gr.Button("Parse")
115
+ with gr.Column(scale=2):
116
+ out = gr.AnnotatedImage(label="Image parsed annotated")
117
 
118
+ btn_predict.click(predict, inputs=[inp], outputs=[out])
119
 
 
120
 
121
+ with app.route("About this", "/about"):
122
+ with open("README.md") as f:
123
+ for line in islice(f, 12, None):
124
+ gr.Markdown(line.strip())
 
 
 
 
 
125
 
126
+ app.launch(share=False, debug=True, show_error=True, mcp_server=True, pwa=True)
127
+ app.queue()
segformer-b5-finetuned-ade-640-640.onnx DELETED
@@ -1,3 +0,0 @@
1
- version https://git-lfs.github.com/spec/v1
2
- oid sha256:f27cde5816910ae5a654fed8be58e7ad0c361244b5494df18ecf37d35d7ea33c
3
- size 341726174