|
|
| |
|
|
|
|
| import torch |
| from timeit import default_timer as timer |
| from typing import List,Dict |
| import gradio as gr |
| from model import vit_model |
| import os |
|
|
|
|
| |
|
|
| class_names = ['pizza', 'steak', 'sushi'] |
|
|
| |
|
|
|
|
| vit, vit_transform = vit_model(num_classes =3) |
|
|
| |
|
|
| vit.load_state_dict( |
| torch.load(f="pretrained_vit_model.pth", |
| map_location = "cpu"), |
| ) |
|
|
| |
|
|
| def predict(img): |
|
|
| |
| start_time = timer() |
|
|
| |
|
|
| transformed_img = vit_transform(img).unsqueeze(0) |
|
|
| |
|
|
| vit.eval() |
| with torch.inference_mode(): |
| pred_logit = vit(transformed_img) |
|
|
| pred = torch.softmax(pred_logit,dim=1) |
|
|
| y_pred = torch.argmax(pred,dim=1) |
|
|
| class_label = class_names[y_pred] |
| |
|
|
|
|
| pred_labels_and_probs = {class_names[i]: float(pred[0][i]) for i in range(len(class_names))} |
|
|
|
|
|
|
| |
| end_time = timer() |
|
|
| Total_predtime = end_time-start_time |
|
|
|
|
| return (pred_labels_and_probs,Total_predtime) |
|
|
|
|
| |
|
|
| |
| example_list = [["examples/" + example] for example in os.listdir("examples")] |
| example_list |
|
|
| |
|
|
|
|
|
|
|
|
| title = "Test vision 1" |
| description = "A basic Vit model to identify foods" |
| article = "Created for testing" |
|
|
|
|
| demo = gr.Interface(fn =predict, |
| inputs = gr.Image(type ="pil"), |
| outputs = [gr.Label(num_top_classes=3,label ="Prediction"), |
| gr.Number(label = "Prediction Time (s)")], |
| examples = example_list, |
| title= title, |
| article =article, |
| description = description, |
| ) |
|
|
| demo.launch(debug= False) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|