File size: 940 Bytes
62bcb6f
 
 
 
ad6a979
62bcb6f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cabe412
62bcb6f
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import torch
import gradio as gr
from model import AlexNet
from torchvision import transforms
#More Libraries ...

model_path = './alexnet_model_v1.pth'
model = AlexNet()
model.load_state_dict(torch.load(model_path, map_location=torch.device('cpu')))
model.eval()

labels = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', 'truck']


def predict(inp):
  inp = transforms.ToTensor()(inp).unsqueeze(0)
  with torch.no_grad():
    prediction = torch.nn.functional.softmax(model(inp)[0], dim=0)
    confidences = {labels[i]: float(prediction[i]) for i in range(10)}    
  return confidences



gr.Interface(fn=predict, 
             inputs=gr.components.Image(type="pil"),
             outputs=gr.components.Label(num_top_classes=5),
             examples=["frog.jpeg", "car.jpeg", "cat.jpeg", "ship.jpeg", "dog.jpeg"],
             theme="default",
             css=".footer{display:none !important}").launch()