yunnn426 commited on
Commit
be15522
1 Parent(s): 4b57b05

initial commit

Browse files
Files changed (9) hide show
  1. README.md +1 -1
  2. app.py +128 -0
  3. labels.txt +35 -0
  4. requirements.txt +6 -0
  5. sidewalk_1.jpg +0 -0
  6. sidewalk_2.jpg +0 -0
  7. sidewalk_3.jpg +0 -0
  8. sidewalk_4.jpg +0 -0
  9. sidewalk_5.jpg +0 -0
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: 🐢
4
  colorFrom: purple
5
  colorTo: indigo
6
  sdk: gradio
7
- sdk_version: 4.1.2
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: purple
5
  colorTo: indigo
6
  sdk: gradio
7
+ sdk_version: 3.44.4
8
  app_file: app.py
9
  pinned: false
10
  ---
app.py ADDED
@@ -0,0 +1,128 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
+ "nielsr/segformer-b0-finetuned-segments-sidewalk"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "nielsr/segformer-b0-finetuned-segments-sidewalk"
15
+ )
16
+
17
+ def ade_palette():
18
+ """ADE20K palette that maps each class to RGB values."""
19
+ return [
20
+ [204, 87, 90],
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
+ ]
56
+
57
+ labels_list = []
58
+
59
+ with open(r'labels.txt', 'r') as fp:
60
+ for line in fp:
61
+ labels_list.append(line[:-1])
62
+
63
+ colormap = np.asarray(ade_palette())
64
+
65
+ def label_to_color_image(label):
66
+ if label.ndim != 2:
67
+ raise ValueError("Expect 2-D input label")
68
+
69
+ if np.max(label) >= len(colormap):
70
+ raise ValueError("label value too large.")
71
+ return colormap[label]
72
+
73
+ def draw_plot(pred_img, seg):
74
+ fig = plt.figure(figsize=(20, 15))
75
+
76
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
77
+
78
+ plt.subplot(grid_spec[0])
79
+ plt.imshow(pred_img)
80
+ plt.axis('off')
81
+ LABEL_NAMES = np.asarray(labels_list)
82
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
83
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
84
+
85
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
86
+ ax = plt.subplot(grid_spec[1])
87
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
88
+ ax.yaxis.tick_right()
89
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
90
+ plt.xticks([], [])
91
+ ax.tick_params(width=0.0, labelsize=25)
92
+ return fig
93
+
94
+ def sepia(input_img):
95
+ input_img = Image.fromarray(input_img)
96
+
97
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
98
+ outputs = model(**inputs)
99
+ logits = outputs.logits
100
+
101
+ logits = tf.transpose(logits, [0, 2, 3, 1])
102
+ logits = tf.image.resize(
103
+ logits, input_img.size[::-1]
104
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
105
+ seg = tf.math.argmax(logits, axis=-1)[0]
106
+
107
+ color_seg = np.zeros(
108
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
109
+ ) # height, width, 3
110
+ for label, color in enumerate(colormap):
111
+ color_seg[seg.numpy() == label, :] = color
112
+
113
+ # Show image + mask
114
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
115
+ pred_img = pred_img.astype(np.uint8)
116
+
117
+ fig = draw_plot(pred_img, seg)
118
+ return fig
119
+
120
+ demo = gr.Interface(fn=sepia,
121
+ title="Sidewalk Segmentation",
122
+ inputs=gr.Image(shape=(400, 600)),
123
+ outputs=['plot'],
124
+ examples=["sidewalk_1.jpg", "sidewalk_2.jpg", "sidewalk_3.jpg", "sidewalk_4.jpg", "sidewalk_5.jpg"],
125
+ allow_flagging='never')
126
+
127
+
128
+ demo.launch()
labels.txt ADDED
@@ -0,0 +1,35 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ unlabeled
2
+ flat-road
3
+ flat-sidewalk
4
+ flat-crosswalk
5
+ flat-cyclinglane
6
+ flat-parkingdriveway
7
+ flat-railtrack
8
+ flat-curb
9
+ human-person
10
+ human-rider
11
+ vehicle-car
12
+ vehicle-truck
13
+ vehicle-bus
14
+ vehicle-tramtrain
15
+ vehicle-motorcycle
16
+ vehicle-bicycle
17
+ vehicle-caravan
18
+ vehicle-cartrailer
19
+ construction-building
20
+ construction-door
21
+ construction-wall
22
+ construction-fenceguardrail
23
+ construction-bridge
24
+ construction-tunnel
25
+ construction-stairs
26
+ object-pole
27
+ object-trafficsign
28
+ object-trafficlight
29
+ nature-vegetation
30
+ nature-terrain
31
+ sky
32
+ void-ground
33
+ void-dynamic
34
+ void-static
35
+ void-unclear
requirements.txt ADDED
@@ -0,0 +1,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ tensorflow
4
+ numpy
5
+ Image
6
+ matplotlib
sidewalk_1.jpg ADDED
sidewalk_2.jpg ADDED
sidewalk_3.jpg ADDED
sidewalk_4.jpg ADDED
sidewalk_5.jpg ADDED