Convert your machine learning model as FAAS (Function as a service).

Everyone teaches how to create a machine learning model. But not that many people teach machine learning model deployment in a real-life scenario. For deploying a machine learning model one should have a professional understanding of software development. Otherwise, a large-scale deployment is not possible. In production, you have to ensure safety and security as well as performance. There are many ways to serve a machine learning model,

  • Serve the model as a rest API developed in Django / fastAPI /flask. or
  • Make a serverless function.

The first option is a good solution, but you need to learn a framework and the deployment process, which is time-consuming.

The second option is also a very good solution, as we know FAAS is a smaller, easily manageable, and reliable solution. The concept of FAAS is, it will do only one task and nothing else. You can deploy a FAAS application and can scale it when needed. There are plenty of paid and opensource FAAS services are available. Like Aws lambda, Cloud functions, etc. These paid solutions have some limitation of usages and often become a hustle. There is also a cheap easy solution, OpenFAAS. OpenFAAS sits above a Kubernetes cluster and easily manageable. You can create a FAAS service in any machine (local or cloud) using OpenFAAS. Or you can use a managed Kubernetes service then install OpenFAAS to create your own lambda like FAAS service.

Let’s come to our topic. Now we are going to deploy a simple machine learning model using OpenFAAS.

There are several ways to install Kubernetes in your local or cloud machine, for demo purposes we will use the minikube, kubectl, and faas-cli. To install these tools you can follow the instruction given in this web link:https://docs.openfaas.com/deployment/kubernetes/

Here I will use a model I have created a CNN model using Pytorch and Trained it in the Cifer10 dataset.

Model Architecture.

We have saved the trained model and keep it for future work.

Now Let’s create a FAAS function, using faas-cli

Now we can see a file architecture.

api

-handler.py

-requirements.txt

template

api.yml

let’s check the handler.py file.

Let’s put our model prediction code into the handler.py

convNet.py

Now our function is ready to use.

To deploy our function as a FAAS. we need to write the below command.

Make sure that you are in the right directory. Wait for few minutes.

When you will see something like this.

Now you are good to go. Make a curl request to the server.

Or you can use OpenFaas’s beautiful UI.

Now visit http://127.0.0.1:8080 and login with user name admin and password from the terminal.

Click the function name.

Start making requests.

I hope you have liked my writing. This is my first story in the medium. Let me know if I have missed something, or something needs to change. I will be happy to correct them.

A IBM certified Data Science Professional, experienced in Data Science, Machine learning, Deep learning, Data Engineering and MLOps.