kimteawan commited on
Commit
031fde4
1 Parent(s): 2ed8aba

Upload 8 files

Browse files
Files changed (6) hide show
  1. app.py +244 -0
  2. labels.txt +150 -0
  3. requirements.txt +6 -0
  4. sample-1.jpg +0 -0
  5. sample-2.jpg +0 -0
  6. sample-3.png +0 -0
app.py ADDED
@@ -0,0 +1,244 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+
3
+ from matplotlib import gridspec
4
+ import matplotlib.pyplot as plt
5
+ import numpy as np
6
+ from PIL import Image
7
+ import tensorflow as tf
8
+ from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
9
+
10
+ feature_extractor = SegformerFeatureExtractor.from_pretrained(
11
+ # "nvidia/segformer-b5-finetuned-ade-640-640"
12
+ "nvidia/segformer-b0-finetuned-ade-512-512"
13
+ )
14
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
15
+ # "nvidia/segformer-b5-finetuned-ade-640-640"
16
+ "nvidia/segformer-b0-finetuned-ade-512-512"
17
+ )
18
+
19
+ def ade_palette():
20
+ """ADE20K palette that maps each class to RGB values."""
21
+ return [
22
+ [78, 145, 57],
23
+ [200, 78, 112],
24
+ [99, 89, 145],
25
+ [200, 156, 78],
26
+ [57, 78, 145],
27
+ [78, 57, 99],
28
+ [145, 112, 78],
29
+ [78, 89, 145],
30
+ [210, 99, 28],
31
+ [145, 78, 189],
32
+ [57, 200, 136],
33
+ [89, 156, 78],
34
+ [99, 28, 210],
35
+ [189, 78, 47],
36
+ [28, 210, 99],
37
+ [200, 78, 112],
38
+ [210, 99, 28],
39
+ [78, 145, 57],
40
+ [145, 78, 99],
41
+ [78, 57, 99],
42
+ [78, 145, 57],
43
+ [99, 28, 210],
44
+ [99, 89, 145],
45
+ [145, 78, 99],
46
+ [145, 112, 78],
47
+ [78, 89, 145],
48
+ [57, 78, 145],
49
+ [57, 200, 136],
50
+ [57, 78, 145],
51
+ [99, 28, 210],
52
+ [89, 156, 78],
53
+ [57, 78, 145],
54
+ [78, 89, 145],
55
+ [145, 112, 78],
56
+ [200, 156, 78],
57
+ [57, 200, 136],
58
+ [28, 210, 99],
59
+ [78, 57, 99],
60
+ [78, 145, 57],
61
+ [57, 200, 136],
62
+ [57, 78, 145],
63
+ [210, 99, 28],
64
+ [145, 78, 189],
65
+ [200, 78, 112],
66
+ [78, 89, 145],
67
+ [99, 28, 210],
68
+ [189, 78, 47],
69
+ [99, 89, 145],
70
+ [78, 145, 57],
71
+ [200, 156, 78],
72
+ [57, 78, 145],
73
+ [210, 99, 28],
74
+ [145, 78, 189],
75
+ [78, 89, 145],
76
+ [200, 78, 112],
77
+ [57, 78, 145],
78
+ [145, 112, 78],
79
+ [99, 28, 210],
80
+ [57, 200, 136],
81
+ [78, 57, 99],
82
+ [28, 210, 99],
83
+ [189, 78, 47],
84
+ [145, 78, 189],
85
+ [78, 57, 99],
86
+ [99, 28, 210],
87
+ [57, 200, 136],
88
+ [145, 112, 78],
89
+ [78, 89, 145],
90
+ [200, 78, 112],
91
+ [78, 57, 99],
92
+ [99, 28, 210],
93
+ [145, 78, 99],
94
+ [28, 210, 99],
95
+ [145, 112, 78],
96
+ [78, 89, 145],
97
+ [57, 200, 136],
98
+ [57, 78, 145],
99
+ [189, 78, 47],
100
+ [200, 156, 78],
101
+ [99, 28, 210],
102
+ [78, 89, 145],
103
+ [145, 78, 189],
104
+ [57, 78, 145],
105
+ [200, 78, 112],
106
+ [78, 57, 99],
107
+ [99, 89, 145],
108
+ [210, 99, 28],
109
+ [145, 78, 189],
110
+ [28, 210, 99],
111
+ [145, 112, 78],
112
+ [57, 200, 136],
113
+ [78, 57, 99],
114
+ [78, 145, 57],
115
+ [99, 28, 210],
116
+ [200, 156, 78],
117
+ [57, 78, 145],
118
+ [145, 78, 99],
119
+ [78, 89, 145],
120
+ [57, 200, 136],
121
+ [28, 210, 99],
122
+ [99, 89, 145],
123
+ [78, 145, 57],
124
+ [145, 78, 99],
125
+ [200, 78, 112],
126
+ [78, 57, 99],
127
+ [210, 99, 28],
128
+ [57, 78, 145],
129
+ [200, 156, 78],
130
+ [99, 28, 210],
131
+ [189, 78, 47],
132
+ [78, 89, 145],
133
+ [57, 200, 136],
134
+ [145, 112, 78],
135
+ [145, 78, 189],
136
+ [28, 210, 99],
137
+ [99, 89, 145],
138
+ [78, 57, 99],
139
+ [57, 200, 136],
140
+ [210, 99, 28],
141
+ [145, 112, 78],
142
+ [78, 145, 57],
143
+ [78, 89, 145],
144
+ [57, 78, 145],
145
+ [200, 78, 112],
146
+ [189, 78, 47],
147
+ [200, 156, 78],
148
+ [57, 200, 136],
149
+ [99, 89, 145],
150
+ [99, 28, 210],
151
+ [145, 112, 78],
152
+ [145, 78, 99],
153
+ [57, 78, 145],
154
+ [28, 210, 99],
155
+ [78, 57, 99],
156
+ [78, 145, 57],
157
+ [57, 200, 136],
158
+ [78, 89, 145],
159
+ [99, 28, 210],
160
+ [200, 156, 78],
161
+ [145, 78, 189],
162
+ [78, 57, 99],
163
+ [57, 78, 145],
164
+ [210, 99, 28],
165
+ [99, 89, 145],
166
+ [28, 210, 99],
167
+ [145, 112, 78],
168
+ [200, 78, 112],
169
+ [78, 57, 99],
170
+ [57, 78, 145],
171
+ [99, 28, 210],
172
+ ]
173
+
174
+ labels_list = []
175
+
176
+ with open(r'labels.txt', 'r') as fp:
177
+ for line in fp:
178
+ labels_list.append(line[:-1])
179
+
180
+ colormap = np.asarray(ade_palette())
181
+
182
+ def label_to_color_image(label):
183
+ if label.ndim != 2:
184
+ raise ValueError("Expect 2-D input label")
185
+
186
+ if np.max(label) >= len(colormap):
187
+ raise ValueError("label value too large.")
188
+ return colormap[label]
189
+
190
+ def draw_plot(pred_img, seg):
191
+ fig = plt.figure(figsize=(20, 15))
192
+
193
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
194
+
195
+ plt.subplot(grid_spec[0])
196
+ plt.imshow(pred_img)
197
+ plt.axis('off')
198
+ LABEL_NAMES = np.asarray(labels_list)
199
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
200
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
201
+
202
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
203
+ ax = plt.subplot(grid_spec[1])
204
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
205
+ ax.yaxis.tick_right()
206
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
207
+ plt.xticks([], [])
208
+ ax.tick_params(width=0.0, labelsize=25)
209
+ return fig
210
+
211
+ def sepia(input_img):
212
+ input_img = Image.fromarray(input_img)
213
+
214
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
215
+ outputs = model(**inputs)
216
+ logits = outputs.logits
217
+
218
+ logits = tf.transpose(logits, [0, 2, 3, 1])
219
+ logits = tf.image.resize(
220
+ logits, input_img.size[::-1]
221
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
222
+ seg = tf.math.argmax(logits, axis=-1)[0]
223
+
224
+ color_seg = np.zeros(
225
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
226
+ ) # height, width, 3
227
+ for label, color in enumerate(colormap):
228
+ color_seg[seg.numpy() == label, :] = color
229
+
230
+ # Show image + mask
231
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
232
+ pred_img = pred_img.astype(np.uint8)
233
+
234
+ fig = draw_plot(pred_img, seg)
235
+ return fig
236
+
237
+ demo = gr.Interface(fn=sepia,
238
+ inputs=gr.Image(shape=(400, 600)),
239
+ outputs=['plot'],
240
+ examples=["sample-1.jpg", "sample-2.jpg", "sample-3.png", ],
241
+ allow_flagging='never')
242
+
243
+
244
+ demo.launch()
labels.txt ADDED
@@ -0,0 +1,150 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ wall
2
+ building
3
+ sky
4
+ floor
5
+ tree
6
+ ceiling
7
+ road
8
+ bed
9
+ windowpane
10
+ grass
11
+ cabinet
12
+ sidewalk
13
+ person
14
+ earth
15
+ door
16
+ table
17
+ mountain
18
+ plant
19
+ curtain
20
+ chair
21
+ car
22
+ water
23
+ painting
24
+ sofa
25
+ shelf
26
+ house
27
+ sea
28
+ mirror
29
+ rug
30
+ field
31
+ armchair
32
+ seat
33
+ fence
34
+ desk
35
+ rock
36
+ wardrobe
37
+ lamp
38
+ bathtub
39
+ railing
40
+ cushion
41
+ base
42
+ box
43
+ column
44
+ signboard
45
+ chest of drawers
46
+ counter
47
+ sand
48
+ sink
49
+ skyscraper
50
+ fireplace
51
+ refrigerator
52
+ grandstand
53
+ path
54
+ stairs
55
+ runway
56
+ case
57
+ pool table
58
+ pillow
59
+ screen door
60
+ stairway
61
+ river
62
+ bridge
63
+ bookcase
64
+ blind
65
+ coffee table
66
+ toilet
67
+ flower
68
+ book
69
+ hill
70
+ bench
71
+ countertop
72
+ stove
73
+ palm
74
+ kitchen island
75
+ computer
76
+ swivel chair
77
+ boat
78
+ bar
79
+ arcade machine
80
+ hovel
81
+ bus
82
+ towel
83
+ light
84
+ truck
85
+ tower
86
+ chandelier
87
+ awning
88
+ streetlight
89
+ booth
90
+ television receiver
91
+ airplane
92
+ dirt track
93
+ apparel
94
+ pole
95
+ land
96
+ bannister
97
+ escalator
98
+ ottoman
99
+ bottle
100
+ buffet
101
+ poster
102
+ stage
103
+ van
104
+ ship
105
+ fountain
106
+ conveyer belt
107
+ canopy
108
+ washer
109
+ plaything
110
+ swimming pool
111
+ stool
112
+ barrel
113
+ basket
114
+ waterfall
115
+ tent
116
+ bag
117
+ minibike
118
+ cradle
119
+ oven
120
+ ball
121
+ food
122
+ step
123
+ tank
124
+ trade name
125
+ microwave
126
+ pot
127
+ animal
128
+ bicycle
129
+ lake
130
+ dishwasher
131
+ screen
132
+ blanket
133
+ sculpture
134
+ hood
135
+ sconce
136
+ vase
137
+ traffic light
138
+ tray
139
+ ashcan
140
+ fan
141
+ pier
142
+ crt screen
143
+ plate
144
+ monitor
145
+ bulletin board
146
+ shower
147
+ radiator
148
+ glass
149
+ clock
150
+ flag
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ tensorflow
4
+ numpy
5
+ Image
6
+ matplotlib
sample-1.jpg ADDED
sample-2.jpg ADDED
sample-3.png ADDED