LuniLand/vision-classifiers
0
1# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
2
3# %% auto 0
4__all__ = ['single_classifier', 'multi_class_classifier', 'multi_label_classifier', 'binary_labels', 'multi_class_labels',
5 'multi_label_labels', 'label_func', 'single_classification', 'multi_class_classification',
6 'multi_label_classification']
7
8# %% app.ipynb 1
9import gradio as gr
10import nbdev
11from fastai.vision.all import *
12import os
13
14# %% app.ipynb 2
15def label_func(f): return 'Cat' if f[0].isupper() else 'Dog'
16
17# %% app.ipynb 3
18single_classifier = load_learner('models/dog-cat-classifier.pkl')
19multi_class_classifier = load_learner('models/breeds-classifier.pkl')
20multi_label_classifier = load_learner('models/multi-label-classification.pkl')
21
22# %% app.ipynb 4
23binary_labels = single_classifier.dls.vocab
24
25def single_classification(img):
26 img = PILImage.create(img)
27 pred, pred_idx, probs = single_classifier.predict(img)
28 return dict(zip(binary_labels, map(float, probs)))
29
30# %% app.ipynb 5
31multi_class_labels = multi_class_classifier.dls.vocab
32
33def multi_class_classification(img):
34 img = PILImage.create(img)
35 pred, pred_idx, probs = multi_class_classifier.predict(img)
36 return dict(zip(multi_class_labels, map(float, probs)))
37
38# %% app.ipynb 6
39multi_label_labels = multi_label_classifier.dls.vocab
40
41def multi_label_classification(img):
42 img = PILImage.create(img)
43 pred, pred_idx, probs = multi_label_classifier.predict(img)
44 return dict(zip(multi_label_labels, map(float, probs)))
45
46# %% app.ipynb 7
47with gr.Blocks() as demo:
48 gr.Markdown("This demo allowing you to try different vision classification models - \
49 From binary classification through multi-class and multi-label classification and finally segmentation.")
50
51 with gr.Tab("Binary"):
52 with gr.Row():
53 with gr.Column():
54 b_image_input = gr.inputs.Image(shape = (460,460))
55 with gr.Row():
56 b_button = gr.Button("Run")
57
58 b_examples = 'models/Examples/Pets'
59 examples = gr.Examples(examples=[b_examples + '/shiba_inu_44.jpg', b_examples + '/Bengal_132.jpg',], inputs = b_image_input)
60 binary_out = gr.Label(num_top_classes=len(binary_labels))
61
62
63 with gr.Tab("MultiClass"):
64 with gr.Row():
65 with gr.Column():
66 m_image_input = gr.inputs.Image(shape = (460,460))
67 with gr.Row():
68 m_button = gr.Button("Run")
69
70 m_examples = 'models/Examples/Pets'
71 examples = gr.Examples(examples=[os.path.join(m_examples, s) for s in os.listdir(m_examples) if s.endswith('jpg')], inputs = m_image_input)
72 multi_out = gr.Label(num_top_classes=len(multi_class_labels))
73
74
75 with gr.Tab("MultiLabel"):
76 with gr.Row():
77 with gr.Column():
78 ml_image_input = gr.inputs.Image(shape = (460,460))
79 with gr.Row():
80 ml_button = gr.Button("Run")
81
82 ml_examples = 'models/Examples/Pascal'
83 examples = gr.Examples(examples=[os.path.join(ml_examples, s) for s in os.listdir(ml_examples) if s.endswith('jpg')], inputs = ml_image_input)
84
85 multil_out = gr.Label(num_top_classes=len(multi_label_labels))
86
87 b_button.click(single_classification, inputs=b_image_input, outputs=binary_out)
88 m_button.click(multi_class_classification, inputs=m_image_input, outputs=multi_out)
89 ml_button.click(multi_label_classification, inputs=ml_image_input, outputs=multil_out)
90
91demo.launch()
92 