First Deep Learning Project End to End | Fashion-MNIST Classification

First Deep Learning Project End to End

In the Machine Learning/Data Science/Deep Learning End to End Project in Python Tutorial in Hindi, we explained each and every step of Machine Learning Project / Data Science Project / Deep Learning Project in detail.

Project name: Fashion MNIST Classification

What we cover in this Project:

  1. Import Libraries
  2. Load Data
  3. Show Image from Numbers
  4. Feature Scaling
  5. Build First Neural Network
  6. Train Model
  7. Test & Evaluate Model
  8. Confusion Matrix
  9. Classification Report
  10. Save Mode

Project Source Code

# -*- coding: utf-8 -*-
"""first deep learning project - MNIST-Fashion Classification.ipynb

Automatically generated by Colaboratory.

Original file is located at
    https://colab.research.google.com/drive/1bCl8PUVo2XP8_-emIE3Zzi8jQ3Jsy2rH

# First Deep Learning Project
##Fashion Classification

### Train Neural Network on 60,000 Fashion-MNIST Images (data in NP array)
### Test Neural Network on 10,000 Fashion-MNIST Images (data in NP array)
"""

'''
class_labels:

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

Classify the given input from above class using Neural Network

image shape 28 X 28 pixel ( Gray scale)

'''

"""### Import Libraries"""

import keras
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns

"""## Load Data"""

(X_train, y_train), (X_test, y_test) = keras.datasets.fashion_mnist.load_data()

X_train.shape, y_train.shape

X_test.shape, y_test.shape

X_train[0]

y_train[0]

class_labels = ["T-shirt/top","Trouser","Pullover","Dress","Coat","Sandal","Shirt","Sneaker","Bag","Ankle boot"]
'''
0 => T-shirt/top 
1 => Trouser 
2 => Pullover 
3 => Dress 
4 => Coat 
5 => Sandal 
6 => Shirt 
7 => Sneaker 
8 => Bag 
9 => Ankle boot '''

plt.imshow(X_train[0], cmap ="Greys")

plt.figure(figsize=(16,16))
for i in range(25):
  plt.subplot(5,5,i+1)
  plt.imshow(X_train[i],cmap="Greys")
  plt.axis('off')
  plt.title(class_labels[y_train[i]]+"="+str(y_train[i]), fontsize=20)

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

"""# Feature Scalling"""

X_train = X_train/255
X_test = X_test/255

X_train[0]

"""## Build Neural Network"""

model = keras.models.Sequential([
                         keras.layers.Flatten(input_shape=[28,28]),
                         keras.layers.Dense(units=32, activation='relu'),
                         keras.layers.Dense(units=10, activation='softmax')
])

model.summary()

model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])

model.fit(X_train, y_train, epochs=1)

model.fit(X_train, y_train, epochs=10)

"""## Test and Evaluate Neural Network Model"""

model.evaluate(X_test,y_test)

y_pred = model.predict(X_test)

y_pred[0].round(2)

np.argmax(y_pred[0].round(2))

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

y_test[0]

plt.figure(figsize=(16,16))
for i in range(25):
  plt.subplot(5,5,i+1)
  plt.imshow(X_test[i],cmap="Greys")
  plt.axis('off')
  plt.title("Actual= {} \n Predicted = {}".format(class_labels[y_test[i]], class_labels[np.argmax(y_pred[i])]))

"""## Confusion Matrix"""

from sklearn.metrics import confusion_matrix
cm = confusion_matrix(y_test, [ np.argmax(i) for i in y_pred])

plt.figure(figsize=(16,9))
sns.heatmap(cm, annot=True, fmt = "d")

"""## Classification Report"""

from sklearn.metrics import classification_report
cr = classification_report(y_test, [ np.argmax(i) for i in y_pred], target_names = class_labels,)

print(cr)

"""## Save Model"""

model.save("MNIST_classifier_nn_model.h5")

model = keras.models.load_model("MNIST_classifier_nn_model.h5")

model.predict(X_test)

2 Replies to “First Deep Learning Project End to End | Fashion-MNIST Classification”

Leave a Reply

Top