Fashion Products classification using CNN

Introduction

In this tutorial we are going to implement a convolutional neural network model on fashion mnist data set.  This model identifies that the given image belongs to which category of clothing like shirt, t-shirt, shoes, dress, etc. This is a simple convolutional neural network model. In this project we didn't use transfer learning or a predefined model.  We implement our own convolutional neural network architecture with different layers like Conv2D, Dense, MaxPooling2D, etc. After Train the CNN model, this gave me above 90% accuracy on the training data set.



About Data set :-


This is a dataset of 60,000 28x28 grayscale images of 10 fashion categories, along with a test set of 10,000 images. This dataset can be used as a drop-in replacement for MNIST.


The classes are:-

  • 0 T-shirt/top
  • 1 Trouser
  • 2 Pullover
  • 3 Dress
  • 4 Coat
  • 5 Sandal
  • 6 Shirt
  • 7 Sneaker
  • 8 Bag
  • 9 Ankle boot

x_train: uint8 NumPy array of grayscale image data with shapes (60000, 28, 28), containing the training data.

y_train: uint8 NumPy array of labels (integers in range 0-9) with shape (60000,) for the training data.

x_test: uint8 NumPy array of grayscale image data with shapes (10000, 28, 28), containing the test data.

y_test: uint8 NumPy array of labels (integers in range 0-9) with shape (10000,) for the test data.



Let’s start :-


First of all, import all the required libraries like numpy, tensorflow, pandas, numpy, etc. These libraries are used to implement the CNN model which classifies the cloth.


Now load the data set. This data set is available in the tensorflow library.  so we need to simply write the code which returns the four data sets: X_train, Y_train, X_test, Y_test.

Check the shape of the data set. you can see that the data set is available in the three-dimensional form. 60000 images for training and 10000 images for testing.



Now check the 0 index image and you can see the result that the image is in the form of a three dimensional array.

Now see the result of the 0 index image. The 0 index image belongs to the 9th category. 9th category is Bag.



Create a list of our fashion categories. which is used further.


Now see the 0 index image using the matplotlib library.


Now display the random 25 images from the training data set using the matplotlib library. You can see the result below.


Now it's time to change the dimension of the image because our model takes the four dimensional images but we have three dimensional images. So we have to convert the  three-dimensional images to four-dimensional images. So we convert all the three-dimensional images to four-dimensional images using the expand_dims method of the numpy library.

Now scaling the data set by dividing 255.

Now split the data set into the training and testing using the train_test_split() method. This method returns four data sets : X_train, Y_train, X_validation, Y_validation. X_train and Y_train are used to train the model. X_validation and Y_validation are used for evaluate the model at the time of training.

Now check the shape of the four data sets using the shape method. You can see the output below.


The most important step to define the CNN architecture are as follows : 


→ define Sequential model


→ 1 convolutional layer with 32 filters and relu activation function and input size is (28,28,1)

→ 1 MaxPooling2D with pool size is (2,2)


→ 1 convolutional layer with 64 filters and relu activation function and input size is (28,28,1)

→ 1 MaxPooling2D with pool size is (2,2)


→ Flatten layer to convert into vectors


→  dense layer with 128 units and relu activation function

→ dropout layer


→  dense layer with 256 units and relu activation function

→ dropout layer


→  dense layer with 128 units and relu activation function

→  dense layer with 10 units and softmax activation function and this is our output layer.


Then compile the CNN model with loss sparse_categorical_crossentropy because this is the multi class classification problem. 

Then finally, trained the model for 20 epochs with batch_size 16. And it gave me 90% validation accuracy.



Now it’s time to test the model using the X_test data set. And then evaluate the model using the X_test and y_test data set.


Now visualize the actual and predicted label of images. I just used a for loop which will run 60 times and display the 60 random images with actual and predicted title of each image as you can see in the output below.


Now it's time to evaluate the CNN model using the confusion matrix and classification report.

Firstly, visualize the confusion matrix using the heatmap method of seaborn library. Confusion matrix help us to identify how many values are correctly or incorrectly predicted.



Then see the classification. Which shows the recall, precision and f1-score as you can see in the below output.

Now save the model using the name “fashion_mnist_model.h5”.

Then load the save CNN model “fashion_mnist_model.h5”.



Now test the model on a single image using our trained model. So we use a 1st image from the X_test data set and the predicted result is 9 (Bag). And the actual result is also 9 (Bag). So, you can see that our model is working correctly.



Source Code
1. Go to GitHub and download or fork repo: Fashion Products Classification

Video Tutorial


Thank You !!!!!!!!!!!!

1 Comments

If you have any doubts, Please let me know

Post a Comment
Previous Post Next Post