Docker Deployment on AWS Sagemaker
Alex Egg,
Clone docker app: https://github.com/awslabs/amazon-sagemaker-examples/tree/master/advanced_functionality/scikit_bring_your_own
First train your model following the SageMaker Interface by building a docker container w/ a train
and serve
program.
- train: trains your model and serializes it to disk
- serve: deserializes your model and starts web stack
Then after you finish that pattern, you can use the Sagemaker training and serving infrastructure.
Local Deployment
Build the docker image:
This will build the docker image and push it to the remote repository (ECS):
cd local_test
sh build_and_push.sh
Train the docker image locally:
This will run the docker image and train the Scikit Decision Tree model and serialize it to disk.
sh train_local.sh 184771037180.dkr.ecr.eu-west-1.amazonaws.com/tree-test
Run the docker image locally:
This will deserialize the model from disk and start a web API stack around it:
- nginx
- gnunicorn
- flask
- Model
- flask
- gnunicorn
sh serve_locally.sh 184771037180.dkr.ecr.eu-west-1.amazonaws.com/tree-test
Test Inference Locally
This will query the model via REST API call:
sh predict.sh payload.csv
Sagemaker Deployment
Do this in sagemaker notebook
# S3 prefix
prefix = 'DEMO-scikit-byo-iris'
# Define IAM role
import boto3
import re
import os
import numpy as np
import pandas as pd
from sagemaker import get_execution_role
role = get_execution_role()
import sagemaker as sage
from time import gmtime, strftime
sess = sage.Session()
WORK_DIRECTORY = 'data'
data_location = sess.upload_data(WORK_DIRECTORY, key_prefix=prefix)
account = sess.boto_session.client('sts').get_caller_identity()['Account']
region = sess.boto_session.region_name
# image = '{}.dkr.ecr.{}.amazonaws.com/decision-trees-sample:latest'.format(account, region)
image = "184771037180.dkr.ecr.eu-west-1.amazonaws.com/tree-test:latest"
tree = sage.estimator.Estimator(image,
role, 1, 'ml.c4.2xlarge',
output_path="s3://{}/output".format(sess.default_bucket()),
sagemaker_session=sess)
tree.fit(data_location)
from sagemaker.predictor import csv_serializer
predictor = tree.deploy(1, 'ml.m4.xlarge', serializer=csv_serializer)
shape=pd.read_csv("data/iris.csv", header=None)
import itertools
a = [50*i for i in range(3)]
b = [40+i for i in range(10)]
indices = [i+j for i,j in itertools.product(a,b)]
test_data=shape.iloc[indices[:-1]]
test_X=test_data.iloc[:,1:]
test_y=test_data.iloc[:,0]
print predictor.predict(test_X.values).decode('utf-8')
Permalink: docker-deployment-on-aws-sagemaker
Tags: