### 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 EfficientNetB2 feature extractor computer vision model to classify 101 food images from Food101 dataset
Current model has been trained with whole Food101 dataset, ~66% test accuracy
I have yet to improve it to label non-food images. Paciencia muchachos" article = "Created at 09_pytorch_model_deploy.ipynb 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***