Automated defect detection using computer vision helps improve quality and lower the cost of inspection. Defect detection involves identifying the presence of a defect, classifying types of defects, and identifying where the defects are located. Many manufacturing processes require detection at a low latency, with limited compute resources, and with limited connectivity.
Amazon Lookout for Vision is a machine learning (ML) service that helps spot product defects using computer vision to automate the quality inspection process in your manufacturing lines, with no ML expertise required. Lookout for Vision now includes the ability to provide the location and type of anomalies using semantic segmentation ML models. These customized ML models can either be deployed to the AWS Cloud using cloud APIs or to custom edge hardware using AWS IoT Greengrass. Lookout for Vision now supports inference on an x86 compute platform running Linux with or without an NVIDIA GPU accelerator and on any NVIDIA Jetson-based edge appliance. This flexibility allows detection of defects on existing or new hardware.
In this post, we show you how to detect defective parts using Lookout for Vision ML models running on an edge appliance, which we simulate using an Amazon Elastic Compute Cloud (Amazon EC2) instance. We walk through training the new semantic segmentation models, exporting them as AWS IoT Greengrass components, and running inference in CPU-only mode with Python example code.
In this post, we use a set of pictures of toy aliens composed of normal and defective images such as missing limbs, eyes, or other parts. We train a Lookout for Vision model in the cloud to identify defective toy aliens. We compile the model to a target X86 CPU, package the trained Lookout for Vision model as an AWS IoT Greengrass component, and deploy the model to an EC2 instance without a GPU using the AWS IoT Greengrass console. Finally, we demonstrate a Python-based sample application running on the EC2 (C5a.2xl) instance that sources the toy alien images from the edge device file system, runs the inference on the Lookout for Vision model using the gRPC interface, and sends the inference data to an MQTT topic in the AWS Cloud. The scripts outputs an image that includes the color and location of the defects on the anomalous image.
The following diagram illustrates the solution architecture. It’s important to note for each defect type you want to detect in localization, you must have 10 marked anomaly images in training and 10 in test data, for a total of 20 images of that type. For this post, we search for missing limbs on the toy.
The solution has the following workflow:
- Upload a training dataset and a test dataset to Amazon Simple Storage Service (Amazon S3).
- Use the new Lookout for Vision UI to add an anomaly type and mark where those anomalies are in the training and test images.
- Train a Lookout for Vision model in the cloud.
- Compile the model to the target architecture (X86) and deploy the model to the EC2 (C5a.2xl) instance using the AWS IoT Greengrass console.
- Source images from local disk.
- Run inferences on the deployed model via the gRPC interface and retrieve an image of anomaly masks overlaid on the original image.
- Post the inference results to an MQTT client running on the edge instance.
- Receive the MQTT message on a topic in AWS IoT Core in the AWS Cloud for further monitoring and visualization.
Steps 5, 6, and 7 are coordinated with the sample Python application.
Before you get started, complete the following prerequisites. For this post, we use an EC2 c5.2xl instance and install AWS IoT Greengrass V2 on it to try out the new features. If you want to run on an NVIDIA Jetson, follow the steps in our previous post, Amazon Lookout for Vision now supports visual inspection of product defects at the edge.
- Create an AWS account.
- Start an EC2 instance that we can install AWS IoT Greengrass on and use the new CPU-only inference mode.You can also use an Intel X86 64 bit machine with 8 gigabytes of ram or more (we use a c5a.2xl, but anything with greater than 8 gigabytes on x86 platform should work) running Ubuntu 20.04.
- Install AWS IoT Greengrass V2:
- Install the needed system and Python 3 dependencies (Ubuntu 20.04):
Upload the dataset and train the model
We use the toy aliens dataset to demonstrate the solution. The dataset contains normal and anomalous images. Here are a few sample images from the dataset.
The following image shows a normal toy alien.
The following image shows a toy alien missing a leg.
The following image shows a toy alien missing a head.
In this post, we look for missing limbs. We use the new user interface to draw a mask around the defects in our training and tests data. This will tell the semantic segmentation models how to identify this type of defect.
- Start by uploading your dataset, either via Amazon S3 or from your computer.
- Sort them into folders titled
- When creating your dataset, select Automatically attach labels to images based on the folder name.This allows us to sort out the anomalous images later and draw in the areas to be labeled with a defect.
- Try to hold back some images for testing later of both
- After all the images have been added to the dataset, choose Add anomaly labels.
- Begin labeling data by choosing Start labeling.
- To speed up the process, you can select multiple images and classify them as
If you want to highlight anomalies in addition to classifying them, you need to highlight where the anomalies are located.
- Choose the image you want to annotate.
- Use the drawing tools to show the area where part of the subject is missing, or draw a mask over the defect.
- Choose Submit and close to keep these changes.
- Repeat this process for all your images.
- When you’re done, choose Save to persist your changes. Now you’re ready to train your model.
- Choose Train model.
After you complete these steps, you can navigate to the project and the Models page to check the performance of the trained model. You can start the process of exporting the model to the target edge device any time after the model is trained.
Retrain the model with corrected images
Sometimes the anomaly tagging may not be quite correct. You have the chance to help your model learn your anomalies better. For example, the following image is identified as an anomaly, but doesn’t show the
Let’s open the editor and fix this.
Go through any images you find like this. If you find it’s tagged an anomaly incorrectly, you can use the eraser tool to remove the incorrect tag.
You can now train your model again and achieve better accuracy.
Compile and package the model as an AWS IoT Greengrass component
In this section, we walk through the steps to compile the toy alien model to our target edge device and package the model as an AWS IoT Greengrass component.
- On the Lookout for Vision console, choose your project.
- In the navigation pane, choose Edge model packages.
- Choose Create model packaging job.
- For Job name, enter a name.
- For Job description, enter an optional description.
- Choose Browse models.
- Select the model version (the toy alien model built in the previous section).
- Choose Choose.
- If you’re running this on Amazon EC2 or an X86-64 device, select Target platform and choose Linux, X86, and CPU.
If using CPU, you can leave the compiler options empty if you’re not sure and don’t have an NVIDIA GPU. If you have an Intel-based platform that supports AVX512, you can add these compiler options to optimize for better performance:
You can see your job name and status showing as
In progress. The model packaging job may take a few minutes to complete.When the model packaging job is complete, the status shows as
- Choose your job name (in our case it’s
aliensblogcpux86) to see the job details.
- Choose Create model packaging job.
- Enter the details for Component name, Component description (optional), Component version, and Component location.Lookout for Vision stores the component recipes and artifacts in this Amazon S3 location.
- Choose Continue deployment in Greengrass to deploy the component to the target edge device.
The AWS IoT Greengrass component and model artifacts have been created in your AWS account.
Deploy the model
Be sure you have AWS IoT Greengrass V2 installed on your target device for your account before you continue. For instructions, refer to Install the AWS IoT Greengrass Core software.
In this section, we walk through the steps to deploy the toy alien model to the edge device using the AWS IoT Greengrass console.
- On the AWS IoT Greengrass console, navigate to your edge device.
- Choose Deploy to initiate the deployment steps.
- Select Core device (because the deployment is to a single device) and enter a name for Target name.The target name is the same name you used to name the core device during the AWS IoT Greengrass V2 installation process.
- Choose your component. In our case, the component name is
aliensblogcpux86, which contains the toy alien model.
- Choose Next.
- Configure the component (optional).
- Choose Next.
- Expand Deployment policies.
- For Component update policy, select Notify components.This allows the already deployed component (a prior version of the component) to defer an update until you’re ready to update.
- For Failure handling policy, select Don’t roll back.In case of a failure, this option allows us to investigate the errors in deployment.
- Choose Next.
- Review the list of components that will be deployed on the target (edge) device.
- Choose Next.You should see the message
Deployment successfully created.
- To validate the model deployment was successful, run the following command on your edge device:
You should see a similar output running the
aliensblogcpux86 lifecycle startup script:
Components currently running in Greengrass:
Run inferences on the model
Note: If you are running Greengrass as another user than what you are logged in as, you will need to change permissions of the file
We’re now ready to run inferences on the model. On your edge device, run the following command to load the model (replace <modelName> with the model name used in your component):
To generate inferences, run the following command with the source file name (replace <path/to/images> with the path and file name of the image to check and replace <modelName> with the model name used for your component):
The model correctly predicts the image as anomalous (
missing_limbs) with a confidence score of 0.9996867775917053. It tells us the mask of the anomaly tag
missing_limbs and the percentage area. The response also contains bitmap data you can decode of what it found.
Download and open the file
blended.png, which looks like the following image. Note the area highlighted with the defect around the legs.
With AWS IoT Greengrass and Lookout for Vision, you can now automate visual inspection with computer vision for processes like quality control and defect assessment—all on the edge and in real time. You can proactively identify problems such as parts damage (like dents, scratches, or poor welding), missing product components, or defects with repeating patterns on the production line itself—saving you time and money. Customers like Tyson and Baxter are discovering the power of Lookout for Vision to increase quality and reduce operational costs by automating visual inspection.
“Operational excellence is a key priority at Tyson Foods. Predictive maintenance is an essential asset for achieving this objective by continuously improving overall equipment effectiveness (OEE). In 2021, Tyson Foods launched a machine learning-based computer vision project to identify failing product carriers during production to prevent them from impacting team member safety, operations, or product quality. The models trained using Amazon Lookout for Vision performed well. The pin detection model achieved 95% accuracy across both classes. The Amazon Lookout for Vision model was tuned to perform at 99.1% accuracy for failing pin detection. By far the most exciting result of this project was the speedup in development time. Although this project utilizes two models and a more complex application code, it took 12% less developer time to complete. This project for monitoring the condition of the product carriers at Tyson Foods was completed in record time using AWS managed services such as Amazon Lookout for Vision.”
—Audrey Timmerman, Sr Applications Developer, Tyson Foods.
“Latency and inferencing speed is critical for real-time assessment and critical quality checks of our manufacturing processes. Amazon Lookout for Vision edge on a CPU device gives us the ability to achieve this on production-grade equipment, enabling us to deliver cost-effective AI vision solutions at scale.”
—A.K. Karan, Global Senior Director – Digital Transformation, Integrated Supply Chain, Baxter International Inc.
Complete the following steps to remove the assets you created from your account and avoid any ongoing billing:
- On the Lookout for Vision console, navigate to your project.
- On the Actions menu, delete your datasets.
- Delete your models.
- On the Amazon S3 console, empty the buckets you created, then delete the buckets.
- On the Amazon EC2 console, delete the instance you started to run AWS IoT Greengrass.
- On the AWS IoT Greengrass console, choose Deployments in the navigation pane.
- Delete your component versions.
- On the AWS IoT Greengrass console, delete the AWS IoT things, groups, and devices.
In this post, we described a typical scenario for industrial defect detection at the edge using defect localization and deployed to a CPU-only device. We walked through the key components of the cloud and edge lifecycle with an end-to-end example using Lookout for Vision and AWS IoT Greengrass. With Lookout for Vision, we trained an anomaly detection model in the cloud using the toy alien dataset, compiled the model to a target architecture, and packaged the model as an AWS IoT Greengrass component. With AWS IoT Greengrass, we deployed the model to an edge device. We demonstrated a Python-based sample application that sources toy alien images from the edge device local file system, runs the inferences on the Lookout for Vision model at the edge using the gRPC interface, and sends the inference data to an MQTT topic in the AWS Cloud.
In a future post, we will show how to run inferences on a real-time stream of images using a GStreamer media pipeline.
About the authors
Manish Talreja is a Senior Industrial ML Practice Manager with AWS Professional Services. He helps AWS customers achieve their business goals by architecting and building innovative solutions that use AWS ML and IoT services on the AWS Cloud.
Ryan Vanderwerf is a partner solutions architect at Amazon Web Services. He previously provided Java virtual machine-focused consulting and project development as a software engineer at OCI on the Grails and Micronaut team. He was chief architect/director of products at ReachForce, with a focus on software and system architecture for AWS Cloud SaaS solutions for marketing data management. Ryan has built several SaaS solutions in several domains such as financial, media, telecom, and e-learning companies since 1996.
Prakash Krishnan is a Senior Software Development Manager at Amazon Web Services. He leads the engineering teams that are building large-scale distributed systems to apply fast, efficient, and highly scalable algorithms to deep learning-based image and video recognition problems.