Steel Defect Detection using CNN


Steel is one of the most important building materials of modern times. Steel buildings are resistant to natural and man-made wear which has made the material ubiquitous around the world. To help make production of steel more efficient, this competition will help identify defects.

Severstal is leading the charge in efficient steel mining and production. They believe the future of metallurgy requires development across the economic, ecological, and social aspects of the industry — and they take corporate responsibility seriously. The company recently created the country’s largest industrial data lake, with petabytes of data that were previously discarded. Severstal is now looking to machine learning to improve automation, increase efficiency, and maintain high quality in their production.

The production process of flat sheet steel is especially delicate. From heating and rolling, to drying and cutting, several machines touch flat steel by the time it’s ready to ship. Today, Severstal uses images from high frequency cameras to power a defect detection algorithm.

In this competition, you’ll help engineers improve the algorithm by localizing and classifying surface defects on a steel sheet.

If successful, you’ll help keep manufacturing standards for steel high and enable Severstal to continue their innovation, leading to a stronger, more efficient world all around us.

Importing necessary libraries

import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
import matplotlib.pyplot as plt
import keras
from sklearn.model_selection import train_test_split
from tqdm import tqdm
import cv2
from sklearn.preprocessing import OneHotEncoder,LabelEncoder
from keras.utils import to_categorical
from keras.models import Sequential
import tensorflow as tf
from keras.layers import Dense,Conv2D,Flatten,MaxPooling2D,Dropout

Reading the dataset

data = pd.read_csv("/kaggle/input/severstal-steel-defect-detection/train.csv")

The dataset has three features ImageID , ClassID and EncodedPixels.

data.shape(7095, 3)

We have 7095 observations and 3 features.

Countplot of ClassID

data["ClassId"].value_counts().plot(kind = 'bar')
3 5150
1 897
4 801
2 247
Name: ClassId, dtype: int64

ClassID 3 has the maximum count.

Image Augmentation

for img,ClassId,EncodedPixels in tqdm(data.values):

Checking Random Steel images to check whether it is defected or not

i = 15
i = 16

Label encoding the input features and one hot encoding the target feature.

encoder = LabelEncoder()

X= np.array(l1)
X = X/255

y = encoder.fit_transform(l2)
y = to_categorical(y)

Splitting the data into 80% train set and 20% test set

X_train,X_test,y_train,y_test=train_test_split(X,y,test_size=0.2,stratify=y,shuffle=True)print("x_train shape:",X_train.shape)
print("x_test shape:",X_test.shape)
print("y_train shape:",y_train.shape)
print("y_test shape:",y_test.shape)
x_train shape: (5676, 120, 120, 3)
x_test shape: (1419, 120, 120, 3)
y_train shape: (5676, 4)
y_test shape: (1419, 4)

Building the CNN Model

early_stopping = tf.keras.callbacks.EarlyStopping(patience=5,min_delta=0.001,restore_best_weights=True)

Compiling the model


Fitting the model

history =,y_train,epochs=15,validation_data=(X_test,y_test),batch_size=128,
verbose=1, callbacks=[early_stopping])

At 8th epoch we are getting an validation accuracy of 77% and validation loss of 0.65 due to callback(early stopping) it stopped at this epoch.

Loss Curve

history_df = pd.DataFrame(history.history)
history_df.loc[:, ['loss', 'val_loss']].plot(title = 'Loss curve')

Accuracy Curve

history_df = pd.DataFrame(history.history)
history_df.loc[:, ['accuracy', 'val_accuracy']].plot(title = 'Accuracy curve')

Evaluating the Model

result = model.evaluate(X_test, y_test)

loss = result[0]
accuracy = result[1]
print(f"[+] Accuracy: {accuracy*100:.2f}%")
45/45 [==============================] - 3s 74ms/step - loss: 0.5669 - accuracy: 0.7590
[+] Accuracy: 75.90%


We are getting an accuracy of 75.90% using CNN model to detect defective steel images. We can further improve our model using more complex layers. Considering it as a baseline model it further needs to be optimized to get a better performance.

Hope you liked the analysis!

You can follow me on Linkedin , Github and Kaggle.

Github Link

Kaggle Link of this project

Linkedin Link




Software Engineer at Cyient | Data Science | Analytics | ML | AI | Deep Learning | NLP

Love podcasts or audiobooks? Learn on the go with our new app.

6 most commonly used Java Machine learning libraries

RTX 2080Ti Vs GTX 1080Ti: FastAI Mixed Precision training & comparisons on CIFAR-100

AI, Sustainability Tweets: Sentiment Analysis Using Pre-trained Models

Artificial object balancing

How to Train SpaCy to Recognize Harry Potter Charms as Spell Entities

Potentiate Artificial Neural Networks: Thinking Learning (TL) and Smart Learning (SL) Theory

Reading the signs of migraines with ML: from algorithm to successful product development

Demystifying the GAN using a 1D Function (Keras)

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Ratul Ghosh

Ratul Ghosh

Software Engineer at Cyient | Data Science | Analytics | ML | AI | Deep Learning | NLP

More from Medium

Human Activity Recognition

Rename images based on the text on a image

Comparison between Classical Statistical Model (ARIMA) and Deep Learning Techniques (RNN, LSTM)…

Dog Breed Classifier, Udacity Nano Degree Capstone Project