![[Part 3] Your first steps in Computer Vision on AWS: deploying models for an ASL recognition app](https://prod-assets.cosmic.aws.dev/a/3LjglnQ2Xtk6VQZdfAXbH8mqR6F/cv-o.webp)
[Part 3] Your first steps in Computer Vision on AWS: deploying models for an ASL recognition app
This is the third and final part of the series "Your first steps in Computer Vision on AWS". If you are just starting, this tutorial will guide you step by step from the initial dataset to a deployed app.
Series: Your first steps in Computer Vision on AWS (3 articles)
- 3[Part 3] Your first steps in Computer Vision on AWS: deploying models for an ASL recognition app This article
What we are trying to build
While most ML tutorials end with model training and validation, we will take things one step further and deploy our models in an app. This is the most interesting and exciting part! We will make our models live!
Here is what the AWS architecture will look like:


Here's a quick overview of the solution:
The best way to test models is to take photos using a mobile phone. A simple static, serverless ASL Alphabet Recognition web application is hosted on S3 and accessed via a Ā CloudFrontĀ URL. To call the models for predictions, we will use Ā API GatewayĀ . A Ā Lambda event functionĀ will preprocess the uploaded photo and send it to a Ā serverless SageMaker inference endpointĀ . The endpoint will be created using a container stored in Ā ECRĀ .
Most of the AWS infrastructure will be created automatically using Ā CloudFormationĀ (not shown on the architecture image). This will make the setup reproducible. However, we will first need to create the ECR repositories, build and push the SageMaker and Lambda container images, and upload the model archive to S3. The website files will also be uploaded to S3 manually after the stack is deployed.
ā Why serverless endpoint?
Our use case will have idle periods between traffic spurts and can tolerate cold starts. Serverless endpoints automatically launch compute resources and scale them in and out (even down to 0) depending on traffic, eliminating the need to choose instance types or manage scaling policies. With a pay-per-use model, Serverless Inference is a cost-effective option in our infrequent traffic pattern. The serverless endpoint supports a maximum memory size of 6144 MB (6 GB), which is more than enough for our three models.
ā What would be the app cost?
I spent less than $5 on the application. The serverless endpoint was running for around a week, but the cost was minimal. I spent more on the Ā
ml.g5.xlargeĀ instance in the previous part.Prepare models for inference
ā ļø For this step, you can use your local environment or a SageMaker notebook instance (Ā
ml.t3.mediumĀ is more than enough).Models trained in the previous part are ready: put Ā
raw.kerasĀ , Ā filtered.kerasĀ , and Ā edges.kerasĀ into Ā models/Ā
You will also need to add the Ā class_names.jsonĀ file to the Ā models/Ā directory (Ā copy/download it from the GitHub repoĀ ).
It contains the exact class mapping used during training.From inside Ā
models/Ā directory run the command:tar -czvf asl-models.tar.gz \ raw.keras \ filtered.keras \ edges.keras \ class_names.jsonYou will get the archive Ā
asl-models.tar.gzĀ . Upload it to S3:aws s3 cp asl-models.tar.gz \ s3://YOUR-BUCKET/asl-app/models/asl-models.tar.gzā ļø Don't forget to replace the placeholder Ā
YOUR-BUCKETĀ and put your S3 bucket name!Once the archive has been uploaded to S3, we will use its S3 URI in CloudFormation.
s3://YOUR-BUCKET/asl-app/models/asl-models.tar.gzBuild and push SageMaker inference container to ECR
ā ļø For this step, you can use your local environment with AWS CLI (Ā How to install/update AWS CLI?Ā ) or a SageMaker notebook instance (Ā
ml.t3.mediumĀ is more than enough).ā ļø Copy/download Ā
inference.pyĀ , Ā requirements.txtĀ , and Ā DockerfileĀ files from the Ā GitHub repoĀ . They should sit inside the Ā sagemakerĀ directory (same structure as in the repo).The code in Ā
inference.pyĀ is pretty simple: its main job is to handle inference for all three models.Now, create an ECR repository:
aws ecr create-repository \ --repository-name asl-sagemaker-inferenceGet your account ID:
aws sts get-caller-identity \ --query Account \ --output textSet variables:
export AWS_REGION=YOUR-REGIONexport AWS_ACCOUNT_ID=$(aws sts get-caller-identity \ --query Account \ --output text)
export ECR_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.comLogin:
aws ecr get-login-password \ --region $AWS_REGION \ | docker login \ --username AWS \ --password-stdin $ECR_URIBuild and tag:
cd sagemaker
docker build \ -t asl-sagemaker-inference:latest .
docker tag \ asl-sagemaker-inference:latest \ $ECR_URI/asl-sagemaker-inference:latestAnd finally push:
docker push \ $ECR_URI/asl-sagemaker-inference:latestā ļø Make sure that your role or identity has the required permissions to do the job. Required actions: Ā
ecr:InitiateLayerUploadĀ , Ā ecr:UploadLayerPartĀ , Ā ecr:CompleteLayerUploadĀ , Ā ecr:BatchCheckLayerAvailabilityĀ , and Ā ecr:PutImageĀ .You will use in CloudFormation:
Ā
ACCOUNT_ID.dkr.ecr.REGION.amazonaws.com/asl-sagemaker-inference:latestĀ Build Lambda image
The main task of the Lambda function will be to accept an image from a phone, crop it to the center at 200Ć200 pixels, do the required preprocessing for the Gaussian-filtered model and Canny edge model and finally obtain predictions from all three models by invoking the SageMaker endpoint. For each model, it will get the top 3 predictions with their probabilities.
ā ļø For this step, you can use your local environment with AWS CLI (Ā How to install/update AWS CLI?Ā ) or a SageMaker notebook instance (Ā
ml.t3.mediumĀ is more than enough).ā ļø Copy/download Ā
lambda_function.pyĀ , Ā requirements.txtĀ , and Ā DockerfileĀ files from the Ā GitHub repoĀ . They should sit inside the Ā lambdaĀ directory (same structure as in the repo).Then, create a new ECR repository:
aws ecr create-repository \ --repository-name asl-preprocessing-lambdaGet your account ID and set variables:
aws sts get-caller-identity \ --query Account \ --output text
export AWS_REGION=YOUR-REGIONexport AWS_ACCOUNT_ID=$(aws sts get-caller-identity \ --query Account \ --output text)
export ECR_URI=$AWS_ACCOUNT_ID.dkr.ecr.$AWS_REGION.amazonaws.comLogin:
aws ecr get-login-password \ --region $AWS_REGION \ | docker login \ --username AWS \ --password-stdin $ECR_URIBuild, tag, and push:
cd lambda
docker buildx build \ --platform linux/amd64 \ --provenance=false \ -t asl-preprocessing-lambda:latest \ .
docker tag asl-preprocessing-lambda:latest \ $ECR_URI/asl-preprocessing-lambda:latest
docker push $ECR_URI/asl-preprocessing-lambda:latestYou will use the Lambda image in CloudFormation:
Ā
ACCOUNT.dkr.ecr.REGION.amazonaws.com/asl-preprocessing-lambda:latestĀ Deploy the CloudFormation stack
ā ļø For this step, you can use your local environment with AWS CLI (Ā How to install/update AWS CLI?Ā ) or a SageMaker notebook instance (Ā
ml.t3.mediumĀ is more than enough).This CloudFormation stack will create all the resources required for the serverless ASL Alphabet Recognition web application: CloudFront, S3 bucket, API Gateway, Lambda event function, SageMaker Serverless endpoint. As shown in the architecture diagram, CloudFront will route requests to Ā
/api/*Ā to API Gateway, giving you a single public URL for the application.
Before deploying the stack, you will need to retrieve three values:Type | Value |
|---|---|
Model | Ā s3://YOUR-BUCKET/asl-app/models/asl-models.tar.gzĀ |
SageMaker image | Ā ACCOUNT.dkr.ecr.REGION.amazonaws.com/asl-sagemaker-inference:latestĀ |
Lambda image | Ā ACCOUNT.dkr.ecr.REGION.amazonaws.com/asl-preprocessing-lambda:latestĀ |
Let's deploy the stack:
aws cloudformation deploy \ --template-file cloudformation.yaml \ --stack-name asl-recognizer \ --capabilities CAPABILITY_IAM \ --parameter-overrides \ ModelS3Uri=s3://YOUR-BUCKET/asl-app/models/asl-models.tar.gz \ SageMakerImageUri=ACCOUNT.dkr.ecr.REGION.amazonaws.com/asl-sagemaker-inference:latest \ LambdaImageUri=ACCOUNT.dkr.ecr.REGION.amazonaws.com/asl-preprocessing-lambda:latestā ļø Make sure that your role or identity has the required permissions to successfully deploy your CloudFormation stack. Required actions: Ā
cloudformation:DescribeStacksĀ , Ā s3:CreateBucketĀ , Ā s3:ListAllMyBucketsĀ , Ā s3:GetBucketLocationĀ , and Ā apigateway:POSTĀ .It will take around 10-15 mins to complete. A good thing is that you can monitor stack events in Ā the AWS CloudFormation Management ConsoleĀ :


Upload the website
ā ļø For this step, you can use your local environment with AWS CLI (Ā How to install/update AWS CLI?Ā ) or a SageMaker notebook instance (Ā
ml.t3.mediumĀ is more than enough).ā ļø Copy/download Ā
app.jsĀ , Ā index.htmlĀ , and Ā styles.cssĀ files from the Ā GitHub repoĀ . They should sit inside the Ā frontendĀ directory (same structure as in the repo).After CloudFormation finishes, retrieve the bucket:
aws cloudformation describe-stacks \ --stack-name asl-recognizer \ --query \ "Stacks[0].Outputs[?OutputKey=='FrontendBucket'].OutputValue" \ --output textThe command will return a bucket name similar to Ā
asl-recognizer-frontend-12000000Ā . Use this value to upload your website pages from the Ā frontendĀ directory to S3:aws s3 sync frontend/ s3://asl-recognizer-frontend-12000000/After this, retrieve the CloudFront URL:
aws cloudformation describe-stacks \ --stack-name asl-recognizer \ --query \ "Stacks[0].Outputs[?OutputKey=='WebsiteURL'].OutputValue" \ --output textYou'll get the CloudFront URL (e.g. Ā
https://d1234567890abc.cloudfront.netĀ ). Paste it in the browser to access your application.
As you can see, the web application's UI is very simple. Its main purpose is to let us test the models and get predictions:

Again, the provided Ā GitHub repoĀ has all the required files for models inference preparation, SageMaker inference container creation, Lambda image creation, as well as website pages and the CloudFormation stack deployment. The repo structure is:
asl-web-app/āāāā models/ā āāā raw.kerasā āāā filtered.kerasā āāā edges.kerasā āāā class_names.jsonāāāā sagemaker/ā āāā inference.pyā āāā Dockerfileā āāā requirements.txtāāāā lambda/ā āāā lambda_function.pyā āāā Dockerfileā āāā requirements.txtāāāā frontend/ā āāā index.htmlā āāā app.jsā āāā styles.cssāāāā cloudformation.yamlTest the app
The most exciting part begins! Use your phone to take photos and make predictions.
First, I tried several images from the Ā
asl_alphabet_testĀ folder, which contains additional images provided alongside the training dataset.
All three models performed reasonably well, producing the correct class predictions:

However, you may notice that the models' predictions on your own photos are not always correct. I tried to show a 'W': the model trained on raw RGB returned 'R', Gaussian-filtered and Canny edge models predicted 'K':


All our models achieved more than 90% accuracy on our held-out test split. They also performed well on additional images from Kaggle's separate Ā
asl_alphabet_testĀ folder, which were not included in the training, validation, or test splits. But when facing real-world images, models started to act weird.This reveals an important lesson about ML models and their applications outside the collected dataset. There is a gap between a toy dataset and real images; and we didn't fully account for it. If you look closer at training images, they all share a similar background, same skin tone, same hand size. Lightning and focus do vary, but not drastically. What if the person signing in ASL has a darker skin tone? What if we use a green/blue/orange background?
In this project, we got too excited and proceeded with training using the data we had. And we are not alone. If you check the Ā Kaggle dataset notebooksĀ , many practitioners achieved almost 100% accuracy. The model works perfectly on the provided images, but its ability to generalize to real-world scenarios is not guaranteed.
š For the future, we need to be aware of data cascades: issues in data are 'cascaded' down the line, and the key strategy is to detect such data issues and work on them early on, before any model is trained. (Useful read: Ā "Everyone wants to do the model work, not the data work": Data Cascades in High-Stakes AIĀ ).
š It is very possible that our models relied on shortcut learning (read Ā Shortcut learning in deep neural networksĀ ). One way to investigate this is to use explainable AI methods, such as Ā Grad-CAMĀ . It can help visualize which parts of an image influence a CNN's predictions.
Conclusion
In this part, we deployed three CNN models to a single serverless SageMaker inference endpoint. We also used CloudFormation to provision the AWS resources needed to build a simple ASL Alphabet Recognition web application. Next, we tested the app with the test dataset images, as well as with our custom images. Finally, we found a gap between a toy Kaggle dataset and real-world conditions. The ability to overcome this gap is what distinguishes a simple proof of concept (POC) from a production-ready ML solution.
Series: Your first steps in Computer Vision on AWS (3 articles)
- 3[Part 3] Your first steps in Computer Vision on AWS: deploying models for an ASL recognition app This article
Enjoyed reading this content? Let the author know!
Your likes, comments, shares, and saves help creators reach more builders.
Loading recommendations
Loading article