File size: 933 Bytes
4030c59
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
from transformers import AutoFeatureExtractor, SegformerForSemanticSegmentation
import gradio as gr
import torch

extractor = AutoFeatureExtractor.from_pretrained("salaz055/my_extractor_segmentation_model")
model = SegformerForSemanticSegmentation.from_pretrained("salaz055/my-segmentation-model")

def classify(im):
  inputs = extractor(images=im, return_tensors="pt").to("cuda")
  outputs = model(**inputs)
  logits = outputs.logits
  classes = logits[0].detach().cpu().numpy().argmax(axis=0)
  colors = np.array([[128,0,0], [128,128,0], [0, 0, 128], 	[128,0,128], [0, 0, 0]])
  return colors[classes]
  
interface = gr.Interface(fn = classify,
                         inputs = gr.Image(type = 'pil'),
                         outputs = 'image',
                         title = "Image Segmentation",
                         description = "Use for semantic image segmentation. Finetuned on sidewalk-semantic") 
interface.launch()