namkwonwoo commited on
Commit
9a0d3f1
·
1 Parent(s): 3d3c082

Upload app.py

Browse files
Files changed (1) hide show
  1. app.py +242 -0
app.py ADDED
@@ -0,0 +1,242 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-b0-finetuned-ade-512-512"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "nvidia/segformer-b0-finetuned-ade-512-512"
15
+ )
16
+
17
+ def ade_palette():
18
+ """ADE20K palette that maps each class to RGB values."""
19
+ return [
20
+ [204, 87, 92],
21
+ [112, 185, 212],
22
+ [45, 189, 106],
23
+ [234, 123, 67],
24
+ [78, 56, 123],
25
+ [210, 32, 89],
26
+ [90, 180, 56],
27
+ [155, 102, 200],
28
+ [33, 147, 176],
29
+ [255, 183, 76],
30
+ [67, 123, 89],
31
+ [190, 60, 45],
32
+ [134, 112, 200],
33
+ [56, 45, 189],
34
+ [200, 56, 123],
35
+ [230, 127, 34],
36
+ [179, 51, 126],
37
+ [122, 122, 201],
38
+ [255, 221, 101],
39
+ [97, 48, 88],
40
+ [225, 49, 112],
41
+ [55, 120, 254],
42
+ [181, 43, 25],
43
+ [212, 59, 3],
44
+ [51, 0, 0],
45
+ [0, 51, 0],
46
+ [0, 0, 51],
47
+ [153, 153, 153],
48
+ [255, 127, 0],
49
+ [128, 255, 0],
50
+ [0, 128, 255],
51
+ [255, 0, 128],
52
+ [128, 255, 128],
53
+ [255, 0, 0],
54
+ [128, 255, 0],
55
+ [255, 0, 128],
56
+ [0, 128, 0],
57
+ [0, 0, 128],
58
+ [0, 128, 255],
59
+ [128, 0, 255],
60
+ [255, 0, 128],
61
+ [128, 255, 128],
62
+ [255, 0, 0],
63
+ [0, 128, 255],
64
+ [128, 0, 255],
65
+ [0, 0, 0],
66
+ [255, 128, 0],
67
+ [0, 255, 0],
68
+ [0, 0, 128],
69
+ [0, 0, 0],
70
+ [255, 0, 0],
71
+ [128, 0, 255],
72
+ [0, 128, 0],
73
+ [255, 255, 128],
74
+ [255, 0, 255],
75
+ [255, 255, 0],
76
+ [128, 0, 0],
77
+ [255, 128, 128],
78
+ [0, 128, 255],
79
+ [128, 0, 255],
80
+ [0, 0, 255],
81
+ [0, 255, 255],
82
+ [255, 255, 0],
83
+ [255, 0, 255],
84
+ [255, 128, 0],
85
+ [255, 255, 255],
86
+ [128, 0, 0],
87
+ [255, 0, 255],
88
+ [255, 255, 0],
89
+ [0, 0, 128],
90
+ [255, 255, 255],
91
+ [0, 255, 0],
92
+ [0, 0, 0],
93
+ [255, 128, 0],
94
+ [0, 255, 128],
95
+ [255, 0, 0],
96
+ [0, 0, 255],
97
+ [128, 255, 0],
98
+ [255, 255, 128],
99
+ [255, 255, 0],
100
+ [255, 128, 128],
101
+ [255, 0, 128],
102
+ [255, 128, 255],
103
+ [255, 0, 128],
104
+ [255, 255, 0],
105
+ [255, 128, 0],
106
+ [204, 87, 92],
107
+ [128, 255, 0],
108
+ [255, 0, 255],
109
+ [0, 255, 128],
110
+ [90, 180, 56],
111
+ [91, 1, 5],
112
+ [92, 64, 34],
113
+ [93, 128, 0],
114
+ [94, 255, 0],
115
+ [95, 34, 87],
116
+ [96, 86, 145],
117
+ [97, 123, 98],
118
+ [98, 0, 255],
119
+ [99, 255, 128],
120
+ [100, 45, 122],
121
+ [101, 134, 245],
122
+ [102, 32, 23],
123
+ [103, 56, 0],
124
+ [104, 76, 98],
125
+ [105, 176, 90],
126
+ [106, 102, 200],
127
+ [107, 56, 78],
128
+ [108, 23, 89],
129
+ [109, 45, 200],
130
+ [110, 87, 5],
131
+ [111, 200, 67],
132
+ [112, 34, 23],
133
+ [113, 98, 76],
134
+ [114, 122, 56],
135
+ [115, 56, 23],
136
+ [116, 78, 90],
137
+ [117, 200, 45],
138
+ [118, 23, 56],
139
+ [119, 56, 189],
140
+ [120, 0, 45],
141
+ [121, 0, 0],
142
+ [122, 89, 34],
143
+ [123, 200, 1],
144
+ [124, 32, 45],
145
+ [125, 89, 0],
146
+ [126, 0, 200],
147
+ [127, 90, 200],
148
+ [128, 45, 200],
149
+ [129, 0, 123],
150
+ [130, 200, 23],
151
+ [131, 32, 200],
152
+ [132, 56, 23],
153
+ [133, 87, 98],
154
+ [134, 0, 32],
155
+ [135, 90, 0],
156
+ [136, 45, 23],
157
+ [137, 0, 89],
158
+ [138, 200, 0],
159
+ [139, 45, 23],
160
+ [140, 123, 0],
161
+ [141, 45, 200],
162
+ [142, 98, 23],
163
+ [143, 0, 98],
164
+ [144, 200, 45],
165
+ [145, 0, 23],
166
+ [146, 23, 87],
167
+ [147, 45, 0],
168
+ [148, 0, 89],
169
+ [149, 200, 32]
170
+ ]
171
+
172
+ labels_list = []
173
+
174
+ with open(r'labels.txt', 'r') as fp:
175
+ for line in fp:
176
+ labels_list.append(line[:-1])
177
+
178
+ colormap = np.asarray(ade_palette())
179
+
180
+ def label_to_color_image(label):
181
+ if label.ndim != 2:
182
+ raise ValueError("Expect 2-D input label")
183
+
184
+ if np.max(label) >= len(colormap):
185
+ raise ValueError("label value too large.")
186
+ return colormap[label]
187
+
188
+ def draw_plot(pred_img, seg):
189
+ fig = plt.figure(figsize=(20, 15))
190
+
191
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
192
+
193
+ plt.subplot(grid_spec[0])
194
+ plt.imshow(pred_img)
195
+ plt.axis('off')
196
+ LABEL_NAMES = np.asarray(labels_list)
197
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
198
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
199
+
200
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
201
+ ax = plt.subplot(grid_spec[1])
202
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
203
+ ax.yaxis.tick_right()
204
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
205
+ plt.xticks([], [])
206
+ ax.tick_params(width=0.0, labelsize=25)
207
+ return fig
208
+
209
+ def sepia(input_img):
210
+ input_img = Image.fromarray(input_img)
211
+
212
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
213
+ outputs = model(**inputs)
214
+ logits = outputs.logits
215
+
216
+ logits = tf.transpose(logits, [0, 2, 3, 1])
217
+ logits = tf.image.resize(
218
+ logits, input_img.size[::-1]
219
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
220
+ seg = tf.math.argmax(logits, axis=-1)[0]
221
+
222
+ color_seg = np.zeros(
223
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
224
+ ) # height, width, 3
225
+ for label, color in enumerate(colormap):
226
+ color_seg[seg.numpy() == label, :] = color
227
+
228
+ # Show image + mask
229
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
230
+ pred_img = pred_img.astype(np.uint8)
231
+
232
+ fig = draw_plot(pred_img, seg)
233
+ return fig
234
+
235
+ demo = gr.Interface(fn=sepia,
236
+ inputs=gr.Image(shape=(400, 600)),
237
+ outputs=['plot'],
238
+ examples=["test1.jpg", "test2.jpg", "test3.jpg", "test4.jpg"],
239
+ allow_flagging='never')
240
+
241
+
242
+ demo.launch()