Rinka0616 commited on
Commit
91eb216
β€’
1 Parent(s): 1fd53b4

Upload 7 files

Browse files
Files changed (7) hide show
  1. README.md +4 -4
  2. app.py +112 -0
  3. city1.jpg +0 -0
  4. city2.jpg +0 -0
  5. city3.jpg +0 -0
  6. labels.txt +19 -0
  7. requirements.txt +6 -0
README.md CHANGED
@@ -1,8 +1,8 @@
1
  ---
2
- title: ThirdPRE
3
- emoji: πŸŒ–
4
- colorFrom: pink
5
- colorTo: blue
6
  sdk: gradio
7
  sdk_version: 4.2.0
8
  app_file: app.py
 
1
  ---
2
+ title: Practice3
3
+ emoji: πŸš€
4
+ colorFrom: yellow
5
+ colorTo: red
6
  sdk: gradio
7
  sdk_version: 4.2.0
8
  app_file: app.py
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
+ feature_extractor = SegformerFeatureExtractor.from_pretrained(
11
+ "nvidia/segformer-b5-finetuned-cityscapes-1024-1024"
12
+ )
13
+ model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "nvidia/segformer-b5-finetuned-cityscapes-1024-1024"
15
+ )
16
+
17
+ def ade_palette():
18
+
19
+ return [
20
+ [204, 87, 92], # road (Reddish)
21
+ [112, 185, 212], # sidewalk (Blue)
22
+ [196, 160, 122], # building (Brown)
23
+ [106, 135, 242], # wall (Light Blue)
24
+ [91, 192, 222], # fence (Turquoise)
25
+ [255, 192, 203], # pole (Pink)
26
+ [176, 224, 230], # traffic light (Light Blue)
27
+ [222, 49, 99], # traffic sign (Red)
28
+ [139, 69, 19], # vegetation (Brown)
29
+ [255, 0, 0], # terrain (Red)
30
+ [0, 0, 255], # sky (Blue)
31
+ [255, 228, 181], # person (Peach)
32
+ [128, 0, 0], # rider (Maroon)
33
+ [0, 128, 0], # car (Green)
34
+ [255, 99, 71], # truck (Tomato)
35
+ [0, 255, 0], # bus (Lime)
36
+ [128, 0, 128], # train (Purple)
37
+ [255, 255, 0], # motorcycle (Yellow)
38
+ [128, 0, 128] # bicycle (Purple)
39
+
40
+ ]
41
+
42
+ labels_list = []
43
+
44
+ with open(r'labels.txt', 'r') as fp:
45
+ for line in fp:
46
+ labels_list.append(line[:-1])
47
+
48
+ colormap = np.asarray(ade_palette())
49
+
50
+ def label_to_color_image(label):
51
+ if label.ndim != 2:
52
+ raise ValueError("Expect 2-D input label")
53
+
54
+ if np.max(label) >= len(colormap):
55
+ raise ValueError("label value too large.")
56
+ return colormap[label]
57
+
58
+ def draw_plot(pred_img, seg):
59
+ fig = plt.figure(figsize=(20, 15))
60
+
61
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
62
+
63
+ plt.subplot(grid_spec[0])
64
+ plt.imshow(pred_img)
65
+ plt.axis('off')
66
+ LABEL_NAMES = np.asarray(labels_list)
67
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
68
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
69
+
70
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
71
+ ax = plt.subplot(grid_spec[1])
72
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
73
+ ax.yaxis.tick_right()
74
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
75
+ plt.xticks([], [])
76
+ ax.tick_params(width=0.0, labelsize=25)
77
+ return fig
78
+
79
+ def sepia(input_img):
80
+ input_img = Image.fromarray(input_img)
81
+
82
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
83
+ outputs = model(**inputs)
84
+ logits = outputs.logits
85
+
86
+ logits = tf.transpose(logits, [0, 2, 3, 1])
87
+ logits = tf.image.resize(
88
+ logits, input_img.size[::-1]
89
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
90
+ seg = tf.math.argmax(logits, axis=-1)[0]
91
+
92
+ color_seg = np.zeros(
93
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
94
+ ) # height, width, 3
95
+ for label, color in enumerate(colormap):
96
+ color_seg[seg.numpy() == label, :] = color
97
+
98
+ # Show image + mask
99
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
100
+ pred_img = pred_img.astype(np.uint8)
101
+
102
+ fig = draw_plot(pred_img, seg)
103
+ return fig
104
+
105
+ demo = gr.Interface(fn=sepia,
106
+ inputs=gr.Image(shape=(400,600)),
107
+ outputs=['plot'],
108
+ examples=["city1.jpg","city2.jpg","city3.jpg"],
109
+ allow_flagging='never')
110
+
111
+
112
+ demo.launch()
city1.jpg ADDED
city2.jpg ADDED
city3.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,6 @@
 
 
 
 
 
 
 
1
+ torch
2
+ transformers
3
+ tensorflow
4
+ numpy
5
+ Image
6
+ matplotlib