Deep learning project : Metal surface defects detection

This notebook presents a case study that aims to classify defects on metal surfaces. To do this we have a dataset of 1800 images of steel surfaces. There are 6 possible defects, so we have 300 images of steel surfaces for each defect. We started with a sub-dataset of only 90 images: 15 for each defect, dividing the set into two parts:

Transformations to apply to each image : resize and transformation to tensor

Dataset with 90 images

The loader will load each time 32 images for one training iteration and we set a seed to have always the same random batches.

All images are square and are made up of 40000 pixels (200 x 200)

Printing the labels of a batch as a result we get labels with numbers in a range between 0 and 5, as there are 6 defects identified by numbers. The defects corresponding to the various numbers are:

Here is the print of the first image of the batch.

Here is the plot of a big part of the batch.

We create our own neural network model: since the image has already been transformed into a tensor, it has already been flattened, so in theory the input layer should consist of 40000 neurons (200x200). However, since our images are considered in colour, they have to pass through 3 filters (RGB) and therefore the input layer is composed of 120,000 neurons (40000x3). The output layer must be made up of 6 neurons because there are 6 possible metal defects. We have decided to create 2 hidden layers with 128 and 64 neurons each.

Each Neuron from the Hidden Layer is implemented with a ReLu activation function and the output layer with a LogSoftmax. This architecture is better suited for Convolutional Neural Network which is the reference for image processing.

NN Representation

We opt for a the negative log likelihood loss. It is useful to train a classification problem with C classes.

The image tensor consists of 32 images (batch size) and therefore 32 rows and 120000 columns (number of pixels in each image multiplied by 3 because of the 3 RGB filters).

We now apply backward propagation to improve the weights and lower the cost function.

Using the stochastic gradient descent optimiser, we try to achieve a good accuracy score by making 20 epochs (as the Gradient Descent is an iterative process and updating the weights with single pass or one epoch is not enough). We set again the seed to have always the same results.

The time to complete the operation is very low even though there are so many neurons because in this case the dataset is only 90 images.

By training our neural network we had an accuracy score of our model of 57%. This means that out of 30 images in our test set, our model correctly classified the defect on the metal in 57% of the cases (i.e. 17 out of 30 images).

Dataset with 1800 images

Not being a very high score, we try in the second part of the notebook to enlarge the dataset from 90 images to 1800. Moreover in the previous dataset the test set was 33.3% of the total dataset, while the dataset of 1800 images is separated in this way:

Again, the batch size chosen is 32 images.

In order to increase the final accuracy of the model we have tried an heavier NN architecture with 4 hidden layers (512, 256, 128, 64 respectively).

NN Representation

The time in this case is much higher (about 5 minutes) due to both the much larger dataset and the addition of 2 hidden layers compared to the previous model.

With the larger dataset we managed to get a higher accuracy score: 64%. This means that out of the 180 images in the test set, the model got the metal default right in 115 images.

Let's test one image prediction from the test set

This image was correctly predicted: in fact it is an inclusion defect (type 1) and our neural network gave the correct defect as a prediction.

Test on a random unlabeled image

In this last part we take an image from the validation set: i.e. an unlabelled image that our model has never seen, to see if it can correctly predict the type of defect.

Of this image, we do not have the label, but comparing by eye with the images representing a defect of type scratches (type 5), it seems that our model correctly predicted this image coming out of the dataset without the labels as well.

Possible business use case

This algorithm is useful in order to revamp the production part of the value-chain of a company. In fact, this model associated to a visual sensor could help to improve quality on the production line of a steel company. The line detects an anomaly on the steel layer and an operator or another robotisation can remove the steel layer from the line. This system exists already in the automotive and aeronautic industry where robots empowered by computer vision are doing the checkup of the pieces in order to validate its quality and lower error rate.