Things to know about Machine learning(ML) models on cloud
Tags: aws, machinelearning, cloud, datascience
Published: 2022-03-31 12:17:45

<!-- wp:paragraph -->
<p>The ability to make decisions is dependent on large volumes of historical data. And as machines are getting better at making decisions by understanding the data. Developers need to comprehend why and when to use Machine 
<!-- wp:paragraph -->
<p>This blog explains what ML is and how Distributed ML works. We will be covering Amazon Rekognition, Amazon Lex, Amazon Sagemaker, and Amazon EMR.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"textAlign":"center","level":3} -->
<h3 class="has-text-align-center"><strong>Q.What is Machine learning?</strong></h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Machine learning is a branch of artificial intelligence (AI) and computer science which focuses on the use of data and algorithms to imitate the way that humans learn, gradually improving its accuracy.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="https://analyticsinsight.b-cdn.net/wp-content/uploads/2021/08/ML-System.jpg" alt=""/><figcaption>Image Credits: https://analyticsinsight.b-cdn.net/wp-content/uploads/2021/08/ML-System.jpg</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>An example of low code Machine learning solutions. <strong>When considering Machine learning solutions, there are many use cases to consider</strong>. Let us consider a few sample use cases,</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"textAlign":"center","level":5} -->
<h5 class="has-text-align-center">A. <strong>Analyse images and videos</strong></h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Amazon Rekognition makes it easy to add image and video analysis to your applications. You just provide an image or video to the Amazon Rekognition API, and the service can identify objects, people, text, scenes, and activities. It can detect any inappropriate content as well.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Images </strong><strong>are uploaded</strong><strong> to the Rekognition service in one of two ways.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Store the image file within an S3 bucket and then provide the S3 location of the image to the Rekognition service. </li><li>Base64-encode the image data and supply this as an input parameter to the API operation.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>Amazon Rekognition provides highly accurate facial analysis, face comparison, and face search capabilities.<strong> Some common use cases for using Amazon Rekognition include the following:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Face-based user verification</li><li>Sentiment and demographic analysis</li><li>Images and stored videos searchable so you can discover objects and scenes that appear within them.</li><li>Y<span style="color:initial;">ou can search images, store videos, and stream videos for faces that match those stored in a container known as a face collection. </span></li><li>Detect adult and violent content in images and stored videos. Developers can filter inappropriate content based on their business needs, using metadata. </li><li>Recognise and extract textual(text and numbers) content from images in different orientations.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d2908q01vomqb2.cloudfront.net/fc074d501302eb2b93e2554793fcaf50b3bf7291/2020/08/22/Video-Redaction-1024x575.png" alt=""/><figcaption>Image Credits: https://aws.amazon.com/blogs/architecture/category/artificial-intelligence/amazon-rekognition/</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Hence we have a separate API to processing videos and video streams.</strong> Since, Processing video files need more compute and thus<strong> s</strong>everal of the Video API operations are asynchronous.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>With the video processing APIs, you always host the video to </strong><strong>be processed</strong><strong> as a file within an S3 bucket.</strong> You then supply the S3 file location as an input parameter to the respective start operation.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>With Custom Labels, you can identify the objects and scenes in images and videos that are specific to your business needs</strong><strong>. </strong>For more information, refer</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a href="https://docs.aws.amazon.com/rekognition/latest/customlabels-dg/what-is.html">https://docs.aws.amazon.com/rekognition/latest/customlabels-dg/what-is.html</a> </li><li><a href="https://aws.amazon.com/rekognition/resources/?nc=sn&amp;loc=6">https://aws.amazon.com/rekognition/resources/?nc=sn&amp;loc=6</a></li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center","level":5} -->
<h5 class="has-text-align-center"><strong>B. Create a Chatbot using Amazon Lex</strong></h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Amazon Lex can be used to create and embed chatbots into your applications. Internally, the Amazon Lex service uses the same deep learning engine that powers Amazon Alexa.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Amazon Lex uses automatic speech recognition(ASR) for converting speech to text, and natural language understanding(NLU) to recognise the intent of the text.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>The unit of build and deployment within Amazon Lex is the bot itself. Developers can build and deploy multiple bots, each with its own set of skills and behaviours. An intent represents a kind of outcome or action that the bot may perform. A single bot can be composed of multiple intents. For each intent, you need to provide the following attributes:</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Intent name: A descriptive name describing what the intent accomplishes. </li><li>Utterances: One or several phrases the user speaks or types activate the intent. </li><li>Fulfilment process: The method used to complete or fulfil the intent.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>Amazon Lex also provides several built-in intents that you can leverage</strong>. Each intent may need and have to request extra attributes(slots) from the user to complete this intended outcome. <strong>Each slot you define requires you to specify a slot type</strong>. You can define and create your custom slot types, or leverage any of the inbuilt slot types.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d2908q01vomqb2.cloudfront.net/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59/2020/10/22/1-SolutionArchitecture.jpg" alt=""/><figcaption>Image Credits: https://aws.amazon.com/blogs/machine-learning/building-a-real-time-conversational-analytics-platform-for-amazon-lex-bots/</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>After the deployment of your chatbot, Amazon Lex provides a feature to </strong><strong>monitor</strong><strong> and track so-called missed utterances</strong><strong>.</strong> For which Amazon Lex cannot match at runtime against any of the registered utterances.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Amazon Lex can integrate into other messaging platforms using channels. All network connections established to Amazon Lex are done so only using HTTPS. Hence they are encrypted, and can thus be considered secure. Additionally, the Amazon Lex API requires a signature to be calculated and supplied with any API calls.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>For more information on various machine learning general use case solutions refer to,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li><a href="https://aws.amazon.com/machine-learning/">https://aws.amazon.com/machine-learning/</a></li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>If you don't prefer Low code solution, you should try looking into Sagemaker.</strong> They are good for computing and deploying your ML models, as you get AWS compute servers.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"textAlign":"center","level":3} -->
<h3 class="has-text-align-center">Q.What is a Sagemaker?</h3>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>At its core, sagemaker is a fully managed service that provides the tools to build, train and deploy machine learning models. It has some components in it such as managing notebooks and helping label and train models deploy models with a variety of ways to use endpoints.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>SageMaker algorithms are available via container images</strong>. Each region that supports SageMaker has its copy of the images. You will begin by retrieving the URI of the container image for the current session's region. You can also utilise your own container images for specific ML algorithms.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"align":"center","sizeSlug":"large"} -->
<figure class="wp-block-image aligncenter size-large"><img src="http://programmerprodigycode.files.wordpress.com/2022/03/0bc53-1mfyty2swftpsulqybcgy-w.png" alt=""/><figcaption>Image Credits: http://programmerprodigycode.files.wordpress.com/2022/03/0bc53-1mfyty2swftpsulqybcgy-w.png</figcaption></figure>
<!-- /wp:image -->

