File size: 1,284 Bytes
1ac57f1
 
 
 
c0ed310
 
1ac57f1
 
 
c0ed310
1ac57f1
 
 
 
 
c0ed310
1ac57f1
 
 
 
 
 
 
 
 
 
c0ed310
 
 
 
 
 
1ac57f1
 
 
c0ed310
 
1ac57f1
 
c0ed310
 
1ac57f1
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
from fastai.vision.core import PILImageBW, TensorImageBW
from datasets import ClassLabel
import gradio as gr
from fastai.learner import load_learner
from PIL import Image
from numpy import array

def get_image_attr(x): return x['image']
def get_target_attr(x): return x['target']
def get_label_attr(x): return x['label']

def img2tensor(im: Image.Image):
  return TensorImageBW(array(im)).unsqueeze(0)

classLabel = ClassLabel(names=['T - shirt / top', 'Trouser', 'Pullover', 'Dress', 'Coat', 'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot'], id=None)
labels = classLabel.names

def add_target(x:dict):
  x['target'] = classLabel.int2str(x['label'])
  return x

learn = load_learner('export.pkl', cpu=True)

def classify(inp):
  img = PILImageBW.create(inp)
  item = dict(image=img)
  pred, _, prob = learn.predict(item)
  return {label: float(prob[i]) for i, label in enumerate(labels)}
  # return classLabel.int2str(int(pred))

examples = ['shoes.jpg', 't-shirt.jpg']
interpretation='default'

iface = gr.Interface(
    fn=classify,
    inputs=gr.inputs.Image(image_mode='L'),
    outputs=gr.outputs.Label(num_top_classes=3),
    title="Fashion Mnist Classifier",
    description="fastai deployment in Gradio.",
    examples=examples,
    interpretation=interpretation,
).launch()