Deep Dive to Train, Deploy and Evaluate a Model in Amazon SageMaker
Tags: aws, awssagemaker, awsiam, awss3
Published: 2021-12-19 09:27:15

“Problems faced to train a model as it is complex and time consuming”. Amazon SageMaker makes a task easy and quick to train, deploy and evaluate a model. It is also secured and cost optimized.

Amazon SageMaker is a managed service in the Amazon Web Services ([AWS](https://searchaws.techtarget.com/definition/Amazon-Web-Services)) public cloud. It provides the tools to build, train and deploy machine learning ([ML](https://www.techtarget.com/searchenterpriseai/definition/machine-learning-ML)) models for predictive analytics applications. The platform automates the tedious work of building a production-ready artificial intelligence (AI) pipeline. Machine learning has a range of uses and benefits. Among them are advanced analytics for customer data and back-end security threat detection. Deploying ML models is challenging, even for experienced application developers. Amazon SageMaker aims to simplify the process. It uses common algorithms and other tools to accelerate the machine learning process.

In this post, you will get to know how to train, deploy and evaluate a model in Amazon Sagemaker. Here I have created a sagemaker notebook instance and created a jupyter notebook where I have run python to train, deploy and evaluate a model with Amazon S3. Also checked the logs and metrics in cloudwatch.

#Architecture Overview
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/jifbx875as0po7ybbryo.png)
The architecture diagram shows the overall deployment architecture with data flow, Amazon SageMaker, Amazon S3.

#Solution overview
The blog post consists of the following phases:
1. Create a Amazon SageMaker Notebook Instance
2. Create a Jupyter Notebook and also Download, Explore and Transform a Dataset
3. Check of IAM role created and Train a Model 
4. Deploy the Model to SageMaker Hosting Services as Amazon EC2 and Evaluate the Model
5. Check of Log Streams in Cloudwatch Log Groups for SageMaker and other parameters in Metrics of Cloudwatch

##Phase 1: Create a Amazon SageMaker Notebook Instance 
1. Open the Amazon SageMaker console and then click on create a notebook instance. Given the notebook name as Notebook-demo and instance type as ml.t2.medium. Choose the platform identifier as notebook-al1-V1 and leave the other options as default. Create a new IAM role as for any s3 buckets. And then create a notebook instance. Once the notebook instance status is InService, we can check the configurations of the instance as per given. We can also modify the instance type and volume size after stopping the notebook instance. 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8w52kpdfchnft9f1zlue.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/eeevicddeun54077uygo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9vtont2c9wdo4uxuq87m.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inltxjn8zm3dwcns2ryx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nt02l5znnegjx7ctqrjz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/axkpa6oar8dtfdy5xej7.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qa4z7pzht4ptcggfcjqi.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yflvhrltsq5hx9ypxw68.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yqzg8o8ho432o93jpnqa.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qqac78hvb72c6qrvvcs8.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqyawa9fi9gw6f3ifoxe.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hhbeqiuranb7qp9hmxps.png)

##Phase 2: Create a Jupyter Notebook and also Download, Explore and Transform a Dataset
1. Open the JupyterLab then choose conda_python3 and save the notebook as test-notebook-demo.ipynb. And download, explore and transform a dataset by running python lines. Also able to restart the kernel by clicking the restart option on the upside bar.  You are able to train, validate and test the data in it. Also able to see the csv files for train and validation in s3 bucket.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/nkevk5df4gwywlmtsppi.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bsehl2bcad8raamoy4do.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mnxp4nqoec2fb56ne42e.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xtfb0bzo0y0cvurjp0d3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7ypvc6dqbcpa7l4nzicl.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r6487dih1vd5bowmq733.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/agojsmhtakdumecb83z3.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xjr5spikblvjn6x7z8gf.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2hbf31rools7yokpb032.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/o9ykva4apekio8gcjif5.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/too7wq7azplty2mu0m07.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1l1too4trtv4lkskhvzj.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/sb261baam48o5ht139li.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/bmxj5dkj241kp6010qd4.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/yamjakzpub3hn1lfa5fu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8g6aeq2dbt9acnhz2aqr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/272r35ifbnr9ikezys30.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/51wqd551xpzfq3kqt3ze.png)

