app.py
CHANGED
@@ -1,7 +1,14 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
2 |
from PIL import Image
|
3 |
import requests
|
4 |
|
|
|
5 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
6 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
7 |
|
@@ -11,3 +18,106 @@ image = Image.open(requests.get(url, stream=True).raw)
|
|
11 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
12 |
outputs = model(**inputs)
|
13 |
logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
|
3 |
+
from matplotlib import gridspec
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import numpy as np
|
6 |
+
import tensorflow as tf
|
7 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
8 |
from PIL import Image
|
9 |
import requests
|
10 |
|
11 |
+
|
12 |
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
13 |
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-cityscapes-640-1280")
|
14 |
|
|
|
18 |
inputs = feature_extractor(images=image, return_tensors="pt")
|
19 |
outputs = model(**inputs)
|
20 |
logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
|
21 |
+
|
22 |
+
def ade_palette():
|
23 |
+
"""ADE20K palette that maps each class to RGB values."""
|
24 |
+
return [
|
25 |
+
[255, 0, 0],
|
26 |
+
[255, 94, 0],
|
27 |
+
[255, 187, 0],
|
28 |
+
[255, 228, 0],
|
29 |
+
[171, 242, 0],
|
30 |
+
[29, 219, 22],
|
31 |
+
[0, 216, 255],
|
32 |
+
[0, 84, 255],
|
33 |
+
[1, 0, 255],
|
34 |
+
[95, 0, 255],
|
35 |
+
[255, 0, 221],
|
36 |
+
[255, 0, 127],
|
37 |
+
[152, 0, 0],
|
38 |
+
[153, 112, 0],
|
39 |
+
[107, 153, 0],
|
40 |
+
[0, 51, 153],
|
41 |
+
[63, 0, 153],
|
42 |
+
[153, 0, 133]
|
43 |
+
]
|
44 |
+
|
45 |
+
|
46 |
+
labels_list = []
|
47 |
+
|
48 |
+
with open(r"labels.txt", "r") as fp:
|
49 |
+
for line in fp:
|
50 |
+
labels_list.append(line[:-1])
|
51 |
+
|
52 |
+
colormap = np.asarray(ade_palette())
|
53 |
+
|
54 |
+
|
55 |
+
def label_to_color_image(label):
|
56 |
+
if label.ndim != 2:
|
57 |
+
raise ValueError("Expect 2-D input label")
|
58 |
+
|
59 |
+
if np.max(label) >= len(colormap):
|
60 |
+
raise ValueError("label value too large.")
|
61 |
+
return colormap[label]
|
62 |
+
|
63 |
+
|
64 |
+
def draw_plot(pred_img, seg):
|
65 |
+
fig = plt.figure(figsize=(20, 15))
|
66 |
+
|
67 |
+
grid_spec = gridspec.GridSpec(1, 2, width_ratios=[6, 1])
|
68 |
+
|
69 |
+
plt.subplot(grid_spec[0])
|
70 |
+
plt.imshow(pred_img)
|
71 |
+
plt.axis("off")
|
72 |
+
LABEL_NAMES = np.asarray(labels_list)
|
73 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
74 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
75 |
+
|
76 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
77 |
+
ax = plt.subplot(grid_spec[1])
|
78 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
79 |
+
ax.yaxis.tick_right()
|
80 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
81 |
+
plt.xticks([], [])
|
82 |
+
ax.tick_params(width=0.0, labelsize=25)
|
83 |
+
return fig
|
84 |
+
|
85 |
+
|
86 |
+
def sepia(input_img):
|
87 |
+
input_img = Image.fromarray(input_img)
|
88 |
+
|
89 |
+
inputs = feature_extractor(images=input_img, return_tensors="tf")
|
90 |
+
outputs = model(**inputs)
|
91 |
+
logits = outputs.logits
|
92 |
+
|
93 |
+
logits = tf.transpose(logits, [0, 2, 3, 1])
|
94 |
+
logits = tf.image.resize(
|
95 |
+
logits, input_img.size[::-1]
|
96 |
+
) # We reverse the shape of `image` because `image.size` returns width and height.
|
97 |
+
seg = tf.math.argmax(logits, axis=-1)[0]
|
98 |
+
|
99 |
+
color_seg = np.zeros(
|
100 |
+
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
101 |
+
) # height, width, 3
|
102 |
+
for label, color in enumerate(colormap):
|
103 |
+
color_seg[seg.numpy() == label, :] = color
|
104 |
+
|
105 |
+
# Show image + mask
|
106 |
+
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
107 |
+
pred_img = pred_img.astype(np.uint8)
|
108 |
+
|
109 |
+
fig = draw_plot(pred_img, seg)
|
110 |
+
return fig
|
111 |
+
|
112 |
+
|
113 |
+
demo = gr.Interface(
|
114 |
+
fn=sepia,
|
115 |
+
inputs=gr.Image(shape=(400, 600)),
|
116 |
+
outputs=["plot"],
|
117 |
+
examples=[
|
118 |
+
"person-1.jpg","person-2.jpg","person-3.jpg","person-4.jpg", "person-5.jpg",],
|
119 |
+
allow_flagging="never",
|
120 |
+
)
|
121 |
+
|
122 |
+
|
123 |
+
demo.launch()
|