Skip to content

awslabs/sagemaker-defect-detection

Visual Inspection Automation with Amazon SageMaker

Actions Status CodeQL Status Code style: black
License: Apache-2.0 Maintenance AMA

This solution detects product defects with an end-to-end Deep Learning workflow for quality control in manufacturing process. The solution takes input of product images and identifies defect regions with bounding boxes. In particular, this solution takes two distinct approaches:

  1. Use an implementation of the Defect Detection Network (DDN) algorithm following An End-to-End Steel Surface Defect Detection on NEU surface defect database (see resources) in PyTorch using PyTorch-lightning.
  2. Use a pre-trained Sagemaker object detection model and fine-tune on the target dataset.

This solution will demonstrate the immense advantage of fine-tuning a high-quality pre-trained model on the target dataset, both visually and numerically.

Contents

  1. Overview
    1. What Does the Input Data Look Like?
    2. How to Prepare Your Data to Feed into the Model?
    3. What are the Outputs?
    4. What is the Estimated Cost?
    5. What Algorithms & Models are Used?
    6. What Does the Data Flow Look Like?
  2. Solution Details
    1. Background
    2. What is Visual Inspection?
    3. What are the Problems?
    4. What Does this Solution Offer?
  3. Architecture Overview
  4. Cleaning up
  5. Customization

1. Overview

1.1. What Does the Input Data Look Like?

Input is an image of a defective / non-defective product. The training data should have relatively balanced classes, with annotations for ground truth defects (locations and defect types) per image. Here are examples of annotations used in the demo, they show some "inclusion" defects on the surface:

"sample2"

The NEU surface defect database (see references) is a balanced dataset which contains

Six kinds of typical surface defects of the hot-rolled steel strip are collected, i.e., rolled-in scale (RS), patches (Pa), crazing (Cr), pitted surface (PS), inclusion (In) and scratches (Sc). The database includes 1,800 grayscale images: 300 samples each of six different kinds of typical surface defects

Here is a sample image of the six classes

"data sample"

1.2. How to Prepare Your Data to Feed into the Model?

There are data preparation and preprocessing steps and should be followed in the notebooks. It's critical to prepare your image annotations beforehand.

  • For training the DDN model, please prepare one xml file for each image with defect annotations. Check notebook 0,1,2,3 for details.
  • For finetuning pretrained Sagemaker models, you need to prepare either a single annotation.json for all data, or a RecordIO file for both all images and all annotations. Check notebook 4 for details.

1.3. What are the Outputs?

  • For each image, the trained model will produce bounding boxes of detected visual defects (if any), the predicted defect type, and prediction confidence score (0~1).
  • If you have a labeled test dataset, you could obtain the mean Average Precision (mAP) score for each model and compare among all the models.
    • For example, the mAP scores on a test set of the NEU dataset

      DDN Type1 Type1+HPO Type2 Type2+HPO
      mAP 0.08 0.067 0.226 0.371 0.375

1.4. What is the Estimated Cost?

  • Running solution notebook 0~3 end-to-end costs around $8 USD and less than an hour, assuming using p3.2xlarge EC2 instance, and $3.06 on-demand hourly rate in US East. These notebooks only train DDN models for a few iterations for demonstration purpose, which is far from convergence. It would take around 8 hours to train from scratch till convergence and cost $25+.

  • Running solution notebook 4 costs around $130~140 USD. This notebook provides advanced materials, including finetuning two types of pretrained Sagemaker models till convergence, with and without hyperparameter optimization (HPO), and result in four models for inference. You could choose to train either one model, or all four models according to your budget and requirements. The cost and runtime for training each model are:

    Model Cost (USD) Runtime (Hours) Billable time (Hours)
    Type 1 1.5 0.5 0.5
    Type 1 with HPO (20 jobs) 30.6 1* 10
    Type 2 4.6 1.5 1.5
    Type 2 with HPO (20 jobs) 92 3* 30
    (*) HPO tasks in this solution consider 20 jobs in total and 10 jobs in parallel. So 1 actual runtime hour amounts to 10 billable cost hours.
  • Please make sure you have read the cleaning up part in Section 4 after training to avoid incurred cost from deployed models.