##Phase 3: Check of IAM role created and Train a Model 
1. Check the permissions in the IAM role for sagemaker and create a folder in s3 bucket named as testdemo_model. And also run of python lines to train a model. Once the model gets trained, you are able to see the model artifact in s3 bucket.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1p8338dsmetl508g0a2s.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xycfk4oeigpncf17ekz6.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vrk6wibs6c3yivkgj7uo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/zjzjat1mn2nj79io4qj2.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2fndugxbivzz7rl25asx.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/x1kn95oye0fb0azb265q.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lb5otwng3nkpxse3fsnj.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/m8z0zdcvkvajrwrrs1sx.png)

##Phase 4: Deploy the Model to SageMaker Hosting Services as Amazon EC2 and Evaluate the Model
1. Run the python lines to deploy and evaluate the model. And can see the training jobs, models, endpoint configurations, endpoints in sagemaker console.
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tas9j3e6pwm2gigtwkpq.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/05c0uutimasduzulotmr.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/g8f12nc79vtrnsu96ryw.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5sgnhgdrsnfx1uhjzq7v.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7o1d3dghggfjrofcn5eu.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/coqz0w3p8kz7pb0wxnox.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/dnsi5bmre1s2z9eoayax.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/rdx48cm6230rehyqnsky.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8h7flwuauqcfkt8gupho.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/9j3sdteiig28ksn5svbv.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/u7fhuott39ad003s0gzz.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ju54jnngcanvjpm4uhjh.png)

##Phase 5: Check of Log Streams in Cloudwatch Log Groups for SageMaker and other parameters in Metrics of Cloudwatch
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/8mf6bo1ksvse6mz290hv.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7guogrbs34lg74af8262.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vbmhfafzve2yi0v1ytxo.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/c0ncyfaidp0t045oc815.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/4qy9gd4z9qsv57oubanm.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f7cycuq50aie75xcb805.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/97p98fzajsuhgdk3igni.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ubjmsnqz9xrm917x7ktd.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2ovi0opklk1rvfnff33l.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kms91qwqy0uz2107rllc.png)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2xs5duqj0iyrzioc1kbr.png)

#Clean-up
Delete Amazon SageMaker, Amazon Simple Storage Service, IAM, Cloudwatch Log group.

#Pricing
I review the pricing and estimated cost of this example. 
Cost of Sagemaker → 
Amazon Sagemaker createvolume-Gp2 = $0.02 
Amazon Sagemaker Invoke-Endpoint = $0.0
Amazon Sagemaker RunInstance = $0.77
Total = ($0.02+$0.0+$0.77) =$0.79
Cost of Simple Storage Service = $0.04 
Total Cost = $(0.79+0.04) = $0.83

#Summary
In this post, I showed “how to train, deploy and evaluate a Model in Amazon SageMaker”.

For more details on Amazon SageMaker, Checkout Get started Amazon SageMaker, open the [Amazon SageMaker console](https://ap-south-1.console.aws.amazon.com/sagemaker/home?region=ap-south-1#/landing). To learn more, read the [Amazon SageMaker documentation](https://docs.aws.amazon.com/sagemaker/?id=docs_gateway). For more details on Amazon S3, Checkout Get started Amazon S3, open the [Amazon S3 console](https://s3.console.aws.amazon.com/s3/home?region=ap-south-1). To learn more, read the [Amazon S3 documentation](https://docs.aws.amazon.com/s3/?id=docs_gateway).

Thanks for reading!

Connect with me: [Linkedin](https://www.linkedin.com/in/gargee-bhatnagar-6b7223114)
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/38sdi4nsp89fnui8h7dk.png)