caneca / app.py
Jotadebeese's picture
Update app.py
472b338 verified
Raw
History Blame Contribute Delete
3.56 kB
### 1. Imports and class names setup ###
import gradio as gr
import os
import torch
from model import create_effnetb2_model, create_vit_model
from timeit import default_timer as timer
from typing import Tuple, Dict
# Setup class names
class_names = ['cardboard', 'glass', 'metal', 'organic', 'paper', 'plastic', 'trash']
### 2. Model and transforms preparation ###
effnetb2, effnetb2_transforms = create_effnetb2_model(
num_classes=len(class_names),
)
vit, vit_transforms = create_vit_model(
num_classes=len(class_names),
)
# Load saved weights
effnetb2.load_state_dict(
torch.load(
f="effnetb2_augmented_dataset_10_epochs.pth",
map_location=torch.device("cpu")
)
)
vit.load_state_dict(
torch.load(
f="vit_b_16_augmented_dataset_10_epochs.pth",
map_location=torch.device("cpu")
)
)
### 3. Predict function ###
def predict(img, model_str: str) -> Tuple[Dict, float]:
# Start a timer
start_time = timer()
if model_str == "effnetb2":
# Transform the image
img = effnetb2_transforms(img).unsqueeze(0)
model = effnetb2
model.eval()
# Put model into eval mode, make prediction
with torch.inference_mode():
# Pass transformed image through the model and turn the prediction logits into probabilities
pred_probs = torch.softmax(model(img), dim=1)
# Create a prediciton label and prediction probability dictionary
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
# Calculate pred time
pred_time = round(timer() - start_time, 4)
# Return pred labels and pred time
return pred_labels_and_probs, pred_time
else:
# Transform the image
img = vit_transforms(img).unsqueeze(0)
model = vit
model.eval()
# Put model into eval mode, make prediction
with torch.inference_mode():
# Pass transformed image through the model and turn the prediction logits into probabilities
pred_probs = torch.softmax(model(img), dim=1)
# Create a prediciton label and prediction probability dictionary
pred_labels_and_probs = {class_names[i]: float(pred_probs[0][i]) for i in range(len(class_names))}
# Calculate pred time
pred_time = round(timer() - start_time, 4)
# Return pred labels and pred time
return pred_labels_and_probs, pred_time
### 4. Gradio app - Gradio interface + launch command ###
# Create title, description and article
title = "Rubbish Classifier 🗑️"
description = "An [EfficientNetb2 feature extractor](https://pytorch.org/vision/stable/models/generated/torchvision.models.efficientnet_b2.html#torchvision.models.efficientnet_b2) and a [ViT feature extractor](https://pytorch.org/vision/stable/models/generated/torchvision.models.vit_b_16.html#torchvision.models.vit_b_16) model to classify rubbish images."
article = "Created by me"
# Create example list
example_list = [["examples/" + example] for example in os.listdir("examples")]
# Create the Gradio demo
demo = gr.Interface(fn=predict,
inputs=[gr.Image(type="pil"),
gr.Dropdown(choices=['effnetb2', 'vit'], label='Model To Use', value='effnetb2')],
outputs=[gr.Label(num_top_classes=3, label="Predictions"),
gr.Number(label="Prediction time (s)")],
examples=example_list,
title=title,
description=description,
article=article)
# Launch the demo
demo.launch(debug=False, share=True)