Deploy machine learning models on AWS Sagemaker.

What is Sagemaker..?
Amazon SageMaker provides every developer and data scientist with the ability to build, train, and deploy machine learning models quickly. Amazon SageMaker is a fully-managed service that covers the entire machine learning workflow to label and prepare your data, choose an algorithm, train the model, tune and optimize it for deployment, make predictions, and take action. Your models get to production faster with much less effort and lower cost.
What is the need of Sagemaker..!
SageMaker is a managed service offering from AWS with the intent of simplifying the process of building, training, and deploying machine learning models. Typically, developers have to spend a lot of time and effort during various stages of incorporating machine learning in their applications. They have to first find the right sources to collate the training data and then find the best algorithm as per their need. They will also set up training environments and start training the model mostly through trial and error. And finally, they will deploy the model on production. But the peril does not end here; the team also has to scale and manage the production environment.
Three major steps in creating a successful machine learning model:

Build:A good machine learning model requires large volumes of data — which is hard to collate — and a manual labeling process, which may take several weeks to complete. AWS offers SageMaker GroundTruth, which uses machine learning to automatically label data and results in considerable time savings. GroundTruth can also work hand-in-hand with manual labeling. This actually makes GroundTruth even better in labeling over a period of time. Where GroundTruth has high confidence is in the results it obtained previously. There, it will apply labels automatically on similar raw data, else it will forward the data for manual intervention.
Train:Training in SageMaker is very easy. Simply specify the S3 location containing data, choose the number and types of instances where the computing will be done, and launch the training by clicking a button in the AWS Console. SageMaker applies the algorithm you chose, builds the cluster of auto-scaling instances, and applies the chosen algorithm.
Automatic Model Tuning is another salient feature of SageMaker. Typically, to train data, you need to provide a variety of data to the model and provide the hyperparameters. SageMaker helps you with the first part by applying machine learning to ascertain the kind and variety of data to apply. It learns the effect that a particular type of data has on the tuning of the model and then subsequently applies that learning on the data.
Deploy:Deployment of your model using SageMaker gives you all the benefits of a Cloud provider — it is here that you truly utilize the Cloud power. Using the AWS Console, you deploy the model on a cluster of highly available compute instances that auto-scale as needed. Just like other AWS Compute services, SageMaker also takes care of monitoring the health of instances, ensures security by applying patches, and takes care of their maintenance. SageMaker integrates with CloudWatch where suitable metrics can be set up and alarms can be triggered so that timely action can be taken. It also connects with CloudWatch Logs, where you can see and debug your model’s execution.
Do we really need a platform?
The most effective way to solve large ML problems is by aiding a data scientist with the necessary software skills in neat abstract yet effective way to deliver an ML solution as a highly scalable web-service (API). The software development team can integrate the API into the required software systems and abstract the ML service as just another service wrapped around an API. (Software engineers love APIs). Therefore, we need a platform that can enable a data scientist with the necessary tools to independently execute a machine learning project in a truly end-to-end way.
HOW can a platform solve this problem?
We can solve the problems discussed above if we have a platform that supplements the required remainder skills in each of these phases with neat abstractions while still being highly effective and flexible for a data scientist to deliver results.
Thus, we need a platform where the data scientist will be able to leverage his existing skills to engineer and study data, train and tune ML models and finally deploy the model as a web-service by dynamically provisioning the required hardware, orchestrating the entire flow and transition for execution with simple abstraction and provide a robust solution that can scale and meet demands elastically.
How to Write the image in Sagemaker:
Since SageMaker machine learning training jobs are managed using Docker image, the first step to running the job is building the container.
When SageMaker launches a training image it injects a handful of files and environment variables from the estimator definition. The full list of resources injected is provided in the documentation. AWS uses these context clues to configure pre-built algorithm runs, but it doesn’t require that custom training jobs do the same, so you can ignore these until you actually need them.
SageMaker also in turn it expects the image to write outputs to specific places inside the container:
/opt/ml/output/failure
. If the training job fails, AWS SageMaker recommends writing the reason why to this file, however this is completely optional./opt/ml/model
, This directory is expected to contain a list of model artifacts created by the training job. AWS SageMaker will automatically harvest the files in this folder at the end of the training run,tar
them, and upload them to S3.
With that in mind, let’s examine an example Docker image that’s SageMaker compatible. Starting with the Dockerfile
:

The SageMaker job runner requires that your container image define an ENTRYPOINT
using the exec syntax (e.g. ENTRYPOINT ["some", "commands"]
), not the shell syntax (e.g. ENTRYPOINT some command
), as it needs to be able to send SIGTERM
and SIGKILL
signals to the container.
Additionally, when executing the container the SageMaker job runner will pass a run-time argument. If it is running the container in training model this will be train
; if the image is being deployed to an endpoint this will be deploy
. If you use the same image for both training your model and for deploying it, you will need to parse this argument to check which mode the container is executing in.
That’s it — that’s the full list of restrictions SageMaker places on your image configuration!
Conclusion:
AWS Sagemaker has been a great deal for most data scientists who would want to accomplish a truly end-to-end ML solution. It takes care of abstracting a ton of software development skills necessary to accomplish the task while still being highly effective and flexible and cost-effective. Most importantly, it helps you focus on the core ML experiments and supplements the remainder necessary skills with easy abstracted tools similar to our existing workflow.