Spaces:
Runtime error
Runtime error
Commit
·
77d99f4
1
Parent(s):
fe51f0e
Colab update - fix classify
Browse files- app.py +36 -0
- ex1.jpg +0 -0
- ex2.jpg +0 -0
- ex3.jpg +0 -0
- requirements.txt +2 -0
app.py
ADDED
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
+
import gradio as gr
|
3 |
+
import torch
|
4 |
+
from torch import nn
|
5 |
+
from transformers import SegformerForSemanticSegmentation, SegformerFeatureExtractor
|
6 |
+
|
7 |
+
|
8 |
+
# This should be the same as the first line of Python code in this Colab notebook
|
9 |
+
extractor = SegformerFeatureExtractor.from_pretrained("neerajprad/street-segmentation-demo")
|
10 |
+
model = SegformerForSemanticSegmentation.from_pretrained("neerajprad/street-segmentation-demo")
|
11 |
+
|
12 |
+
def classify(im):
|
13 |
+
inputs = extractor(images=im, return_tensors="pt")
|
14 |
+
outputs = model(**inputs)
|
15 |
+
logits = outputs.logits
|
16 |
+
# Reshape output to be the same size as input
|
17 |
+
logits_tensor = nn.functional.interpolate(
|
18 |
+
logits,
|
19 |
+
size=im.shape[:2],
|
20 |
+
mode="bilinear",
|
21 |
+
align_corners=False,
|
22 |
+
)
|
23 |
+
classes = logits_tensor[0].detach().cpu().numpy().argmax(axis=0)
|
24 |
+
colors = np.array([[128,0,0], [128,128,0], [0, 0, 128], [128,0,128], [0, 0, 0]])
|
25 |
+
return colors[classes]
|
26 |
+
|
27 |
+
interface = gr.Interface(
|
28 |
+
title="Road segmentation",
|
29 |
+
description="Segment the scene into road/sidewalk, human, vehicles, other obj, nature/background",
|
30 |
+
examples=["ex1.jpg", "ex2.jpg", "ex3.jpg"],
|
31 |
+
fn=classify,
|
32 |
+
inputs=gr.Image(),
|
33 |
+
outputs=gr.Image(),
|
34 |
+
)
|
35 |
+
|
36 |
+
interface.launch(debug=True)
|
ex1.jpg
ADDED
![]() |
ex2.jpg
ADDED
![]() |
ex3.jpg
ADDED
![]() |
requirements.txt
ADDED
@@ -0,0 +1,2 @@
|
|
|
|
|
|
|
1 |
+
torch
|
2 |
+
transformers
|