|
import gradio as gr |
|
import os |
|
import torch |
|
|
|
from model import create_effnetb2_model |
|
from timeit import default_timer as timer |
|
|
|
|
|
with open("class_names.txt", 'r') as f: |
|
classes = [name.strip() for name in f] |
|
|
|
|
|
model, transform = create_effnetb2_model( |
|
num_classes=len(classes) |
|
) |
|
|
|
model.load_state_dict( |
|
torch.load( |
|
f="model_v3.pth", |
|
map_location=torch.device("cpu") |
|
) |
|
) |
|
|
|
|
|
def predict(img): |
|
|
|
start_time = timer() |
|
|
|
|
|
img = transform(img).unsqueeze(0) |
|
|
|
model.eval() |
|
with torch.inference_mode(): |
|
|
|
predictions = torch.softmax(model(img), dim=1) |
|
|
|
|
|
pred_labels_and_probs = {classes[i]: float(predictions[0][i]) for i in range(len(classes))} |
|
|
|
pred_time = round(timer() - start_time, 4) |
|
|
|
return pred_labels_and_probs, pred_time |
|
|
|
|
|
example_list = [['examples/cloudy.jpg'], |
|
['examples/dew.jpg'], |
|
['examples/fog.jpg'], |
|
['examples/frost.jpg'], |
|
['examples/hail.jpg'], |
|
['examples/lightning.jpg'], |
|
['examples/rain.jpg'], |
|
['examples/rainbow.jpeg'], |
|
['examples/shine.jpg'], |
|
['examples/snow.jpg'], |
|
['examples/sunrise.jpg'], |
|
['examples/tornado.jpg']] |
|
|
|
|
|
title = "Weather image classification ⛅❄☔" |
|
description = "Classifies the weather from an image, able to recognize 12 types of weather." |
|
|
|
demo = gr.Interface(fn=predict, |
|
inputs=gr.Image(type="pil"), |
|
outputs=[gr.Label(num_top_classes=1, label="Predictions"), |
|
gr.Number(label="Prediction time (s)")], |
|
examples=example_list, |
|
title=title, |
|
description=description) |
|
|
|
|
|
demo.launch(debug=False, |
|
share=False) |
|
|