Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
Physical Address
304 North Cardinal St.
Dorchester Center, MA 02124
I’ve worked as a web developer for a while, and most times, I’m amazed at how web apps such as Facebook and Instagram are able to detect and recognize objects in images. I reckon a machine learning model like MobileNet makes this possible, but how do you deploy this model to the web?
I’ve worked as a web developer for a while, and most times, I’m amazed at how web apps such as Facebook and Instagram are able to detect and recognize objects in images. I reckon a machine learning model like MobileNet makes this possible, but how do you deploy this model to the web? Deploying machine learning (ML) models to the web involves the process of taking an ML model that has been trained and tested offline and making it available for use in a web application. This process typically involves several steps, including selecting an appropriate web framework, setting up the necessary infrastructure, and integrating the model with the web application. Once the model is deployed, it can be used to make predictions or provide other intelligent services to web app users.
Integrating ML models into web applications can provide a number of benefits, including:
In this article, I’ll help you get familiar with machine learning models and popular frameworks like TensorFlow. We’ll explore some of these models, paying special attention to TensorFlow.js. After that, we’ll integrate a MobileNet model into a web app. So, let’s jump right in!
An ML model is like a computer program trained to learn from examples. It’s like a student who is taught using many examples of a problem until they can solve similar problems on their own. The more examples the model is given, the better it becomes at making predictions or decisions based on new data. For example, an ML model might be trained to recognize different types of animals in pictures. After being shown many pictures of different animals and told what they are, the model can identify animals in new pictures that it hasn’t seen before.
This section will provide an overview of each type and offer illustrative examples for better understanding.
1. Supervised Learning: Supervised learning is the most common form of machine learning, where the model is trained on a labeled dataset. This means that the input data is paired with the correct output. The model learns to map input data to the desired output through a series of iterations, adjusting its parameters to minimize the error between its predictions and the actual outputs.
Example: Suppose we want to create a model that can recognize handwritten digits. We would train this model using a dataset containing images of handwritten digits (input) along with their correct numerical labels (output). Once trained, the model should be able to identify the correct digit when presented with a new, unlabeled image.
Subtypes of Supervised Learning
Learn more about supervised learning:
Supervised Learning – Towards Data Science
Supervised Learning with scikit-learn
Supervised Learning algorithms
2. Unsupervised Learning
Unsupervised learning involves training a model on an unlabeled dataset, meaning the input data does not come with the correct output. The model must discover patterns or structures within the data without guidance, typically by finding similarities or differences among the data points.
Example: Suppose we have a dataset containing customer data for an e-commerce website. An unsupervised learning model can be used to group customers with similar behaviors, preferences, or demographics. This could be used to create targeted marketing campaigns or personalized product recommendations.
Subtypes of Unsupervised Learning
Learn more about unsupervised learning:
Unsupervised learning demystified
Supervised vs Unsupervised Learning
3. Reinforcement Learning
Reinforcement learning is a type of machine learning where an agent learns to make decisions by interacting with its environment. The agent receives feedback in the form of rewards or penalties, which it uses to adjust its actions to maximize its cumulative reward over time. This process is often used to train models for tasks that require decision-making or control.
Example: Consider a self-driving car that needs to learn how to navigate traffic safely and efficiently. A reinforcement learning model can be employed to train the car by allowing it to interact with a simulated environment. The car receives positive rewards for reaching its destination and negative rewards for collisions or violating traffic rules. Over time, the model learns to make better decisions that maximize its cumulative reward.
Subtypes of Reinforcement Learning
Learn more about reinforcement learning:
A beginners guide to reinforcement learning
An Introduction to Reinforcement learning
Several popular machine-learning frameworks make it easier for developers and researchers to build, train, and deploy machine-learning models. TensorFlow, PyTorch, and scikit-learn are three of the most widely used frameworks. This section will discuss each of these frameworks in detail, along with examples and use cases.
1. TensorFlow
TensorFlow is an open-source machine learning framework developed by Google Brain. It is designed to be flexible, efficient, and scalable, allowing users to build and deploy machine learning models on various platforms, including web, desktop, mobile, and cloud environments. TensorFlow supports deep learning and other machine learning techniques and is particularly popular in computer vision and natural language processing.
Use-Cases:
2. PyTorch
PyTorch is an open-source machine learning framework developed by Facebook’s AI Research lab (FAIR). It is based on the Torch library and is designed to provide flexibility, ease of use, and high performance for deep learning applications. PyTorch is particularly popular among researchers due to its dynamic computation graph and strong support for GPU acceleration, which makes it ideal for rapid prototyping and experimentation.
Use-Cases:
3. Scikit-learn
Scikit-learn is an open-source machine learning library built on top of NumPy, SciPy, and Matplotlib. It is specifically designed for traditional machine learning techniques rather than deep learning, and offers a simple, consistent API that makes it easy for users to experiment with various models and techniques.
Use-Cases:
Having covered the fundamentals of machine learning models, their types, popular frameworks, and respective use-cases, it’s time to delve into the practical aspects of integrating a machine learning model into a web application. We will be focusing on utilizing the TensorFlow machine learning framework, which is highly recommended for this purpose, owing to its versatility and compatibility with web environments.
TensorFlow provides a powerful JavaScript library called TensorFlow.js, specifically designed to facilitate developing and deploying machine learning models within web browsers and Node.js environments. This enables users to harness the capabilities of TensorFlow without leaving the comfort of their favorite web technologies.
To start implementing a machine learning model using TensorFlow.js, you will first need to include the library in your project, either by adding it as a dependency via npm or by including a script tag in your HTML file. Once this is done, you can choose from a variety of pre-trained models, modify them as needed, or even create your own custom model using TensorFlow.js APIs. Finally, you can integrate the model into your web application, providing users with powerful machine learning capabilities right within their browser.
Tensorflow.js provides us with a set of pre-trained models. For the purpose of this article, we will work with some of these models.
The MobileNet model is a convolutional neural network (CNN) designed for efficient image classification tasks, particularly on mobile and embedded devices.
The primary purpose of the MobileNet model is to provide an efficient solution for image classification tasks while maintaining high accuracy. It can recognize and categorize objects within images, such as identifying animals, plants, vehicles, and other objects. MobileNet can be applied in various applications, including:
When you want to integrate a TensorFlow.js model into a web application, it’s a good idea to follow these steps:
Let’s get started.
First, include the TensorFlow.js library in your HTML file by adding the following script tag:
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
Next, load the MobileNet model using the load() method from the @tensorflow-models/mobilenet package. You can include this package in your project using npm or by adding a script tag:
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script>
Now you can load the model in your JavaScript code:
const loadModel = async () => { const model = await mobilenet.load(); console.log('Model loaded successfully'); return model; };
This asynchronous function is responsible for loading the MobileNet model using the mobilenet.load()
method from the TensorFlow.js MobileNet package. When the model is successfully loaded, it logs a message to the console and returns the model.
MobileNet expects input images to be 224×224 pixels. You can obtain an image from a file input, a canvas element, or an image element. In this example, we will use an image element with the ID inputImage
. You will need to preprocess the image before feeding it to the model:
const preprocessImage = (imageElement) => { const imageTensor = tf.browser.fromPixels(imageElement); const resizedImageTensor = tf.image.resizeBilinear(imageTensor, [224, 224]); return resizedImageTensor; };
This function takes an image element as input and preprocesses it to meet the MobileNet model’s requirements. It first converts the image element to a TensorFlow.js tensor using tf.browser.fromPixels(imageElement)
. Then, it resizes the tensor to the expected dimensions of 224×224 pixels using tf.image.resizeBilinear(imageTensor, [224, 224])
.
Create a function that takes the preprocessed image tensor and feeds it to the model to obtain predictions:
const classifyImage = async (model, preprocessedImage) => { const predictions = await model.classify(preprocessedImage); return predictions; };
This asynchronous function takes the loaded model and preprocessed image tensor as inputs. It then calls the model.classify()
method to obtain predictions for the given image. The function returns these predictions.
The classify()
function returns an array of predictions containing a className
and probability. You can display the predictions in a user-friendly format, such as an HTML table or list. Here’s we will display the top 3 predictions in a list:
const displayPredictions = (predictions) => { const predictionList = document.getElementById('predictions'); predictionList.innerHTML = ''; predictions.slice(0, 3).forEach((prediction) => { const listItem = document.createElement('li'); listItem.textContent = `${prediction.className}: ${Math.round(prediction.probability * 100)}%`; predictionList.appendChild(listItem); }); };
This function takes the predictions array as input and displays the top 3 predictions in a user-friendly format. It first clears the predictions list, then iterates through the top 3 predictions and creates a list item for each. The list item contains the class name and the probability rounded to a percentage value. Finally, the list items are appended to the predictions list on the page.
Now you can load the model, preprocess the input image, classify it, and display the predictions when an image is selected:
document.getElementById('inputImage').addEventListener('change', async (event) => { const model = await loadModel(); const preprocessedImage = preprocessImage(inputImage); const predictions = await classifyImage(model, preprocessedImage); displayPredictions(predictions); });
That’s it! The application can now preprocess user-uploaded images, classify them using the MobileNet model, and display the top 3 predictions to the user.
The complete code is on GitHub.
In conclusion, deploying an ML model to the web requires a multi-step process, including selecting an appropriate web framework, setting up the necessary infrastructure, and integrating the model with the web application. However, the benefits of integrating ML models into web applications can be significant, including improved user experience, enhanced functionality, increased efficiency, and improved decision-making.
In this article, we have explored the process of deploying machine learning models to the web using popular frameworks like Tensorflow. We have demonstrated the steps in integrating a model into a web application using Tensorflow.js.
I encourage further exploration and experimentation with machine learning models and web frameworks to discover new ways to improve user experience, functionality, efficiency, and decision-making in web applications. With the right tools and techniques, you can create powerful and innovative web applications that take advantage of the latest advances in machine learning technology. So, keep exploring and experimenting to discover new possibilities in this exciting and rapidly evolving field.
TensorFlow.js Tutorials: https://www.tensorflow.org/js/tutorials
“Build and Deploy a Machine Learning Model with TensorFlow.js” by TensorFlow: https://codelabs.developers.google.com/codelabs/tensorflowjs-teachablemachine-codelab/index.html
“Machine Learning in the Browser with TensorFlow.js” by Gant Laborde: https://www.apress.com/gp/book/9781484252022
TensorFlow.js YouTube playlist by TensorFlow: https://www.youtube.com/playlist?list=PLQY2H8rRoyvwLbzbnKJ59NkZvQAW9wLbx
TensorFlow.js Pre-trained Models: https://github.com/tensorflow/tfjs-models