김영중 commited on
Commit
76b043d
1 Parent(s): 4c5c1e1

modify segmentation

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