File size: 1,039 Bytes
a5f03cd
b71bb90
a5f03cd
43fa7c4
a5f03cd
 
 
 
 
 
 
7648e7b
a5f03cd
 
6cf1895
a5f03cd
 
 
 
6cf1895
a5f03cd
 
 
 
 
 
b346c0f
b560045
0e11cd3
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
from torchvision import transforms, models
from PIL import Image
import gradio as gr

#model initialization
model= models.resnet18(pretrained=True)

model.fc = torch.nn.Linear(model.fc.in_features,2)
state_dict=torch.load('up500Model.pt', map_location='cpu')
model.load_state_dict(state_dict)
model.eval()

#predictions
imgTransforms = 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 =['Fiat 500', 'VW Up']
def predict(inp):
    inp= Image.fromarray(inp.astype('unit8'), 'RGB')
    inp =imgTransforms(inp).unsqueeze(0)

    with torch.no_grad():
        predictions = torch.nn.functional.softmax(model(inp)[0])

    return {LABELS[i]: float(predictions[i]) for i in range(2)}
examples=[["fiat500.jpg"],["VWUP.jpg"]]
#app deploy
interface = gr.Interface(fn=predict, inputs='image', outputs='label', title='App', description= 'upload the car image', examples=examples, cache_examples= False)
interface.launch()