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.

Solution overview

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.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

The solution has the following workflow:

  1. Upload a training dataset and a test dataset to Amazon Simple Storage Service (Amazon S3).
  2. Use the new Lookout for Vision UI to add an anomaly type and mark where those anomalies are in the training and test images.
  3. Train a Lookout for Vision model in the cloud.
  4. Compile the model to the target architecture (X86) and deploy the model to the EC2 (C5a.2xl) instance using the AWS IoT Greengrass console.
  5. Source images from local disk.
  6. Run inferences on the deployed model via the gRPC interface and retrieve an image of anomaly masks overlaid on the original image.
  7. Post the inference results to an MQTT client running on the edge instance.
  8. 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.

Prerequisites

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.

  1. Create an AWS account.
  2. 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.
  3. Install AWS IoT Greengrass V2:
    git clone https://github.com/aws-samples/amazon-lookout-for-vision.git
    cd edge
    # be sure to edit the installation script to match your region, also adjust any device names and groups!
    vi install_greengrass.sh

  4. Install the needed system and Python 3 dependencies (Ubuntu 20.04):
    # install Ubuntu dependencies on the EC2 instance
    ./install-ec2-ubuntu-deps.sh
    pip3 install -r requirements.txt
    # Replace ENDPOINT variable in sample-client-file-mqtt.py with the value on the AWS console AWS IoT->Things->l4JetsonXavierNX->Interact. # Under HTTPS. It will be of type <name>-ats.iot.<region>.amazon.com 

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.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

The following image shows a toy alien missing a leg.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

The following image shows a toy alien missing a head.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

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.

  1. Start by uploading your dataset, either via Amazon S3 or from your computer.
  2. Sort them into folders titled normal and anomaly.
  3. 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.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  4. Try to hold back some images for testing later of both normal and anomaly.
  5. After all the images have been added to the dataset, choose Add anomaly labels.
  6. Begin labeling data by choosing Start labeling.
  7. To speed up the process, you can select multiple images and classify them as Normal or Anomaly.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
    If you want to highlight anomalies in addition to classifying them, you need to highlight where the anomalies are located.
  8. Choose the image you want to annotate.
  9. Use the drawing tools to show the area where part of the subject is missing, or draw a mask over the defect.
  10. Choose Submit and close to keep these changes.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  11. Repeat this process for all your images.
  12. When you’re done, choose Save to persist your changes. Now you’re ready to train your model.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  13. 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 missing_limbs tag.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

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.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

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.

  1. On the Lookout for Vision console, choose your project.
  2. In the navigation pane, choose Edge model packages.
  3. Choose Create model packaging job.
  4. For Job name, enter a name.
  5. For Job description, enter an optional description.
  6. Choose Browse models.
  7. Select the model version (the toy alien model built in the previous section).
  8. Choose Choose.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  9. 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: {"mcpu": "skylake-avx512"}.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,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 Success.
  10. Choose your job name (in our case it’s aliensblogcpux86) to see the job details.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  11. Choose Create model packaging job.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  12. 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.
  13. Choose Continue deployment in Greengrass to deploy the component to the target edge device.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

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.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

  1. On the AWS IoT Greengrass console, navigate to your edge device.
  2. Choose Deploy to initiate the deployment steps.
  3. 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.
  4. Choose your component. In our case, the component name is aliensblogcpux86, which contains the toy alien model.
  5. Choose Next.
  6. Configure the component (optional).
  7. Choose Next.
  8. Expand Deployment policies.
  9. 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.
  10. For Failure handling policy, select Don’t roll back.In case of a failure, this option allows us to investigate the errors in deployment.
  11. Choose Next.
  12. Review the list of components that will be deployed on the target (edge) device.
  13. Choose Next.You should see the message Deployment successfully created.
  14. To validate the model deployment was successful, run the following command on your edge device:
    sudo /greengrass/v2/bin/greengrass-cli component list

You should see a similar output running the aliensblogcpux86 lifecycle startup script:

Components currently running in Greengrass:

Components currently running in Greengrass: Component Name: aws.iot.lookoutvision.EdgeAgent Version: 0.1.34 State: RUNNING Configuration: {"Socket":"unix:///tmp/aws.iot.lookoutvision.EdgeAgent.sock"} Component Name: aliensblogcpux86 Version: 1.0.0 State: RUNNING Configuration: {"Autostart":false}

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 /tmp/aws.iot.lookoutvision.EdgeAgent.sock:

chmod 666 /tmp/aws.iot.lookoutvision.EdgeAgent.sock

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):

# run command to load the model# This will load the model into running state pass
# the name of the model component as a parameter.
python3 warmup-model.py <modelName>

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):

python3 sample-client-file-mqtt.py </path/to/images> <modelName> start client ['sample-client-file.py', 'aliens-dataset/anomaly/1.png', 'aliensblogcpux86']
channel set
shape=(380, 550, 3)
Image is anomalous, (90.05860090255737 % confidence) contains defects with total area over .1%: {'missing_limbs': '#FFFFFF'}

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.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,

Customer stories

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.

Cleanup

Complete the following steps to remove the assets you created from your account and avoid any ongoing billing:

  1. On the Lookout for Vision console, navigate to your project.
  2. On the Actions menu, delete your datasets.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  3. Delete your models.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  4. On the Amazon S3 console, empty the buckets you created, then delete the buckets.
  5. On the Amazon EC2 console, delete the instance you started to run AWS IoT Greengrass.
  6. On the AWS IoT Greengrass console, choose Deployments in the navigation pane.
  7. Delete your component versions.
    Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,
  8. On the AWS IoT Greengrass console, delete the AWS IoT things, groups, and devices.

Conclusion

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.

Start your journey towards industrial anomaly detection and identification by visiting the Amazon Lookout for Vision and AWS IoT Greengrass resource pages.


About the authors

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,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.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,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.

Hyperedge- . IoT, Embedded Systems, Artificial Intelligence,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.

Read more about this on: AWS