Spaces:
Sleeping
Sleeping
ASTUMJ12
commited on
Commit
·
9fd28bf
1
Parent(s):
dc41a37
segmentation
Browse files- app.py +193 -0
- indoor.jpg +0 -0
- indoor1.jpg +0 -0
- indoor2.jpg +0 -0
- indoor3.jpg +0 -0
- labels.txt +100 -0
- requirements.txt +6 -0
app.py
ADDED
@@ -0,0 +1,193 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
[22, 122, 213],
|
21 |
+
[240, 3, 156],
|
22 |
+
[87, 176, 33],
|
23 |
+
[154, 88, 111],
|
24 |
+
[63, 54, 244],
|
25 |
+
[201, 235, 59],
|
26 |
+
[102, 66, 183],
|
27 |
+
[94, 147, 5],
|
28 |
+
[39, 198, 247],
|
29 |
+
[17, 149, 92],
|
30 |
+
[130, 78, 184],
|
31 |
+
[246, 119, 107],
|
32 |
+
[225, 23, 68],
|
33 |
+
[52, 189, 140],
|
34 |
+
[142, 10, 22],
|
35 |
+
[114, 161, 251],
|
36 |
+
[168, 55, 34],
|
37 |
+
[75, 203, 89],
|
38 |
+
[32, 45, 235],
|
39 |
+
[74, 1, 129],
|
40 |
+
[31, 166, 96],
|
41 |
+
[223, 51, 202],
|
42 |
+
[57, 72, 27],
|
43 |
+
[143, 191, 176],
|
44 |
+
[111, 33, 244],
|
45 |
+
[20, 155, 62],
|
46 |
+
[128, 99, 209],
|
47 |
+
[254, 120, 14],
|
48 |
+
[229, 67, 175],
|
49 |
+
[53, 206, 40],
|
50 |
+
[198, 77, 10],
|
51 |
+
[8, 166, 142],
|
52 |
+
[133, 45, 111],
|
53 |
+
[222, 199, 239],
|
54 |
+
[56, 18, 90],
|
55 |
+
[164, 98, 206],
|
56 |
+
[239, 135, 60],
|
57 |
+
[106, 28, 139],
|
58 |
+
[49, 172, 224],
|
59 |
+
[179, 109, 34],
|
60 |
+
[12, 191, 157],
|
61 |
+
[121, 64, 88],
|
62 |
+
[243, 214, 127],
|
63 |
+
[82, 11, 165],
|
64 |
+
[158, 37, 192],
|
65 |
+
[31, 144, 55],
|
66 |
+
[176, 220, 252],
|
67 |
+
[68, 5, 123],
|
68 |
+
[220, 157, 73],
|
69 |
+
[41, 183, 210],
|
70 |
+
[173, 85, 14],
|
71 |
+
[16, 131, 99],
|
72 |
+
[135, 50, 177],
|
73 |
+
[227, 202, 244],
|
74 |
+
[47, 175, 217],
|
75 |
+
[181, 112, 28],
|
76 |
+
[15, 190, 160],
|
77 |
+
[124, 66, 91],
|
78 |
+
[241, 217, 130],
|
79 |
+
[80, 13, 168],
|
80 |
+
[157, 40, 195],
|
81 |
+
[30, 147, 52],
|
82 |
+
[175, 223, 249],
|
83 |
+
[67, 7, 126],
|
84 |
+
[218, 160, 76],
|
85 |
+
[235, 141, 45],
|
86 |
+
[101, 33, 149],
|
87 |
+
[46, 178, 220],
|
88 |
+
[182, 114, 31],
|
89 |
+
[14, 193, 163],
|
90 |
+
[122, 69, 94],
|
91 |
+
[240, 219, 133],
|
92 |
+
[79, 16, 171],
|
93 |
+
[156, 43, 198],
|
94 |
+
[29, 150, 58],
|
95 |
+
[225, 207, 243],
|
96 |
+
[51, 27, 121],
|
97 |
+
[159, 107, 229],
|
98 |
+
[234, 143, 48],
|
99 |
+
[100, 35, 152],
|
100 |
+
[239, 221, 136],
|
101 |
+
[78, 19, 174],
|
102 |
+
[155, 46, 201],
|
103 |
+
[28, 152, 61],
|
104 |
+
[173, 227, 243],
|
105 |
+
[20, 127, 108],
|
106 |
+
[138, 59, 179],
|
107 |
+
[224, 209, 245],
|
108 |
+
[50, 29, 124],
|
109 |
+
[161, 109, 232],
|
110 |
+
[233, 145, 51],
|
111 |
+
[99, 37, 155],
|
112 |
+
[44, 174, 226],
|
113 |
+
[184, 118, 20],
|
114 |
+
[12, 195, 169],
|
115 |
+
[125, 73, 100],
|
116 |
+
[238, 223, 139],
|
117 |
+
[77, 22, 177],
|
118 |
+
[154, 49, 204],
|
119 |
+
[27, 154, 64],
|
120 |
+
[51, 86, 205]
|
121 |
+
]
|
122 |
+
|
123 |
+
labels_list = []
|
124 |
+
|
125 |
+
with open(r'labels.txt', 'r') as fp:
|
126 |
+
for line in fp:
|
127 |
+
labels_list.append(line[:-1])
|
128 |
+
|
129 |
+
colormap = np.asarray(ade_palette())
|
130 |
+
|
131 |
+
def label_to_color_image(label):
|
132 |
+
if label.ndim != 2:
|
133 |
+
raise ValueError("Expect 2-D input label")
|
134 |
+
|
135 |
+
if np.max(label) >= len(colormap):
|
136 |
+
raise ValueError("label value too large.")
|
137 |
+
return colormap[label]
|
138 |
+
|
139 |
+
def draw_plot(pred_img, seg):
|
140 |
+
fig = plt.figure(figsize=(20, 15))
|
141 |
+
|
142 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
143 |
+
|
144 |
+
plt.subplot(grid_spec[0])
|
145 |
+
plt.imshow(pred_img)
|
146 |
+
plt.axis('off')
|
147 |
+
LABEL_NAMES = np.asarray(labels_list)
|
148 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
149 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
150 |
+
|
151 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
152 |
+
ax = plt.subplot(grid_spec[1])
|
153 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
154 |
+
ax.yaxis.tick_right()
|
155 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
156 |
+
plt.xticks([], [])
|
157 |
+
ax.tick_params(width=0.0, labelsize=25)
|
158 |
+
return fig
|
159 |
+
|
160 |
+
def sepia(input_img):
|
161 |
+
input_img = Image.fromarray(input_img)
|
162 |
+
|
163 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
164 |
+
outputs = model(**inputs)
|
165 |
+
logits = outputs.logits
|
166 |
+
|
167 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
168 |
+
logits = tf.image.resize(
|
169 |
+
logits, input_img.size[::-1]
|
170 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
171 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
172 |
+
|
173 |
+
color_seg = np.zeros(
|
174 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
175 |
+
) # height, width, 3
|
176 |
+
for label, color in enumerate(colormap):
|
177 |
+
color_seg[seg.numpy() == label, :] = color
|
178 |
+
|
179 |
+
# Show image + mask
|
180 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
181 |
+
pred_img = pred_img.astype(np.uint8)
|
182 |
+
|
183 |
+
fig = draw_plot(pred_img, seg)
|
184 |
+
return fig
|
185 |
+
|
186 |
+
demo = gr.Interface(fn=sepia,
|
187 |
+
inputs=gr.Image(shape=(800, 600)),
|
188 |
+
outputs=['plot'],
|
189 |
+
examples=["indoor.jpg", "indoor1.jpg", "indoor2.jpg", "indoor3.jpg"],
|
190 |
+
allow_flagging='never')
|
191 |
+
|
192 |
+
|
193 |
+
demo.launch()
|
indoor.jpg
ADDED
![]() |
indoor1.jpg
ADDED
![]() |
indoor2.jpg
ADDED
![]() |
indoor3.jpg
ADDED
![]() |
labels.txt
ADDED
@@ -0,0 +1,100 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
wall
|
2 |
+
building
|
3 |
+
floor
|
4 |
+
ceiling
|
5 |
+
bed
|
6 |
+
windowpane
|
7 |
+
cabinet
|
8 |
+
sidewalk
|
9 |
+
person
|
10 |
+
door
|
11 |
+
table
|
12 |
+
plant
|
13 |
+
curtain
|
14 |
+
chair
|
15 |
+
water
|
16 |
+
painting
|
17 |
+
sofa
|
18 |
+
shelf
|
19 |
+
mirror
|
20 |
+
rug
|
21 |
+
field
|
22 |
+
armchair
|
23 |
+
seat
|
24 |
+
fence
|
25 |
+
desk
|
26 |
+
wardrobe
|
27 |
+
lamp
|
28 |
+
bathtub
|
29 |
+
cushion
|
30 |
+
base
|
31 |
+
box
|
32 |
+
column
|
33 |
+
chest of drawers
|
34 |
+
counter
|
35 |
+
sand
|
36 |
+
sink
|
37 |
+
fireplace
|
38 |
+
refrigerator
|
39 |
+
grandstand
|
40 |
+
path
|
41 |
+
stairs
|
42 |
+
case
|
43 |
+
pool table
|
44 |
+
pillow
|
45 |
+
screen door
|
46 |
+
stairway
|
47 |
+
bridge
|
48 |
+
bookcase
|
49 |
+
blind
|
50 |
+
coffee table
|
51 |
+
toilet
|
52 |
+
book
|
53 |
+
bench
|
54 |
+
stove
|
55 |
+
palm
|
56 |
+
kitchen island
|
57 |
+
computer
|
58 |
+
swivel chair
|
59 |
+
bar
|
60 |
+
towel
|
61 |
+
light
|
62 |
+
chandelier
|
63 |
+
booth
|
64 |
+
television receiver
|
65 |
+
apparel
|
66 |
+
bannister
|
67 |
+
bottle
|
68 |
+
buffet
|
69 |
+
poster
|
70 |
+
stage
|
71 |
+
van
|
72 |
+
conveyer belt
|
73 |
+
canopy
|
74 |
+
washer
|
75 |
+
plaything
|
76 |
+
stool
|
77 |
+
basket
|
78 |
+
bag
|
79 |
+
cradle
|
80 |
+
oven
|
81 |
+
ball
|
82 |
+
food
|
83 |
+
step
|
84 |
+
trade name
|
85 |
+
microwave
|
86 |
+
pot
|
87 |
+
dishwasher
|
88 |
+
screen
|
89 |
+
blanket
|
90 |
+
hood
|
91 |
+
vase
|
92 |
+
ashcan
|
93 |
+
fan
|
94 |
+
crt screen
|
95 |
+
plate
|
96 |
+
monitor
|
97 |
+
shower
|
98 |
+
radiator
|
99 |
+
glass
|
100 |
+
clock
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
tensorflow
|
4 |
+
numpy
|
5 |
+
Image
|
6 |
+
matplotlib
|