File size: 1,955 Bytes
11edf28
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
afcdc7f
11edf28
 
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
61
62
63
64
65
66
67
68
69
70
71

from model import create_effnetb2
from typing import Tuple, Dict
from PIL import Image

from time import time
import torch
import torchvision
import gradio as gr
import os

from pathlib import Path

class_names = ["pizza", "steak", "sushi"]

effnetb2 , effnetb2_transforms = create_effnetb2()
# Load weights
PATH = "09_pretrained_effnetb2_feature_extractor_pizza20%_10epochs.pth"

effnetb2.load_state_dict(torch.load(f=PATH,
                                   map_location=torch.device('cpu')
                                   ))
effnetb2.eval()




def predict(img) ->Tuple[Dict, float]:
    start_time = time()

    img_tr = img
    img_tr = effnetb2_transforms(img_tr).unsqueeze(0)
    #predict
    effnetb2.eval()
    with torch.inference_mode():
     
        pred_prob = torch.softmax(effnetb2(img_tr), dim=1)
       

    pred_labesls_and_pobs ={class_names[i]:pred_prob[0][i] for i in range(len(class_names)) }
    
 


    end_time = time()
    pred_time =  round(end_time - start_time,4)
    return pred_labesls_and_pobs ,pred_time


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

# Create title, description and article strings
title = "FoodVision Classification"
description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi."
article = "Created at [Using pre-trained model efficientnet_b2](https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html)."

# Create the Gradio demo
demo = gr.Interface(fn=predict,
                    inputs=gr.Image(type="pil"),
                    outputs=[gr.Label(num_top_classes=3, label="Predictions"),
                             gr.Number(label="Prediction time (s)")], 
                    examples=example_list, 
                    title=title,
                    description=description,
                    article=article)

# Launch the demo!
demo.launch(debug=False)