| import gradio as gr |
| import numpy as np |
| import torch |
|
|
| learn_inf = torch.jit.load("checkpoints/transfer_exported.pt") |
|
|
| def classify(img): |
| |
| |
| timg = torch.from_numpy(img.transpose((2,0,1))).unsqueeze_(0) |
| |
| |
| softmax = learn_inf(timg).data.cpu().numpy().squeeze() |
| |
| |
| |
| idxs = np.argsort(softmax)[::-1] |
| |
| |
| label = "" |
| for i in range(5): |
| |
| p = softmax[idxs[i]] |
| |
| |
| landmark_name = learn_inf.class_names[idxs[i]] |
| |
| label += f"{landmark_name[3:]} (prob: {p:.2f})" + "\n" |
| return label |
|
|
| app = gr.Interface( |
| fn=classify, |
| inputs=gr.Image(), |
| outputs=["text"], |
| api_name="classify", |
| title="Landmark Classifier", |
| description="Upload an image of a landmark and this program will guess what landmark is in the picture.", |
| |
| ) |
|
|
| app.launch() |
|
|