dakkoong commited on
Commit
c79a453
โ€ข
1 Parent(s): e2067ba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +95 -24
app.py CHANGED
@@ -1,37 +1,108 @@
1
  import gradio as gr
 
 
 
2
  import numpy as np
3
  from PIL import Image
4
- from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
5
  import tensorflow as tf
 
6
 
7
- # Segformer ๋ชจ๋ธ ๋ฐ feature extractor ๋ถˆ๋Ÿฌ์˜ค๊ธฐ
8
  feature_extractor = SegformerFeatureExtractor.from_pretrained(
9
- "nvidia/segformer-b1-finetuned-cityscapes-1024-1024")
 
10
  model = TFSegformerForSemanticSegmentation.from_pretrained(
11
- "nvidia/segformer-b1-finetuned-cityscapes-1024-1024")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
12
 
13
- # ๋ชจ๋ธ ์˜ˆ์ธก ํ•จ์ˆ˜๋ฅผ ์ •์˜ํ•ฉ๋‹ˆ๋‹ค.
14
- def classify_image(img):
15
- try:
16
- # ์ด๋ฏธ์ง€๋ฅผ ์ „์ฒ˜๋ฆฌํ•ฉ๋‹ˆ๋‹ค.
17
- inputs = feature_extractor(images=img, return_tensors="tf")
18
 
19
- # ๋ชจ๋ธ๋กœ ์˜ˆ์ธก์„ ์ˆ˜ํ–‰ํ•ฉ๋‹ˆ๋‹ค.
20
- predictions = model(**inputs)
 
 
 
21
 
22
- # ์˜ˆ์ธก ๊ฒฐ๊ณผ ์ค‘์—์„œ ๊ฐ€์žฅ ๋†’์€ ํ™•๋ฅ ์„ ๊ฐ€์ง„ ํด๋ž˜์Šค๋ฅผ ์„ ํƒํ•ฉ๋‹ˆ๋‹ค.
23
- predicted_label = tf.argmax(predictions.logits[0], axis=-1).numpy()
 
 
 
24
 
25
- # ๋ผ๋ฒจ์„ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
26
- return predicted_label
27
- except Exception as e:
28
- # ์˜ˆ์™ธ๊ฐ€ ๋ฐœ์ƒํ•˜๋ฉด ์˜ˆ์™ธ ๋ฉ”์‹œ์ง€๋ฅผ ๋ฐ˜ํ™˜ํ•ฉ๋‹ˆ๋‹ค.
29
- return str(e)
30
 
31
- # Gradio UI๋ฅผ ์ƒ์„ฑํ•ฉ๋‹ˆ๋‹ค.
32
- iface = gr.Interface(fn=classify_image,
33
- inputs="image",
34
- outputs="label", live=True)
35
 
36
- # Gradio UI๋ฅผ ์‹œ์ž‘ํ•ฉ๋‹ˆ๋‹ค.
37
- iface.launch()
 
 
 
 
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-b1-finetuned-cityscapes-1024-1024"
12
+ )
13
  model = TFSegformerForSemanticSegmentation.from_pretrained(
14
+ "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
15
+ )
16
+
17
+ def ade_palette():
18
+ """ADE20K palette that maps each class to RGB values."""
19
+ return [
20
+ [204, 166, 62],
21
+ [188, 229, 92],
22
+ [47, 157,39],
23
+ [178, 235, 244],
24
+ [0, 51, 153],
25
+ [181, 178, 255],
26
+ [128, 65, 217],
27
+ [255, 178, 245],
28
+ [153, 0, 76],
29
+ [25, 186, 52],
30
+ [81, 162, 235],
31
+ [255, 255, 0],
32
+ [62, 57, 159],
33
+ [91, 189, 203],
34
+ [0, 0, 255],
35
+ [0, 255, 255],
36
+ [12, 168, 0],
37
+ [255, 0, 0],
38
+ [231, 32, 65]
39
+ ]
40
+
41
+ labels_list = []
42
+
43
+ with open(r'labels.txt', 'r') as fp:
44
+ for line in fp:
45
+ labels_list.append(line[:-1])
46
+
47
+ colormap = np.asarray(ade_palette())
48
+
49
+ def label_to_color_image(label):
50
+ if label.ndim != 2:
51
+ raise ValueError("Expect 2-D input label")
52
+
53
+ if np.max(label) >= len(colormap):
54
+ raise ValueError("label value too large.")
55
+ return colormap[label]
56
+
57
+ def draw_plot(pred_img, seg):
58
+ fig = plt.figure(figsize=(20, 15))
59
+
60
+ grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
61
+
62
+ plt.subplot(grid_spec[0])
63
+ plt.imshow(pred_img)
64
+ plt.axis('off')
65
+ LABEL_NAMES = np.asarray(labels_list)
66
+ FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
67
+ FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
68
+
69
+ unique_labels = np.unique(seg.numpy().astype("uint8"))
70
+ ax = plt.subplot(grid_spec[1])
71
+ plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
72
+ ax.yaxis.tick_right()
73
+ plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
74
+ plt.xticks([], [])
75
+ ax.tick_params(width=0.0, labelsize=25)
76
+ return fig
77
+
78
+ def sepia(input_img):
79
+ input_img = Image.fromarray(input_img)
80
 
81
+ inputs = feature_extractor(images=input_img, return_tensors="tf")
82
+ outputs = model(**inputs)
83
+ logits = outputs.logits
 
 
84
 
85
+ logits = tf.transpose(logits, [0, 2, 3, 1])
86
+ logits = tf.image.resize(
87
+ logits, input_img.size[::-1]
88
+ ) # We reverse the shape of `image` because `image.size` returns width and height.
89
+ seg = tf.math.argmax(logits, axis=-1)[0]
90
 
91
+ color_seg = np.zeros(
92
+ (seg.shape[0], seg.shape[1], 3), dtype=np.uint8
93
+ ) # height, width, 3
94
+ for label, color in enumerate(colormap):
95
+ color_seg[seg.numpy() == label, :] = color
96
 
97
+ # Show image + mask
98
+ pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
99
+ pred_img = pred_img.astype(np.uint8)
 
 
100
 
101
+ fig = draw_plot(pred_img, seg)
102
+ return fig
 
 
103
 
104
+ demo = gr.Interface(fn=sepia,
105
+ inputs=gr.Image(shape=(800, 600)),
106
+ outputs=['plot'],
107
+ examples=["cityoutdoor-1.jpg", "cityoutdoor-2.jpg", "cityoutdoor-3.jpg"],
108
+ allow_flagging='never')