Site icon The Last Dev Talk about Technologies

Amazon SageMaker – Bring your own Algorithm

In previous posts, we explored Amazon SageMaker’s AutoPilot, which was terrific, and we learned how to use your own algorithm with Docker, which was lovely but a bit of a fuzz. In this post, we will see how easy it is to bring your own algorithm using the script mode in SageMaker. Again we will use the new SageMaker Studio!

Let’s take a look at the resources we will use how much it will cost this tutorial:

You can find the code of this post here: https://github.com/siakon89/machine-learning/tree/master/sagemaker/fashion-mnist-byom

Prerequisites

The only thing we need is a dataset. I chose the popular Fashion MNIST dataset.
We will download Fashion MNIST from Keras datasets as follows:

See the gist on github.

Let’s take a look at the data we just download:

See the gist on github.

Sample of Fashion-MNIST dataset

Pre-process dataset

We have to make some adjustments to our data for our model.

See the gist on github.

First, we normalize the images to 0-1. Fashion MNIST images are 28×28 arrays with values from 0 to 255, with 0 being color black and 255 being color white. We divide the array with 255 to bring its value to [0, 1] (line 4-5).

We also need to transform our data from 28×28 to 28x28x1 for our model (line 8-9)

Finally, we one-hot encode our labels (line 13-14)

Building the model

We will use Convolutional Neural Networks (CNNs) to solve that classification problem. Construct a relatively simple CNN as follows:

See the gist on github.

For this model, we used a pretty simple CNN network with three Conv2D layers, one 64 and two 32, both with kernel size 3×3 and activation function, our favorite ReLu. Below is a summary of the model.

_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d_28 (Conv2D)           (None, 26, 26, 64)        640       
_________________________________________________________________
conv2d_29 (Conv2D)           (None, 24, 24, 32)        18464     
_________________________________________________________________
conv2d_30 (Conv2D)           (None, 22, 22, 32)        9248      
_________________________________________________________________
flatten_12 (Flatten)         (None, 15488)             0         
_________________________________________________________________
dense_12 (Dense)             (None, 10)                154890    
=================================================================
Total params: 183,242
Trainable params: 183,242
Non-trainable params: 0
_________________________________________________________________

Setting up SageMaker

In the same Jupyter Notebook, we upload our data to S3 and “let SageMaker” train our model. First of all, we need to transform our data in a binary format; we use NumPy. After the transformation, we upload the files into SageMaker’s S3 bucket.

See the gist on github.

Train the model

To train the model, first, we need to bring the model code in an acceptable format by SageMaker. To do that we need to have in mind two things:

  1. SageMaker will get a python file as input, and it will run it with arguments
  2. The training of the model will be performed in another instance, so we need to get the data from S3

Script

Given the above, we first need to accept the arguments that SageMaker uses in our script, and secondly, we need to read the data from S3. We create a new file named model.py (you can use whatever name you want)

See the gist on github.

SageMaker Estimator

After setting the model.py file, we need to set the Estimator for the script mode. We will use the built-in TensorFlow estimator from SageMaker to use the script mode.

See the gist on github.

You can monitor the progress of your training job either in Jupyter Notebook or at the Experiment section. Remember when we used the name tf-fashion-mnist in the Estimator? Well, this is how we find the training job. Double-click “Unassigned trial components,” then find the name of the job, left-click and select “Open in trial details.” Select the section Metrics to see the evaluation score.

 

Experiment Section

 

Metrics of the training job

Furthermore, remember Line 76 on model.py, where we printed the evaluation score of the model?

Test MSE : [0.2660780865192413, 0.9116]

Well, that is pretty good, we can certainly do better, but for now, we are set. We can see that the monitoring of our model is a bit tricky; in future posts, I will take a closer look at the new debugger for SageMaker studio.

You can find the model in S3 and deploy it manually or by using the deploy endpoint command from SageMaker.

Conclusion

The script mode in SageMaker is definitely making the whole process of “Bring our own model” easier. You have everything set up out of the box, the only thing you have to do is to pass the arguments of the Estimator. In addition, SageMaker Studio makes the monitoring of the training job a bit easier, and the metrics are a very nice addition. On the other hand, there is limited support for the framework versions, for example, I would like to see Tensorflow 2.0 be available in the script mode. Furthermore, it was hard to set up the Local mode to train my model. The whole initialization process usually takes 2-4 minutes in order to start training your model, and I would like to test my model or run it locally first.

Well, that is it for today folks. I hope you find that article interesting. If you have any questions, suggestions or notices please let me know in the comment section below or at my Twitter account @siaterliskonsta. Make sure to check out my earlier posts about SageMaker AutoPilot and Serverless Data Lake. Until next time, take care!