Machine Learning Model Deployment

Asha Ganesh
5 min readAug 14, 2020

--

Using Server less Deployment

Serverless compute abstracts away provisioning, managing severs and configuring software

ML models serverless deployment

What is serverless deployment

Serverless is the next step in Cloud Computing. This means that servers are simply hidden from the picture. In serverless computing, this separation of server and application is managed by using a platform.The responsibility of the platform or serverless provider is to manage all the needs and configurations for your application. These platforms manage the configuration of your server behind the scenes. This is how in serverless computing, one can simply focus on the application or code itself being built or deployed.

Machine Learning Model Deployment is not exactly the same as software development here in ML models a constant stream of new data is needed to keep models working well.Model need to adjust in the real world because of various reasons like adding new categories ,new levels and many reasons.Deploying models is just the beginning,many times model need to retrain and check the performance. So, using server less deployment can save time effort and all for retraining models every time its cool! to use serverless deployment.

ML Workflow

Models are performing worse in production than in development ,and the solution need to be sought in deployment. So, its easy to deploy ML models through serverless deployment.

Prerequisites to understand serverless deployment

Basic understanding of cloud computing

Basic understanding Cloud functions

Machine Learning

Deployment Models for prediction

We can deploy our ML model in 3 ways those are

web hosting frameworks like Flask and Django etc

Server less compute AWS lambda, Google Cloud Functions,Azure Functions.

Cloud Platform specific frameworks like AWS sagemaker, Google AI platform, Azure Functions.

Deploy Models in various ways

Server less deployment architecture overview

Image taken and modified from google cloud

Store models in google cloud storage buckets then write google cloud function using python for retrieving models from bucket and by using HTTP JSON requests we can get predicted values for the given inputs with the help of google cloud function.

Steps to start model deployment

  1. About Data, code and models

Taken Movie reviews dataset for Sentiment Analysis, see the solution here in my git hub repository and data, models also available in the same repository.

2 . Create storage Bucket

By executing “ServerlessDeployment.ipynb“ file you will get 3-ML models DecisionClassifier, LinearSVC, Logistic regression.

Click on Browser in Storage option for creating new bucket as shown in the image

Create Storage bucket

3. Create New Function

Create a new bucket then create a folder and upload 3- models in that folder by creating 3 sub folders as shown.

Here models is my main folder name and my sub folders are decision_tree_model

linear_svc_model

logistic_regression_model

storing models in google storage in 3 folders

4 . Create Function

Then go to google cloud functions and create a function then select Trigger type as HTTP and select language as python (any language can choose)

Create Cloud Function

5. Write cloud function in the editor

Check the cloud function in my repository, here I have imported required libraries for calling models from google cloud bucket and other libraries for HTTP request

GET method used to test the URL response and POST method

delete default template and paste our code then pickel is used for deserialized our model

google.cloud — access our cloud storage function.

If incoming request is GET we simply return “welcome to classifier”

If incoming request is POST access the json data in the body request

get json gives us instantiate the storage client object and access models from the bucket, here we have 3 — classification models in the bucket.

If the user specify “DecisionClassifier” we access the the model from the respective folder respectively with other models.

If user not specify any model default model is Logistic Regression model .

The blob variable contains reference to the model.pkl file for the correct model.

We download the .pkl file on to the local machine where this cloud function is running. Now every invocation might running on different VM and we only access /temp folder on the VM thats why we save our model.pkl file.

we desterilize the model by invoking pkl.load access the prediction instances from the incoming request and call model.predict on the prediction data.

the response that will send back from the serverless function is the original text that is the review that we want to classify and our pred class.

after main.py write requirement.txt with required libraries and versions

Google Cloud Function(Image is not clear but for understanding purpose mentioned here)

5. Deploy the model

Green tick represent successful model deployment

6. Test the model

Give model name and review(s) for testing

Test function with other model

Will meet you with complete UI details with this model deployment

Code References:

My Github Repository : https://github.com/Asha-ai/ServerlessDeployment

Don’t hesitate to give more & more claps :)…

--

--

No responses yet