Quality Control In Manufacturing - A Complete Guide To Build Defect Detection Model

Metal Industry with AI-Powered Defect Detection
Quality Control In Manufacturing - A Complete Guide To Build Defect Detection Model

Table of Contents

  1. Introduction
  2. About Dataset
  3. Hands-on Tutorial
  4. Conclusion
  5. Frequently Asked Questions

Introduction

In a time of unrelenting innovation and technological growth, industries all over the world are going through revolutionary changes. The industry that produces metal, which is essential to contemporary infrastructure, is no exception. Maintaining the integrity of the production process is crucial as the demand for premium steel keeps rising. This is the point at which emerging technologies, including artificial intelligence, come into play and change the field of metal surface defect identification.

This tutorial is for ML researchers, product managers and data scientist who want to build a quick metal defect detection for construction site safety use case.

Model comprising a neural network that has been precisely trained to identify and classify these faults with unmatched precision is revealed when seen through the lenses of TensorFlow and Keras. We explore preprocessing methods, see the layers of the architecture come to life, and follow the model during its training epochs, all led by a callback that stops at 98% accuracy.

The neural network's learning trajectory is plotted using visualisations, which show how accuracy and loss work together harmoniously. The blog highlights the model's capacity to interpret the language of flaws with a visual symphony where anticipated and true labels merge. This isn't just a code excursion; it's an adventure into the metallurgy-AI nexus, where algorithms rewrite the standards for quality control in the steel sector. Greetings from the cutting edge of metal surface defect detection, where pixels tell the story of unwavering quality.

About Dataset

Six common surface defects discovered in hot-rolled steel strips are listed in the NEU Metal Surface Defects Database: crazing (Cr), pitted surface (PS), inclusion (In), rolled-in scale (RS), patches (Pa), and scratches (Sc). There are 1,800 grayscale photos in the dataset, 300 samples for every kind of problem.

The dataset is split into training, testing, and validation sets for this study. Of the 276 images in each class that make up the training set, the remaining 24 images are divided between the test and validation sets.

Hands-on Tutorial

Here are steps that we'll follow to build our model from scratch.

Outline

1.Dataset loading and directory information
2.Data Preprocessing
3.Convolutional Neural Network Architecture
4.Training the Model
5.Model Evaluation and Visualization
6.Test Result Visualization

Corrosion and defects in metal surfaces can lead to significant structural issues and economic losses. Detecting these defects early is crucial for preventive maintenance and ensuring the longevity of materials.

Here we offer a practical approach for metal defect detection using deep learning, specifically convolutional neural networks (CNNs), to identify surface defects in metal.

1.Dataset loading and directory information

The importing of necessary Python libraries Numpy,Pandas,OS,TensorFlow ,ImageDataGenerator and RMSprop from Keras.. The careful definition of dataset pathways makes it possible to integrate metal defect detection datasets with ease.

import numpy as np
import pandas as pd 
import os
import tensorflow as tf
from tensorflow.keras.optimizers import RMSprop
from tensorflow.keras.preprocessing.image import ImageDataGenerator
train_dir = '/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data/train'
val_dir = '/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data/valid'
test_dir='/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data/test'
print("Path Direcorty: ",os.listdir("/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data"))
print("Train Direcorty: ",os.listdir("/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data/train"))
print("Test Direcorty: ",os.listdir("/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data/test"))
print("Validation Direcorty: ",os.listdir("/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data/valid"))

Dataset 1

print("Training Inclusion data:",len(os.listdir(train_dir+'/'+'Inclusion')))

print("Testing Inclusion data:",len(os.listdir(test_dir+'/'+'Inclusion')))

print("Validation Inclusion data:",len(os.listdir(val_dir+'/'+'Inclusion')))

Dataset 2

2.Data Preprocessing

Preprocessing the data is crucial before training the model. The pictures are normalised to values between 0 and 1 and shrunk to 200 by 200 pixels.

# All images will be rescaled by 1./255
train_datagen = ImageDataGenerator(
    rescale=1. / 255,
    shear_range=0.2,
    zoom_range=0.2,
    horizontal_flip=True)


test_datagen = ImageDataGenerator(rescale=1./255)

# Flow training images in batches of 10 using train_datagen generator
train_generator = train_datagen.flow_from_directory(
        train_dir,
        target_size=(200, 200),
        batch_size=10,
        class_mode='categorical')

# Flow validation images in batches of 10 using test_datagen generator
validation_generator = test_datagen.flow_from_directory(
        val_dir,
        target_size=(200, 200),
        batch_size=10,
        class_mode='categorical')

Dataset 3

3.Convolutional Neural Network Architecture

This part defines the architecture of the Convolutional Neural Network (CNN) using Keras Sequential API. It stacks layers like Conv2D, MaxPooling2D, Dropout, Flatten, and Dense layers to create a neural network for image classification. The model is compiled using the rmsprop optimizer and  categorical cross-entropy loss function.

class myCallback(tf.keras.callbacks.Callback):
    def on_epoch_end(self, epoch, logs={}):
        if(logs.get('accuracy') > 0.98 ):
            print("\nReached 98% accuracy so cancelling training!")
            self.model.stop_training = True 
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, (2,2), activation='relu', input_shape=(200, 200, 3)),
    tf.keras.layers.MaxPooling2D(2, 2),
    tf.keras.layers.Conv2D(64, (2,2), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Conv2D(128, (2,2), activation='relu'),
    tf.keras.layers.MaxPooling2D(2,2),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(256, activation='relu'),
    tf.keras.layers.Dropout(0.2),
    tf.keras.layers.Dense(6, activation='softmax')
])

