Create app.py
Browse files
app.py
ADDED
@@ -0,0 +1,39 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from huggingface_hub import from_pretrained_fastai
|
2 |
+
import gradio as gr
|
3 |
+
from fastai.vision.all import *
|
4 |
+
import PIL
|
5 |
+
import torchvision.transforms as transforms
|
6 |
+
|
7 |
+
repo_id = "paascorb/practica3_Segmentation"
|
8 |
+
|
9 |
+
learner = from_pretrained_fastai(repo_id)
|
10 |
+
|
11 |
+
def transform_image(image):
|
12 |
+
my_transforms = transforms.Compose([transforms.ToTensor(),
|
13 |
+
transforms.Normalize(
|
14 |
+
[0.485, 0.456, 0.406],
|
15 |
+
[0.229, 0.224, 0.225])])
|
16 |
+
image_aux = image
|
17 |
+
return my_transforms(image_aux).unsqueeze(0).to(device)
|
18 |
+
|
19 |
+
def predict(img):
|
20 |
+
img = PIL.Image.fromarray(img, "RGB")
|
21 |
+
image = transforms.Resize((480,640))(img)
|
22 |
+
tensor = transform_image(image=image)
|
23 |
+
|
24 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
25 |
+
learner.to(device)
|
26 |
+
with torch.no_grad():
|
27 |
+
outputs = learner(tensor)
|
28 |
+
|
29 |
+
outputs = torch.argmax(outputs,1)
|
30 |
+
mask = np.array(outputs.cpu())
|
31 |
+
mask[mask==1]=255
|
32 |
+
mask[mask==2]=150
|
33 |
+
mask[mask==3]=76
|
34 |
+
mask[mask==4]=29
|
35 |
+
mask=np.reshape(mask,(480,640))
|
36 |
+
return Image.fromarray(mask.astype('uint8'))
|
37 |
+
|
38 |
+
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=outputs=[gr.outputs.Image(type="pil", label="VFNet Inference")],
|
39 |
+
examples=['color_155.jpg','color_154.jpg']).launch(share=False)
|