Spaces:
Sleeping
Sleeping
| import torch | |
| from typing import Tuple, Dict | |
| from timeit import default_timer as timer | |
| import os | |
| import gradio as gr | |
| from model import create_effnetb2 | |
| with open("class_names.txt", "r") as f: | |
| classes = [food.strip() for food in f.readlines()] | |
| effnetb2, effnetb2_transform = create_effnetb2(len(classes), 42) | |
| effnetb2.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor_food101_20_percent.pth", map_location=torch.device("cpu"))) | |
| def predict(img) -> Tuple[Dict, float]: | |
| start_time = timer() | |
| img = effnetb2_transform(img).unsqueeze(0) | |
| effnetb2.eval() | |
| with torch.inference_mode(): | |
| probs = torch.softmax(effnetb2(img), dim=1) | |
| pred_label_and_probs = {classes[i]: float(probs[0][i]) for i in range(len(classes))} | |
| pred_time = round(timer() - start_time, 5) | |
| return pred_label_and_probs, pred_time | |
| example_list = [["examples/" + example] for example in os.listdir('examples')] | |
| title = "FoodVision Big" | |
| description = "An EffNetB2 feature extractor that characterizes images of food into one of 101 different classes" | |
| article = "Created through Pytorch Zero To Hero" | |
| 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) | |
| demo.launch() | |