File size: 3,064 Bytes
824c00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
de0d79c
824c00e
 
 
 
 
 
aff9341
824c00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f7d686d
824c00e
 
5011949
 
824c00e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
72
73
74
75
76
77
78
79
80
81
82

### 1. Import and class names setup ###
import gradio as gr
import os
from pathlib import Path
import torch

from model import create_effnetb2_model
from time import perf_counter
from typing import Tuple, Dict

from PIL import Image
import torchvision

# Setup class names (hardcoded, these shall reside in a json file or sth like that...)
# Open Food101 class names fromt file and import it to list

with open("class_names.txt","r") as f:
  class_names = f.read().split("\n")

### 2. Model and transforms preparation ###
effnetb2_model, effnetb2_transforms = create_effnetb2_model(num_classes=len(class_names))

# Load save weights
effnetb2_model.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor_food101_FULL.pth",
                                          map_location=torch.device("cpu"))) # map location to cpu is a must, as we have trained our model in the GPU

### 3. Predict function

def predict(img) -> Tuple[Dict,float]:
  # Start a timer
  start_time = perf_counter()

  # Transform the input image for use with EffNetB2
  effnetb2_transforms = torchvision.models.EfficientNet_B2_Weights.DEFAULT.transforms()
  img_tensor = effnetb2_transforms(img)

  # Put model in eval and inference
  effnetb2_model.eval()
  with torch.inference_mode():
    y_logits = effnetb2_model(img_tensor.unsqueeze(dim=0))
    y_pred_probs = torch.softmax(y_logits,dim=1)
    y_pred_probs_list = y_pred_probs.squeeze().tolist()

  # Creatae a prediction probability dictionary
  pred_prob_dict = {class_names[i]:float(prob) for i,prob in enumerate(y_pred_probs_list)}

  # End timer
  end_time = perf_counter()

  return pred_prob_dict, round(end_time-start_time,4)


### 4. Launch app

import gradio as gr

foodvision_big_examples_path = "examples"

example_list = [str(path) for path in Path(foodvision_big_examples_path).rglob("*.jpeg")]

# Create title, description and article
title = "FoodVisionBig V1.0 🥘 🧗"
description = "An <a href='https://pytorch.org/vision/main/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2'>EfficientNetB2</a> feature extractor computer vision model to classify 101 food images from Food101 dataset<br>Current model has been trained with whole Food101 dataset, ~66% test accuracy<br>I have yet to improve it to label non-food images. Paciencia muchachos"
article = "Created at <a href='#'>09_pytorch_model_deploy.ipynb</a> Google Colab notebook"

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

# Launch the demo
demo.launch()


# *** IMPORTANTE: The Flag button of the interface will create a folder named "flagged" that will contain the images and predictions of those images that someone has Flagged***