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