app.py
CHANGED
@@ -1,105 +1,83 @@
|
|
1 |
import gradio as gr
|
2 |
-
#
|
3 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
4 |
import matplotlib.pyplot as plt
|
5 |
from matplotlib import gridspec
|
6 |
import numpy as np
|
7 |
-
import tensorflow as tf
|
8 |
from PIL import Image
|
9 |
-
from io import BytesIO
|
10 |
import requests
|
11 |
|
12 |
-
#
|
13 |
-
|
14 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
15 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
16 |
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
-
|
23 |
-
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
30 |
|
31 |
labels_list = []
|
32 |
-
|
|
|
33 |
for line in fp:
|
34 |
labels_list.append(line[:-1])
|
35 |
|
36 |
-
colormap = np.asarray(
|
37 |
-
[131, 162, 255],
|
38 |
-
[180, 189, 255],
|
39 |
-
[255, 227, 187],
|
40 |
-
[255, 210, 143],
|
41 |
-
[248, 117, 170],
|
42 |
-
[255, 223, 223],
|
43 |
-
[255, 246, 246],
|
44 |
-
[174, 222, 252],
|
45 |
-
[150, 194, 145],
|
46 |
-
[255, 219, 170],
|
47 |
-
[244, 238, 238],
|
48 |
-
[50, 38, 83],
|
49 |
-
[128, 98, 214],
|
50 |
-
[146, 136, 248],
|
51 |
-
[255, 210, 215],
|
52 |
-
[255, 152, 152],
|
53 |
-
[162, 103, 138],
|
54 |
-
[63, 29, 56]
|
55 |
-
])
|
56 |
-
|
57 |
-
# with open(r"labels.txt", "r") as fp:
|
58 |
-
# for line in fp:
|
59 |
-
# labels_list.append(line[:-1])
|
60 |
-
#
|
61 |
-
# colormap = np.asarray(my_palette())
|
62 |
-
|
63 |
|
64 |
def greet(input_img):
|
65 |
-
input_img = Image.open(BytesIO(input_img))
|
66 |
-
|
67 |
inputs = feature_extractor(images=input_img, return_tensors="pt")
|
68 |
outputs = model(**inputs)
|
69 |
logits = outputs.logits
|
70 |
|
|
|
71 |
logits = logits.detach().numpy()
|
|
|
|
|
|
|
72 |
|
73 |
-
|
74 |
-
|
|
|
|
|
|
|
75 |
|
76 |
-
seg = tf.math.argmax(logits, axis=-1)[0]
|
77 |
-
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
|
78 |
for label, color in enumerate(colormap):
|
79 |
-
color_seg[seg
|
80 |
|
81 |
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
82 |
pred_img = pred_img.astype(np.uint8)
|
83 |
|
84 |
-
# Draw the plot
|
85 |
fig = draw_plot(pred_img, seg)
|
86 |
return fig
|
87 |
|
88 |
-
|
89 |
def draw_plot(pred_img, seg):
|
90 |
fig = plt.figure(figsize=(20, 15))
|
91 |
-
|
92 |
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
93 |
|
94 |
plt.subplot(grid_spec[0])
|
95 |
plt.imshow(pred_img)
|
96 |
plt.axis("off")
|
97 |
-
|
98 |
LABEL_NAMES = np.asarray(labels_list)
|
99 |
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
100 |
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
101 |
|
102 |
-
unique_labels = np.unique(seg.
|
103 |
ax = plt.subplot(grid_spec[1])
|
104 |
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
105 |
ax.yaxis.tick_right()
|
@@ -108,7 +86,6 @@ def draw_plot(pred_img, seg):
|
|
108 |
ax.tick_params(width=0.0, labelsize=25)
|
109 |
return fig
|
110 |
|
111 |
-
|
112 |
def label_to_color_image(label):
|
113 |
if label.ndim != 2:
|
114 |
raise ValueError("Expect 2-D input label")
|
@@ -117,7 +94,6 @@ def label_to_color_image(label):
|
|
117 |
raise ValueError("label value too large.")
|
118 |
return colormap[label]
|
119 |
|
120 |
-
|
121 |
iface = gr.Interface(
|
122 |
fn=greet,
|
123 |
inputs="image",
|
@@ -125,4 +101,4 @@ iface = gr.Interface(
|
|
125 |
examples=["image (1).jpg", "image (2).jpg", "image (3).jpg", "image (4).jpg", "image (5).jpg"],
|
126 |
allow_flagging="never"
|
127 |
)
|
128 |
-
iface.launch(share=True)
|
|
|
1 |
import gradio as gr
|
|
|
2 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
3 |
import matplotlib.pyplot as plt
|
4 |
from matplotlib import gridspec
|
5 |
import numpy as np
|
|
|
6 |
from PIL import Image
|
|
|
7 |
import requests
|
8 |
|
9 |
+
# Load the pre-trained model and feature extractor
|
|
|
10 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
11 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
12 |
|
13 |
+
def my_palette():
|
14 |
+
return [
|
15 |
+
[131, 162, 255],
|
16 |
+
[180, 189, 255],
|
17 |
+
[255, 227, 187],
|
18 |
+
[255, 210, 143],
|
19 |
+
[248, 117, 170],
|
20 |
+
[255, 223, 223],
|
21 |
+
[255, 246, 246],
|
22 |
+
[174, 222, 252],
|
23 |
+
[150, 194, 145],
|
24 |
+
[255, 219, 170],
|
25 |
+
[244, 238, 238],
|
26 |
+
[50, 38, 83],
|
27 |
+
[128, 98, 214],
|
28 |
+
[146, 136, 248],
|
29 |
+
[255, 210, 215],
|
30 |
+
[255, 152, 152],
|
31 |
+
[162, 103, 138],
|
32 |
+
[63, 29, 56]
|
33 |
+
]
|
34 |
|
35 |
labels_list = []
|
36 |
+
|
37 |
+
with open(r"labels.txt", "r") as fp:
|
38 |
for line in fp:
|
39 |
labels_list.append(line[:-1])
|
40 |
|
41 |
+
colormap = np.asarray(my_palette())
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
42 |
|
43 |
def greet(input_img):
|
|
|
|
|
44 |
inputs = feature_extractor(images=input_img, return_tensors="pt")
|
45 |
outputs = model(**inputs)
|
46 |
logits = outputs.logits
|
47 |
|
48 |
+
# Use .detach().numpy() to convert PyTorch tensor to NumPy array
|
49 |
logits = logits.detach().numpy()
|
50 |
+
logits = np.transpose(logits, [0, 2, 3, 1])
|
51 |
+
|
52 |
+
logits = np.resize(logits, input_img.size[::-1])
|
53 |
|
54 |
+
seg = np.argmax(logits, axis=-1)[0]
|
55 |
+
|
56 |
+
color_seg = np.zeros(
|
57 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
58 |
+
)
|
59 |
|
|
|
|
|
60 |
for label, color in enumerate(colormap):
|
61 |
+
color_seg[seg == label, :] = color
|
62 |
|
63 |
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
64 |
pred_img = pred_img.astype(np.uint8)
|
65 |
|
|
|
66 |
fig = draw_plot(pred_img, seg)
|
67 |
return fig
|
68 |
|
|
|
69 |
def draw_plot(pred_img, seg):
|
70 |
fig = plt.figure(figsize=(20, 15))
|
|
|
71 |
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
72 |
|
73 |
plt.subplot(grid_spec[0])
|
74 |
plt.imshow(pred_img)
|
75 |
plt.axis("off")
|
|
|
76 |
LABEL_NAMES = np.asarray(labels_list)
|
77 |
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
78 |
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
79 |
|
80 |
+
unique_labels = np.unique(seg.astype("uint8"))
|
81 |
ax = plt.subplot(grid_spec[1])
|
82 |
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
83 |
ax.yaxis.tick_right()
|
|
|
86 |
ax.tick_params(width=0.0, labelsize=25)
|
87 |
return fig
|
88 |
|
|
|
89 |
def label_to_color_image(label):
|
90 |
if label.ndim != 2:
|
91 |
raise ValueError("Expect 2-D input label")
|
|
|
94 |
raise ValueError("label value too large.")
|
95 |
return colormap[label]
|
96 |
|
|
|
97 |
iface = gr.Interface(
|
98 |
fn=greet,
|
99 |
inputs="image",
|
|
|
101 |
examples=["image (1).jpg", "image (2).jpg", "image (3).jpg", "image (4).jpg", "image (5).jpg"],
|
102 |
allow_flagging="never"
|
103 |
)
|
104 |
+
iface.launch(share=True)
|