New ABCD Of Machine Learning

Aryan Pegwar
Analytics Vidhya
Published in
4 min readMar 30, 2020

Fashion MNIST Image Classification

If you have ever tried an image classification I bet your first model would be MNIST Handwritten Digit Classification, which is kinda cool. It is considered the ‘Hello World’ of machine learning, but if you want something new I’ll have it “Fashion MNIST Image Classification”. It has the same difficulty level as its bro but something different. So let's unpack it.

Github link for this article: https://github.com/aryan109/medium/blob/master/medium_fashion_MNIST.ipynb

Fashion MNIST Dataset

It consists of 10 classes of different types of clothing and accessories like shoes, shirts, pants etc.

Each image is of 28*28 pixels and in Black and white, sounds retro? This is a single-channel image, means it is in grayscale, now sounds Modern 😉. If any image would be in multi Coloured then it will have 3 channels, each for red blue and green. Here is a sample image from the dataset.

sample of an image

Model Introduction

We are going to use TensorFlow and Keras in this project as these provide high-level implementations of many machine learning utilities. I suggest you to use Colaboratory to run the code as it has TensorFlow Preinstalled and also has GPU hardware acceleration available that will help us to shorten the Training time for our model.

Intro to Colaboratory

It is an online platform that provides Jupyter notebook-like interface to execute python. It supports both Python 2 and Python 3. As I already told you earlier It has TensorFlow and ha GPU hardware acceleration available.

It is the best platform to get started with. Later on, you can install TensorFlow on your own device.

here is the link to Colaboratory: https://colab.research.google.com/notebooks/welcome.ipynb#scrollTo=5fCEDCU_qrC0

the Wellcome window of colaboratory

Begin with Code

We will start with importing required libraries.

import tensorflow as timport numpy as npfrom tensorflow import keras

Data loading and Pre-processing

Here we are going to neural networks for our image classification model. As you may know, ‘data’ is fuel for any model so we first have to load data into our system and then pre-process it to make it usable for our model.

Fashion MNIST dataset is already present in Keras so we don’t have to externally download it.

fashion_mnist = keras.datasets.fashion_mnist(train_images,train_labels),(test_images,test_lables)=fashion_mnist.load_data()

We divide entire data into two sets ‘Training Dataset’ and ‘ Testing Dataset’ each of them is further divided into ‘Images’ and ‘Labels’.

Here train_images and train_labels will be used for training the model and test_images and test_labels will be used to testing the accuracy of our model.

We can see our data using this code:

import matplotlib.pyplot as pltplt.imshow(train_images[0])
this will be the output

Reshaping dataset

we cannot feed images directly into the model. we need to create a NumPy array of images and then feed it to the model. It’s very easy to convert, by just calling reshape().

train_images = train_images.reshape(60000,28,28,1)
#60000 is number of train images
test_images = test_images.reshape(10000,28,28,1)
#10000 is number of test images

Normalizing our dataset

Image normalization is a typical process in image processing that changes the range of pixel intensity values. Its normal purpose is to convert an input image into a range of pixel values that are more familiar or normal to the senses, hence the term normalization.

To normalize we will simply divide by 255.

train_images,test_images=train_images/225.0,test_images/255

Defining our Model

Now we will define our model using Keras. we will use a CNN connected to Fully connected DNN.

model = keras.Sequential([
keras.layers.Conv2D(64,(3,3),activation = ‘relu’,input_shape=(28,28,1)),
keras.layers.MaxPooling2D(2,2),
keras.layers.Flatten(),
keras.layers.Dense(128,activation = ‘relu’),
keras.layers.Dense(10,activation = ‘softmax’)
])
model.summary()

model.summary() will give us information about the model

just see the number of trainable parameters 1,366,506 !!!

Compiling model with optimizer and loss function

Optimizers update the weight parameters to minimize the loss function. Loss function acts as guides to the terrain telling optimizer if it is moving in the right direction to reach the bottom of the valley, the global minimum.

We will use Adam as our optimizer and sparse categorical crossentropy

model.compile(optimizer = tf.train.AdamOptimizer(),loss = ‘sparse_categorical_crossentropy’,metrics = [‘accuracy’])

Training our model

we will use model.fit() to train our model for about 50 epochs.

model.fit(train_images,train_labels,epochs = 50)

Evaluating our model

now we will evaluate our model using test data and find out how good it will perform on unseen data.

model.evaluate(test_images,test_lables)
that's the resulting output

loss:0.26, accuracy: 91.4%

This result is pretty good. you may get a slightly different result due to random initialization of the model parameters.

--

--

Aryan Pegwar
Analytics Vidhya

Empowering Brands and Professionals to Achieve 10X Profit Growth through AI & Automation