bigcats / app.py
Aspiring Astro
model selection and confirmation
b343d52
raw
history blame
2.18 kB
# AUTOGENERATED! DO NOT EDIT! File to edit: app.ipynb.
# %% auto 0
__all__ = ['title', 'description', 'learners', 'models', 'active_model', 'example_images', 'demo', 'classify_image',
'select_model']
# %% 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_model = learners["resnet-18"]
# %% 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_model = learners[model_name]
return model_name
# %% app.ipynb 5
example_images = [ 'cheetah.jpg', 'jaguar.jpg', 'tiger.jpg', 'cougar.jpg', 'lion.jpg', 'african leopard.jpg', 'clouded leopard.jpg', 'snow leopard.jpg' ]
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")
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)
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)