kms commited on
Commit
bb988cc
β€’
1 Parent(s): 1ed04e8
Files changed (7) hide show
  1. README.md +1 -1
  2. app.py +191 -0
  3. cityscape-1.jpg +0 -0
  4. cityscape-2.jpg +0 -0
  5. cityscape-3.jpg +0 -0
  6. labels.txt +19 -0
  7. requirements.txt +8 -0
README.md CHANGED
@@ -4,7 +4,7 @@ emoji: πŸ‘
4
  colorFrom: green
5
  colorTo: pink
6
  sdk: gradio
7
- sdk_version: 4.2.0
8
  app_file: app.py
9
  pinned: false
10
  ---
 
4
  colorFrom: green
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,191 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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-b3-finetuned-cityscapes-1024-1024"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "nvidia/segformer-b3-finetuned-cityscapes-1024-1024"
15
+ )
16
+
17
+ def ade_palette():
18
+ """ADE20K palette that maps each class to RGB values."""
19
+ return [
20
+ [255, 0, 0],
21
+ [255, 94, 0],
22
+ [255, 187, 0],
23
+ [255, 228, 0],
24
+ [171, 242, 0],
25
+ [29, 219, 22],
26
+ [0, 216, 255],
27
+ [0, 84, 255],
28
+ [1, 0, 255],
29
+ [95, 0, 255],
30
+ [255, 0, 221],
31
+ [255, 0, 127],
32
+ [0, 0, 0],
33
+ [255, 255, 255],
34
+ [255, 216, 216],
35
+ [250, 224, 212],
36
+ [250, 236, 197],
37
+ [250, 244, 192],
38
+ [228, 247, 186],
39
+ [206, 251, 201],
40
+ [212, 244, 250],
41
+ [217, 229, 255],
42
+ [218, 217, 255],
43
+ [232, 217, 255],
44
+ [255, 217, 250],
45
+ [255, 217, 236],
46
+ [246, 246, 246],
47
+ [234, 234, 234],
48
+ [255, 167, 167],
49
+ [255, 193, 158],
50
+ [255, 224, 140],
51
+ [250, 237, 125],
52
+ [206, 242, 121],
53
+ [183, 240, 177],
54
+ [178, 235, 244],
55
+ [178, 204, 255],
56
+ [181, 178, 255],
57
+ [209, 178, 255],
58
+ [255, 178, 245],
59
+ [255, 178, 217],
60
+ [213, 213, 213],
61
+ [189, 189, 189],
62
+ [241, 95, 95],
63
+ [242, 150, 97],
64
+ [242, 203, 97],
65
+ [229, 216, 92],
66
+ [188, 229, 92],
67
+ [134, 229, 127],
68
+ [92, 209, 229],
69
+ [103, 153, 255],
70
+ [107, 102, 255],
71
+ [165, 102, 255],
72
+ [243, 97, 220],
73
+ [243, 97, 166],
74
+ [166, 166, 166],
75
+ [140, 140, 140],
76
+ [93, 93, 93],
77
+ [116, 116, 116],
78
+ [217, 65, 140],
79
+ [217, 65, 197],
80
+ [128, 65, 217],
81
+ [70, 65, 217],
82
+ [67, 116, 217],
83
+ [61, 183, 204],
84
+ [71, 200, 62],
85
+ [159, 201, 60],
86
+ [196, 183, 59],
87
+ [204, 166, 61],
88
+ [204, 114, 61],
89
+ [204, 61, 61],
90
+ [152, 0, 0],
91
+ [153, 56, 0],
92
+ [153, 112, 0],
93
+ [153, 138, 0],
94
+ [107, 153, 0],
95
+ [47, 157, 39],
96
+ [0, 130, 153],
97
+ [0, 51, 153],
98
+ [5, 0, 153],
99
+ [63, 0, 153],
100
+ [153, 0, 133],
101
+ [153, 0, 76],
102
+ [76, 76, 76],
103
+ [53, 53, 53],
104
+ [25, 25, 25],
105
+ [33, 33, 33],
106
+ [102, 0, 51],
107
+ [102, 0, 88],
108
+ [42, 0, 102],
109
+ [3, 0, 102],
110
+ [0, 34, 102],
111
+ [0, 87, 102],
112
+ [34, 116, 28],
113
+ [71, 102, 0],
114
+ [102, 92, 0],
115
+ [102, 75, 0],
116
+ [102, 37, 0],
117
+ [103, 0, 0]
118
+
119
+ ]
120
+
121
+ labels_list = []
122
+
123
+ with open(r'labels.txt', 'r') as fp:
124
+ for line in fp:
125
+ labels_list.append(line[:-1])
126
+
127
+ colormap = np.asarray(ade_palette())
128
+
129
+ def label_to_color_image(label):
130
+ if label.ndim != 2:
131
+ raise ValueError("Expect 2-D input label")
132
+
133
+ if np.max(label) >= len(colormap):
134
+ raise ValueError("label value too large.")
135
+ return colormap[label]
136
+
137
+ def draw_plot(pred_img, seg):
138
+ fig = plt.figure(figsize=(20, 15))
139
+
140
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
141
+
142
+ plt.subplot(grid_spec[0])
143
+ plt.imshow(pred_img)
144
+ plt.axis('off')
145
+ LABEL_NAMES = np.asarray(labels_list)
146
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
147
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
148
+
149
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
150
+ ax = plt.subplot(grid_spec[1])
151
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
152
+ ax.yaxis.tick_right()
153
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
154
+ plt.xticks([], [])
155
+ ax.tick_params(width=0.0, labelsize=25)
156
+ return fig
157
+
158
+ def sepia(input_img):
159
+ input_img = Image.fromarray(input_img)
160
+
161
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
162
+ outputs = model(**inputs)
163
+ logits = outputs.logits
164
+
165
+ logits = tf.transpose(logits, [0, 2, 3, 1])
166
+ logits = tf.image.resize(
167
+ logits, input_img.size[::-1]
168
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
169
+ seg = tf.math.argmax(logits, axis=-1)[0]
170
+
171
+ color_seg = np.zeros(
172
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
173
+ ) # height, width, 3
174
+ for label, color in enumerate(colormap):
175
+ color_seg[seg.numpy() == label, :] = color
176
+
177
+ # Show image + mask
178
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
179
+ pred_img = pred_img.astype(np.uint8)
180
+
181
+ fig = draw_plot(pred_img, seg)
182
+ return fig
183
+
184
+ demo = gr.Interface(fn=sepia,
185
+ inputs=gr.Image(shape=(400, 600)),
186
+ outputs=['plot'],
187
+ examples=["cityscape-1.jpg", "cityscape-2.jpg", "cityscape-3.jpg"],
188
+ allow_flagging='never')
189
+
190
+
191
+ demo.launch()
cityscape-1.jpg ADDED
cityscape-2.jpg ADDED
cityscape-3.jpg 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,8 @@
 
 
 
 
 
 
 
 
 
1
+ torch
2
+ transformers~=4.35.0
3
+ tensorflow~=2.14.0
4
+ numpy~=1.26.1
5
+ Image
6
+ matplotlib~=3.8.1
7
+ gradio~=4.2.0
8
+ Pillow~=10.1.0