Introduction To Deep Learning 🤖 — Chapter 4
Bringing Your Deep Learning Model To The Open World
All The code for this series is available at github.
Flashback📽️
In last three chapters we started by collecting our data from the web , built out dataset and then we built our model. We also tried to improve our model by cleaning the data. In the end we saved our final model.
Now, that we have our model ready and exported we have to figure out how to bring it to the world.
How we do it?
It’s Alive👾
One way to make your model useful to the end user is to make a GUI. An app that the can be useful to the user.
We will try to build a very simple GUI for our waffle vs ice-cream detector which will give us an idea about how we can bring our model to life.
You can build a simple GUI in jupyter notebook itself. Follow the following steps.
The GUI
We start by importing “ipywidgets”. These are simple GUI components provided in Jupyter notebook which helps you to create buttons, siders, checkboxes etc.
from IPython.display import Image
from ipywidgets import widgets
from fastai.vision.all import *
Next, we will invoke the load_learner
function from fastai and pass the model path to it. This will load the model from the pickle file and all it’s weight and make it ready to use for prediction. After this we can invoke the predict
function on the loaded model and pass it an image. On doing this our model will check the image and give us the prediction, the index of the label and the confidence level i.e. the probability about how much confident it is about the predicted class. We summarize these in the following function.
def dessert_detector(model, file):
learn=load_learner(model)
dessert,idx,confidence = learn.predict(file)
return dessert,idx,confidence
We invoke the label
function from the widgets class of ipywidgets module which will give us a placeholder for storing our texts.
lbl = widgets.Label()
Don’t forget to create a button to upload an image and trigger the prediction.
upload= widgets.FileUpload()
button= widgets.Button(description="Guess The Dessert")
We need a way to tell this button to trigger the prediction when we click on it. This is done by defining the on_click_classify
. In the below definition of this function we define how to take the data i.e. the image which was uploaded by using the PILImage.create(upload.data[-1])
. Then we display a thumbnail of the uploaded image when prediction is done with the helpof with out_pl: display(im.to_thumb(128,128))
. We fetch our prediction by calling the dessert_detector
function and finally display it with this lbl.value = f’prediction: {dessert}’
.
def dessert_detector(model, file):
learn=load_learner(model)
dessert,idx,confidence = learn.predict(file)
return dessert,idx,confidencedef on_click_classify(change):
im=PILImage.create(upload.data[-1])
out_pl.clear_output()
with out_pl: display(im.to_thumb(128,128))
dessert,idx,confidence=dessert_detector("../input/introduction-to-deep-learning-chapter-2/export.pkl",im)
lbl.value = f'prediction: {dessert}'button.on_click(on_click_classify)upload= widgets.FileUpload()widgets.VBox([widgets.Label('Right now My taste buds can recognize an waffle or an ice-cream. Show me a photograph of any one of these (I promise not to peek)'),
lbl,upload, button,out_pl])
Putting it all Together, the above code will generate the following GUI.
You can upload the image using the upload button and then get your prediction by using the “Guess The Dessert” button. Like so.
That’s it. You have built a simple GUI to show your ML idea to the world. You can also upload this notebook as a web app to a service known as Binder. This will host your notebook as an webapp which others can then use.
Read the following article by Butch Landingin where he describes the steps to deploy your notebook as a web app.
Conclusion
Here we go we have successfully built a deep learning project and have demonstrated a way to showcase it. What’s next? Well! more cool things✨ are to come up so keep an eye on this page.
How To Show Your Support To The Publication🤗
Creating a content requires a lot of research, planning, writing and rewriting . This is important because I want to deliver practical content to you without any fluff.
If you like my work and my content and want to support me then the following are the ways to show your support →
- If you like my work then click on this link to Buy me a coffee.
- Buy my deep learning course at udemy. Just click on the course link in the show notes and get awesome discount on my deep learning course.
- Subscribe to my publication and share it across so that more people can discover it.
- Subscribe and share my podcast “SimpleAI” on google podcast or any other podcast player of your choice. Don’t forget to give it a 5 star.
- Subscribe to my newsletter.