model.summary()

CNN Model

model.compile(loss='categorical_crossentropy',
              optimizer='rmsprop',
              metrics=['accuracy'])
print('Compiled!')

Compiled

4.Training the Model

This section trains the constructed CNN model using the fit() method on the training dataset. It also validates the model's performance on the validation dataset over 32 epochs. The model is trained using the training set, and a callback is implemented to stop training when the accuracy exceeds 98%.

callbacks = myCallback()
history = model.fit(train_generator,
        batch_size = 32,
        epochs=20,
        validation_data=validation_generator,
        callbacks=[callbacks],
        verbose=1, shuffle=True)

CNN Model training 1

CNN Model training 2

5.Model Evaluation and Visualization

After training, the model is evaluated on the validation set, and the training history is visualized to observe the accuracy and loss trends over epochs.

import matplotlib.pyplot as plt 
plt.figure(1)  
# summarize history for accuracy  
plt.subplot(211)  
plt.plot(history.history['accuracy'])  
plt.plot(history.history['val_accuracy'])  
plt.title('model accuracy')  
plt.ylabel('accuracy')  
plt.xlabel('epoch')  
plt.legend(['train', 'test'], loc='upper left')  
   
 # summarize history for loss  
   
plt.subplot(212)  
plt.plot(history.history['loss'])  
plt.plot(history.history['val_loss'])  
plt.title('model loss')  
plt.ylabel('loss')  
plt.xlabel('epoch')  
plt.legend(['train', 'test'], loc='upper left')  
plt.show()

CNN Model Evaluation

6.Test Result Visualization

To assess the model's performance on unseen data, a set of test images is loaded, predictions are made, and the results are visualized alongside the ground truth.

# First, we are going to load the file names and their respective target labels into numpy array! 
from sklearn.datasets import load_files
import numpy as np

test_dir = '/kaggle/input/neu-metal-surface-defects-data/NEU Metal Surface Defects Data/test'

def load_dataset(path):
    data = load_files(path)
    files = np.array(data['filenames'])
    targets = np.array(data['target'])
    target_labels = np.array(data['target_names'])
    return files,targets,target_labels
    
x_test, y_test,target_labels = load_dataset(test_dir)
no_of_classes = len(np.unique(y_test))
no_of_classes

CNN Model Test class

from keras.utils import np_utils
y_test = np_utils.to_categorical(y_test,no_of_classes)
# We just have the file names in the x set. Let's load the images and convert them into array.
from keras.preprocessing.image import array_to_img, img_to_array, load_img

def convert_image_to_array(files):
    images_as_array=[]
    for file in files:
        # Convert to Numpy Array
        images_as_array.append(img_to_array(load_img(file)))
    return images_as_array

x_test = np.array(convert_image_to_array(x_test))
print('Test set shape : ',x_test.shape)

CNN Model Test shape

x_test = x_test.astype('float32')/255
# Let's visualize test prediction.

y_pred = model.predict(x_test)

# plot a raandom sample of test images, their predicted labels, and ground truth
fig = plt.figure(figsize=(16, 9))
for i, idx in enumerate(np.random.choice(x_test.shape[0], size=16, replace=False)):
    ax = fig.add_subplot(4, 4, i + 1, xticks=[], yticks=[])
    ax.imshow(np.squeeze(x_test[idx]))
    pred_idx = np.argmax(y_pred[idx])
    true_idx = np.argmax(y_test[idx])
    ax.set_title("{} ({})".format(target_labels[pred_idx], target_labels[true_idx]),
                 color=("green" if pred_idx == true_idx else "red"))

CNN Model Test Prediction

Conclusion

After exploring the relationship between artificial intelligence and steel surface perfection, we can clearly see that the NEU Metal Surface Defects Database, when combined with advanced neural networks, signals the beginning of a new era in quality assurance for the metal production sector. The neural network, refined with TensorFlow and Keras, has demonstrated its ability to classify complex flaws and has evolved into a predictor of accuracy and efficiency.

Frequently Asked Questions

1.Which AI approach is used to identify manufacturing defects from images?

Automated visual inspection based on deep learning (DL) models, which identify surface flaws, deformations, etc., is the foundation of AI defect detection systems in manufacturing.

2.What is AI visual inspection?

Electronics manufacturers utilise Visual Inspection AI to concurrently examine dozens of individual components on high volume printed circuit boards (PCBs) in order to find soldering problems, missing, misplaced, or broken components.

3.What is defect detection in computer vision?

Utilising computer vision for defect identification enhances manufacturing efficiency and minimises expenses. Through earlier and faster detection, it reduces expenses for the producer. Additionally, it lowers scrap and raises worker productivity.

4.How AI can be used in material inspection?

Robots and AI systems can recognise and distinguish items from inputs such as still and video camera images thanks to object recognition. 3D models, component identification, edge detection, and appearance analysis from various perspectives are some of the methods used for object identification.

Looking for high quality training data to train your build defect detection model? Talk to our team to get a tool demo.

Train Your Vision/NLP/LLM Models 10X Faster

Book our demo with one of our product specialist

Book a Demo