Spaces:
Build error
Build error
Create app.py
Browse files
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 |
+
#extractor = AutoFeatureExtractor.from_pretrained("andresgtn/segformer-b0-finetuned-ade-64-64-finetuned-semantic-sidewalk")
|
8 |
+
extractor = SegformerFeatureExtractor()
|
9 |
+
model = SegformerForSemanticSegmentation.from_pretrained("andresgtn/segformer-b0-finetuned-ade-64-64-finetuned-semantic-sidewalk")
|
10 |
+
|
11 |
+
def rescale_output_image(logits, image):
|
12 |
+
|
13 |
+
upsampled_logits = nn.functional.interpolate(
|
14 |
+
logits,
|
15 |
+
size=image.shape[::-1][1:][::-1], # (height, width)
|
16 |
+
mode='bilinear',
|
17 |
+
align_corners=False
|
18 |
+
)
|
19 |
+
pred_seg = upsampled_logits.argmax(dim=1)[0]
|
20 |
+
return pred_seg
|
21 |
+
|
22 |
+
# classify function
|
23 |
+
def classify(im):
|
24 |
+
inputs = extractor(images=im, return_tensors="pt")#.to("cuda")
|
25 |
+
outputs = model(**inputs)
|
26 |
+
logits = outputs.logits
|
27 |
+
#classes = logits[0].detach().cpu().numpy().argmax(axis=0)
|
28 |
+
#classes = rescale_output_image(logits, im).detach().cpu().numpy()
|
29 |
+
classes = rescale_output_image(logits, im).detach().numpy()
|
30 |
+
colors = np.array([[128,0,0], [128,128,0], [0, 0, 128], [128,0,128], [0, 0, 0]])
|
31 |
+
return colors[classes]
|
32 |
+
|
33 |
+
# define gradio interface
|
34 |
+
interface = gr.Interface(classify, gr.Image(), 'image')# FILL HERE
|
35 |
+
|
36 |
+
interface.launch(debug=True)
|