timmy0079 commited on
Commit
619cb37
·
1 Parent(s): 657db49

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +86 -7
app.py CHANGED
@@ -8,18 +8,97 @@ import matplotlib.pyplot as plt
8
  import numpy as np
9
  import tensorflow as tf
10
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11
 
12
  def sepia():
13
- feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
14
- model = SegformerForSemanticSegmentation.from_pretrained("segments-tobias/segformer-b0-finetuned-segments-sidewalk")
15
- url = "https://segmentsai-prod.s3.eu-west-2.amazonaws.com/assets/admin-tobias/439f6843-80c5-47ce-9b17-0b2a1d54dbeb.jpg"
16
- image = Image.open(requests.get(url, stream=True).raw)
17
-
18
- inputs = feature_extractor(images=image, return_tensors="pt")
19
  outputs = model(**inputs)
20
-
21
  logits = outputs.logits
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  demo = gr.Interface(fn=sepia,
24
  inputs=gr.Image(shape=(400, 600)),
25
  outputs=['plot'],
 
8
  import numpy as np
9
  import tensorflow as tf
10
 
11
+ feature_extractor = SegformerFeatureExtractor.from_pretrained(
12
+ "nvidia/segformer-b0-finetuned-ade-512-512")
13
+ model = SegformerForSemanticSegmentation.from_pretrained(
14
+ "segments-tobias/segformer-b0-finetuned-segments-sidewalk")
15
+
16
+ def ade_palette():
17
+ """ADE20K palette that maps each class to RGB values."""
18
+ return [
19
+ [255, 255, 255],
20
+ [255, 255, 0],
21
+ [255, 0, 0],
22
+ [0, 255, 255],
23
+ [255, 0, 255],
24
+ [0, 0, 255],
25
+ [0, 255, 0],
26
+ [255, 255, 128],
27
+ [255, 128, 255],
28
+ [128, 255, 255],
29
+ [0, 0, 128],
30
+ [0, 128, 0],
31
+ [128, 0, 0],
32
+ [128, 255, 128],
33
+ [255, 255, 128],
34
+ [128, 255, 255],
35
+ [128, 0, 255],
36
+ [0, 255, 128],
37
+ ]
38
+
39
+ labels_list = []
40
+
41
+ with open(r'labels.txt', 'r') as fp:
42
+ for line in fp:
43
+ labels_list.append(line[:-1])
44
+
45
+ colormap = np.asarray(ade_palette())
46
+
47
+ def label_to_color_image(label):
48
+ if label.ndim != 2:
49
+ raise ValueError("Expect 2-D input label")
50
+
51
+ if np.max(label) >= len(colormap):
52
+ raise ValueError("label value too large.")
53
+ return colormap[label]
54
+
55
+ def draw_plot(pred_img, seg):
56
+ fig = plt.figure(figsize=(20, 15))
57
+
58
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
59
+
60
+ plt.subplot(grid_spec[0])
61
+ plt.imshow(pred_img)
62
+ plt.axis('off')
63
+ LABEL_NAMES = np.asarray(labels_list)
64
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
65
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
66
+
67
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
68
+ ax = plt.subplot(grid_spec[1])
69
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
70
+ ax.yaxis.tick_right()
71
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
72
+ plt.xticks([], [])
73
+ ax.tick_params(width=0.0, labelsize=25)
74
+ return fig
75
 
76
  def sepia():
77
+ input_img = Image.fromarray(input_img)
78
+
79
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
 
 
 
80
  outputs = model(**inputs)
 
81
  logits = outputs.logits
82
 
83
+ logits = tf.transpose(logits, [0, 2, 3, 1])
84
+ logits = tf.image.resize(
85
+ logits, input_img.size[::-1]
86
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
87
+ seg = tf.math.argmax(logits, axis=-1)[0]
88
+
89
+ color_seg = np.zeros(
90
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
91
+ ) # height, width, 3
92
+ for label, color in enumerate(colormap):
93
+ color_seg[seg.numpy() == label, :] = color
94
+
95
+ # Show image + mask
96
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
97
+ pred_img = pred_img.astype(np.uint8)
98
+
99
+ fig = draw_plot(pred_img, seg)
100
+ return fig
101
+
102
  demo = gr.Interface(fn=sepia,
103
  inputs=gr.Image(shape=(400, 600)),
104
  outputs=['plot'],