Skip to article frontmatterSkip to article content
Site not loading correctly?

This may be due to an incorrect BASE_URL configuration. See the MyST Documentation for reference.

Visual Quality Control Inspections

Visual Quality Control Inspections

Improve Automated Inspection with Computer Vision

A model that classifies images as ‘Good’ / ‘Anomaly’. Trained without any labels for defective regions, model in the inference mode is able to predict a bounding box for a defective region in the image. This is achieved by processing feature maps of the deep convolutional layers. AI-powered systems can inspect products for defects with a high degree of accuracy, which can help to reduce the number of defective products that are produced.

Visual Quality Control Inspections as a AI for Manufacturing Usecase:

Automating visual quality control inspections for life sciences can be considered as a use case of AI in manufacturing. In life sciences, AI-powered visual quality control inspections involve using computer vision algorithms to analyze images and video of products, such as drugs, medical devices, and biologics, in order to identify defects, contaminants, or other issues that may affect product quality.

AI Project Cycle AI_Project_Cycle.png

Context: Understanding the Problem Statement --------Problem Scoping (AI Project Cycle - Step 1)

An Deep Learning based classification solution for Defect Detection Using Convolutional Neural Networks

  • In the manufacturing of life sciences products, visual quality control inspections are an important step in ensuring that products meet the necessary standards for safety and efficacy. AI-based visual inspection systems can help to automate this process by analyzing images and videos of products in real-time, identifying defects, and flagging any issues that may arise. This can help to improve the speed and accuracy of visual inspections, and ensure that only high-quality products reach the market.

  • Additionally, AI-based visual inspection systems can also be used to monitor the manufacturing process, identifying any issues that may arise during production and helping to prevent defects from occurring in the first place. Overall, automating visual quality control inspections for life sciences is a good example of AI in manufacturing usecase which helps to improve the overall quality and safety of life sciences products.

Import the useful Packages & Libraries

To know more about the pytorch click here

To know more about OS module click here

To know more about what is Seaborn click here

To know more about matrices and scoring click here

To know more about warnings click here

Dataset: Data Acquisition (AI Project Cycle - Step 2)

Dataset used - MVTEC Anomaly Detection Dataset. This dataset is released under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License (CC BY-NC-SA 4.0), which means it is not allowed to use it for commertial purposes.

This MVTec anomaly detection dataset (MVTec AD) is built with data derived from augmentation techniques, such as histogram equalization, letter box, random perspective, flip, scaling, and rotation.

DataSet: https://www.mvtec.com/company/research/datasets/mvtec-ad (only download Leather dataset for this use case)

Data preparation

The leather dataset is downloaded and extracted in a folder before running the training python module.

The dataset available from the source requires a filtering before the training. Assuming the leather dataset is downloaded from the dataset source given above in this document, Follow the below steps to filter the dataset extracted from the source.

tar -xf leather.tar.xz

mkdir -p data/{train/{good,bad},test/{good,bad}}

cd leather/train/good/ cp (ls | head -n 210) ../../../data/train/good/ cp (ls | tail -n 65) ../../../data/test/good/

cd leather/test/combined cp (ls | head -n 17) ../../../data/train/bad/ cp (ls | tail -n 5) ../../../data/test/bad/

overview_dataset

Data Preprocessing ------ Data Exploration(AI Project Cycle - Step 3)

Data exploration is performed to gather information on data and based on the gaind insights, various pre-processing functions are implemented.

Here, A custom dataset class, called MVTEC_AD_DATASET has been defined that is meant to be used with the MVTEC Anomaly Detection dataset. The dataset contains images and associated labels, which can be used to train and evaluate machine learning models.

The class inherits from the PyTorch Dataset class and implements several methods to allow it to be used with PyTorch’s data loading and processing tools. The init method sets up the class by defining some parameters, such as the class labels, which are either “Good” and “Anomaly”.

To know more about how to create classes click here where as for learning how to create functions click here

To know more about the init function click here

Splitting and Loading dataset into separate training and test set

80% of the data has been used for training and 20% is used for testing. The resulting train and test sets are loaded into PyTorch DataLoaders with the specified batch size.

The get_cv_train_test_loaders function splits the dataset into train and test sets for N-Fold cross-validation.

Building a Model ------ Modeling ( AI Project Cycle - Step 4)

Modeling is the fourth step in the AI project cycle, which involves building a machine learning model to solve a particular problem. This step is where we use the pre-processed data to train a model that can predict the desired outcome.

Here, a function called “train” has been defined that trains a machine learning model. The function takes several arguments and trains the model by looping over the training data in dataloader for epochs number of times. In each epoch, the model makes predictions on the inputs, calculates the loss between the predictions and the actual labels, and optimizes its parameters based on the loss gradient. The running loss and accuracy are calculated and displayed at the end of each epoch.

Evaluating the Model ------- Evaluation (AI Project Cycle - Step 5)

Evaluation of an AI project refers to the process of evaluating the performance of a model on a test set after it has been trained. The purpose of evaluation is to assess the model’s ability to generalize to unseen data and to estimate its accuracy in terms of relevant metrics.

Here,the confusion matrix is generated. It takes as inputs the true labels (y_true) and the predicted labels (y_pred) of the test data, and an optional argument class_names which provides the names of the classes. The function uses confusion_matrix from scikit-learn to compute the confusion matrix between the true and predicted labels. It then plots the confusion matrix using sns.heatmap from the seaborn library. The plot shows the actual labels on the y-axis and the predicted labels on the x-axis. The color of each cell represents the count of samples that belong to the class in the corresponding row and column. The x and y axis labels, title and the figure size are set. Finally, the function displays the plot using plt.show().

Model Deployment Over the Test Dataset ------- Deployment (AI Project Cycle - Step 6)

Parameters to run the functions defined above

Loading the Data

Two data loaders for training and testing data are created.

root, batch_size, test_size and random_state are the four parameters used here. The returned data loaders will be used to feed the data to the model for training and evaluation.

Model Training

Model training is the process of using a set of training data to learn the parameters of a machine learning model. The goal of model training is to find a set of parameters that minimize the difference between the model’s predictions and the true target values in the training data.

Here, we initialized and set up components for training the model using Pytorch.

Evaluation

Cross Validation

Cross validation is a technique used in machine learning to evaluate the performance of a model by dividing the dataset into subsets, training the model on one subset and testing it on another subset.

Pytorch library has been used here to perform cross validation for training and evaluating the model.

Overall, we have trained a custom VGG model and evaluated its performance using a cross-validation procedure, where the performance is estimated by averaging over multiple trials with different training and testing sets.

Visualization

Visualization is an easier way to understand the results obtained.

Here, you can see that predicted as well as the true labels for each of the images along with the prediction of the likelihood of a positive outcome known as Probability(Prob) have been displayed above with the images.

After the model gets trained, The images get classified as ‘Good’ or ‘Anomaly’.