File size: 1,523 Bytes
5e0099e
 
 
 
 
22483c5
5e0099e
f76e377
 
5e0099e
 
f76e377
 
4edaa99
5e0099e
9e14d7c
330178d
5e0099e
 
82de39e
 
f76e377
 
feab5d7
f76e377
 
 
 
 
5e0099e
deae187
c5b118c
 
 
 
f012932
5e0099e
deae187
22483c5
5e0099e
 
330178d
5e0099e
 
 
 
705a628
5e0099e
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
47
48
import timm
import torch
import torch.nn.functional as nnf
import gradio as gr
import numpy as np
import pandas as pd
import json
from torchvision.transforms import ToTensor, Resize
from PIL import Image


#model = torch.load("/home/user/app/model_scripted.pkl",map_location=torch.device('cpu')) 
model = torch.jit.load("/home/user/app/model_scripted.pkl")
model.eval()

with open("/home/user/app/class_mapping.json", "r") as read_file:
    classes = json.load(read_file)

def classify_image(inp):
  
  inp = Image.fromarray(inp)
  inp= Resize((300, 300))(inp)
  inp= ToTensor()(inp)
  inp = torch.unsqueeze(inp, 0)
  ##print(inp.shape)
  #inp = inp.astype(np.uint8).reshape((-1, 3, 300, 300))
  ##print(inp.shape)
  #inp = torch.from_numpy(inp).float()
  ##confidences = model(inp)

  preds = model(inp).data[0]
  #means = preds.mean(dim=0, keepdim=True)
  #stds = preds.std(dim=0, keepdim=True)
  #preds = 4 * (preds - means) / stds
  ##preds = nnf.normalize(model(inp).data[0], dim=0)
  preds = nnf.softmax(preds, dim=0)
  preds = [pred.cpu() for pred in preds]
  preds = [float(pred.detach()) for pred in preds]
  print(pd.Series(preds).describe())

  #confidences_dict = {classes[i]: float(confidences.data[0][i]) for i in range(len(confidences.data[0]))}
  confidences_dict = {classes[str(i)]: float(preds[i]) for i in range(len(preds))}

  return confidences_dict

gr.Interface(fn=classify_image, 
             inputs=gr.Image(shape=(300, 300)),
             outputs=gr.Label(num_top_classes=3)).launch(debug = True)