<!-- wp:heading {"textAlign":"center","level":4} -->
<h4 class="has-text-align-center"><strong>Q.How can we host Sagemaker models?</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>SageMaker can host models through its hosting services. The model is accessible to the client through a SageMaker endpoint. The Endpoint is accessible over HTTPS and SageMaker Python SDK.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Another way would be using AWS Batch. <strong>It manages the processing of large datasets within the limits of specified parameters</strong>. When a batch transform job starts, SageMaker initialises compute instances and distributes the inference or pre-processing workload between them.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>In Batch Transform, you provide your inference data as an S3 URI and SageMaker will care of downloading it, running the prediction and uploading the results afterwards to S3 again</strong><strong>.</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Batch Transform partitions the Amazon S3 objects in the input by key and maps Amazon S3 objects to an instance. To split input files into mini-batches you create a batch transform job, set the SplitType parameter value to Line. You can control the size of the mini-batches by using the BatchStrategy and MaxPayloadInMB parameters.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>After processing, it creates an output file with the same name and the .out file extension. The batch transforms job stores the output files in the specified location in Amazon S3, such as s3://awsexamplebucket/output/.</p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d2908q01vomqb2.cloudfront.net/f1f836cb4ea6efb2a0b1b99f41ad8b103eff4b59/2019/08/21/inference-with-tensorflow-1.gif" alt=""/><figcaption>Image Credits: https://aws.amazon.com/blogs/machine-learning/performing-batch-inference-with-tensorflow-serving-in-amazon-sagemaker/</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>The predictions in an output file are in the same order as the corresponding records in the input file. To combine the results of many output files into a single output file, set the AssembleWith parameter to Line.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>For more information refer,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>https://docs.aws.amazon.com/sagemaker/latest/dg/batch-transform.html </li><li>https://docs.aws.amazon.com/sagemaker/latest/dg/how-it-works-batch.html </li><li>https://docs.aws.amazon.com/sagemaker/latest/dg/ex1-model-deployment.html#ex1-batch-transform</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center","level":4} -->
<h4 class="has-text-align-center"><strong>Q. What is distributed machine learning?</strong></h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>When machine learning processes have been deployed across a cluster of computing resources. So why use Distributed Machine learning?</p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>To paralyse your machine learning processing requirements, allowing you to achieve quicker results </li><li>The complexity of the data set (features) may exceed the capabilities of a single node setup. </li><li>The accuracy of a machine learning model can be enhanced by processing more data. This, in turn, is connected back to the large datasets point above.</li></ol>
<!-- /wp:list -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://www.guavus.com/wp-content/uploads/2020/05/centralised-decentralised-1024x474.png" alt=""/><figcaption>Image Credits: https://www.guavus.com/wp-content/uploads/2020/05/centralised-decentralised-1024x474.png</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p><strong>Apache Spark can provision a cluster of machines, configured in a manner that provides a distributed computing engine</strong>. Your datasets are partitioned and spread across the Spark cluster, allowing the cluster to process the data in parallel.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Q. Why Apache Spark?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Spark contains Resilient Distributed datasets (RDD) which saves time taken in reading and writing operations. </li><li>In-memory computing: In spark, data is stored in the RAM, so it can access the data quickly and speed up the speed of analytics. </li><li>Flexible: Spark supports many languages and allows the developers to write applications in Java, Scala, R or Python. </li><li>Resilient Distributed Datasets(RDD) are designed to handle the failure of any worker node in the cluster. </li><li>Better analytics: Spark has a rich set of SQL queries, machine learning algorithms, complex analytics.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>MLlib is Apache Spark's scalable machine learning library.</strong> It contains fast and scalable implementations of standard machine learning algorithms.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Q. Why Spark MLLib?</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>Spark MLlib is on top of Spark which eases the development of efficient large-scale machine learning algorithms. </li><li>MLlib is easy to deploy and does not need any pre-installation if Hadoop cluster is already installed and running.</li><li>MLlib provides ultimate performance gains (about 10 to 100 times faster than Hadoop and Apache Mahout).</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For more information on Apache Spark and MLLib, refer to,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>https://programmerprodigy.code.blog/2022/01/09/introduction-to-apache-spark-sparkql-and-spark-mlib/</li></ol>
<!-- /wp:list -->

