- app.py +37 -36
- requirements.txt +7 -6
app.py
CHANGED
@@ -3,9 +3,10 @@ import gradio as gr
|
|
3 |
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
4 |
import matplotlib.pyplot as plt
|
5 |
from matplotlib import gridspec
|
6 |
-
from PIL import Image
|
7 |
import numpy as np
|
8 |
import tensorflow as tf
|
|
|
|
|
9 |
import requests
|
10 |
|
11 |
#
|
@@ -27,60 +28,58 @@ model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-fi
|
|
27 |
# outputs = model(**inputs)
|
28 |
# logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
|
29 |
|
30 |
-
def my_palette():
|
31 |
-
return [
|
32 |
-
[131, 162, 255],
|
33 |
-
[180, 189, 255],
|
34 |
-
[255, 227, 187],
|
35 |
-
[255, 210, 143],
|
36 |
-
[248, 117, 170],
|
37 |
-
[255, 223, 223],
|
38 |
-
[255, 246, 246],
|
39 |
-
[174, 222, 252],
|
40 |
-
[150, 194, 145],
|
41 |
-
[255, 219, 170],
|
42 |
-
[244, 238, 238],
|
43 |
-
[50, 38, 83],
|
44 |
-
[128, 98, 214],
|
45 |
-
[146, 136, 248],
|
46 |
-
[255, 210, 215],
|
47 |
-
[255, 152, 152],
|
48 |
-
[162, 103, 138],
|
49 |
-
[63, 29, 56]
|
50 |
-
]
|
51 |
-
|
52 |
-
|
53 |
labels_list = []
|
54 |
-
|
55 |
-
with open(r"labels.txt", "r") as fp:
|
56 |
for line in fp:
|
57 |
labels_list.append(line[:-1])
|
58 |
|
59 |
-
colormap = np.asarray(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
60 |
|
61 |
|
62 |
def greet(input_img):
|
|
|
|
|
63 |
inputs = feature_extractor(images=input_img, return_tensors="pt")
|
64 |
outputs = model(**inputs)
|
65 |
logits = outputs.logits
|
66 |
|
67 |
logits = tf.transpose(logits, [0, 2, 3, 1])
|
|
|
68 |
|
69 |
-
logits = tf.image.resize(
|
70 |
-
logits, input_img.size[::-1]
|
71 |
-
) # We reverse the shape of `image` because `image.size` returns width and height.
|
72 |
seg = tf.math.argmax(logits, axis=-1)[0]
|
73 |
-
|
74 |
-
color_seg = np.zeros(
|
75 |
-
(seg.shape[0], seg.shape[1], 3), dtype=np.uint8
|
76 |
-
) # height, width, 3
|
77 |
for label, color in enumerate(colormap):
|
78 |
color_seg[seg.numpy() == label, :] = color
|
79 |
|
80 |
-
# Show image + mask
|
81 |
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
82 |
pred_img = pred_img.astype(np.uint8)
|
83 |
|
|
|
84 |
fig = draw_plot(pred_img, seg)
|
85 |
return fig
|
86 |
|
@@ -93,6 +92,7 @@ def draw_plot(pred_img, seg):
|
|
93 |
plt.subplot(grid_spec[0])
|
94 |
plt.imshow(pred_img)
|
95 |
plt.axis("off")
|
|
|
96 |
LABEL_NAMES = np.asarray(labels_list)
|
97 |
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
98 |
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
@@ -121,5 +121,6 @@ iface = gr.Interface(
|
|
121 |
inputs="image",
|
122 |
outputs=["plot"],
|
123 |
examples=["image (1).jpg", "image (2).jpg", "image (3).jpg", "image (4).jpg", "image (5).jpg"],
|
124 |
-
allow_flagging="never"
|
|
|
125 |
iface.launch(share=True)
|
|
|
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 |
#
|
|
|
28 |
# outputs = model(**inputs)
|
29 |
# logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
|
30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
31 |
labels_list = []
|
32 |
+
with open("labels.txt", "r") as fp:
|
|
|
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 = tf.transpose(logits, [0, 2, 3, 1])
|
72 |
+
logits = tf.image.resize(logits, input_img.size[::-1])
|
73 |
|
|
|
|
|
|
|
74 |
seg = tf.math.argmax(logits, axis=-1)[0]
|
75 |
+
color_seg = np.zeros((seg.shape[0], seg.shape[1], 3), dtype=np.uint8)
|
|
|
|
|
|
|
76 |
for label, color in enumerate(colormap):
|
77 |
color_seg[seg.numpy() == label, :] = color
|
78 |
|
|
|
79 |
pred_img = np.array(input_img) * 0.5 + color_seg * 0.5
|
80 |
pred_img = pred_img.astype(np.uint8)
|
81 |
|
82 |
+
# Draw the plot
|
83 |
fig = draw_plot(pred_img, seg)
|
84 |
return fig
|
85 |
|
|
|
92 |
plt.subplot(grid_spec[0])
|
93 |
plt.imshow(pred_img)
|
94 |
plt.axis("off")
|
95 |
+
|
96 |
LABEL_NAMES = np.asarray(labels_list)
|
97 |
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
98 |
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
|
|
121 |
inputs="image",
|
122 |
outputs=["plot"],
|
123 |
examples=["image (1).jpg", "image (2).jpg", "image (3).jpg", "image (4).jpg", "image (5).jpg"],
|
124 |
+
allow_flagging="never"
|
125 |
+
)
|
126 |
iface.launch(share=True)
|
requirements.txt
CHANGED
@@ -1,6 +1,7 @@
|
|
1 |
-
|
2 |
-
|
3 |
-
|
4 |
-
|
5 |
-
|
6 |
-
|
|
|
|
1 |
+
gradio==2.3.0
|
2 |
+
torch==1.10.0
|
3 |
+
transformers==4.13.0
|
4 |
+
matplotlib==3.4.3
|
5 |
+
numpy==1.21.2
|
6 |
+
pillow==8.3.2
|
7 |
+
tensorflow==2.6.0
|