File size: 1,343 Bytes
86cfa24
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
463babe
 
86cfa24
 
 
 
f160d6c
86cfa24
 
 
 
 
 
 
 
a4d4ec4
 
 
 
 
 
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
import gradio
from fastai.vision.all import *

MODELS_PATH = Path('./models')
EXAMPLES_PATH = Path('./examples')

# Required function used by fastai learner (at training setup)
def label_func(filepath):
    return filepath.parent.name

learn = load_learner(MODELS_PATH/'food-101-resnet50.pkl')

labels = learn.dls.vocab

def predict(img):
    img = PILImage.create(img)
    pred,pred_idx,probs = learn.predict(img)
    return {labels[i]: float(probs[i]) for i in range(len(labels))}

with open('gradio_article.md') as f:
    article = f.read()

interface_options = {
    "title": "Food Image Classifier (Food-101|ResNet50|fast.ai)",
    "description": "A food image classifier trained on the Food-101 dataset, using ResNet50 and fast.ai.(https://data.vision.ee.ethz.ch/cvl/datasets_extra/food-101/)",
    "article": article,
    "examples" : [f'{EXAMPLES_PATH}/{f.name}' for f in EXAMPLES_PATH.iterdir()],
    "interpretation": "default",
    "layout": "horizontal",
    "theme": "default",
    "allow_flagging": "never",
}

demo = gradio.Interface(fn=predict,
                      inputs=gradio.inputs.Image(shape=(512, 512)),
                      outputs=gradio.outputs.Label(num_top_classes=5),
                      **interface_options)

launch_options = {
    "enable_queue": True,
    "share": False, 
}

demo.launch(**launch_options)