File size: 3,464 Bytes
9935a4c
 
 
2b05e43
 
9935a4c
 
 
1633182
8d2450a
 
 
9935a4c
 
 
 
 
 
 
 
 
 
 
 
 
2b05e43
 
8d2450a
9935a4c
 
8d2450a
 
9935a4c
 
 
8d2450a
 
 
2b05e43
 
 
8d2450a
2b05e43
 
 
 
 
 
1633182
9935a4c
70a1bb4
8d2450a
2b05e43
 
 
 
 
 
8d2450a
 
 
 
 
 
 
b343d52
2b05e43
 
 
8d2450a
 
b343d52
8d2450a
 
2b05e43
 
 
 
8d2450a
 
 
 
 
1633182
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.

# %% auto 0
__all__ = ['title', 'description', 'learners', 'models', 'active_name', 'active_model', 'example_images', 'model_matrix',
           'model_losses', 'demo', 'classify_image', 'select_model', 'update_matrix', 'update_losses']

# %% app.ipynb 1
from fastai.vision.all import *
import gradio as gr
import warnings
warnings.filterwarnings('ignore')

title = "FastAI - Big Cats Classifier"
description = "Classify big cats using all Resnet models available pre-trained in FastAI"

# %% app.ipynb 2
learners = {
    "resnet-18" : 'models/resnet18-model.pkl',
    "resnet-34" : 'models/resnet34-model.pkl',
    "resnet-50" : 'models/resnet50-model.pkl',
    "resnet-101": 'models/resnet101-model.pkl',
    "resnet-152": 'models/resnet152-model.pkl'
}
models = list(learners.keys())

active_name  = "resnet-18"
active_model = learners[active_name]


# %% app.ipynb 3
def classify_image(img):
    learn = load_learner(active_model)
    pred,idx,probs = learn.predict(img)
    return dict(zip(learn.dls.vocab, map(float, probs)))

def select_model(model_name):
    if model_name not in models:
        model_name = "resnet-18"
    active_name = model_name
    active_model = learners[active_name]
    return model_name.upper()

def update_matrix():
    return "models/" + active_name.replace('-','',1) + "-confusion-matrix.png"
    
def update_losses():
    return "models/" + active_name.replace('-','',1) + "-top-losses.png"
    

# %% app.ipynb 5
example_images = [ 'cheetah.jpg', 'jaguar.jpg', 'tiger.jpg', 'cougar.jpg', 'lion.jpg', 'african leopard.jpg', 'clouded leopard.jpg', 'snow leopard.jpg', 'hidden.png', 'hidden2.png' ]

model_matrix = [ 'models/resnet101-confusion-matrix.png', 'models/resnet18-confusion-matrix.png',  'models/resnet50-confusion-matrix.png',
'models/resnet152-confusion-matrix.png', 'models/resnet34-confusion-matrix.png' ]

model_losses = [ 'models/resnet101-top-losses.png', 'models/resnet18-top-losses.png',  'models/resnet50-top-losses.png',
'models/resnet152-top-losses.png', 'models/resnet34-top-losses.png' ]

demo = gr.Blocks()
with demo:
    with gr.Column(variant="panel"):
        image = gr.inputs.Image(label="Pick an image")
        model = gr.inputs.Dropdown(label="Select a model", choices=models)
        btnClassify = gr.Button("Classify")
    with gr.Column(variant="panel"):
        selected = gr.outputs.Textbox(label="Active Model")
        with gr.Row(equal_height=True):
            matrix=gr.outputs.Image(type='filepath', label="Confusion Matrix")
            losses=gr.outputs.Image(type='filepath', label="Top Losses")
        result = gr.outputs.Label(label="Result")
        
    model.change(fn=select_model, inputs=model, outputs=selected)
    btnClassify.click(fn=classify_image, inputs=image, outputs=result)
    img_gallery = gr.Examples(examples=example_images, inputs=image)
    matrix_gallery = gr.Examples(examples=model_matrix, label='Models Confusion Matrix', inputs=matrix)
    loss_gallery = gr.Examples(examples=model_losses, label='Models Top Losses', inputs=losses)
    result.change(fn=update_matrix, outputs=matrix)
    result.change(fn=update_losses, outputs=losses)

demo.launch(debug=True, inline=False)
    # intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=example_images, title=title, description=description )
    # if __name__ == "__main__":
    #     intf.launch(debug=True, inline=False)