1.5. What Algorithms & Models are Used?

  • The DDN model is based on Faster RCNN. For more details, please read the paper An End-to-End Steel Surface Defect Detection.
  • The pretrained Sagemaker models include SSD models and FasterRCNN model, using either VGG, ResNet, or MobileNet as backbone, pretrained on either ImageNet, COCO, VOC, or FPN dataset.

1.6. What Does the Data Flow Look Like?

Data flow

2. Solution Details

2.1. Background

According to the Gartner study on the top 10 strategic tech trends for 2020, hyper-automation is the number one trend in 2020 and will continue advancing in future. When it comes to manufacturing, one of the main barriers to hyper-automation is in areas where Human involvements is still struggling to be reduced and intelligent systems have hard times to become on-par with Human visual recognition abilities and become mainstream, despite great advancement of Deep Learning in Computer Vision. This is mainly due to lack of enough annotated data (or when data is sparse) in areas such as Quality Control sections where trained Human eyes still dominates.

2.2. What is Visual Inspection?

The analysis of products on the production line for the purpose of Quality Control. According to Everything you need to know about Visual Inspection with AI, visual inspection can also be used for internal and external assessment of the various equipment in a production facility such as storage tanks, pressure vessels, piping, and other equipment which expands to many industries from Electronics, Medical, Food and Raw Materials.

2.3. What are the Problems?

  • Human visual inspection error is a major factor in this area. According to the report The Role of Visual Inspection in the 21st Century

    Most inspection tasks are much more complex and typically exhibit error rates of 20% to 30% (Drury & Fox, 1975)

which directly translates to cost.

  • Cost: according to glassdoor estimate, a trained quality inspector salary varies between 29K (US) - 64K per year.

2.4. What Does this Solution Offer?

This solution offers

  1. an implementation of the state-of-the-art Deep Learning approach for automatic Steel Surface Defect Detection using Amazon SageMaker. The model enhances Faster RCNN and outputs possible defects in an steel surface image. This solution trains a classifier on NEU-CLS dataset as well as a detector on NEU-DET dataset.
  2. a complete solution using high-quality pretrained Sagemaker models to finetune on the target dataset with and without hyperparameter optimization (HPO).

The most important information this solution delivers, is that training a deep learning model from scratch on a small dataset can be both time-consuming and less effective, whereas finetuning a high-quality pretrained model, which was trained on large-scale dataset, could be both cost- and runtime-efficient and highly performant. Here are the sample detection results

drawing

3. Architecture Overview

The following illustration is the architecture for the end-to-end training and deployment process

"Solution Architecture"

  1. The input data located in an Amazon S3 bucket
  2. The provided SageMaker notebook that gets the input data and launches the later stages below
  3. Training Classifier and Detector models and evaluating its results using Amazon SageMaker. If desired, one can deploy the trained models and create SageMaker endpoints
  4. SageMaker endpoint created from the previous step is an HTTPS endpoint and is capable of producing predictions
  5. Monitoring the training and deployed model via Amazon CloudWatch

4. Cleaning up

When you've finished with this solution, make sure that you delete all unwanted AWS resources. AWS CloudFormation can be used to automatically delete all standard resources that have been created by the solution and notebook. Go to the AWS CloudFormation Console, and delete the parent stack. Choosing to delete the parent stack will automatically delete the nested stacks.

Caution: You need to manually delete any extra resources that you may have created in this notebook. Some examples include, extra Amazon S3 buckets (to the solution's default bucket), extra Amazon SageMaker endpoints (using a custom name).

5. Customization

For using your own data, make sure it is labeled and is a relatively balanced dataset. Also make sure the image annotations follow the required format.

Useful Links

References

  • K. Song and Y. Yan, “A noise robust method based on completed local binary patterns for hot-rolled steel strip surface defects,” Applied Surface Science, vol. 285, pp. 858-864, Nov. 2013.

  • Yu He, Kechen Song, Qinggang Meng, Yunhui Yan, “An End-to-end Steel Surface Defect Detection Approach via Fusing Multiple Hierarchical Features,” IEEE Transactions on Instrumentation and Measuremente, 2020,69(4),1493-1504.

  • Hongwen Dong, Kechen Song, Yu He, Jing Xu, Yunhui Yan, Qinggang Meng, “PGA-Net: Pyramid Feature Fusion and Global Context Attention Network for Automated Surface Defect Detection,” IEEE Transactions on Industrial Informatics, 2020.

Security

See CONTRIBUTING for more information.

License

This project is licensed under the Apache-2.0 License.