Smile Innovation
Published in

Smile Innovation

SageMaker : Bring your own Algorithms

With the help of Docker, you will be able to customize training and infering models using other frameworks that those provided by SageMaker

Photo by chuttersnap on Unsplash

And thanks to SageMaker using Docker!

SageMaker train, deploy and use

Create Docker image

sage
├── Dockerfile
├── local_test
│ ├── predict.sh
│ ├── train.sh
│ ├── serve.sh
│ └── test_dir => /opt/ml in container
│ ├── input
│ │ ├── config
│ │ └── data
│ │ └── training
│ ├── model
│ └── output
└── program
├── (some scripts...)
├── train
└── serve
FROM nvidia/cuda:9.0-base-ubuntu16.04
ENV PATH=/program:${PATH}
ADD program /program
RUN chmod +x /program/train /program/serve
class ScoringService(object):
model = None # Where we keep the model when it's loaded
graph = None
@classmethod
def get_model(cls):
if cls.model is None:
cls.model = load_model(
os.path.join(model_path, 'model.h5'))
cls.model._make_predict_function()
cls.graph = K.get_session().graph
return cls.model
@classmethod
def predict(cls, input):
clf = cls.get_model()
with cls.graph.as_default():
return clf.predict(input)
docker build -t sagemaker-test .

Prepare Docker image and test your code on your computer

docker run --rm -v $(pwd)/test_dir:/opt/ml sagemaker-test train
https://media.giphy.com/media/4MxMvzhTEuqty/giphy.gif

Now that the image is ready, let’s test it locally

docker run --rm -v $(pwd)/test_dir:/opt/ml -p 8080:8080 sagemaker-test serve

And now, make a test!

Now, let’s push it to AWS SageMaker

./build_and_push.sh sagemaker-test
predictor = tree.deploy(initial_instance_count=1,
instance_type='ml.m4.xlarge')
res = predictor.predict(some_data)
print(res)

Using our API outside the notebook

Burger and Salad recognition

Winding up

Downsides

--

--

A series of articles about innovative technologies we try @ Smile

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Patrice Ferlet

R&D engineer for Innovation Dept. at Smile.eu group. Coding in Golang, Python — Devops, Machine Learning, cloud computing…