Update app.py
Browse files
app.py
CHANGED
|
@@ -1,41 +1,46 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastai.vision.all import *
|
|
|
|
|
|
|
|
|
|
| 3 |
import torchvision.transforms as transforms
|
| 4 |
-
|
| 5 |
-
|
| 6 |
-
import numpy as np
|
| 7 |
|
| 8 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 9 |
-
model = torch.jit.load("unet.pth")
|
|
|
|
| 10 |
model.eval()
|
| 11 |
|
| 12 |
def transform_image(image):
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
])
|
| 19 |
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
tensor = tensor_transforms(image).unsqueeze(0).to(device)
|
| 23 |
|
| 24 |
-
|
|
|
|
| 25 |
with torch.no_grad():
|
| 26 |
outputs = model(tensor)
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
mask = np.
|
| 32 |
-
mask
|
| 33 |
-
mask
|
| 34 |
-
mask
|
| 35 |
-
mask
|
| 36 |
-
|
| 37 |
-
|
|
|
|
| 38 |
return Image.fromarray(mask.astype('uint8'))
|
| 39 |
|
| 40 |
-
|
| 41 |
-
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from fastai.vision.all import *
|
| 3 |
+
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
import PIL
|
| 6 |
import torchvision.transforms as transforms
|
| 7 |
+
|
| 8 |
+
|
|
|
|
| 9 |
|
| 10 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 11 |
+
model = torch.jit.load("unet.pth")
|
| 12 |
+
model = model.cpu()
|
| 13 |
model.eval()
|
| 14 |
|
| 15 |
def transform_image(image):
|
| 16 |
+
my_transforms = transforms.Compose([transforms.ToTensor(),
|
| 17 |
+
transforms.Normalize(
|
| 18 |
+
[0.485, 0.456, 0.406],
|
| 19 |
+
[0.229, 0.224, 0.225])])
|
| 20 |
+
image_aux = image
|
|
|
|
| 21 |
|
| 22 |
+
image = transforms.Resize((480,640))(Image.fromarray(image))
|
| 23 |
+
tensor = my_transforms(image_aux).unsqueeze(0).to(device)
|
|
|
|
| 24 |
|
| 25 |
+
|
| 26 |
+
model.to(device)
|
| 27 |
with torch.no_grad():
|
| 28 |
outputs = model(tensor)
|
| 29 |
+
|
| 30 |
+
outputs = torch.argmax(outputs,1)
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
mask = np.array(outputs.cpu())
|
| 34 |
+
mask[mask==0]=255
|
| 35 |
+
mask[mask==1]=150
|
| 36 |
+
mask[mask==2]=76
|
| 37 |
+
mask[mask==3]=25
|
| 38 |
+
mask[mask==4]=0
|
| 39 |
+
|
| 40 |
+
mask=np.reshape(mask,(480,640))
|
| 41 |
return Image.fromarray(mask.astype('uint8'))
|
| 42 |
|
| 43 |
+
|
| 44 |
+
|
| 45 |
+
# Creamos la interfaz y la lanzamos.
|
| 46 |
+
gr.Interface(fn=transform_image, inputs=gr.inputs.Image(shape=(640, 480)), outputs=gr.outputs.Image(),examples=['color_188.jpg','color_189.jpg']).launch(share=False)
|