Spaces:
Runtime error
Runtime error
JinHyeong99
commited on
Commit
ยท
f9b645e
1
Parent(s):
6cf7c67
app.py
CHANGED
|
@@ -1,111 +1,36 @@
|
|
| 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
|
| 8 |
-
from transformers import SegformerFeatureExtractor, TFSegformerForSemanticSegmentation
|
| 9 |
-
|
| 10 |
-
feature_extractor = SegformerFeatureExtractor.from_pretrained(
|
| 11 |
-
"nvidia/segformer-b5-finetuned-cityscapes-1024-1024"
|
| 12 |
-
)
|
| 13 |
-
model = TFSegformerForSemanticSegmentation.from_pretrained(
|
| 14 |
-
"nvidia/segformer-b5-finetuned-cityscapes-1024-1024"
|
| 15 |
-
)
|
| 16 |
-
|
| 17 |
-
def ade_palette():
|
| 18 |
-
"""ADE20K palette that maps each class to RGB values."""
|
| 19 |
-
return [
|
| 20 |
-
[255,0,0], #๋นจ๊ฐ
|
| 21 |
-
[255,228,0], #๋
ธ๋
|
| 22 |
-
[171,242,0], # ์ฐ๋
|
| 23 |
-
[0,216,255], #ํ๋
|
| 24 |
-
[0,0,255], #ํ๋
|
| 25 |
-
[255,0,221], #ํํฌ
|
| 26 |
-
[116,116,116], #ํ์
|
| 27 |
-
[95,0,255], #๋ณด๋ผ
|
| 28 |
-
[255,94,0], #์ฃผํฉ
|
| 29 |
-
[71,200,62], #์ด๋ก
|
| 30 |
-
[153,0,76], #๋ง์ ํ
|
| 31 |
-
[67,116,217], #์ ๋งคํํ๋ + ํ๋
|
| 32 |
-
[153,112,0], #๊ฒจ์
|
| 33 |
-
[87,129,0], #๋
น์
|
| 34 |
-
[255,169,169], #๋ถํ๋ถํ
|
| 35 |
-
[35,30,183], #์ด๋์ด ํ๋
|
| 36 |
-
[225,186,133], #์ด์
|
| 37 |
-
[206,251,201], #์ฐํ์ด๋ก
|
| 38 |
-
[165,102,255] #์ ๋งคํ ๋ณด๋ผ
|
| 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 |
-
|
| 79 |
-
|
|
|
|
| 80 |
|
| 81 |
-
|
|
|
|
|
|
|
| 82 |
outputs = model(**inputs)
|
| 83 |
logits = outputs.logits
|
| 84 |
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
return fig
|
| 103 |
-
|
| 104 |
-
demo = gr.Interface(fn=sepia,
|
| 105 |
-
inputs=gr.Image(shape=(400, 600)),
|
| 106 |
-
outputs=['plot'],
|
| 107 |
-
examples=["image1", "image2", "image3"],
|
| 108 |
-
allow_flagging='never')
|
| 109 |
-
|
| 110 |
|
| 111 |
-
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from transformers import SegformerFeatureExtractor, SegformerForSemanticSegmentation
|
|
|
|
|
|
|
|
|
|
| 3 |
from PIL import Image
|
| 4 |
+
import numpy as np
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 5 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 6 |
|
| 7 |
+
# ๋ชจ๋ธ๊ณผ ํน์ง ์ถ์ถ๊ธฐ ๋ถ๋ฌ์ค๊ธฐ
|
| 8 |
+
feature_extractor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
| 9 |
+
model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b5-finetuned-cityscapes-1024-1024")
|
| 10 |
|
| 11 |
+
def segment_image(image):
|
| 12 |
+
# ์ด๋ฏธ์ง๋ฅผ ์ฒ๋ฆฌํ๊ณ ๋ชจ๋ธ์ ์ ๋ฌํ๊ธฐ
|
| 13 |
+
inputs = feature_extractor(images=image, return_tensors="pt")
|
| 14 |
outputs = model(**inputs)
|
| 15 |
logits = outputs.logits
|
| 16 |
|
| 17 |
+
# ๊ฒฐ๊ณผ ์ฒ๋ฆฌ ๋ฐ ์ด๋ฏธ์ง๋ก ๋ณํ
|
| 18 |
+
result = logits.argmax(dim=1)[0]
|
| 19 |
+
result = result.cpu().detach().numpy()
|
| 20 |
+
result_image = Image.fromarray(result.astype(np.uint8), mode="P")
|
| 21 |
+
|
| 22 |
+
# ๊ฒฐ๊ณผ ์ด๋ฏธ์ง ๋ฐํ
|
| 23 |
+
return result_image
|
| 24 |
+
|
| 25 |
+
# Gradio ์ธํฐํ์ด์ค ์ ์
|
| 26 |
+
iface = gr.Interface(
|
| 27 |
+
fn=segment_image,
|
| 28 |
+
inputs=gr.inputs.Image(type="pil"),
|
| 29 |
+
examples = ['image1.jpg', 'image2.jpg', 'image3.jpg'],
|
| 30 |
+
outputs=['plot'],
|
| 31 |
+
title="SegFormer Image Segmentation",
|
| 32 |
+
description="Upload an image to segment it using the SegFormer model trained on Cityscapes dataset."
|
| 33 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 34 |
|
| 35 |
+
# ์ธํฐํ์ด์ค ์คํ
|
| 36 |
+
iface.launch()
|