Spaces:
Runtime error
Runtime error
Add application file
Browse files
README.md
CHANGED
|
@@ -1,6 +1,6 @@
|
|
| 1 |
---
|
| 2 |
title: Segformer Demo
|
| 3 |
-
emoji:
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
|
|
|
| 1 |
---
|
| 2 |
title: Segformer Demo
|
| 3 |
+
emoji: 💻
|
| 4 |
colorFrom: green
|
| 5 |
colorTo: gray
|
| 6 |
sdk: gradio
|
app.py
ADDED
|
@@ -0,0 +1,125 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from matplotlib import gridspec
|
| 3 |
+
import matplotlib.pyplot as plt
|
| 4 |
+
import numpy as np
|
| 5 |
+
from torch import nn
|
| 6 |
+
from PIL import Image
|
| 7 |
+
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
| 8 |
+
|
| 9 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained("zoheb/mit-b5-finetuned-sidewalk-semantic")
|
| 10 |
+
model = SegformerForSemanticSegmentation.from_pretrained("zoheb/mit-b5-finetuned-sidewalk-semantic")
|
| 11 |
+
|
| 12 |
+
def sidewalk_palette():
|
| 13 |
+
"""Sidewalk palette that maps each class to RGB values."""
|
| 14 |
+
return [
|
| 15 |
+
[0, 0, 0],
|
| 16 |
+
[216, 82, 24],
|
| 17 |
+
[255, 255, 0],
|
| 18 |
+
[125, 46, 141],
|
| 19 |
+
[118, 171, 47],
|
| 20 |
+
[161, 19, 46],
|
| 21 |
+
[255, 0, 0],
|
| 22 |
+
[0, 128, 128],
|
| 23 |
+
[190, 190, 0],
|
| 24 |
+
[0, 255, 0],
|
| 25 |
+
[0, 0, 255],
|
| 26 |
+
[170, 0, 255],
|
| 27 |
+
[84, 84, 0],
|
| 28 |
+
[84, 170, 0],
|
| 29 |
+
[84, 255, 0],
|
| 30 |
+
[170, 84, 0],
|
| 31 |
+
[170, 170, 0],
|
| 32 |
+
[170, 255, 0],
|
| 33 |
+
[255, 84, 0],
|
| 34 |
+
[255, 170, 0],
|
| 35 |
+
[255, 255, 0],
|
| 36 |
+
[33, 138, 200],
|
| 37 |
+
[0, 170, 127],
|
| 38 |
+
[0, 255, 127],
|
| 39 |
+
[84, 0, 127],
|
| 40 |
+
[84, 84, 127],
|
| 41 |
+
[84, 170, 127],
|
| 42 |
+
[84, 255, 127],
|
| 43 |
+
[170, 0, 127],
|
| 44 |
+
[170, 84, 127],
|
| 45 |
+
[170, 170, 127],
|
| 46 |
+
[170, 255, 127],
|
| 47 |
+
[255, 0, 127],
|
| 48 |
+
[255, 84, 127],
|
| 49 |
+
[255, 170, 127],
|
| 50 |
+
]
|
| 51 |
+
|
| 52 |
+
labels_list = []
|
| 53 |
+
|
| 54 |
+
with open(r'labels.txt', 'r') as fp:
|
| 55 |
+
labels_list.extend(line[:-1] for line in fp)
|
| 56 |
+
|
| 57 |
+
colormap = np.asarray(sidewalk_palette())
|
| 58 |
+
|
| 59 |
+
def label_to_color_image(label):
|
| 60 |
+
if label.ndim != 2:
|
| 61 |
+
raise ValueError("Expect 2-D input label")
|
| 62 |
+
|
| 63 |
+
if np.max(label) >= len(colormap):
|
| 64 |
+
raise ValueError("label value too large.")
|
| 65 |
+
|
| 66 |
+
return colormap[label]
|
| 67 |
+
|
| 68 |
+
def draw_plot(pred_img, seg):
|
| 69 |
+
fig = plt.figure(figsize=(20, 15))
|
| 70 |
+
|
| 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 |
+
|
| 77 |
+
LABEL_NAMES = np.asarray(labels_list)
|
| 78 |
+
FULL_LABEL_MAP = np.arange(len(LABEL_NAMES)).reshape(len(LABEL_NAMES), 1)
|
| 79 |
+
FULL_COLOR_MAP = label_to_color_image(FULL_LABEL_MAP)
|
| 80 |
+
|
| 81 |
+
unique_labels = np.unique(seg.numpy().astype("uint8"))
|
| 82 |
+
ax = plt.subplot(grid_spec[1])
|
| 83 |
+
plt.imshow(FULL_COLOR_MAP[unique_labels].astype(np.uint8), interpolation="nearest")
|
| 84 |
+
ax.yaxis.tick_right()
|
| 85 |
+
plt.yticks(range(len(unique_labels)), LABEL_NAMES[unique_labels])
|
| 86 |
+
plt.xticks([], [])
|
| 87 |
+
ax.tick_params(width=0.0, labelsize=25)
|
| 88 |
+
return fig
|
| 89 |
+
|
| 90 |
+
def main(input_img):
|
| 91 |
+
input_img = Image.fromarray(input_img)
|
| 92 |
+
|
| 93 |
+
inputs = feature_extractor(images=input_img, return_tensors="pt")
|
| 94 |
+
outputs = model(**inputs)
|
| 95 |
+
logits = outputs.logits # shape (batch_size, num_labels, height/4, width/4)
|
| 96 |
+
|
| 97 |
+
# First, rescale logits to original image size
|
| 98 |
+
upsampled_logits = nn.functional.interpolate(
|
| 99 |
+
logits,
|
| 100 |
+
size=input_img.size[::-1], # (height, width)
|
| 101 |
+
mode='bilinear',
|
| 102 |
+
align_corners=False
|
| 103 |
+
)
|
| 104 |
+
|
| 105 |
+
# Second, apply argmax on the class dimension
|
| 106 |
+
pred_seg = upsampled_logits.argmax(dim=1)[0]
|
| 107 |
+
|
| 108 |
+
color_seg = np.zeros((pred_seg.shape[0], pred_seg.shape[1], 3), dtype=np.uint8) # height, width, 3
|
| 109 |
+
palette = np.array(sidewalk_palette())
|
| 110 |
+
for label, color in enumerate(palette):
|
| 111 |
+
color_seg[pred_seg == label, :] = color
|
| 112 |
+
|
| 113 |
+
# Show image + mask
|
| 114 |
+
img = np.array(input_img) * 0.5 + color_seg * 0.5
|
| 115 |
+
pred_img = img.astype(np.uint8)
|
| 116 |
+
|
| 117 |
+
return draw_plot(pred_img, pred_seg)
|
| 118 |
+
|
| 119 |
+
demo = gr.Interface(main,
|
| 120 |
+
gr.Image(shape=(200, 200)),
|
| 121 |
+
outputs=['plot'],
|
| 122 |
+
examples=["test.jpg"],
|
| 123 |
+
allow_flagging='never')
|
| 124 |
+
|
| 125 |
+
demo.launch()
|
labels.txt
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
unlabeled
|
| 2 |
+
flat-road
|
| 3 |
+
flat-sidewalk
|
| 4 |
+
flat-crosswalk
|
| 5 |
+
flat-cyclinglane
|
| 6 |
+
flat-parkingdriveway
|
| 7 |
+
flat-railtrack
|
| 8 |
+
flat-curb
|
| 9 |
+
human-person
|
| 10 |
+
human-rider
|
| 11 |
+
vehicle-car
|
| 12 |
+
vehicle-truck
|
| 13 |
+
vehicle-bus
|
| 14 |
+
vehicle-tramtrain
|
| 15 |
+
vehicle-motorcycle
|
| 16 |
+
vehicle-bicycle
|
| 17 |
+
vehicle-caravan
|
| 18 |
+
vehicle-cartrailer
|
| 19 |
+
construction-building
|
| 20 |
+
construction-door
|
| 21 |
+
construction-wall
|
| 22 |
+
construction-fenceguardrail
|
| 23 |
+
construction-bridge
|
| 24 |
+
construction-tunnel
|
| 25 |
+
construction-stairs
|
| 26 |
+
object-pole
|
| 27 |
+
object-trafficsign
|
| 28 |
+
object-trafficlight
|
| 29 |
+
nature-vegetation
|
| 30 |
+
nature-terrain
|
| 31 |
+
sky
|
| 32 |
+
void-ground
|
| 33 |
+
void-dynamic
|
| 34 |
+
void-static
|
| 35 |
+
void-unclear
|
test.jpg
ADDED
|