import gradio as gr from fastai.vision.all import * from os import listdir import random title = "Who Am I? (Hockey Edition)" desc = """
This app uses a 'neural network' (a type of machine learning model) to classify an image as containing a hockey player, a hockey goalie or a hockey referee.
""" art = """I built this model using about 50 hockey related images found on the web and in my own collection. I started with a pretrained `resnet18` model (resnet18 is trained on `imagenet`, a very large dataset with millions of images) and then performed `fine tuning` of this using python and the `fast.ai` library. The total training time for this was about 5 minutes on a basic GPU. It's impressive how accurate this quick / small model can be!""" learn = load_learner('hockey_model.pkl') categories = ('Hockey Goalie', 'Hockey Player', "Hockey Referee") image = gr.Image(shape=(192, 192)) label = gr.Label() skater_example = 'assets/skaters/' + random.choice(listdir('assets/skaters')) ref_example = 'assets/referees/' + random.choice(listdir('assets/referees')) goalie_example = 'assets/goalies/' + random.choice(listdir('assets/goalies')) def classify_image(img): pred,idx,prob = learn.predict(img) return dict(zip(categories, map(float, prob))) iface = gr.Interface(fn=classify_image, inputs=image, outputs=label, theme=gr.themes.Glass(), examples=[skater_example, ref_example, goalie_example], title=title, description=desc, article=art) iface.launch(share=False, debug=True)