File size: 1,786 Bytes
9342e63
 
 
17e4bee
9342e63
 
 
 
 
 
 
 
 
 
 
 
 
 
8dbb475
9342e63
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8497b67
dc7ca93
9342e63
 
 
dc7ca93
 
9342e63
 
e82d8f6
9342e63
 
 
e82d8f6
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
import gradio as gr
import os
import torch

from model import create_effnetb2_model
from timeit import default_timer as timer

# Setup class names
with open("class_names.txt", 'r') as f:
  classes = [name.strip() for name in f]

# Model and transforms
model, transform = create_effnetb2_model(
    num_classes=len(classes)
)

model.load_state_dict(
    torch.load(
        f="model_v3.pth",
        map_location=torch.device("cpu")
    )
)

# Predict function
def predict(img):
 
    start_time = timer()
    
    # Transform the target image and add a batch dimension
    img = transform(img).unsqueeze(0)
    
    model.eval()
    with torch.inference_mode():
        
        predictions = torch.softmax(model(img), dim=1)
    
    # Create a prediction label and prediction probability dictionary for each prediction class (this is the required format for Gradio)
    pred_labels_and_probs = {classes[i]: float(predictions[0][i]) for i in range(len(classes))}
    
    pred_time = round(timer() - start_time, 4)
    
    return pred_labels_and_probs, pred_time

example_list = [["examples/" + example] for example in os.listdir("examples")]

# Gradio interface
title = "Clasificador de clima a partir de imágenes (Finetuneado)"
description = "Clasificador de imágenes https://docs.google.com/presentation/d/1V2R1CIK8Iav3Hf8RIoWiypo6xGJg4l_w2TCY06QCoLM/edit?usp=sharing"

demo = gr.Interface(fn=predict,
                    inputs=gr.Image(type="pil"),
                    outputs=[gr.Label(num_top_classes=1, label="Predicciones"), 
                             gr.Number(label="Tiempo de Predicción(s)")],
                    examples=example_list,
                    title=title,
                    description=description)


demo.launch(debug=False,
            share=False)