Spaces:
Runtime error
Runtime error
urbanManul
commited on
Commit
Β·
beab4e3
1
Parent(s):
866a272
update-model
Browse files- .gitattributes +1 -0
- README.md +5 -5
- app.py +112 -0
- example-1.jpg +0 -0
- example-2.jpg +0 -0
- example-3.jpg +0 -0
- example-4.png +3 -0
- example-5.png +0 -0
- labels.txt +19 -0
- requirements.txt +6 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
example-4.png filter=lfs diff=lfs merge=lfs -text
|
README.md
CHANGED
@@ -1,10 +1,10 @@
|
|
1 |
---
|
2 |
-
title:
|
3 |
-
emoji:
|
4 |
-
colorFrom:
|
5 |
-
colorTo:
|
6 |
sdk: gradio
|
7 |
-
sdk_version:
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
|
|
1 |
---
|
2 |
+
title: Segmentation Task 2
|
3 |
+
emoji: π
|
4 |
+
colorFrom: blue
|
5 |
+
colorTo: pink
|
6 |
sdk: gradio
|
7 |
+
sdk_version: 3.44.4
|
8 |
app_file: app.py
|
9 |
pinned: false
|
10 |
---
|
app.py
ADDED
@@ -0,0 +1,112 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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 |
+
model_name = 'nvidia/segformer-b0-finetuned-cityscapes-768-768'
|
11 |
+
|
12 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
13 |
+
model_name
|
14 |
+
)
|
15 |
+
model = TFSegformerForSemanticSegmentation.from_pretrained(
|
16 |
+
model_name
|
17 |
+
)
|
18 |
+
|
19 |
+
def ade_palette():
|
20 |
+
"""ADE20K palette that maps each class to RGB values."""
|
21 |
+
return [
|
22 |
+
[184, 65, 1],
|
23 |
+
[184, 157, 1],
|
24 |
+
[120, 184, 1],
|
25 |
+
[28, 184, 1],
|
26 |
+
[1, 184, 65],
|
27 |
+
[1, 184, 157],
|
28 |
+
[160, 184, 1],
|
29 |
+
[69, 184, 1],
|
30 |
+
[1, 184, 25],
|
31 |
+
[1, 184, 117],
|
32 |
+
[1, 160, 184],
|
33 |
+
[1, 69, 184],
|
34 |
+
[1, 184, 180],
|
35 |
+
[1, 96, 184],
|
36 |
+
[1, 5, 184],
|
37 |
+
[89, 1, 184],
|
38 |
+
[180, 1, 184],
|
39 |
+
[184, 1, 97],
|
40 |
+
[184, 1, 102]
|
41 |
+
]
|
42 |
+
|
43 |
+
labels_list = []
|
44 |
+
|
45 |
+
with open(r'labels.txt', 'r') as fp:
|
46 |
+
for line in fp:
|
47 |
+
labels_list.append(line[:-1])
|
48 |
+
|
49 |
+
colormap = np.asarray(ade_palette())
|
50 |
+
|
51 |
+
def label_to_color_image(label):
|
52 |
+
if label.ndim != 2:
|
53 |
+
raise ValueError("Expect 2-D input label")
|
54 |
+
|
55 |
+
if np.max(label) >= len(colormap):
|
56 |
+
raise ValueError("label value too large.")
|
57 |
+
return colormap[label]
|
58 |
+
|
59 |
+
def draw_plot(pred_img, seg):
|
60 |
+
fig = plt.figure(figsize=(20, 15))
|
61 |
+
|
62 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
63 |
+
|
64 |
+
plt.subplot(grid_spec[0])
|
65 |
+
plt.imshow(pred_img)
|
66 |
+
plt.axis('off')
|
67 |
+
LABEL_NAMES = np.asarray(labels_list)
|
68 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
69 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
70 |
+
|
71 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
72 |
+
ax = plt.subplot(grid_spec[1])
|
73 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
74 |
+
ax.yaxis.tick_right()
|
75 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
76 |
+
plt.xticks([], [])
|
77 |
+
ax.tick_params(width=0.0, labelsize=25)
|
78 |
+
return fig
|
79 |
+
|
80 |
+
def sepia(input_img):
|
81 |
+
input_img = Image.fromarray(input_img)
|
82 |
+
|
83 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
84 |
+
outputs = model(**inputs)
|
85 |
+
logits = outputs.logits
|
86 |
+
|
87 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
88 |
+
logits = tf.image.resize(
|
89 |
+
logits, input_img.size[::-1]
|
90 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
91 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
92 |
+
|
93 |
+
color_seg = np.zeros(
|
94 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
95 |
+
) # height, width, 3
|
96 |
+
for label, color in enumerate(colormap):
|
97 |
+
color_seg[seg.numpy() == label, :] = color
|
98 |
+
|
99 |
+
# Show image + mask
|
100 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
101 |
+
pred_img = pred_img.astype(np.uint8)
|
102 |
+
|
103 |
+
fig = draw_plot(pred_img, seg)
|
104 |
+
return fig
|
105 |
+
|
106 |
+
demo = gr.Interface(fn=sepia,
|
107 |
+
inputs=gr.Image(shape=(400, 600)),
|
108 |
+
outputs=['plot'],
|
109 |
+
examples=['person-1.jpg', 'person-2.jpg', 'person-3.jpg', 'person-4.jpg', 'person-5.jpg'],
|
110 |
+
allow_flagging='never')
|
111 |
+
|
112 |
+
demo.launch()
|
example-1.jpg
ADDED
example-2.jpg
ADDED
example-3.jpg
ADDED
example-4.png
ADDED
Git LFS Details
|
example-5.png
ADDED
labels.txt
ADDED
@@ -0,0 +1,19 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
road
|
2 |
+
sidewalk
|
3 |
+
building
|
4 |
+
wall
|
5 |
+
fence
|
6 |
+
pole
|
7 |
+
traffic light
|
8 |
+
traffic sign
|
9 |
+
vegetation
|
10 |
+
terrain
|
11 |
+
sky
|
12 |
+
person
|
13 |
+
rider
|
14 |
+
car
|
15 |
+
truck
|
16 |
+
bus
|
17 |
+
train
|
18 |
+
motorcycle
|
19 |
+
bicycle
|
requirements.txt
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|
3 |
+
tensorflow
|
4 |
+
numpy
|
5 |
+
Image
|
6 |
+
matplotlib
|