File size: 1,064 Bytes
02c950a
279f12f
02c950a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7091ca5
02c950a
 
10fa38c
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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
import torch
import torch.nn as nn
from torchvision import models, transforms, datasets

from PIL import Image

import gradio as gr


model_ft = models.resnet18(pretrained = True)
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 2)

state_dict = torch.load("up500Model.pt", map_location = "cpu")

model_ft.load_state_dict(state_dict)
model_ft.eval()

img_transforms = transforms.Compose(
    [
        transforms.Resize(256),
        transforms.CenterCrop(224),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
    ]
)

labels = ["fiat500", "VW Up!"]
def predict(img):
    inp = img.fromarray(inp.astype("unit8"), "RGB")
    inp = img_transforms(inp).unsqueeze(0)

    # We don't want to compute gradients
    with torch.no_grad():
        preds = torch.np.functional.softmax(model_ft(inp)[0])

    return {labels[i]: preds[i] for i in range(2)}

interface = gr.Interface(
    predict, 
    inputs = "image", 
    outputs = "label",
    title = "Car classification"
)
interface.launch()