Gluoncv Yolo Neo
GluonCV YoloV3 training and optimizing using Neo
This notebook's CI test result for us-west-2 is as follows. CI test results in other regions can be found at the end of the notebook.
Introduction
This is an end-to-end example of GluonCV YoloV3 model training inside of Amazon SageMaker notebook and then compile the trained model using Neo runtime. In this demo, we will demonstrate how to train and to host an MXNet model on the Pascal VOC dataset using the YoloV3 algorithm. We will also demonstrate how to optimize this trained model using Neo.
This notebook is for demonstration purpose only. Please fine tune the training parameters based on your own dataset.
Setup
To train the YoloV3 MXNet model on Amazon SageMaker, we need to setup and authenticate the use of AWS services.
To start, we need to upgrade the SageMaker SDK for Python to v2.33.0 or greater and restart the kernel.
Then we need an AWS account role with SageMaker access. This role is used to give SageMaker access to your data in S3. We also create a session.
We also need the S3 bucket that is used for training, and storing the tranied model artifacts.
To easily visualize the detection outputs we also define the following function. The function visualizes the high-confidence predictions with bounding box by filtering out low-confidence detections.
Finally we load the test image into the memory. The test image used in this notebook is from PEXELS which remains unseen until the time of preditcion.
Data Preparation
Pascal VOC was a popular computer vision challenge and they released annual challenge datasets for object detection from 2005 to 2012. In this notebook, we will use the data sets from 2007 and 2012, named as VOC07 and VOC12 respectively. Cumulatively, we have more than 20,000 images containing about 50,000 annotated objects. These annotated objects are grouped into 20 categories.
Notes:
- While using the Pascal VOC dataset, please be aware of the database usage rights. The VOC data includes images obtained from flickr's website. Use of these images must respect the corresponding terms of use: https://www.flickr.com/help/terms
- Default EBS Volume size for SageMaker Notebook instances is 5GB. While performing this step if you run out of storage then consider increasing the volume size. One way to do so is by using AWS CLI as documented here.
Download data
Download the Pascal VOC datasets from 2007 and 2012 from Oxford University's website.
Upload data to S3
Upload the data to the S3 bucket.
Next, we need to setup training and compilation output locations in S3, where the respective model artifacts will be dumped. We also setup the s3 location for training data, validation data and custom code.
Training
Now that we are done with all the setup that is needed, we are ready to train our object detector. To begin, let us create a sagemaker.MXNet object. This estimator will launch the training job. It may take some time for training job to complete. To make it faster num-epochs can be reduced.
Compile the trained model using SageMaker Neo
After training the model we can use SageMaker Neo's compile_model() API to compile the trained model. When calling compile_model() user is expected to provide all the correct input shapes required by the model for successful compilation. We also specify the target instance family, the name of our IAM execution role, S3 bucket to which the compiled model would be stored and we set MMS_DEFAULT_RESPONSE_TIMEOUT environment variable to 500.
For this example, we will choose ml_p3 as the target instance family while compiling the trained model.
Deploy the compiled model and request Inferences
We have to deploy the compiled model on one of the instance family for which the trained model was compiled for. Since we have compiled for ml_p3 we can deploy to any ml.p3 instance type. For this example we will choose ml.p3.2xlarge
Delete the Endpoint
Having an endpoint running will incur some costs. Therefore as a clean-up job, we should delete the endpoint.
Notebook CI Test Results
This notebook was tested in multiple regions. The test results are as follows, except for us-west-2 which is shown at the top of the notebook.