Site icon The Last Dev Talk about Technologies

Convolutional NN with Keras and SageMaker

In today’s post, I am going to show you how you can use Amazon’s SageMaker to classify images from the CIFAR-10 dataset using Keras with MXNet backend. For this tutorial, you do not need the GPU version of Tensorflow.

This tutorial is a continuation of my previous one, Convolutional NN with Keras Tensorflow on CIFAR-10 Dataset, Image Classification and you can find it here. Please, make some time and take a quick look at the article and also download the code (IPython notebook) because you will need it.

The sections we will cove are the following:

  1. Prepare DockerFile for GPU container
  2. Creating the keras.json file
  3. Creating the python file with our model, cifar_10_keras.py
  4. Set up SageMaker to run our Model
  5. Save the model to use for predictions

Setup SageMaker and the environment

There is an amazing post from Julien Simon (@julsimon) that explains how to use SageMaker with MXNet-Keras and run your custom code. You can find his article here.

We are going to use the same method to deploy our model and train it in a powerful machine. You can find all files and code that I am going to use here.

Prepare DockerFile for GPU container

See the gist on github.

We are installing all the appropriate packages in order to have an image to run our model in GPU

Creating the keras.json file

See the gist on github.

The important variables here are `image_data` and `backend`.

Creating the python file with our model, cifar_10_keras.py

You can find the complete file here. But let’s dig more into it. First, we have to load the data, in a similar manner as we did in the first chapter.

See the gist on github.

Then we have to open the parameters for our model, specified by the estimator.

See the gist on github.

Then we do the same things as we did when we created the model in the previous tutorial

  1. Load the data from the pickled files (we use the load_data() function)
  2. Preprocess the data
  3. Create the model
  4. Augment the images
  5. Compile the model
  6. Train the model and save it

And we are done creating the model for SageMaker. Now we are going to set up the SageMaker to run our custom model.

Set up SageMaker to run our Model

We will go step by step. You can find the Jupyter notebook with the whole code here.

  1. Download dataset and save it as pickle
  2. Create the Docker image with our custom model
  3. Upload dataset to S3
  4. Train the algorithm
  5. Get the saved model

1. Download dataset and save it as pickle

It is the same code as we did in the first section, the only that changes are that we are downloading the dataset in SageMaker’s EBS now and we have to save the pickled file to specific folders as we did last time.

See the gist on github.

2. Create the Docker image with our custom model

First, we have to log in to ECR and create a repository. Before advancing to this step make sure you have an ECR up and running with the same name as in the variable repo_name. If you encounter an authentication error, make sure you have given the appropriate access to the repository.

See the gist on github.

Then we move the files that we want to a folder called build/ and then we build the Docker image. When the image is done, we push it to ECR.

See the gist on github.

3. Upload dataset to S3

We upload the dataset we created earlier in step1.

See the gist on github.

4. Train the model

To train the model we call SageMaker’s Estimator with the image we just built, and input data the ones that we just created.

See the gist on github.

5. Get the saved model

You can find your saved model, compressed, in your S3 bucket. You can download it, load it to your algorithm and start predicting!

If you want a tutorial on how to set up your account for SageMaker, please let me know in the comment section below or you can tweet to me @siaterliskonsta.

That’s it for today!  I know that I raised some questions about SageMaker, but be sure I will upload many more tutorials! Any questions you have you can find me on Twitter @siaterliskonsta, I would love to hear them and I will do my best to answer them! The following SageMaker post will be way simpler! We will use the AWS’s algorithms! Till next time, take care and bye bye!