Data Analysis
Step 1: Importing the required libraries
We will first import all the required libraries to complete our objective. To show images, we will use matplotlib, and for array manipulations, we will use NumPy. Tensorflow and Keras will be used for ML and deep learning stuff.
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 |
The Fashion MNIST dataset is readily made available in the keras.dataset library, so we have just imported it from there.
The dataset consists of 70,000 images, of which 60,000 are for training, and the remaining are for testing purposes. The images are in grayscale format. Each image consists of 28×28 pixels, and the number of categories is 10. Hence there are 10 labels available to us, and they are as follows:
- T-shirt/top
- Trouser
- Pullover
- Dress
- Coat
- Sandal
- Shirt
- Sneaker
- Bag
- Ankle boot
Step 2: Loading data and auto-splitting it into training and test
We will load out data using the load_dataset function. It will return us with the training and testing dataset split mentioned above.
Python3
# Split the data into training and testing (trainX, trainy), (testX, testy) = fashion_mnist.load_data() # Print the dimensions of the dataset print ( 'Train: X = ' , trainX.shape) print ( 'Test: X = ' , testX.shape) |
The train contains data from 60,000 images, and the test contains data from 10,000 images
Step 3: Visualise the data
As we have loaded the data, we will visualize some sample images from it. To view the images, we will use the iterator to iterate and, in Matplotlib plot the images.
Python3
for i in range ( 1 , 10 ): # Create a 3x3 grid and place the # image in ith position of grid plt.subplot( 3 , 3 , i) # Insert ith image with the color map 'grap' plt.imshow(trainX[i], cmap = plt.get_cmap( 'gray' )) # Display the entire plot plt.show() |
With this, we have come to the end of the data analysis. Now we will move forward to model training.
Build the Model for Fashion MNIST dataset Using TensorFlow in Python
The primary objective will be to build a classification model which will be able to identify the different categories of the fashion industry from the Fashion MNIST dataset using Tensorflow and Keras
To complete our objective, we will create a CNN model to identify the image categories and train it on the dataset. We are using deep learning as a method of choice since the dataset consists of images, and CNN’s have been the choice of algorithm for image classification tasks. We will use Keras to create CNN and Tensorflow for data manipulation tasks.
The task will be divided into three steps data analysis, model training and prediction. Let us start with data analysis.