Build a Custom Object Detection Model from Scratch with Amazon SageMaker and Deploy it at the Edge with AWS DeepLens. This workshop explains how you can leverage DeepLens to capture data at the edge and build a training data set with Amazon SageMaker Ground Truth. Then, train an object detection model with Amazon SageMaker and deploy it to AWS DeepLens.
This repository contains the code used during the namesake webinar broadcasted at the AI & ML Web Day in July 11th 2019.
- Directory structure
- Prerequisites
- How to deploy the Lambda functions
- How to run the trigger application
- License
.
├── LICENSE
├── README.md
├── deeplens # Contains the resources for functions to run on the AWS DeepLens device
│ ├── deployment # Deployment templates and scripts
│ │ ├── capture-template.yaml # SAM template to deploy the picture capturing function
│ │ ├── deploy-capture-function.sh # Bash script to automate the deployment of the picture capturing function to AWS Lambda
│ │ ├── deploy-inference-function.sh # Bash script to automate the deployment of the inference function to AWS Lambda
│ │ └── inference-template.yaml # SAM template to deploy the picture inference function
│ └── src # Source code of the functions
│ ├── capture
| | ├── capture.py # Source code for the picture capturing function
| | └── requirements.txt # Dependencies for the picture capturing function
│ └── inference
| └── inference.py # Source code for the inference function
| └── requirements.txt # Dependencies for the inference function
├── notebook
│ └── finger_counting.ipynb # Jupyter notebook to train and deploy the object detection model
└── trigger # Contains the resources for the application that triggers the picture capture and validation
├── certs # Directory to place the certificates for the AWS IoT Thing that represents the trigger
└── src # Source code for the trigger application
├── get_gg_ca_cert.py # Utility to download the root CA certificate of the Greengrass Group the DeepLens device belongs to
└── trigger_app.py # Source code for the trigger application
In order to run this solution you need:
- Python 3.x
- The AWS CLI installed and configured with permissions to:
- Create CloudFormation stacks.
- Create an S3 bucket.
- Create/update Lambda functions.
- Attach an IAM policy to the DeepLensGreengrassGroupRole role.
- List and get Greengrass groups certificate authorities.
- AWS SAM CLI installed
- An Amazon S3 bucket to which you have write access.
Clone or download this Git repository to a local directory and follow the instructions below.
- Give execution permissions to the .sh files in the ./deeplens/deployment directory
chmod +x ./deeplens/deployment/*.sh
- Run the following commands from the deeplens/deployment directory (substitute the arguments between <> with appropriate ones):
./deploy-capture-function.sh --bucket <your-s3-bucket-name> --stack-name <your-stack-name>
./deploy-inference-function.sh --bucket <your-s3-bucket-name> --stack-name <your-stack-name>
- Open a terminal window
- Press windows key + r
- Introduce cmd and press enter
- Go to the deeplens/deployment folder and run the following commands:
sam build -t capture-template.yaml
sam package --region us-east-1 --s3-bucket <your-s3-bucket> --output-template-file packaged-template.yaml;
(substitute<your-s3-bucket>
with the S3 bucket name where SAM can upload the package)sam deploy --region us-east-1 --template-file ./packaged-template.yaml --stack-name <your-stack-name> --capabilities CAPABILITY_NAMED_IAM;
(substitute<your-stack-name>
with an appropriate name for your CloudFormation Stack)
This application requires the following Python packages:
- OpenCV (
pip install python-opencv
) - AWS IoT Device SDK for Python (
pip install awsiotpythonsdk
) - Numpy (
pip install numpy
)
Once you have registered an AWS IoT Thing, download the private key and the certificate and place them in the trigger/certs folder.
Rename the private key file to private.pem.key
and the certificate file to certificate.pem.crt
.
You also need the certificate of the root CA in the Greengrass group of your DeepLens.
- Go to the Greengrass Console and click on Groups. Click on the DeepLens group and then on Settings.
- Copy the Group ID.
- Open the get_gg_ca_cert.py file in your favorite text editor and substitute
<YOUR_GREENGRASS_GROUP_ID>
with the Group Id you copied. - Go to the trigger directory and run
python src/get_gg_ca_cert.py
- Go to the trigger directory and run
python src/trigger_app.py
- The application will open a new window that may be hidden, so switch applications (alt+tab) and you'll see a Python application.
- Once in that window, click 'y' to save a picture, 'q' to quit the application, or any other key to ignore the current picture and request a new one.
This library is licensed under the Apache 2.0 License.