We will cover the following tasks in 1 hour and 3 minutes:
Introduction and Setup
Hi and welcome to this project on Semantic Segmentation using Amazon Sagemaker. Sagemaker is a set of managed services by Amazon which allow machine learning developers to create datasets, create and train models, tune and deploy models very easily. Sagemaker comes with a bunch of algorithms and pre-trained models for most common machine learning tasks but you can easily create your own custom architectures and algorithms as well. In this task, we will load the relevant libraries and helper functions and we will also download the data that we’d be working with.
In order to perform training, we will need to setup and authenticate the use of AWS services. We will need an execution role, a sagemaker session and we will also need a s3 bucket to store our dataset in as well as to store the final trained model in. The session object manages interactions with Amazon SageMaker APIs and any other AWS service that the training job uses.
We are going to use the default Sagemaker bucket. Sagemaker uses Docker to contain various images for various tasks. We are interested in semantic segmentation so we will need to access that docker image as well.
We are using the PASCAL VOC dataset from VOC 2012. This is a very popular dataset used for a variety of computer vision tasks including semantic segmentation. Let’s move the data to s3 in the structure that the algorithm requires. Sagemaker algorithms read data from s3 buckets so this is important. We need to move the training images to
train directory, validation images to
validation and so on. Fortunately, the dataset’s annotations are already named in sync with the image names, satisfying that requirement of the Amazon SageMaker Semantic Segmentation algorithm. Let’s first create a directory structure mimicking the s3 bucket where data is to be dumped. Once that is done, we can simply copy the directories to s3.
Upload to S3
Let us now move our prepared datset to the S3 bucket that we decided to use in a project earlier. Let’s now upload our dataset with KEY PREFIX: S3 uses the prefix to create a directory structure for the bucket content that it display in the S3 console. Once the model is trained, we will need to access for deployment - so we will dump this model artifact in s3 as well. Let’s set a location where it will be dumped. We will use the same s3 bucket.
Now that we have uploaded the data to s3, we are ready to train our semantic segmentation algorithm. First, we will create an estimator. This estimator will handle the end-to-end Amazon SageMaker training and deployment tasks. We will need a fast GPU instance for training a task like this. We are going to use a
ml.p3.2xlarge instance to train.
The semantic segmentation algorithm at its core has an encoder network and a decoder network. The encoder is usually a regular convolutional neural network typically pre-trained on imagenet or other popular classification datasets.
The decoder is a network that picks up the outputs of one or many layers from the backbone and reconstructs the segmentation mask from it. There have been some very cool papers which use different ideas for decoding the encoder output and the most popular ones right now are FCN and DeepLab. Deeplab version 3, at the time of creating this project, is the state of the art approach to semantic segmentation.
Input Objects and Model Training
Now that the hyperparameters are setup, let us prepare the handshake between our data channels and the algorithm. To do this, we need to create the S3 INPUT objects within Sagemaker from our data channels. These objects are then put in a dictionary, which the algorithm uses to train. Training the algorithm involves a few steps. Firstly, the instances that we requested while creating the Estimator classes are provisioned and are setup with the appropriate libraries. Then, the data from our channels are downloaded into the instance. Once this is done, the training job begins. The provisioning and data downloading will take some time.
However, we just have a dump of the model on s3, we can’t yet use it for any inference. To do that, let’s deploy our model in an EC2 instance. For inference, we don’t need a GPU so we will use a
ml.c5.xlarge instance as an example.
We are almost at the end of our project. Now that the model is deployed, let’s download a random image from the internet and see how the pixels are classified or segmented by our trained model. Having an endpoint running will incur some costs. Therefore as a clean-up job, we should delete the endpoint after we are done with inference. Please go to your AWS console and if you want, delete the s3 buckets, the sagemaker notebook, etc. if you need to. The training job, once completed, will not incur any further costs as the cleaning up for that is already taken care of by Sagemaker.
It would be great to allow copy+paste. I couldn't do it from my Mac laptop :(
About the Host (Amit Yadav)
I am a machine learning engineer with focus in computer vision and sequence modelling for automated signal processing using deep learning techniques. My previous experiences include leading chatbot development for a large corporation.