<!-- wp:heading {"textAlign":"center","level":5} -->
<h5 class="has-text-align-center">Q. <strong>How to use Distributed ML on AWS Cloud?</strong></h5>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Amazon EMR provides a managed Hadoop frame loop that makes it easy, fast and cost effective to process vast amounts of data. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>Amazon EMR can be used to perform: log analysis, web indexing, ETL, financial forecasting, bioinformatics and, machine learning. </p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Amazon EMR, together with Spark, simplifies the task of cluster and distributed job management. As we can use Amazon EMR at every stage of the Machine Learning pipeline. </strong></p>
<!-- /wp:paragraph -->

<!-- wp:image {"sizeSlug":"large"} -->
<figure class="wp-block-image size-large"><img src="https://d1.awsstatic.com/products/EMR/Product-Page-Diagram_Amazon-EMR.803d6adad956ba21ceb96311d15e5022c2b6722b.png" alt=""/><figcaption>Image Credits: https://d1.awsstatic.com/products/EMR/Product-Page-Diagram_Amazon-EMR.803d6adad956ba21ceb96311d15e5022c2b6722b.png</figcaption></figure>
<!-- /wp:image -->

<!-- wp:paragraph -->
<p>You can customize the installation of applications that complement the core EMR Hadoop application. When you launch an EMR cluster, you need to define and allocate compute resources to three different nodes: Master, Core and Task.</p>
<!-- /wp:paragraph -->

<!-- wp:heading {"textAlign":"center","level":4} -->
<h4 class="has-text-align-center">Q. How to Select the right to compute instance?</h4>
<!-- /wp:heading -->

<!-- wp:paragraph -->
<p>Choosing a Compute instance completely biased on either price or compute, might not be a good option. <strong>As you select a cheaper compute instance, it takes you about 30 mins. But if you would have selected a better compute instance, it takes 10 mins.</strong> The second alternative would have been a better alternative economically and time-based.</p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p><strong>Some points to remember while choosing CPU and GPU will be</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>The CPU time grows proportional to the size of the matrix squared or cubed. </li><li>The GPU time grows almost linearly with the size of the matrix for the sizes used in the experiment. It can add more compute cores to complete the computation in much shorter times than a CPU. </li><li>Sometimes the CPU performs better than GPU for these small sizes. In general, GPU excel for large-scale problems. </li><li>For larger problems, GPUs can offer speedups in the hundreds. For Example, an application used for facial or object detection in an image or a video will need more computing. Hence GPUs might be a better solution.</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For more information, feel free to listen to my session on introduction to Algorithms and AWS Sagemaker:</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>https://vimeo.com/586886985/7faddfb340</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p><strong>For more information on Sagemaker,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:list {"ordered":true} -->
<ol><li>https://aws.amazon.com/blogs/aws/sagemaker/</li></ol>
<!-- /wp:list -->

<!-- wp:paragraph -->
<p>After considering all the no/low code solutions and coding solutions.<strong> Let's consider a use case,</strong></p>
<!-- /wp:paragraph -->

<!-- wp:paragraph -->
<p>If you have a relatively simple algorithm with a less diverse data set. Then i would recommend no/low code solution using a centralised  compute instance. If your algorithm is complicated, and your data set is diverse. Then i would recommend a distributed machine learning approach.</p>
<!-- /wp:paragraph -->