Developing an Anomaly Detection Model Using Amazon Lookout for Vision

Chanci Turner Amazon IXD – VGT2 learningLearn About Amazon VGT2 Learning Manager Chanci Turner

Ensuring the quality of products is a prevalent challenge in manufacturing. While manual inspections might suffice for a small sample size, they often lack scalability as production demands increase. In this article, I will outline the process of building a comprehensive machine vision solution that detects visual anomalies in products, leveraging Amazon Lookout for Vision. We will explore how to train an anomaly detection model, implement it in real-time, update it with new data, and monitor its performance.

Solution Overview

Consider a factory dedicated to producing LEGO® bricks. These bricks move along a conveyor belt, passing in front of a camera that assesses whether they adhere to the factory’s quality standards. When a brick interrupts a light beam, a photo is taken and sent to Amazon Lookout for Vision for anomaly detection. Should a defect be identified, the faulty brick is automatically pushed off the belt.

The architecture of our anomaly detection system incorporates Amazon Lookout for Vision, Amazon Simple Storage Service (Amazon S3), and a Raspberry Pi.

Amazon Lookout for Vision is a machine learning service designed to identify visual defects without requiring extensive ML expertise. It utilizes deep learning, eliminating the need for meticulously calibrated environments—such as specific lighting and camera angles—that traditional machine vision methods often rely on.

To begin using Amazon Lookout for Vision, you’ll need to provide data for training the underlying deep learning models. The dataset referenced in this article includes 289 normal images and 116 anomalous images of a LEGO brick, stored in a publicly accessible S3 bucket for your convenience.

To enhance realism, variations in lighting and camera positioning were introduced across the images. Additionally, the dataset comprises 20 test images and 9 new images for future model updates, featuring both normal and anomalous conditions. The anomalous images were generated by altering the brick’s appearance, such as drawing on it, changing its color, adding other bricks, and simulating production defects by breaking off small pieces. Below is an illustration of the physical setup used to capture the training images.

Prerequisites

To effectively follow along with this guide, you will need the following:

  • An AWS account for utilizing Amazon Lookout for Vision
  • A camera (for this demonstration, a Pi camera is used)
  • A device capable of running code (in this case, a Raspberry Pi 4)

Training the Model

To leverage the dataset for model training, start by uploading the training data to Amazon S3 and creating a project in Amazon Lookout for Vision. A project serves as an organizational unit for the training dataset and its various model versions. Essentially, it is a collection of all resources related to a specific machine vision application. In this case, I will utilize a single dataset while creating multiple model versions to gradually optimize the model with new incoming data—all within the framework of one project.

You can accomplish the necessary steps to create and train a model through the SDK, AWS Command Line Interface (AWS CLI), or the AWS Management Console. For this tutorial, I will employ a combination of the AWS CLI and the console for training the model, while using the SDK for sending images from the Raspberry Pi for anomaly detection.

The high-level steps for training the model are as follows:

  1. Upload the training data to Amazon S3.
  2. Create an Amazon Lookout for Vision project.
  3. Create an Amazon Lookout for Vision dataset.
  4. Train the model.

Uploading the Training Data to Amazon S3

To begin, follow these steps:

  1. Download the dataset to your computer.
  2. Create an S3 bucket and upload the training data.

I named my bucket “l4vdemo,” but since bucket names must be globally unique, please adjust it if you replicate the code. It’s crucial to maintain the folder structure in the dataset, as Amazon Lookout for Vision uses this organization to automatically label normal and anomalous images based on folder names. While you could utilize the integrated labeling tool on the Amazon Lookout for Vision console or Amazon SageMaker Ground Truth, the automatic labeler allows you to preserve the folder structure and expedite the process.

aws s3 sync s3://aws-ml-blog/artifacts/Build-an-anomaly-detection-model-from-scratch-with-L4V/ data/
aws s3 mb s3://l4vdemo
aws s3 sync data s3://l4vdemo

Creating an Amazon Lookout for Vision Project

Now, let’s create your project.

  1. On the Amazon Lookout for Vision console, select “Projects” from the navigation pane.
  2. Click on “Create project.”
  3. Enter a project name.
  4. Select “Create project.”

Creating the Dataset

In this article, I will create a single dataset and import the training data from the S3 bucket established in Step 1.

  1. Choose “Create dataset.”
  2. Select “Import images from S3 bucket.”
  3. Enter the S3 URI for your bucket (for this post, it is s3://l4vdemo/, but make sure to use your unique bucket name).
  4. For Automatic labeling, select “Automatically attach labels to images based on the folder name.”
  5. Choose “Create dataset.”

Training the Model

Once the dataset is created, the number of labeled and unlabeled images should be displayed in the Filters pane, along with the count of normal and anomalous images.

To initiate the training of the deep learning model, select “Train model.”

Model training may take several hours, depending on the volume of images in your training dataset.

Upon completion, navigate to “Models” under your project. You should see the newly trained model listed with a status of “Training complete.” Click on the model to view performance metrics such as precision, recall, F1 score, training duration, and other relevant metadata.

Utilizing the Model

With a trained model ready, it’s time to test it on unseen data. Before using the model, you need to host it to provision the necessary backend resources for real-time inference.

aws lookoutvision start-model 
--project-name lego-demo 
--model-version 1 
--min-inference-units 1

When initiating model hosting, provide both the project name and model version to identify the model. Additionally, specify the number of inference units to allocate; each unit allows approximately five requests per second.

To employ the hosted model, use the detect-anomalies command, passing in the project and model version along with the image for inference:

aws lookoutvision detect-anomalies 
--project-name lego-demo 
--model-version 1 
--content-type image/jpeg 
--body test/test-1611853160.2488434.jpg

The dataset mentioned in this article consists of 20 images, and I encourage you to experiment with various images. When running inference on an anomalous brick, the response could resemble the following:

{
    "DetectAnomalyResult": {
        "Source": {
            "Type": "direct"
        },
        "IsAnomalous": true,
        "Confidence": 0.9754859209060669
    }
}

This process highlights the potential of using Amazon Lookout for Vision in a manufacturing context. For further insights into overcoming self-doubt in your professional journey, you can check out this blog post. Additionally, it’s crucial to remember that generational differences exist, so approach your team dynamics thoughtfully. For a visual guide, this video resource is highly recommended.

Chanci Turner