Deploy TensorFlow-hub models on Amazon Sagemaker

TensorFlow deployments made easy

Pranav Waikar
Globant
6 min readMay 13, 2021

--

In this article, we will take a look at TensorFlow hub models and how to deploy these models locally as well as on the AWS cloud.

Introduction

Tensorflow-hub is a repository of highly useful, pre-trained machine learning models. These models are freely available and can be utilized with Keras or TensorFlow.

These models are easy to deploy via TensorFlow servings. Tensorflow servings are designed to deploy machine learning models easily on production environments with an easy API interface. Tensorflow models provide out-of-the-box integration with TensorFlow servings.

Local execution

We will use the image to test TensorFlow servings locally first. We will start with one of the most used models ie Google — universal sentence encoder for example.

  1. Download and install docker. Pull TensorFlow/servings images from here.
  2. Download google universal sentence encoder 4 from here.
  3. Extract the model from the zip file
  4. The folder structure looks like this-
The folder structure after downloading & extracting the universal-sentence-encoder model
The directory structure of the universal-sentence-encoder model

5. To use any model with TensorFlow servings it should follow the following rules

6. Create a directory named “models” and subdirectory with the name of the model like “sentence-encoder”

7. Create a subdirectory with a version number. By default, it will serve the model with the highest version number.

8. Inside the subdirectory copy all the model files.

9. Your final directory structure will look like this -

Final directory structure as per TensorFlow servings format

10. Now we can start TensorFlow servings on our local docker container. Run the command -

12. Now you can send requests and use the model as a Rest-API endpoint. If the request is successful then you will get an encoded sentence as a result.

The local deployment result

Sagemaker execution

The Sagemaker allows you to bring the models which are trained outside of the Sagemaker environment. So, one way is you could compress the model with the created directory structure in tar.gz format. Once ready, you can upload the zip to an S3 bucket and pass the s3 location while deploying.

But the issue with this approach is you need to download the model, extract and change directory structure, again compress the model and upload to the s3 bucket manually. This causes a lot of overhead and it’s very inefficient.

But there is another option!

The Sagemaker training jobs allow you to do some processing and save the model artifacts back to the s3 bucket after completion. You can provide your custom code for training jobs using Sagemaker script mode.

Pre-requisites

We need to set up some services of AWS for this.

  1. Create an S3 bucket.

2. Create a directory called — “trainingData” and a subdirectory called — “train”

3. Upload any CSV file under the train directory with the name “tranin.csv” . We just want it as a placeholder.

4. Create a Sagemaker notebook instance & attach the policy of “AmazonS3FullAccess” to the default Sagemaker role.

5. Create a new Sagemaker notebook with kernel — “conda_amazonei_tensorflow_p36”

6. Create a new text file and save it with the name “encoder.py”

The directory structure in the Sagemaker notebook instance

Training script

Sagemaker provides script mode execution where the user provides his own script to perform the training job. Let's add the script in “encoder.py”.

Encoder.py →

Jupyter notebook

First, let's set up S3 configurations.

Next is to import all required packages.

Next, we will create a TensorFlow estimator

Next is to set up training data as a placeholder. It will not actually process that data for training.

Now, just start the training job by calling the fit method.

This will start a training job with a Tensorflow-2 image. Once ready, it will execute the training script. The script will

  1. Install TensorFlow-hub
  2. Download universal sentence encoder model
  3. Save the model into the desired directory structure
  4. Sagemaker will save model artifacts to s3 bucket output path in -

Now all artifacts are ready. With these stored artifacts, the model can be deployed to an endpoint —

The process takes some time to complete and it prints the endpoint name.

Once the model is deployed and we can make some predictions using Sagemaker Predictor —

This returns a JSON response with the encoded sentence values. This is means the model is successfully deployed.

In the end, delete the deployed instance to stop billing.

That’s it!

In this way, you can deploy TensorFlow models to Sagemaker leveraging Sagemaker features. You can create a pipeline and automate this workflow so deploying the model in a different environment will be extremely easy.

References

https://sagemaker.readthedocs.io/en/stable/frameworks/tensorflow/deploying_tensorflow_serving.html

https://www.tensorflow.org/tfx/serving/docker

https://www.tensorflow.org/api_docs/python/tf/saved_model/save

--

--