This is an attempt to make a classifier which can detect if an image is of a beercan, bacon. The classifier is a simple homage to the awkward time of the internet between web1.0 and web2.0, where a single-serving humour website brought to the attention that beercan and bacon sound identical in Jamaican accents.
It kinda works ok!
# nbi:hide_in
# nbi:hide_out
# imports
from fastai.vision.all import *
import ipywidgets as widgets
# nbi:hide_in
# Load pickle file with learned model
path = Path()
learn_inf = load_learner(path/'export.pkl')
# nbi:hide_in
# Create upload widgets
# upload button
btn_upload = widgets.FileUpload()
# classify button
btn_run = widgets.Button(description='Classify')
# image output
out_pl = widgets.Output()
# prediction output
lbl_pred = widgets.Label()
# nbi:hide_in
# function to change image when uploaded
def on_data_change(change):
img = PILImage.create(btn_upload.data[-1])
lbl_pred.value = ''
out_pl.clear_output()
with out_pl: display(img.to_thumb(128,128))
# function to run classification
def on_click_classify(change):
img = PILImage.create(btn_upload.data[-1])
pred,pred_idx,probs = learn_inf.predict(img)
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
btn_upload.observe(on_data_change, names=['data'])
btn_run.on_click(on_click_classify)
ui = widgets.VBox([widgets.Label('Upload an image and classify!'),
btn_upload, btn_run, out_pl, lbl_pred])
display(ui)