alkzar90 commited on
Commit
bf601e4
1 Parent(s): a3cd6ce

create an image segmentation model

Browse files
Files changed (1) hide show
  1. app.py +38 -0
app.py ADDED
@@ -0,0 +1,38 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import torch
3
+ from transformers import (SegformerFeatureExtractor,
4
+ SegformerForSemanticSegmentation)
5
+ from PIL import Image
6
+
7
+
8
+ MODEL_PATH="./best_model_test/"
9
+
10
+ device = torch.device("cpu")
11
+
12
+ preprocessor = SegformerFeatureExtractor.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512")
13
+ model = SegformerForSemanticSegmentation.from_pretrained(MODEL_PATH)
14
+ model.eval()
15
+
16
+
17
+ def query_image(img):
18
+ """Función para generar predicciones a la escala origina"""
19
+ inputs = preprocessor(images=img, return_tensors="pt")
20
+ with torch.no_grad():
21
+ preds = model(inputs.unsqueeze(0).to(device))["logits"]
22
+ #preds = model(image.unsqueeze(0).to(device))["logits"]
23
+ preds_upscale = upscale_logits_modified(preds, image.shape[2])
24
+ predict_label = torch.argmax(preds_upscale, dim=1).to(device)
25
+ return predict_label[0,:,:].detach().cpu().numpy()
26
+
27
+
28
+ def visualize_instance_seg_mask(mask):
29
+ return mask
30
+
31
+ demo = gr.Interface(
32
+ query_image,
33
+ inputs=[gr.Image()],
34
+ outputs="image",
35
+ title="SegFormer Model for rock glacier image segmentation"
36
+ )
37
+
38
+ demo.launch()