Model Analysis
In this section, we will plot some graphs related to accuracy and loss to evaluate model performance. First, we will see the accuracy and plot the loss.
Python3
# Accuracy vs Epoch plot plt.plot(history.history[ 'sparse_categorical_accuracy' ]) plt.plot(history.history[ 'val_sparse_categorical_accuracy' ]) plt.title( 'Model Accuracy' ) plt.ylabel( 'Accuracy' ) plt.xlabel( 'epoch' ) plt.legend([ 'train' , 'val' ], loc = 'upper left' ) plt.show() |
Output:
Python3
# Loss vs Epoch plot plt.plot(history.history[ 'loss' ]) plt.plot(history.history[ 'val_loss' ]) plt.title( 'Model Accuracy' ) plt.ylabel( 'loss' ) plt.xlabel( 'epoch' ) plt.legend([ 'train' , 'val' ], loc = 'upper left' ) plt.show() |
Output:
To make the predictions call the predict() function on the model and pass the image into it. To perform the prediction, we will first create a list of labels in order of the corresponding output layer of the CNN. The predict() function will return the list of values of probabilities that the current input belongs probably belongs to which class. Then by using the argmax(), we will find the highest value and then output the correct label.
Python3
# There are 10 output labels for the Fashion MNIST dataset labels = [ 't_shirt' , 'trouser' , 'pullover' , 'dress' , 'coat' , 'sandal' , 'shirt' , 'sneaker' , 'bag' , 'ankle_boots' ] # Make a prediction predictions = model.predict(testX[: 1 ]) label = labels[np.argmax(predictions)] print (label) plt.imshow(testX[: 1 ][ 0 ]) plt.show() |
Output:
Hence we have successfully performed image classification on the fashion MNIST dataset.
Fashion MNIST with Python Keras and Deep Learning
Deep learning is a subfield of machine learning related to artificial neural networks. The word deep means bigger neural networks with a lot of hidden units. Deep learning’s CNN’s have proved to be the state-of-the-art technique for image recognition tasks. Keras is a deep learning library in Python which provides an interface for creating an artificial neural network. It is an open-sourced program. It is built on top of Tensorflow.
The prime objective of this article is to implement a CNN to perform image classification on the famous fashion MNIST dataset. In this, we will be implementing our own CNN architecture. The process will be divided into three steps: data analysis, model training, and prediction.
First, let’s include all the required libraries
Python3
# To load the mnist data from keras.datasets import fashion_mnist from tensorflow.keras.models import Sequential # importing various types of hidden layers from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dense, Flatten # Adam optimizer for better LR and less loss from tensorflow.keras.optimizers import Adam import matplotlib.pyplot as plt import numpy as np |