Spaces:
Sleeping
Sleeping
Delete app.py
Browse files
app.py
DELETED
@@ -1,105 +0,0 @@
|
|
1 |
-
from huggingface_hub import from_pretrained_fastai
|
2 |
-
import gradio as gr
|
3 |
-
|
4 |
-
from fastai.vision.all import *
|
5 |
-
|
6 |
-
import torchvision.transforms as transforms
|
7 |
-
import torchvision.transforms as transforms
|
8 |
-
|
9 |
-
from fastai.basics import *
|
10 |
-
from fastai.vision import models
|
11 |
-
from fastai.vision.all import *
|
12 |
-
from fastai.metrics import *
|
13 |
-
from fastai.data.all import *
|
14 |
-
from fastai.callback import *
|
15 |
-
from pathlib import Path
|
16 |
-
|
17 |
-
import random
|
18 |
-
import PIL
|
19 |
-
|
20 |
-
#Definimos las funciones de transformacion que hemos creado en la practica para poder tratar los datos de entrada y que funcione bien
|
21 |
-
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
22 |
-
def transform_image(image):
|
23 |
-
my_transforms = transforms.Compose([transforms.ToTensor(),
|
24 |
-
transforms.Normalize(
|
25 |
-
[0.485, 0.456, 0.406],
|
26 |
-
[0.229, 0.224, 0.225])])
|
27 |
-
image_aux = image
|
28 |
-
return my_transforms(image_aux).unsqueeze(0).to(device)
|
29 |
-
|
30 |
-
class TargetMaskConvertTransform(ItemTransform):
|
31 |
-
def __init__(self):
|
32 |
-
pass
|
33 |
-
def encodes(self, x):
|
34 |
-
img,mask = x
|
35 |
-
|
36 |
-
#Convertimos a array
|
37 |
-
mask = np.array(mask)
|
38 |
-
|
39 |
-
mask[(mask!=255) & (mask!=150) & (mask!=76) & (mask!=74) & (mask!=29) & (mask!=25)]=0
|
40 |
-
mask[mask==255]=1
|
41 |
-
mask[mask==150]=2
|
42 |
-
mask[mask==76]=4
|
43 |
-
mask[mask==74]=4
|
44 |
-
mask[mask==29]=3
|
45 |
-
mask[mask==25]=3
|
46 |
-
|
47 |
-
# Back to PILMask
|
48 |
-
mask = PILMask.create(mask)
|
49 |
-
return img, mask
|
50 |
-
|
51 |
-
from albumentations import (
|
52 |
-
Compose,
|
53 |
-
OneOf,
|
54 |
-
ElasticTransform,
|
55 |
-
GridDistortion,
|
56 |
-
OpticalDistortion,
|
57 |
-
HorizontalFlip,
|
58 |
-
Rotate,
|
59 |
-
Transpose,
|
60 |
-
CLAHE,
|
61 |
-
ShiftScaleRotate
|
62 |
-
)
|
63 |
-
|
64 |
-
def get_y_fn (x):
|
65 |
-
return Path(str(x).replace("Images","Labels").replace("color","gt").replace(".jpg",".png"))
|
66 |
-
|
67 |
-
class SegmentationAlbumentationsTransform(ItemTransform):
|
68 |
-
split_idx = 0
|
69 |
-
|
70 |
-
def __init__(self, aug):
|
71 |
-
self.aug = aug
|
72 |
-
|
73 |
-
def encodes(self, x):
|
74 |
-
img,mask = x
|
75 |
-
aug = self.aug(image=np.array(img), mask=np.array(mask))
|
76 |
-
return PILImage.create(aug["image"]), PILMask.create(aug["mask"])
|
77 |
-
|
78 |
-
#Cargamos el modelo
|
79 |
-
repo_id = "luisvarona/Practica3"
|
80 |
-
learn = from_pretrained_fastai(repo_id)
|
81 |
-
model = learn.model
|
82 |
-
model = model.cpu()
|
83 |
-
|
84 |
-
|
85 |
-
# Funcion de predicción
|
86 |
-
def predict(img_ruta):
|
87 |
-
img = PIL.Image.fromarray(img_ruta)
|
88 |
-
image = transforms.Resize((480,640))(img)
|
89 |
-
tensor = transform_image(image=image)
|
90 |
-
model.to(device)
|
91 |
-
with torch.no_grad():
|
92 |
-
outputs = model(tensor)
|
93 |
-
|
94 |
-
outputs = torch.argmax(outputs,1)
|
95 |
-
mask = np.array(outputs.cpu())
|
96 |
-
mask[mask==1]=255
|
97 |
-
mask[mask==2]=150
|
98 |
-
mask[mask==3]=29
|
99 |
-
mask[mask==4]=74
|
100 |
-
mask = np.reshape(mask,(480,640))
|
101 |
-
return Image.fromarray(mask.astype('uint8'))
|
102 |
-
|
103 |
-
|
104 |
-
# Creamos la interfaz y la lanzamos.
|
105 |
-
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(480, 640)), outputs=gr.inputs.Image(shape=(480, 640)), examples=['color_184.jpg','color_189.jpg']).launch(share=False)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|