adiren7/FoodVisionVIT
0
1 2import gradio as gr3import os4import torch5 6from model import create_vit_model7from timeit import default_timer as timer8 9class_names = ["pizza", "steak", "sushi"]10 11vit , vit_transforms = create_vit_model()12 13vit.load_state_dict(torch.load(f="09_pretrained_vit_feature_extractor_pizza_steak_sushi_20_percent.pth",14 map_location=torch.device("cpu")))15 16def predict(img):17 18 img_tranformed = vit_transforms(img).unsqueeze(0)19 20 start_time = timer()21 vit.eval()22 with torch.inference_mode():23 y_pred = vit(img_tranformed)24 25 pred_time = round(timer() - start_time , 4)26 y_proba = torch.softmax(y_pred , dim =1)27 28 pred_dict = { class_names[i]:j for i, j in enumerate( y_proba[0]) }29 30 return pred_dict , pred_time31 32 33title = "FoodVision Mini ๐๐ฅฉ๐ฃ"34description = "An VITfeature extractor computer vision model to classify images of food as pizza, steak or sushi."35article = "Created at [PyTorch Model Deployment]."36 37# Create examples list from "examples/" directory38example_list = [["examples/" + example] for example in os.listdir("examples")]39 40# Create the Gradio demo41demo = gr.Interface(fn=predict, # mapping function from input to output42 inputs=gr.Image(type="pil"), # what are the inputs?43 outputs=[gr.Label(num_top_classes=3, label="Predictions"), # what are the outputs?44 gr.Number(label="Prediction time (s)")], # our fn has two outputs, therefore we have two outputs45 examples=example_list,46 title=title,47 description=description,48 article=article)49 50# Launch the demo!51demo.launch()52 