Tensorflow serving with firebase

Tensorflow serving is a mechanism to host the trained model on the server so that inference can be done without downloading model on the local system. A machine learning model is a mathematical function that takes some input and delivers the result, the result is nothing but the prediction(inference) which is based on features provided to it as the input.
Before we begin with the hosting model let’s see the type of machine learning.

Types of machine learning?
There are broadly 3 types of machine learning
1. Supervised learning

2. Unsupervised learning

3. Reinforcement learning

Supervised learning
It’s a type of machine learning wherein the input and output which is already known, in ML term input is nothing but the features and output is the label. The two categories of this type of ML is Classification and Regression.
In the case of classification, the output is a definite value like true or false (0 or 1) whereas in regression the output is a continuous value like the price of a stock after a certain time.

Unsupervised learning
It’s one such type of machine learning which is very widely used by the analytical tool to get some sort of prediction like the number of users who will churn and not churn by clustering them. In this type, the features are known but the label is not known and the whole objective here is to find some sort of similarities within the input data to group them under different categories.

Now let’s see how to deploy the MNIST pre-trained model on firebase using cloud function, cloud storage and cloud firestore.

So here is the architecture for the model serving through firebase, we will use three of firebase services
1. Cloud storage
2. Cloud firestore
3. Cloud functions

Create an app through which we will upload the image to cloud storage and show the prediction of a model by reading it from the cloud firestore. Now the main part here is how to feed image to the machine learning model?
as cloud storage is just a mechanism to store multimedia.

Cloud functions
It’s the heart of entire architecture as cloud functions support nodejs and fortunately, now tensorflow savedmodel (.pb) model is directly supported on js environment so we can directly use the pb model without converting it into any other format.

Let’s club all together with the following steps
1. Create app and add firebase required dependencies
2. Install firebase CLI using npm install -g firebase-tools
3. Use cloud functions to deploy the functions which will have index.js file
4. Upload image to cloud storage which will trigger cloud functions to read the image and feed it to the model
5. Get the prediction from the model and write to cloud firestore using cloud functions

That’s it, with just 5 steps you have hosted your tensorflow model.

Cloud functions for predictions
1. Create an empty folder say Tensorflow and from that folder using cmd run following commandfirebase init functions
2. Inside functions folder, there will be some prewritten files, open index.js and copy following code

3. In the same functions folder where you have index.js file, there might be package.json also. Edit that to add the dependencies of tensorflow and run command npm install from that directory through command prompt

With these 3 simple steps now your model is ready for the inference (prediction). Hosting model on the server is quite costly as it requires computation capability so based on that need to pay for it. However, it provides major benefits also for example rather than converting a model to tflite you can directly use protobuffer (pb) format and the most important part is security of the model as the model itself is never downloaded on the client device so the model is always secured and no one can use it for there own purpose.

Software Engineer | GDE Firebase | YouTuber — All Techies

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store