Skip to content

Deploy personalized Deep Learning model within a Docker container on Amazon Sagemaker

Training custom deep learning algorithms on Amazon Sagemaker: A guide to deploying your own containerized algorithm. Sagemaker offers two primary deployment options: the first is utilizing pre-built algorithms available, such as KNN, Xgboost, Linear Learner, etc., while the second option allows...

Deploy a custom deep learning algorithm within a Docker container on Amazon Sagemaker for...
Deploy a custom deep learning algorithm within a Docker container on Amazon Sagemaker for deployment

Deploy personalized Deep Learning model within a Docker container on Amazon Sagemaker

In this article, we will discuss the deployment of a custom deep learning container algorithm on Amazon Sagemaker, focusing on the process of building a Docker container and hosting it on the platform.

The project, named , implements a Flask web server and Titanic predictions for the app. It is built using libraries such as TensorFlow, Scikit-learn, and Keras for processing, training the model, and taking inference.

To get started, the container image can be pushed to Amazon Elastic Container Registry (ECR) after local testing. This process involves uploading the container directory to a Sagemaker Notebook instance and executing the commands in the file or Bring_Your_Own-Creating_Algorithm_and_Model_Package notebook.

Local testing of the container involves several steps: building the docker image, training the model, running the serve script, and running the predict script with a payload file. The model is trained for around 200 epochs with a batch size of 8, without hyperparameter tuning.

Preprocessing steps are performed in the file before taking inference from the trained model. These include splitting titles, creating new features, handling missing values, and one-hot encoding. The TensorFlow default graph is initialized in to take predictions from the trained neural network.

The Dockerfile describes how to build the Docker container image, with the working directory set as . The built Docker image can be named using the command from the container directory.

The model architecture has two hidden layers with 8 and 4 neurons respectively, an output layer with 1 neuron, and specific activation functions. It is compiled using Adam optimizer, binary_crossentropy loss, and accuracy as the validation metric.

The local testing framework for the container includes three shell scripts: , , and . runs training on the local tree and requires setting up the correct channels and data for the algorithm in the directory. serves the model, runs the file in the container, and starts a Python application that listens on port 8080, nginx, and gunicorn.

Once the container image is pushed to ECR, the model can be trained, tested, and deployed on Amazon Sagemaker. Additional steps can be found in the jupyter notebook inside the repository.

For serving the web application, is a small wrapper used to invoke the Flask app. The trained model pickle file is saved in the model directory, and the path for loading the model in is . To run the predict script, the command can be used.

Sagemaker provides two options: using built-in algorithms or a custom docker container from ECR. The Titanic dataset from Kaggle is used for data purposes.

In conclusion, deploying a custom deep learning container algorithm on Amazon Sagemaker can be a straightforward process, with the provided steps serving as a guide for developers looking to host their models on the platform.

Read also: