Making Your Models Come To Life
Building and training models is something every Machine Learning dev or data scientist is familiar with — be it the MNIST Handwritten Digits classifier we all have built, or even more complex networks. I’m sure you must have many pickle files (or .pt if you’re team PyTorch) all across GitHub or your own file storage, but have you ever tried to test them out on something more user-friendly, like a web app? If you haven’t, follow me for the next few paragraphs as I pull you out of your Jupyter notebooks and into the real world.
Using a Python Backend
If you have minimal experience with web development, the best option to deploy your models is in a language you’re most familiar with — Python!
Using a Python-based framework for backend has a lot of benefits, especially when it comes to personalizing functions for input preprocessing and prediction. You can choose any of these two frameworks — Django, or Flask. Each provide different features, so select your framework on the basis of what you expect from your website.
Flask
Flask is a lightweight WSGI web application framework. It is designed to make getting started quick and easy, with the ability to scale up to complex applications.
If your goal is to build a basic web interface for your model, Flask is your go-to. It does not enforce any dependencies or expect a certain layout, which makes it the easiest framework to create an API on. Learning how to use Flask is extremely easy, you can actually get creative and figure out how to build your own functions for prediction — or follow a tutorial.
Django
Django is a high-level Python Web framework that encourages rapid development and clean, pragmatic design. Built by experienced developers, it takes care of much of the hassle of Web development, so you can focus on writing your app without needing to reinvent the wheel. It’s free and open source.
Like I mentioned before, Flask is perfect if you just want a basic API. But if you’re ready to get your hands dirty and develop a proper backend, I’d suggest you go for Django. It is a beautiful framework with amazing functionalities, which will help you make a fully functional website for your model.
Since Django also uses Python, learning it won’t be difficult. However, it is not as simple or customisable as Flask. I’d suggest you first learn how to make a basic Django app, then once you’re comfortable try making your own prediction function — or follow a tutorial.
Machine Learning for Web Developers
A major problem many face in their projects is “Incorporating AI”. If your team composes of an ML dev and a web dev, I bet they’ve spent 10% of their time on actually building a model and the rest 90% figuring out how to integrate it.(reminds me of how many hacks I messed up just because I didn’t know how to integrate my code!). Based on my personal experience, here are some ways you can learn how to finally get the best of both worlds.*
The beautiful world of TensorFlow.js
TF.js is a common ground for experienced ML devs or a JavaScript dev with no ML knowledge.
TensorFlow.js lets you
- Run existing models — For when you want to incorporate an official TJ.js model, or your Python dev makes you use the model they built themselves
- Retrain existing models — To avoid the hassle of building an entire architecture from scratch but also get the benefits of using your own data
- Develop ML with JavaScript — If you as a JS dev want to build and train your own model!
The official TensorFlow website is the best place to learn, they provide a bunch of helpful tutorials along with some fun demos to try out.
A few other major companies that provide amazing APIs
check out References for more!
*come on, I can make ONE Hannah Montana reference!
Bonus Tip!
Building Real-Time Programs via a Web Socket Server
Getting back to Python. I’ve already talked about using Flask/Django as a backend framework and building custom prediction functions. However, basic functions will render useless in cases of real-time applications — like online games. Here, we enter the world of WebSockets.
The
WebSocket
protocol, described in the specification RFC 6455 provides a way to exchange data between browser and server via a persistent connection.WebSocket is especially great for services that require continuous data exchange, e.g. online games, real-time trading systems and so on.
By establishing a socket connection between the Python script and the frontend, we can effectively run a script to collect data and throw it to the frontend. A very popular example is a game called Skyfall, where the paddle is controlled by the hand. The hand detection is done on a Python script using the TensorFlow Object Detection API, and the (x,y) coordinates of the hand are sent to the frontend, where the paddle is moved accordingly. You can learn how to build this here.
If you feel that the tutorial is too complicated, and want to learn the basic functionality — you can check out my GitHub repo where I created a Virtual Mouse using the same concept.
This article is meant to be an overview of the best ways to integrate ML with web. If you want to know more, or want me to write a tutorial, reach out to me through my LinkedIn, and do check out my projects on GitHub!
I hope you can translate these tips to make some amazing projects!
Resources
- https://palletsprojects.com/p/flask/
- https://www.djangoproject.com/
- https://www.tensorflow.org/js
- https://towardsdatascience.com/productionize-a-machine-learning-model-with-flask-and-heroku-8201260503d2
- https://towardsdatascience.com/how-to-build-a-gesture-controlled-web-based-game-using-tensorflow-object-detection-api-587fb7e0f907