Update app.py
Browse files
app.py
CHANGED
@@ -1,18 +1,61 @@
|
|
1 |
from huggingface_hub import from_pretrained_fastai
|
2 |
import gradio as gr
|
|
|
|
|
3 |
from fastai.vision.all import *
|
|
|
|
|
|
|
|
|
|
|
4 |
|
5 |
-
|
6 |
-
|
|
|
7 |
|
8 |
-
|
9 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
10 |
|
11 |
-
# Definimos una función que se encarga de llevar a cabo las predicciones
|
12 |
def predict(img):
|
13 |
-
|
14 |
-
|
15 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
16 |
|
17 |
# Creamos la interfaz y la lanzamos.
|
18 |
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Label(num_top_classes=3),examples=['color_154.jpg','color_155.jpg']).launch(share=False)
|
|
|
1 |
from huggingface_hub import from_pretrained_fastai
|
2 |
import gradio as gr
|
3 |
+
from fastai.basics import *
|
4 |
+
from fastai.vision import models
|
5 |
from fastai.vision.all import *
|
6 |
+
from fastai.metrics import *
|
7 |
+
from fastai.data.all import *
|
8 |
+
from fastai.callback import *
|
9 |
+
import PIL
|
10 |
+
import torchvision.transforms as transforms
|
11 |
|
12 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
13 |
+
model = torch.jit.load("pract3.pth")
|
14 |
+
model = model.cpu()
|
15 |
|
16 |
+
def transform_image(image):
|
17 |
+
|
18 |
+
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
19 |
+
|
20 |
+
my_transforms = transforms.Compose([transforms.ToTensor(),
|
21 |
+
transforms.Normalize(
|
22 |
+
[0.485, 0.456, 0.406],
|
23 |
+
[0.229, 0.224, 0.225])])
|
24 |
+
image_aux = image
|
25 |
+
|
26 |
+
return my_transforms(image_aux).unsqueeze(0).to(device)
|
27 |
|
|
|
28 |
def predict(img):
|
29 |
+
img = PIL.Image.fromarray(img, "RGB")
|
30 |
+
image = transforms.Resize((480,640))(img)
|
31 |
+
tensor = transform_image(image=image)
|
32 |
+
|
33 |
+
model.to(device)
|
34 |
+
with torch.no_grad():
|
35 |
+
outputs = model(tensor)
|
36 |
+
|
37 |
+
outputs = torch.argmax(outputs,1)
|
38 |
+
mask = np.array(outputs.cpu())
|
39 |
+
mask[mask==1]=255
|
40 |
+
mask[mask==2]=150
|
41 |
+
mask[mask==3]=76
|
42 |
+
mask[mask==4]=29
|
43 |
+
mask=np.reshape(mask,(480,640))
|
44 |
+
return Image.fromarray(mask.astype('uint8'))
|
45 |
+
|
46 |
+
# repo_id = "YOUR_USERNAME/YOUR_LEARNER_NAME"
|
47 |
+
# repo_id = "Alesteba/deep_model_03"
|
48 |
+
|
49 |
+
# learner = from_pretrained_fastai(repo_id)
|
50 |
+
# labels = learner.dls.vocab
|
51 |
+
|
52 |
+
|
53 |
+
|
54 |
+
# # Definimos una función que se encarga de llevar a cabo las predicciones
|
55 |
+
# def predict(img):
|
56 |
+
# #img = PILImage.create(img)
|
57 |
+
# pred,pred_idx,probs = learner.predict(img)
|
58 |
+
# return {labels[i]: float(probs[i]) for i in range(len(labels))}
|
59 |
|
60 |
# Creamos la interfaz y la lanzamos.
|
61 |
gr.Interface(fn=predict, inputs=gr.inputs.Image(shape=(128, 128)), outputs=gr.outputs.Label(num_top_classes=3),examples=['color_154.jpg','color_155.jpg']).launch(share=False)
|