Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,16 @@
|
|
| 1 |
import numpy as np
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
-
from transformers import
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
|
| 8 |
# ---------------------------
|
| 9 |
-
# Load model &
|
| 10 |
# ---------------------------
|
| 11 |
model_checkpoint = "apple/deeplabv3-mobilevit-small"
|
| 12 |
-
|
|
|
|
| 13 |
model = MobileViTForSemanticSegmentation.from_pretrained(model_checkpoint).eval()
|
| 14 |
|
| 15 |
palette = np.array(
|
|
@@ -39,27 +40,30 @@ def predict(image):
|
|
| 39 |
return None, None
|
| 40 |
|
| 41 |
with torch.no_grad():
|
| 42 |
-
inputs =
|
| 43 |
outputs = model(**inputs)
|
| 44 |
|
| 45 |
-
#
|
| 46 |
resized = (
|
| 47 |
-
inputs["pixel_values"]
|
|
|
|
|
|
|
|
|
|
| 48 |
).astype(np.uint8)
|
| 49 |
|
| 50 |
-
#
|
| 51 |
classes = outputs.logits.argmax(1).squeeze().cpu().numpy().astype(np.uint8)
|
| 52 |
|
| 53 |
-
# Vectorized coloring
|
| 54 |
colored = palette[classes]
|
| 55 |
|
| 56 |
-
# Resize
|
| 57 |
colored_img = Image.fromarray(colored).resize(
|
| 58 |
(resized.shape[1], resized.shape[0]),
|
| 59 |
resample=Image.Resampling.NEAREST
|
| 60 |
)
|
| 61 |
|
| 62 |
-
#
|
| 63 |
mask = (classes != 0).astype(np.uint8) * 255
|
| 64 |
mask_img = Image.fromarray(mask).resize(
|
| 65 |
(resized.shape[1], resized.shape[0]),
|
|
@@ -73,9 +77,10 @@ def predict(image):
|
|
| 73 |
|
| 74 |
|
| 75 |
# ---------------------------
|
| 76 |
-
#
|
| 77 |
# ---------------------------
|
| 78 |
-
inverted = {0,1,4,5,8,9,12,13,16,17,20}
|
|
|
|
| 79 |
labels_html = " ".join(
|
| 80 |
f"<span style='background-color: rgb{tuple(palette[i])}; "
|
| 81 |
f"color: {'white' if i in inverted else 'black'}; padding: 2px 4px;'>"
|
|
@@ -100,7 +105,7 @@ article = """
|
|
| 100 |
|
| 101 |
|
| 102 |
# ---------------------------
|
| 103 |
-
#
|
| 104 |
# ---------------------------
|
| 105 |
with gr.Blocks(title="Semantic Segmentation with MobileViT") as demo:
|
| 106 |
gr.Markdown("# Semantic Segmentation with MobileViT & DeepLabV3")
|
|
|
|
| 1 |
import numpy as np
|
| 2 |
from PIL import Image
|
| 3 |
import torch
|
| 4 |
+
from transformers import AutoImageProcessor, MobileViTForSemanticSegmentation
|
| 5 |
import gradio as gr
|
| 6 |
|
| 7 |
|
| 8 |
# ---------------------------
|
| 9 |
+
# Load model & processor
|
| 10 |
# ---------------------------
|
| 11 |
model_checkpoint = "apple/deeplabv3-mobilevit-small"
|
| 12 |
+
|
| 13 |
+
image_processor = AutoImageProcessor.from_pretrained(model_checkpoint)
|
| 14 |
model = MobileViTForSemanticSegmentation.from_pretrained(model_checkpoint).eval()
|
| 15 |
|
| 16 |
palette = np.array(
|
|
|
|
| 40 |
return None, None
|
| 41 |
|
| 42 |
with torch.no_grad():
|
| 43 |
+
inputs = image_processor(image, return_tensors="pt")
|
| 44 |
outputs = model(**inputs)
|
| 45 |
|
| 46 |
+
# Re-normalize back to uint8
|
| 47 |
resized = (
|
| 48 |
+
inputs["pixel_values"]
|
| 49 |
+
.numpy()
|
| 50 |
+
.squeeze()
|
| 51 |
+
.transpose(1, 2, 0)[..., ::-1] * 255
|
| 52 |
).astype(np.uint8)
|
| 53 |
|
| 54 |
+
# Class map
|
| 55 |
classes = outputs.logits.argmax(1).squeeze().cpu().numpy().astype(np.uint8)
|
| 56 |
|
| 57 |
+
# Vectorized lookup table coloring
|
| 58 |
colored = palette[classes]
|
| 59 |
|
| 60 |
+
# Resize segmentation to match resized input
|
| 61 |
colored_img = Image.fromarray(colored).resize(
|
| 62 |
(resized.shape[1], resized.shape[0]),
|
| 63 |
resample=Image.Resampling.NEAREST
|
| 64 |
)
|
| 65 |
|
| 66 |
+
# Binary mask for overlay
|
| 67 |
mask = (classes != 0).astype(np.uint8) * 255
|
| 68 |
mask_img = Image.fromarray(mask).resize(
|
| 69 |
(resized.shape[1], resized.shape[0]),
|
|
|
|
| 77 |
|
| 78 |
|
| 79 |
# ---------------------------
|
| 80 |
+
# Labels HTML
|
| 81 |
# ---------------------------
|
| 82 |
+
inverted = {0, 1, 4, 5, 8, 9, 12, 13, 16, 17, 20}
|
| 83 |
+
|
| 84 |
labels_html = " ".join(
|
| 85 |
f"<span style='background-color: rgb{tuple(palette[i])}; "
|
| 86 |
f"color: {'white' if i in inverted else 'black'}; padding: 2px 4px;'>"
|
|
|
|
| 105 |
|
| 106 |
|
| 107 |
# ---------------------------
|
| 108 |
+
# Gradio App (Blocks)
|
| 109 |
# ---------------------------
|
| 110 |
with gr.Blocks(title="Semantic Segmentation with MobileViT") as demo:
|
| 111 |
gr.Markdown("# Semantic Segmentation with MobileViT & DeepLabV3")
|