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.
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.
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:
- 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.
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.
With this, we have come to the end of the data analysis. Now we will move forward to model training.
Step 1: Creating a CNN architecture
We will create a basic CNN architecture from scratch to classify the images. We will be using 3 convolution layers along with 3 max-pooling layers. At last, we will add a softmax layer of 10 nodes as we have 10 labels to be identified.
Now we will see the model summary. To do that, we will first compile our model and set out loss to sparse categorical crossentropy and metrics as sparse categorical accuracy.
Step 2: Train the data on the model
As we have compiled the model, we will now train our model. To do this, we will use mode.fit() function and set the epochs to 10. We will also perform a validation split of 33% to get better test accuracy and have a minimum loss.
Step 3: Save the model
We will now save the model in the .h5 format so it can be bundled with any web framework or any other development domain.
Step 4: Plotting the training and loss functions
Training and loss functions are important functions in any ML project. they tell us how well the model performs under how many epochs and how much time the model taske actually to converge.
Now we will use model.predict() to get the prediction. It will return an array of size 10, consisting of the labels’ probabilities. The max probability of the label will be the answer.