Sunday, 8 August 2021

Fashion Mnist Image Classification using Deep Learning

 



Fashion Mnist Image Classification using Deep Learning for Beginners


Introduction:


    In this blog, I created image classification using feed forward network on fashion mnist data which I have implemented using TensorFlow and Keras.

    This blog offers you a step-by-step instruction guide with source code, so you can build your model. It is not designed to be a deep dive into model design, statistical analysis, improvement, and validation. If you want to learn more, please check out my blog site: Techy Scientists.

It contains the following parts:


  1. Setup your environment
  2. Build your Fashion image classification model
  3. Model Prediction


Setup your environment


   To run the program on your local computer, install the following required libraries, These libraries are 


  1.   python 3.8.0
  2.   numpy
  3.   pandas
  4.   matplotlib
  5.   tensorflow 2.0
  6.   keras 2.3.0


Build your Fashion image classification model


Step 1: Understand the data


  The first step of model prediction is to understand the data. It is more important to all machine learning and deep learning projects. You can find more information about the data, go to Fashion MNIST Kaggle(Note: If you don't know Kaggle, please check this blog What is Kaggle? How to use it?).




Step 2: Import the Packages


  Create a python file (for example model.py). After installed the required packages, import packages  in your python file.


import numpy as np 
import pandas as pd
import tensorflow as tf
from tensorflow import keras

Step 3: Import the data

    Next, import the data using pandas

mnist = keras.datasets.fashion_mnist

Step 4: Split the data
    
    We want to create a model, must have split it into training and testing. the model trained by training dataset and then apply the evaluation of model used by test dataset.

(X_train, y_train), (X_test, y_test) = mnist.load_data()


Step 5: Normalize the data

    Then, Normalize the data values to the range [0, 1].

X_train = X_train/X_train.max()
X_test = X_test/X_test.max()


Step 6: Create Deep Learning Model

    We create model for image classification using feed forward network. The feed forward network is first and simple type of deep learning networks. It is a classification algorithm. In this network, information moves only forward not from a cycle. 


from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten, Dense

model = Sequential()
model.add(Flatten(input_shape=[28,28]))
model.add(Dense(100, activation='relu'))
model.add(Dense(10, activation='softmax'))


  •     Sequential - appropriate for a plain stack of layers where each layer has    exactly one input tensor and one output tensor.
  •     Flatten -  matrix flatten to one dimensional array.
  •     Dense - fully connected neural network layer and it implement the operations.
  •     Activation - used through an activation  layer, or through the activation argument supported by all forward layers.


Step 7: Train the Model

    Now, we ready to train the model.

model.compile(loss="sparse_categorical_crossentropy", 
              optimizer="adam",
              metrics=["accuracy"])
model.fit(X_train, y_train, epochs=2)
Out[]:
Train on 60000 samples
Epoch 1/2
60000/60000 [=============] - 11s 191us/sample 
- loss: 0.2524 - accuracy: 0.9275
Epoch 2/2
60000/60000 [=============] - 11s 186us/sample 
- loss: 0.2458 - accuracy: 0.9290


Step 8: Evaluate the Model

    Finally, We created the model and then evaluate it.

test_loss, test_acc = model.evaluate(X_test, 
	y_test, 
    	verbose=0)
print('Loss:', test_loss)
print('Accuracy:', test_acc)
Out[]:
Loss: 0.3907148540019989
Accuracy:  0.8590999841690063


Model Prediction

    Finally, We predict the label of data on the basis of trained model. It returns the labels of the data passed as argument based upon the learned or trained data obtained from the model.

    It has 10 different classes labelled from 0 to 9 where 0 – T-shirt /top, 1 – Trouser, 2 –  Pullover, 3 – Dress, 4 – Coat, 5 – Sandal, 6 – Shirt, 7 – Sneaker, 8 – Bag, 9 – Ankle Boot.  First we create a list for class label.

class_labels = ["T-shirt/top",
	"Trouser",
      "Pullover",
      "Dress",
      "Coat",
      "Sandal",
      "Shirt",
      "Sneaker",
      "Bag",
      "Ankle boot"]

    

Then, we predict the model using test dataset.

y_pred = model.predict(X_test)
       
Check that prediction,
class_labels[np.argmax(y_pred[0])]
Out[]:
'Ankle boot'

import matplotlib.pyplot as plt
plt.matshow(X_test[0])

Conclusion:

     In summary, we created the image classification using feed forward network on fashion mnist data which I have implemented using TensorFlow and Keras. If you want to source code, check this GitHub linkFashion mninst image classification.



1 comment: