Neural Network Example Using Fashion MNIST dataset

About Fashion MNIST dataset

Chamara Gunarathne
5 min readApr 5, 2019

Fashion-MNIST is a dataset of Zalando’s article images — consisting of a training set of 60,000 examples and a test set of 10,000 examples.

The ten fashion class labels include:

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

Throughout this tutorial, you will learn how to train a simple Convolutional Neural Network (CNN) with Keras on the Fashion MNIST dataset.

Figure 1: Fashion MNIST samples

Starting working on CNN(Convolutional Neural Network) design

First, we import some useful libraries for this project.

Figure 2

Then you can retrieve data set using Keras library as bellow.

Figure 3

Line 15 we load data into an array of size 4. Here train_images and train_labels use to train the network. For that, we use 60,000 images. And test_images, test_labels includes 10,000 images that are used to find how accurately the network.

Figure 4
Figure 5

Labels are included numerical values between 0–9 and those are mapped as bellow.

Table 1

Those class labels are saved into an array because using names easy than use numbers.

Figure 6

All those images are 28*28 pixel size images and those pixel values fall in the range of 0 to 255.

Figure 7
Figure 8

Feeding 0–255 values into the neural network generate complex model so that we convert pixel values into binary as bellow.

Figure 9

Now we plot some sample images from a training set with labels to clarify that the data is in, correct format.

Figure 10
Figure 11

Build the model

Setup the layers

Layers are the basic building blocks of a neural network. Layers extract representations from the data fed into them.

Figure 12

Flatten (Line 52)

This layer only reformats the data. That's mean to transform the format of input image 28*28 pixels 2d array to a 1d — array of 28*28 pixels.

Dense (Line 53, Line 54)

These are densely connected or fully connected, neural layers. The 1st dense layer has 128 nodes(neurons) and takes relu as the activation function. The second layer is a 10 node softmax layer this returns an array of 10 probability scores that sum to 1.

Compile the model

Configures the model for training.

Figure 13
  • Optimizer — This is how the model is updated based on the data it sees and its loss function.
  • Loss function — If the model has multiple outputs, you can use a different loss on each output by passing a dictionary or a list of losses. The loss value that will be minimized by the model will then be the sum of all individual losses.
  • Metrics — Used to monitor the training and testing steps.

Train the model

Feed the training data (train_images,train_labels) to the model.

Then the model learns to associate images and labels.

Then using test data set (test_images) find prediction labels and find accuracy using the test_labels array.

Figure 14

Above model.fit start training model using the train data set.

one epoch = one forward pass and one backward pass of all the training examples.

Figure 15

After the model train, every epoch display respect loss and an accuracy value.

The lower the loss, the better a model. It is a summation of the errors made for each example in training or validation sets.

Then using the trained model you can compare user test data as bellow.

Figure 16
Figure 17

Make predictions

You can get final scores of the last layer as bellow. From that, you can predict which node give the highest score.

Figure 18

From bellow results, you can see 5th index (node) give the highest output score, so that label of that image is Sandal.

Let’s plot several images with their predictions.

Figure 19
Figure 20

Now we use the trained model to make a prediction about a single image.

Figure 21
Figure 22

References

https://www.tensorflow.org/tutorials/keras/basic_classification

--

--