macapa commited on
Commit
921daef
·
1 Parent(s): c43e6af

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +45 -11
app.py CHANGED
@@ -1,17 +1,51 @@
1
- from huggingface_hub import from_pretrained_fastai
2
- import gradio as gr
3
  from fastai.vision.all import *
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4
 
5
- repo_id = "macapa/blindness-image-classification"
6
 
7
- learner = from_pretrained_fastai(repo_id)
8
- labels = learner.dls.vocab
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
- # Definimos una función que se encarga de llevar a cabo las predicciones
11
- def predict(img):
12
- #img = PILImage.create(img)
13
- pred,pred_idx,probs = learner.predict(img)
14
- return {labels[i]: float(probs[i]) for i in range(len(labels))}
15
 
16
  # Creamos la interfaz y la lanzamos.
17
- gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Label(num_top_classes=3),examples=['026dcd9af143.png','042470a92154.png']).launch(share=False)
 
 
 
1
  from fastai.vision.all import *
2
+ import gradio as gr
3
+ import torchvision.transforms as transforms
4
+ from pathlib import Path
5
+ import PIL
6
+ from huggingface_hub import from_pretrained_fastai
7
+
8
+ # Cargamos el learner
9
+
10
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
11
+ repo_id = "macapa/segmentation-mod"
12
+ model = from_pretrained_fastai(repo_id)
13
+ model = model.cpu()
14
+ model.eval()
15
+
16
 
17
+ def transform_image(image):
18
 
19
+ #mask = PILMask.create(Path(str(image).replace("Images","Labels").replace("color","gt").replace(".jpg",".png")))
20
+ #image = PIL.Image.open(image)
21
+ my_transforms = transforms.Compose([transforms.ToTensor(),
22
+ transforms.Normalize(
23
+ [0.485, 0.456, 0.406],
24
+ [0.229, 0.224, 0.225])])
25
+ image_aux = image
26
+ #my_transforms(image_aux).unsqueeze(0).to(device)
27
+ image = transforms.Resize((480,640))(Image.fromarray(image))
28
+ tensor = my_transforms(image_aux).unsqueeze(0).to(device)
29
+ #tensor = transform_image(image=image)
30
+
31
+
32
+
33
+ model.to(device)
34
+ with torch.no_grad():
35
+ outputs = model(tensor)
36
+
37
+ outputs = torch.argmax(outputs,1)
38
+
39
+ mask = np.array(outputs.cpu())
40
+ mask[mask==0]=255
41
+ mask[mask==1]=150
42
+ mask[mask==2]=76
43
+ mask[mask==3]=25
44
+ mask[mask==4]=0
45
+
46
+ mask=np.reshape(mask,(480,640))
47
+ return Image.fromarray(mask.astype('uint8'))
48
 
 
 
 
 
 
49
 
50
  # Creamos la interfaz y la lanzamos.
51
+ gr.Interface(fn=transform_image, inputs=gr.inputs.Image(shape=(640, 480)), outputs=gr.outputs.Image(),examples=['color_156.jpg','color_179.jpg']).launch(share=False)