Deploy your own TensorFlow object detection model to AWS DeepLens
In this blog post, we’ll show you how to deploy a TensorFlow object detection model to AWS DeepLens. This enables AWS DeepLens to perform real-time object detection using the built-in camera. Object detection is the technique for machines to correctly identify different objects in the image or video. Image recognition, specifically object detection is a very interesting topic in the AI deep learning world. For example, in autonomous driving, the camera on the vehicle first needs to be able to detect objects (such as people, cars, and signs) on the road before making any decisions to turn, slow down, or stop.
To deploy your own TensorFlow model, first you need to build a model with a supported base network. AWS DeepLens currently supports most of the popular base networks, such as Inception, MobileNet, NASNet, ResNet, and VGG. There are many pre-trained models using these networks from TensorFlow’s detection model zoo on their public GitHub repository. These pre-trained models can already identify many objects, such as humans and cars. You can just use them out of the box. Or you can extend these pre-trained models to train against your dataset to identify additional objects.
To keep this blog post easy to follow and relevant, you will learn to deploy one of the pre-built models from the model zoo that can detect up 90 common objects such as a bicycle, car, airplane, bus, or person. Keep in mind that the deployment process is the same if you are building and deploying your own custom model with the supported base networks mentioned previously.
The following sections walk you through the implementation steps in detail.
- Make sure to register your AWS DeepLens device before you begin. You can follow this link for a step-by-step guide to register the device.
- At the time of writing, TensorFlow 1.5 is not yet installed on the device in Python 2.7. Use SSH to connect to the device and execute the following command to install TensorFlow.
Download a pre-built TensorFlow object detection model from the model zoo
- On your workstation, download the COCO-trained MobileNet model (ssd_mobilenet_v1_coco) from the model zoo https://github.com/tensorflow/models/blob/master/research/object_detection/g3doc/detection_model_zoo.md
- Untar and copy file named frozen_inference_graph.pb to a separate folder. This frozen graph is the model.
- On your workstation, download the label map for the model (mscoco_label_map.pbtxt) from the following link to the folder where frozen_inference_graph.pb file is located. https://github.com/tensorflow/models/tree/master/research/object_detection/data
- The label translates the model output into a human-readable name. For example, if the model returns a number 2, it is a bicycle.
- Zip and tar pb and mscoco_label_map.pbtxt together, in Linux or OSX. The command would be similar to the following:
On the Windows platform, you can use a third-party software such as 7-Zip to put them together.
Upload the tar file to Amazon S3
In this section, you will upload the tar file to Amazon S3 so the AWS DeepLens service can deploy it to the DeepLens device for local inference.
- Open the AmazonS3 console.
- Create an Amazon S3 bucket in the Northern Virginia Region that must contain the term “deeplens”. The AWS DeepLens default role has permission only to access the bucket with the name containing ”deeplens”. You can name it deeplens-tfmodel-yourinitials
- After the bucket is created, upload the tar file to the bucket.
Creating an AWS Lambda function
In this section, you’ll create a custom Lambda function that will be deployed as part of the AWS DeepLens deployment. This Lambda function contains code to load the custom TensorFlow model that was downloaded from the S3 bucket mentioned previously. This allows you to perform local inferencing (object detection) without connecting back to the AWS Cloud.
- Open the AWS Lambda console.
- Create a new function from a blueprint, make sure to search for AWS Greengrass and select the Python version.
- Give the function a meaning name and select AWSDeepLensLambdaRole from Existing role drop-down list. Then create the function. Note: If you do not see this role, make sure that you registered your AWS DeepLens device. The role is automatically created during the registration process.
- In the function editor, copy and paste the following code into greengrassHelloWorld.py. Note: Do not rename the file or the function handler, leave everything at the default.
- Save the Lambda function.
- To ensure code integrity and consistency, AWS DeepLens only works with the published version of a Lambda function. To publish a Lambda function, choose Actions and select Publish new version.
- Give a description to the published version and choose Publish.
Setting up an AWS DeepLens project and deploying a custom TensorFlow object detection model
- Open the AWS DeepLens console.
- Make sure your DeepLens device is registered before you begin. You can follow this link for a step-by-step guide to register the device.
- Make sure TensorFlow 1.5 is installed on the device for Python 2.7. Installation instructions are in prerequisites section.
- Make sure you are in the same region where you created the S3 bucket. At the time of writing, AWS DeepLens is only available in the Northern Virginia Region.
- In the AWS DeepLens console, in the left navigation panel, choose Models. Then choose Import model.
- On the Import model page, choose Externally trained model. In the Model artefact path, enter the S3 bucket and model path you created earlier. Give it a meaningful name and description, and make sure to choose TensorFlow as the model framework. Choose Import model.
- In the left navigation panel, choose Project. Then choose Create new project.
- Create a new blank project.
- Give your project a meaningful name and description, and then choose Add model.
- Select the model that you imported earlier, and then choose Add model.
- On the project detail page, choose Add function.
- Choose the Lambda function you created earlier, then choose Add function.
- Choose Create to create the project. On the project page, select the new project that you just set up, and choose Deploy to device.
- Select the AWS DeepLens device that you want to deploy the project to, choose Review, and then chose Deploy. The deployment process will take few minutes because the AWS DeepLens device needs to download the model tar file from S3 and the Lambda function to execute locally on the device.
- The status pane at the top of the screen displays deployment progress. Wait until it turns green and displays status that the deployment succeeded.
- In the Device details pane, copy the MQTT topic, and then navigate to the AWS IoT console.
- In the AWS IoT console, in the left navigation pane choose Test, paste the MQTT topic copied in the previous step, then choose Subscribe to topic.
- Detected objects and their prediction confidence score are sent in real time through MQTT to the AWS IoT platform.
In this blog post, you learned how to deploy a custom TensorFlow object detection model to AWS DeepLens. This enables AWS DeepLens to perform real-time object detection using the built-in camera. You can see that it’s possible to use AWS services to build some very powerful AI solutions. Why not take your skills to the next level by integrating AWS DeepLens with other Amazon services, such as Alexa.
About the Authors
Tristan Li is a Solutions Architect with Amazon Web Services. He works with enterprise customers in the US, helping them adopt cloud technology to build scalable and secure solutions on AWS.
Wayne Davis is an Enterprise Solutions Archtiect for Amazon Web Services. Over the last 24 months he has been rapidly customers come up to speed on cloud technologies as fast as possible.