Quality inspection of manufactured product¶
Credit: AITS Cainvas Community
Deep learning can be used to automate the inspection process of manufactured goods.
It is crucial to identify defective products to maintian the reputation of the product in the market as well as for safety concerns in some cases. Deep learning help identify the defective ones with high accuracy.
The output of the model can be used to triiger a movement to seperate the defective product from the non defective ones.
The notebook differentiates between defective and non-defective casting products.
Casting defect is an undesired irregularity in a metal casting process.
There are many types of defect in casting like blow holes, pinholes, burr, shrinkage defects, mould material defects, pouring metal defects, metallurgical defects, etc.
import numpy as np
import os
import tensorflow as tf
import matplotlib.pyplot as plt
from tensorflow.keras import layers
import random
from sklearn.metrics import confusion_matrix
#!wget -N "https://cainvas-static.s3.amazonaws.com/media/user_data/cainvas-admin/casting_data.zip"
#!unzip -q casting_data.zip
The dataset has two folders - train and test, each with subfolders - def_front (defective) and ok_front (non-defective).
dataset_path = 'casting_data'
batch = 32
# The train and test datasets
print("Train dataset")
train_ds = tf.keras.preprocessing.image_dataset_from_directory(dataset_path+'/train', batch_size=batch)
print("Test dataset")
test_ds = tf.keras.preprocessing.image_dataset_from_directory(dataset_path+'/test', batch_size=batch)
Understanding the subfolders¶
class_names = train_ds.class_names
print("Train class names: ", train_ds.class_names)
print("Test class names: ", test_ds.class_names)
Visualizing the samples in the dataset¶
plt.figure(figsize=(10, 10))
for images, labels in train_ds:
for i in range(9):
ax = plt.subplot(3, 3, i + 1)
plt.imshow(images[i].numpy().astype("uint8"))
plt.title(class_names[labels[i]])
plt.axis("off")
break
print("Looking into the shape of images and labels in one batch\n")
for image_batch, labels_batch in train_ds:
print("Shape of images input for one batch: ", image_batch.shape)
print("Shape of images labels for one batch: ", labels_batch.shape)
break
# Normalizing the pixel values
normalization_layer = layers.experimental.preprocessing.Rescaling(1./255)
train_ds = train_ds.map(lambda x, y: (normalization_layer(x), y))
test_ds = test_ds.map(lambda x, y: (normalization_layer(x), y))
Model¶
model = tf.keras.Sequential([
layers.Conv2D(32, 3, activation='relu'),
layers.MaxPool2D(),
layers.Conv2D(32, 3, activation='relu'),
layers.MaxPool2D(),
layers.Conv2D(32, 3, activation='relu'),
layers.MaxPool2D(),
layers.Flatten(),
layers.Dense(128, activation='relu'),
layers.Dense(len(class_names), activation = 'softmax')
])
model.compile(optimizer='adam', loss=tf.losses.SparseCategoricalCrossentropy(), metrics=['accuracy'])
history = model.fit(train_ds, epochs=8)
model.summary()
model.evaluate(test_ds)
Plotting the metrics¶
def plot(history, variable):
plt.plot(range(len(history[variable])), history[variable])
plt.title(variable)
plot(history.history, "accuracy")
plot(history.history, "loss")
Prediction¶
# pick random test data sample from one batch
x = random.randint(0, batch - 1)
for i in test_ds.as_numpy_iterator():
img, label = i
plt.axis('off') # remove axes
plt.imshow(img[x]) # shape from (32, 256, 256, 3) --> (256, 256, 3)
output = model.predict(np.expand_dims(img[x],0)) # getting output; input shape (256, 256, 3) --> (1, 256, 256, 3)
pred = np.argmax(output[0]) # finding max
print("Prdicted: ", class_names[pred]) # Picking the label from class_names base don the model output
print("True: ", class_names[label[x]])
print("Probability: ", output[0][pred])
break
deepC¶
model.save('quality_check.h5')
!deepCC quality_check.h5
# pick random test data sample from the batch
x = random.randint(0, batch - 1)
for i in test_ds.as_numpy_iterator():
img, label = i # i contains all test samples
np.savetxt('sample.data', (img[x]).flatten()) # xth sample into text file
plt.axis('off')
plt.imshow(img[x])
print("True: ", class_names[label[x]])
break
# run exe with input
!quality_check_deepC/quality_check.exe sample.data
# show predicted output
nn_out = np.loadtxt('dense_1.out')
pred = np.argmax(nn_out)
print ("Model predicted the product quality: ", class_names[pred], " with prbability ", nn_out[pred])