Image classifier model
First model with fastai
- Importing the pre-trained model
- Initialization
- Create the button to add an image to classify
- Create the button "Classify" to run the classification
- Display the added image
- Create the function that performs the classification from the model on the image
- Create the widget containing all the elements
- Picture of Initialization
- Picture of classification of a photo
Here, we are to use a model trained elsewhere (on Google Cloud).
path = Path('./src')
path.ls(file_exts='.pkl')
Note :use the one cell below if you are on Windows
import pathlib
temp = pathlib.PosixPath
pathlib.PosixPath = pathlib.WindowsPath
learn_inf = load_learner(path/'exportBear.pkl')
learn_inf.dls.vocab
btn_upload = SimpleNamespace(data = ['src/grizzly.jpg'])
img = PILImage.create(btn_upload.data[-1])
lbl_pred = widgets.Label()
lbl_pred.value = "Your prediction here"
btn_upload = widgets.FileUpload()
btn_run = widgets.Button(description='Classify')
out_pl = widgets.Output()
out_pl.clear_output()
with out_pl: display(img.to_thumb(128,128))
def on_click_classify(change):
img = PILImage.create(btn_upload.data[-1])
out_pl.clear_output()
with out_pl: display(img.to_thumb(128,128))
pred,pred_idx,probs = learn_inf.predict(img)
lbl_pred.value = f'Prediction: {pred}; Probability: {probs[pred_idx]:.04f}'
btn_run.on_click(on_click_classify)
VBox([widgets.Label('Select your bear!'),
btn_upload, btn_run, out_pl, lbl_pred])
Note :use the one cell below if you are on Windows
pathlib.PosixPath = temp
Note: To test my model in real life, I invite you to visit my github and launch this notebook on your own environment.