{"id":1886,"date":"2019-04-16T08:34:28","date_gmt":"2019-04-16T08:34:28","guid":{"rendered":"https:\/\/cloudxlab.com\/blog\/?p=1886"},"modified":"2019-05-02T12:57:11","modified_gmt":"2019-05-02T12:57:11","slug":"deploying-machine-learning-model-in-production","status":"publish","type":"post","link":"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/","title":{"rendered":"Deploying Machine Learning model in production"},"content":{"rendered":"\n<p>In this article, I am going to explain steps to deploy a trained and tested Machine Learning model in production environment.<\/p>\n\n\n\n<p>Though, this article talks about Machine Learning model, the same steps apply to Deep Learning model too.<\/p>\n\n\n\n<p>Below is a typical setup for deployment of a Machine Learning model, details of which we will be discussing in this article.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img width=\"575\" height=\"376\" src=\"https:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment-1.png\" alt=\"Process to build and deploy a REST service (for ML model) in production\" class=\"wp-image-2150\" srcset=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment-1.png 575w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment-1-300x196.png 300w\" sizes=\"(max-width: 575px) 85vw, 575px\" \/><figcaption>Process to build and deploy a REST service (for ML model) in production<\/figcaption><\/figure>\n\n\n\n<p>The complete code for creating a REST service for your Machine Learning model can be found at the below link:<\/p>\n\n\n\n<p> <a href=\"https:\/\/github.com\/cloudxlab\/ml\/tree\/master\/projects\/deploy_mnist \">https:\/\/github.com\/cloudxlab\/ml\/tree\/master\/projects\/deploy_mnist <\/a><br><\/p>\n\n\n\n<p>Let us say, you have trained, fine-tuned and tested Machine Learning(ML) model &#8211; <em>sgd_clf<\/em>, which was trained and tested using SGD Classifier on MNIST dataset. &nbsp;And now you want to deploy it in production, so that consumers of this model could use it. What are different options you have to deploy your ML model in production?<\/p>\n\n\n\n<!--more-->\n\n\n\n<p>Before you start thinking about the deployment of your ML model in production, there are a few more tasks that needs to be performed. <\/p>\n\n\n\n<p>There may be more steps involved, depending on what specific requirements you have, but below are some of the main steps:<\/p>\n\n\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul id=\"mce_4\" class=\"editor-rich-text__tinymce mce-content-body\" role=\"textbox\" contenteditable=\"true\" aria-label=\"Write list\u2026\" aria-autocomplete=\"list\" aria-multiline=\"true\" data-is-placeholder-visible=\"false\">\n<li>Packaging your ML model<\/li>\n<li>Securing your packaged ML model<\/li>\n<li>Planning of how to serve\/expose your ML model to the consumers (as a REST service, etc.)<\/li>\n<li>If you have planned for a REST service, then, creating a REST API for your ML model<\/li>\n<li>Securing your REST API<\/li>\n<li>Deploying your REST service in production (using Docker and Kubernetes)<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n<p><\/p>\n\n\n<h2><strong>Packaging your ML model<\/strong><\/h2>\n\n\n<p>Instead of saving the ML model as it is, you can package your ML model (say <em>mnist<\/em>), and create a .pkl file for it, using Python&#8217;s<em> joblib<\/em> library. <\/p>\n\n\n\n<p> For creating and restoring the .pkl file, you can either use <em>joblib<\/em> library or <em>pickle <\/em>library of Python. You normally use <em>joblib<\/em> to save an object having large data, else, you use <em>pickle<\/em> library. Here, in this case, we have used <em>joblib<\/em> library.<\/p>\n\n\n\n<p>.pkl file is nothing but a serialized <em>pickle<\/em> file, which if you want, you can compress it further, to save storage space, using say Python&#8217;s <em>gzip<\/em> library. After you apply compression, your ML model file name will look like &#8211; mnist.pkl.gz<br><\/p>\n\n\n<h2><strong>Securing your packaged ML model<\/strong><\/h2>\n\n\n<p>The content of your pickle file (in this case your ML model) could be intercepted and modified by anyone over the network if your network is not secured. Hence, it is advisable to use secured (encrypted) network connection while exchanging the pickle file.<\/p>\n\n\n\n<p>In addition to this, the pickle file could be signed (using <em>&#8216;cryptographic signature&#8217;<\/em>) before storing or transmitting, and this signature can be verified before it is restored at the receiver&#8217;s end (say your REST API). Cryptographic signature helps in detecting any alterations to your pickle file data.<\/p>\n\n\n\n<p>Cryptographic signature uses a cryptographic algorithm which generates a cryptographic hash of your pickle file data along with shared secret key. SHA-1 cryptographic algorithm is considered to be the best algorithm to create a stronger hash, hence, it is highly advisable to use it.<br><\/p>\n\n\n<h2>Options to deploy your ML model in production<\/h2>\n<h3>First option<\/h3>\n\n\n<p>One way to deploy your ML model is, simply save the trained and tested ML model (<em>sgd_clf<\/em>), with a proper relevant name (e.g. <em>mnist<\/em>), in some file location on the production machine. The consumers can read (restore) this ML model file (<em>mnist.pkl<\/em>) from this file location and start using it to make predictions on their dataset.<br><\/p>\n\n\n\n<p>But, simply deploying your ML model file on the production machine may not be sufficient, as only a handful of consumers who have access to your production machine, will be able to use it. <\/p>\n\n\n<h3>Second option<\/h3>\n\n\n<p>In most of the cases, the consumers of your model may not limited to your team members, who have access to your production machine. There may be consumers, who are from different departments (and located globally) who don&#8217;t have access to your production environment. Also, if you are building the ML model to be consumed <em>&#8220;as a service&#8221;<\/em> by public (anyone), then, in that case also your consumers will not have access to your production environment.<\/p>\n\n\n\n<p>In this case, where the consumers don&#8217;t have access to your production environment, how do you make your deployed ML model available to them?<\/p>\n\n\n\n<p>The answer is &#8211; exposing your deployed ML model to the consumers, as a service (say REST service or REST API).<br><\/p>\n\n\n\n<p>REST API increases the reach of your ML model to wider audience and as it can be called from any Application &#8211; mobile app, Java application, web application, .Net application, PHP\/javascript, Python etc.<br><\/p>\n\n\n\n<p>The &#8216;second option&#8217; option seems to be a good option, if your audience is globally located and you want to provide this ML model as a service to a wider audience.<br>Hence, in this article, we will be focussing our discussion around this &#8216;second option&#8217; &#8211; exposing your ML model as a REST service.<br><\/p>\n\n\n<h2>Tools and libraries to build REST API in Python<\/h2>\n\n\n<p>Most of the ML models are written using Python libraries now a days, hence, in this article, we will discuss about how to expose your ML model as a REST service using Python frameworks.<\/p>\n\n\n\n<p>The most common Python framework, which is used to create a REST API in Python is, Flask.<\/p>\n\n\n\n<p><strong>Flask<\/strong> is a lightweight micro web framework written in Python, which can be used to create small web applications in Python.<\/p>\n\n\n\n<p>Flask is not only a framework, but it also has a web server, which could be used to develop and test small Python web applications.<\/p>\n\n\n\n<p>Flask also has APIs\/functions using which you can create a REST API (service) in Python, and for testing this REST service, you can deploy in on Flask web server.<br><\/p>\n\n\n\n<p>However, since, Flask is a lightweight web server, it should not be used in production environment, to deploy your web application or REST service. It should only be used in development environment, for development and testing of the web applications or REST API.<\/p>\n\n\n\n<p>For production purposes, you can deploy this REST service (written using Python and Flask framework) on a more matured WSGI protocol compliant application server like Gunicorn or <strong>uWSGI<\/strong> along with <strong>Nginx<\/strong> as the web server.<br><\/p>\n\n\n\n<p>If you are coming from Java background, then, you can think of:<\/p>\n\n\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul id=\"mce_5\" class=\"editor-rich-text__tinymce mce-content-body\" role=\"textbox\" contenteditable=\"true\" aria-label=\"Write list\u2026\" aria-autocomplete=\"list\" aria-multiline=\"true\" data-is-placeholder-visible=\"false\">\n<li>Flask framework as something like Spring or Jersey framework &#8211; to create REST API<\/li>\n<li>Nginx as like Apache Tomcat &nbsp;&#8211; web server<\/li>\n<li>uWSGI (or Gunicorn) as like JBoss or Websphere &nbsp;&#8211; application server<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<p><\/p>\n\n\n<figure class=\"wp-block-image\"><img width=\"575\" height=\"376\" src=\"https:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment-2.png\" alt=\"Process to build and deploy a REST service (for ML model) in production\" class=\"wp-image-2152\" srcset=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment-2.png 575w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment-2-300x196.png 300w\" sizes=\"(max-width: 575px) 85vw, 575px\" \/><figcaption>Process to build and deploy a REST service (for ML model) in production<\/figcaption><\/figure>\n\n\n<h2>Building (and testing) your REST API (service) using Flask framework<\/h2>\n\n\n<p>In your ML model training Python code, you can save your trained and tested ML model (say <em>sgd_clf<\/em>), using a proper file name, on a file location of your production application server using <em>joblib<\/em> library of Python, as shown below:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># dump the model to the file\njoblib.dump(sgd_clf, \"trained_models\/mnist_model.pkl\")<\/code><\/pre>\n\n\n\n<p>Here, ML model <em>sgd_clf<\/em> is being saved with a file name <em>mnist_model.pkl <\/em>in file location <em>&#8216;trained_models\/&#8217; <\/em>on the production machine. This ML model you can retrieve (restore) inside your REST API code to make the predictions on the input digit images.<br><\/p>\n\n\n\n<p>Below is the code in Python (using Flask framework) to create a REST API (<em>predict_image()<\/em>) which predicts a digit from a given image (containing a handwritten digit). <br><\/p>\n\n\n\n<p>Currently, this REST API just takes a &#8216;image file name&#8217; (<em>&#8216;file&#8217;<\/em>) as input (sent in the request)<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>import numpy as np\nfrom sklearn.externals import joblib\nfrom PIL import Image\nfrom flask import Flask, jsonify, request\n\n# Create flask app\napp = Flask(__name__)\n\n# Load the previously trained model from the file\nmodel = joblib.load(\"..\/trained_models\/mnist_model.pkl\")\n\n# \/predict is the end point\n@app.route('\/predict', methods=[\"POST\"])\ndef predict_image():\n    # Read the image uploaded by the curl command\n    requested_img = request.files['file']\n\n    '''\n    Convert the uploaded image to grayscale.\n    Since in MNIST the training images are grayscaled hence we will have to convert the uploaded image to grayscale\n    '''\n    grayscale_img = Image.open(requested_img).convert('L')\n\n    '''\n    Resize the uploaded image to 28x28 pixels.\n    Since in MNIST the training images are of 28x28 pixels hence we will have to resize the uploaded image to 28x28 pixels.\n    '''\n    resized_image = grayscale_img.resize((28,28))\n    \n    # Convert the image to an array\n    img = np.asarray(resized_image)\n\n    # Reshape the image to (784, 1)\n     img = img.reshape(784,)\n\n    # Predict the digit using the trained model\n    pred = model.predict(img.reshape(1, -1))\n\n    # Get the digit\n    result = int(pred.tolist()[0])\n\n    # Return the JSON response\n    return jsonify({\"digit\": result})\n<\/code><\/pre>\n\n\n\n<p>The below line of code loads (retrieves) the stored (saved) ML model <em>mnist_model.pkl<\/em> from the file location <em>&#8216;..\/trained_models\/&#8217; <\/em>using the Scikit Learn&#8217;s (sklearn) &nbsp;library <em>joblib<\/em> and stores it in a variable called <em>model<\/em>.<\/p>\n\n\n\n<p>We can use this <em>model<\/em> object to make the ML predictions using its <em>predict() <\/em>function. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Load the previously trained model from the file\nmodel = joblib.load(\"..\/trained_models\/mnist_model.pkl\")<\/code><\/pre>\n\n\n\n<p>We use something called as <em>decorators<\/em> in Python (like we have annotations in Java\/Spring) to modify behaviour of a function or a class.  <br><\/p>\n\n\n\n<p>Below, we are defining a <em>decorator<\/em> ( <em>@app.route(&#8230;)<\/em> ) for the <em>predict_image() <\/em>function, which says that any &#8216;POST&#8217; request URL which matches <em>&#8216;\/predict&#8217;<\/em> pattern, should be redirected to this function &#8211; <em>predict_image()<\/em>. <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@app.route('\/predict', methods=[\"POST\"])\ndef predict_image():<\/code><\/pre>\n\n\n\n<p>Thus, here, we are defining the <em>&#8216;End Point&#8217;<\/em> for our REST API (<em>predict_image()<\/em>). The <em>&#8216;End Point URL&#8217;<\/em> for your REST API will look like as below<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>http:\/\/mydomain.com:8080\/predict<\/code><\/pre>\n\n\n\n<p>To convert an input image to <em>grayscale<\/em> image, which is required by <em>mnist<\/em> ML model (as the model was trained on <em>grayscale<\/em> images), we can use <em>convert()<\/em> function of <em>Image<\/em> module of Python&#8217;s Pillow library. <br><\/p>\n\n\n\n<p>Please don&#8217;t forget to pass value &#8216;L&#8217; value to the convert() function.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>grayscale_img = Image.open(requested_img).convert('L')<\/code><\/pre>\n\n\n\n<p>Now, we can make predictions on this input image using the <em>model<\/em> object, using its <em>predict()<\/em> function.  <\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Predict the digit using the trained model \u00a0\u00a0\npred = model.predict(img.reshape(1, -1))<\/code><\/pre>\n\n\n\n<p>We have used Flask&#8217;s jsonify() function to convert our &#8216;result&#8217; of the ML model to a JSON object. Our REST API <em>predict_image()<\/em> returns result in JSON format.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>return jsonify({ \u201cdigit\u201d: result })<\/code><\/pre>\n\n\n\n<p>This JSON object (&#8216;digit&#8217;: result), you can use to retrieve the result in your calling application. <\/p>\n\n\n\n<p>This REST API can be called from any application &#8211; mobile app, Java application, web application, .Net application, PHP\/javascript, Python, etc. The only information your consumers need about this REST API is its <em>&#8216;end point URL&#8217;<\/em> and the credentials to access it.<br><\/p>\n\n\n\n<p>Using a web browser to test a web service isn&#8217;t the best idea since web browsers can&#8217;t generate all types of http requests easily. Instead, use <strong>&#8216;curl&#8217;<\/strong>command of unix to test your REST API.<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>curl -F 'file=@test-images\/7.png' 127.0.0.1:4041\/predict<\/code><\/pre>\n\n\n\n<p>Still some more work needs to be done, after you have created and deployed your REST API, you need to secure it. But, not all REST API functions may need to be secured.<\/p>\n\n\n\n<p>You can use HTTP secure server (<em>https:\/\/\u2026.<\/em>) to encrypt all the communications, to make your REST service communications more secure. <\/p>\n\n\n\n<p>You can secure your REST service using Flask extension called flask-httpauth. Using <em>@auth.login_required<\/em> decorator with your REST API functions, you can specify which functions (APIs) in the REST service are secured (protected). <\/p>\n\n\n\n<p>You can use a <em>&#8216;token&#8217;<\/em> based authentication for requests. In this type of authentication, the client application (which is calling this REST API), for the first request, sends the credentials to the REST service, and in return gets back a token, which it needs to send to the REST service in all its future requests. You can use any of the hashing algorithms (like SHA1) to create these tokens.<br><\/p>\n\n\n\n<p>Also, you may get <em>unauthorized access<\/em> error thrown from these protected REST APIs (functions), which would need to be handled in the code.<\/p>\n\n\n\n<p>In your REST API code file, you can write a separate function to handle these authorization errors. And, to specify, which function in your REST API code file, will handle these authorization errors, you can use <em>@auth.error_handler <\/em>decorator on top of such a function, as shown below:<br><\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>@auth.error_handler\ndef unauthorized():<\/code><\/pre>\n\n\n\n<p>Here, unauthorized() is the function that will be invoked if your protected REST API function throws an <em>unauthorized access<\/em> error. You can return a relevant error message from this function as a response.<br><\/p>\n\n\n\n<p> There are many other security implementations available that you can use according to the level of your security requirements. The main idea is to secure your REST API.<br><\/p>\n\n\n<h2>Achieving Scalability and Fault Tolerance for your deployed ML model<\/h2>\n\n\n<p>When you are creating the deployment plan for your model, you may need to consider two important aspects &#8211; Scalability and Fault Tolerance.<\/p>\n\n\n\n<p>To achieve fault tolerance and scalability for your REST service, you can use Docker and Kubernetes for the deployment.<br><\/p>\n\n\n\n<p>Using Docker application, you can package your REST service code\/application in a Docker container. For this you need to create a Dockerfile for your REST service application (code), mentioning the following:<\/p>\n\n\n<ul>\n<li style=\"list-style-type: none;\">\n<ul>\n<li style=\"list-style-type: none;\">\n<ul id=\"mce_6\" class=\"editor-rich-text__tinymce mce-content-body\" role=\"textbox\" contenteditable=\"true\" aria-label=\"Write list\u2026\" aria-autocomplete=\"list\" aria-multiline=\"true\" data-is-placeholder-visible=\"false\">\n<li>the OS (operating system) requirement for your application, say CentOS 7, Ubuntu 16.04 LTS, etc.<\/li>\n<li>images of required libraries, servers like &#8211; Python, Flask, Nginx, uWSGI<\/li>\n<li>working directory of your application<\/li>\n<li>pip install command to install all the dependent libraries and packages (e.g. Flask, Pillow, etc.) for your code as listed in your requirements.txt file.<\/li>\n<li>Python command to create package file for your REST API application\/code<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n<\/li>\n<\/ul>\n\n\n<p>You can use the above created Dockerfile to create a Docker container using <em>&#8216;docker build&#8217; <\/em>command.<br><\/p>\n\n\n\n<p>Now, after packaging your REST service in a Docker container, you can deploy and run your Docker containers on any machine or the VM (virtual machine). <\/p>\n\n\n\n<p>Docker enables you to create new Docker containers (for your REST service). And Kubernetes can be used to deploy these Docker containers on a Kubernetes cluster.<br><\/p>\n\n\n\n<p>Since, you can spin (create) new Docker containers on the fly (say when an existing container goes down or you need new containers due to increase in user requests), it provides you the required <em>&#8216;fault tolerance&#8217;<\/em>.<\/p>\n\n\n\n<p>Kubernetes cluster (cluster of machines) provides the required <em>&#8216;scalability&#8217; <\/em>for your REST service, it can spin (create) new Docker containers based on demand, using the Docker image. Kubernetes master node which also takes care of <em>&#8216;fault tolerance&#8217;<\/em>, if a container goes down, it spins another Docker container.<\/p>\n\n\n\n<p>Kubernetes also has a load balancer mechanism, which takes care of distributing the load (user requests) on the containers.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img width=\"607\" height=\"461\" src=\"https:\/\/blog.cloudxlab.com\/wp-content\/uploads\/2019\/04\/Deployment-Architecture-for-REST-service.png\" alt=\"Deployment architecture for REST service using Nginx, uWSGI, Flask, Docker and Kubernetes\" class=\"wp-image-2155\" srcset=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Deployment-Architecture-for-REST-service.png 607w, https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Deployment-Architecture-for-REST-service-300x228.png 300w\" sizes=\"(max-width: 709px) 85vw, (max-width: 909px) 67vw, (max-width: 984px) 61vw, (max-width: 1362px) 45vw, 600px\" \/><figcaption>Deployment architecture for REST service using Nginx, uWSGI, Flask, Docker and Kubernetes<\/figcaption><\/figure>\n\n\n<h2>Kubernetes, Spark MLLib and TensorFlow Distributed<\/h2>\n\n\n<p>Since, we are discussing about clusters (of machines) and Kubernetes, thought of sharing a small note on Kubernetes clusters and Spark clusters. Often, when we talk about Big Data and Spark MLLib, people get confused between Kubernetes clusters and Spark clusters. When to use which cluster?<\/p>\n\n\n\n<p>To understand, when (<em>and why<\/em>) we use Kubernetes clusters and when we use Spark clusters, we need to understand their purpose of use. <br><\/p>\n\n\n\n<p>Spark MLLib is basically a library of Spark, which has various Machine Learning algorithms (which are also available in Scikit Learn), customized to run on a Spark cluster i.e. using multiple machines. We use the ML algorithms from Spark MLLib library (in place of normal Scikit Learn version of ML algorithms), when our dataset is so huge that we need <em>Big Data<\/em> kind of processing to reduce the training and prediction time of our ML model.<\/p>\n\n\n\n<p>For Deep Learning models, you can use TensorFlow Distributed instead of Spark MLLib.<br><\/p>\n\n\n\n<p>Hence, in a nutshell, we use Spark MLLib on Spark cluster to reduce the training and prediction time of our ML model.<\/p>\n\n\n\n<p>Whereas, we use Kubernetes cluster is used to achieve <em>&#8216;scalability&#8217;<\/em>, of the finally trained and tested ML model, when we deployed it on production. <\/p>\n\n\n\n<p>When we get multiple requests simultaneously, Kubernetes spins new Docker containers (containing your ML model) and distributes the requests to the multiple containers to reduce the load.<br><\/p>\n\n\n\n<p>Hence, after we have trained and tested our Spark MLLib Machine Learning model, using huge amount of data (Big Data), on a Spark cluster, we can package and deploy the same on a Kubernetes cluster in production.<br><\/p>\n\n\n\n<p>But, in case, your requirement is to run Spark on a Kubernetes cluster of machines, you can do so, as Spark (<em>version 2.3 onwards<\/em>) supports this. You can create Docker containers for Spark nodes and deploy these Docker containers on Kubernetes cluster (of machines). Spark (<em>version 2.3 and above<\/em>) already comes with a Dockerfile that you can use for this purpose. For more details on this, you can refer <a href=\"https:\/\/spark.apache.org\/docs\/2.3.0\/running-on-kubernetes.html \">Spark documentation<\/a>.  <br><\/p>\n\n\n<h2>Case Studies<\/h2>\n\n\n<p>Below are a few case studies for different types of deployment modes:<br><\/p>\n\n\n<ol>\n<li style=\"list-style-type: none;\">\n<ol id=\"mce_7\" class=\"editor-rich-text__tinymce mce-content-body\" role=\"textbox\" contenteditable=\"true\" aria-label=\"Write list\u2026\" aria-autocomplete=\"list\" aria-multiline=\"true\" data-is-placeholder-visible=\"false\">\n<li>Building a<em> &#8216;You may also like it&#8217;<\/em> Amazon recommendations:\n<ol>\n<li>Perform ML model training on Spark Cluster<\/li>\n<li>Perform prediction on the ML model on a scalable environment built using a mature application server and Kubernetes cluster.<\/li>\n<\/ol>\n<\/li>\n<li>Say, on &nbsp;a huge database like &nbsp;LinkedIn, we need to find profile pictures which don&#8217;t have faces\n<ol>\n<li>Train a Deep Learning Model using GPU etc.<\/li>\n<li>Run the prediction on the yarn cluster (Spark, EC2, etc.) or Kubernetes<\/li>\n<\/ol>\n<\/li>\n<li>Build a small gadget that detects motions or tracks faces without any internet connectivity\n<ol>\n<li>Train a Deep Learning Model using CPU\/GPU etc. and push it to a device (e.g. mobile phone)<\/li>\n<li>Prediction on the device without any service. In this case you need to copy the model on the device itself.<\/li>\n<\/ol>\n<\/li>\n<li>Build a small gadget that detects motions or tracks faces without any internet connectivity\n<ol>\n<li>Train a Deep Learning Model using CPU\/GPU etc. and push it to a device (e.g. mobile phone)<\/li>\n<li>Prediction on the device without any service. In this case you need to copy the model on the device itself.<\/li>\n<\/ol>\n<\/li>\n<li>Build a firewall which predict the packets as malicious or not\n<ol>\n<li>Train a Deep Learning Model using CPU\/GPU etc. and push it to the router.<\/li>\n<li>Prediction on the router without any service. You will need to copy the model on the router for this.<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n<\/li>\n<\/ol>\n\n\n<p>        <\/p>\n\n\n\n<p>Again, complete code for creating a REST service for your Machine Learning model, can be found at the below link:<\/p>\n\n\n\n<p> <a href=\"https:\/\/github.com\/cloudxlab\/ml\/tree\/master\/projects\/deploy_mnist \">https:\/\/github.com\/cloudxlab\/ml\/tree\/master\/projects\/deploy_mnist <\/a><br><br><\/p>\n\n\n\n<p>For the complete course on Machine Learning, please visit&nbsp;<a href=\"https:\/\/cloudxlab.com\/course\/specialization\/1\/machine-learning-specialization\">Specialization Course on Machine Learning &amp; Deep Learning<\/a><br><\/p>\n\n\n\n<p><br><\/p>\n","protected":false},"excerpt":{"rendered":"<p>In this article, I am going to explain steps to deploy a trained and tested Machine Learning model in production environment. Though, this article talks about Machine Learning model, the same steps apply to Deep Learning model too. Below is a typical setup for deployment of a Machine Learning model, details of which we will &hellip; <a href=\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Deploying Machine Learning model in production&#8221;<\/span><\/a><\/p>\n","protected":false},"author":21,"featured_media":2148,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":[],"categories":[29,28,13],"tags":[19,17,16],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v16.2 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Deploying Machine Learning model in production | CloudxLab Blog<\/title>\n<meta name=\"description\" content=\"This blog explains various ways to deploy your Machine Learning or Deep Learning model in production using various tools like Flask, Docker, Kubernetes, etc\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Deploying Machine Learning model in production | CloudxLab Blog\" \/>\n<meta property=\"og:description\" content=\"This blog explains various ways to deploy your Machine Learning or Deep Learning model in production using various tools like Flask, Docker, Kubernetes, etc\" \/>\n<meta property=\"og:url\" content=\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/\" \/>\n<meta property=\"og:site_name\" content=\"CloudxLab Blog\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/cloudxlab\" \/>\n<meta property=\"article:published_time\" content=\"2019-04-16T08:34:28+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-02T12:57:11+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment.png\" \/>\n\t<meta property=\"og:image:width\" content=\"575\" \/>\n\t<meta property=\"og:image:height\" content=\"376\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:site\" content=\"@CloudxLab\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\">\n\t<meta name=\"twitter:data1\" content=\"15 minutes\">\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebSite\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"CloudxLab Blog\",\"description\":\"Learn AI, Machine Learning, Deep Learning, Devops &amp; Big Data\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":\"https:\/\/cloudxlab.com\/blog\/?s={search_term_string}\",\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/#primaryimage\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment.png\",\"contentUrl\":\"https:\/\/cloudxlab.com\/blog\/wp-content\/uploads\/2019\/04\/Machine-Learning-Model-Deployment.png\",\"width\":575,\"height\":376,\"caption\":\"Machine Learning Model Deployment\"},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/#webpage\",\"url\":\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/\",\"name\":\"Deploying Machine Learning model in production | CloudxLab Blog\",\"isPartOf\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/#primaryimage\"},\"datePublished\":\"2019-04-16T08:34:28+00:00\",\"dateModified\":\"2019-05-02T12:57:11+00:00\",\"author\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/cb0503c4e740565d85cd28a1b167f48b\"},\"description\":\"This blog explains various ways to deploy your Machine Learning or Deep Learning model in production using various tools like Flask, Docker, Kubernetes, etc\",\"breadcrumb\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"item\":{\"@type\":\"WebPage\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/\",\"url\":\"https:\/\/cloudxlab.com\/blog\/\",\"name\":\"Home\"}},{\"@type\":\"ListItem\",\"position\":2,\"item\":{\"@id\":\"https:\/\/cloudxlab.com\/blog\/deploying-machine-learning-model-in-production\/#webpage\"}}]},{\"@type\":\"Person\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#\/schema\/person\/cb0503c4e740565d85cd28a1b167f48b\",\"name\":\"Deepak Singh\",\"image\":{\"@type\":\"ImageObject\",\"@id\":\"https:\/\/cloudxlab.com\/blog\/#personlogo\",\"inLanguage\":\"en-US\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/93f963a3a3600e7852f1a3677966d5c4?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/93f963a3a3600e7852f1a3677966d5c4?s=96&d=mm&r=g\",\"caption\":\"Deepak Singh\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","_links":{"self":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1886"}],"collection":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/comments?post=1886"}],"version-history":[{"count":248,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1886\/revisions"}],"predecessor-version":[{"id":2212,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/posts\/1886\/revisions\/2212"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media\/2148"}],"wp:attachment":[{"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/media?parent=1886"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/categories?post=1886"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cloudxlab.com\/blog\/wp-json\/wp\/v2\/tags?post=1886"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}