Spaces:
Paused
Paused
| import gradio as gr | |
| import os | |
| import torch | |
| from model import create_effnetb2_model | |
| from typing import Tuple, Dict | |
| from timeit import default_timer as timer | |
| class_names = ["pizza", "steak", "sushi"] | |
| effnetb2, effnetb2_transform = create_effnetb2_model(num_classes = len(class_names)) | |
| effnetb2.load_state_dict(torch.load(f="09_pretrained_effnetb2_feature_extractor_pizza_steak_sushi_20_percent.pth", | |
| map_location = torch.device("cpu"))) | |
| def predict(img) -> Tuple[Dict, float]: | |
| start = timer() | |
| effnetb2.eval() | |
| with torch.inference_mode(): | |
| img = effnetb2_transform(img) | |
| img = img.unsqueeze(dim=0) | |
| pred = effnetb2(img) | |
| pred_probs = torch.softmax(pred, dim=1) | |
| class_label = torch.argmax(pred_probs, dim=1) | |
| pred_dict = {class_names[i]: float(pred_probs[0][i].cpu().item()) for i in range(len(class_names))} | |
| end = timer() | |
| pred_time = round(end-start, 5) | |
| return pred_dict, pred_time | |
| title = "FoodVision Mini ππ₯©π£" | |
| description = "An EfficientNetB2 feature extractor computer vision model to classify images of food as pizza, steak or sushi" | |
| example_list = [["examples/"+example] for example in os.listdir("examples")] | |
| 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="") | |
| demo.launch(debug=False, | |
| share=True) | |