Build a Neural Network with TensorFlow and Keras

In this tutorial, we would build a neural network to recognized images. The images includes shirts, sneakers, shoes and others. We would take the following 10 steps.

Step 1 – Setup of Tensorflow and keras
Step 2 – Import and view the MNIST Fashion Dataset
Step 3 – Examine the Image Data
Step 4 – Preprocessing
Step 5 – Setup Neural Network Layers
Step 6 – Compile the Model
Step 7 – Train the Model
Step 8 – Make Predictions
Step 9 – Evaluate Model Results
Step 10 – Prediction on Single Image

 

Step 1 – Setup of Tensorflow and keras

The easiest way to set up TensorFlow is using Anaconda Navigation. I recommend you watch the video here.

After you set up TensorFlow, go ahead and open Jupyter Notebook. Add the following lines and run it to make sure TensorFlow and Keras is working

# import tensorflow and kerasl
import tensorflow as tf
from tensorflow import keras

# Import matplotlib and numpy
import matplotlib.pyplot as plt
import numpy as np

 

 

Step 2 – Import and view the MNIST Fashion Dataset

The MNISt Fashion dataset is available as part of keras. You can import and view the dataset using the code below.

# import the fashion mnist dataset
fashion_mnist = keras.datasets.fashion_mnist
(train_images, train_labels), (test_images, test_labels) = fashion_mnist.load_data()
print(train_images.shape)
print(np.unique(train_labels))

Output:

(60000, 28, 28)
[0 1 2 3 4 5 6 7 8 9]

 

 

Step 3 – Examine the Image Data

Let’s now choose  a random image and display it. You can achieve this using the imgshow() funcition. Run the code below:

plt.figure() 
plt.imshow(train_images[8]) 
plt.colorbar() plt.show()

If you run the above code, you will have the figure

TensorFlow Image

 

Step 4 – Preprocessing

We would do a little preprocessing task. We would scale the images such that the values to be between 0 and 1, instead of between 0 and 255.

Simply divide by 255

train_images = train_images/ 255 
test_images = test_images / 255

Then we also need to assign text the class name text to a list so we can use it later. It is based on the table below:

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

Table 1: Output classes and names.

 

class_names = ['T-shirt/top', 'Trouser', 'Pullover', 'Dress', 'Coat',
               'Sandal', 'Shirt', 'Sneaker', 'Bag', 'Ankle boot']

 

Step 5 – Setup Neural Network Layers

The network would be made up of three layers. These are annotated in the code below:

# Input layer - Flatten the two dimensional array(28x28) into 1d array (28x28=784)
# Hidden layer - Dense fully connected layer of 28 nodes (relu)
# Output layer - Dense fully connected layer of 10 nodes (softmax)

model = keras.Sequential([
    keras.layers.Flatten(input_shape=(28, 28)),
    keras.layers.Dense(128, activation='relu'),
    keras.layers.Dense(10, activation='softmax')
])

 

Step 6 – Compile the Model

Now we have set up the network, we now have to compile the model. This is explained in the code below

# The compilation process is use to set the optimizer, loss function and metrics:
#1. Loss function - measures how accurate the output is
#2. Optimizer - measures how the model is updated
#3. Metrics - monitors the trainig to determine when to stopmodel.compile()

    optimizer='adam',
    loss='sparse_categorical_crossentropy',
    metrics=['accuracy']
)

 

Step 7 – Train the Model

The training is explained in the annotation. See code below:

# We are going o train the models in 10 epochs (that is a complete presentation of the training dataset)
# This means that we would feed the 60,000 images and labes into the network 10 different time
# For each epoch we would see both the los and the accuracy of the model

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

The output of the training is shown below:

Epoch 1/10
60000/60000 [==============================] - 27s 445us/sample - loss: 0.4975 - acc: 0.8270
Epoch 2/10
60000/60000 [==============================] - 20s 336us/sample - loss: 0.3745 - acc: 0.8657
Epoch 3/10
60000/60000 [==============================] - 19s 313us/sample - loss: 0.3338 - acc: 0.8781
Epoch 4/10
60000/60000 [==============================] - 18s 301us/sample - loss: 0.3114 - acc: 0.8860
Epoch 5/10
60000/60000 [==============================] - 19s 314us/sample - loss: 0.2928 - acc: 0.8913
Epoch 6/10
60000/60000 [==============================] - 24s 400us/sample - loss: 0.2796 - acc: 0.8970
Epoch 7/10
60000/60000 [==============================] - 23s 390us/sample - loss: 0.2673 - acc: 0.9003
Epoch 8/10
60000/60000 [==============================] - 17s 290us/sample - loss: 0.2581 - acc: 0.9038
Epoch 9/10
60000/60000 [==============================] - 24s 395us/sample - loss: 0.2471 - acc: 0.9084
Epoch 10/10
60000/60000 [==============================] - 28s 463us/sample - loss: 0.2361 - acc: 0.9113

 

Step 8 – Evaluate Model Results

If you look at the last line on the output above, you can see that the an accuracy of 91.13% was achieved during the training with the training dataset. Now we would try to evaluate the accuracy of the model with the test dataset.

test_loss, test_accuracy = model.evaluate(test_images, test_labels)

The output give a test accuracy of 88.41% as you can see

10000/10000 [==============================] - 3s 323us/sample - loss: 0.3306 - acc: 0.8841

 

Step 9 – Make Predictions

Let’s now use our model to make predictions using the test dataset.

predictions = model.predict(test_images)

What does this predictions look like? This answer is given in the annotation as we veiw prediction[0]

# A prediction is an array of 10 numbers. 
# Each element represents the confidence that the image corresponds to the each of the 10
# different classes

predictions[0]

The output as expected is:

Out[38]:
array([1.3719939e-07, 3.0358432e-10, 1.9884057e-07, 3.7327821e-11,
       2.2751689e-10, 2.1002933e-03, 3.8164407e-07, 2.0410890e-02,
       2.9359937e-06, 9.7748524e-01], dtype=float32)

From the predictions results, we can see that the model thinks that this image is 9, with a confidence level of 97.75%. If you look at Table 1, this corresponds to Ankle Book.

Now, let’s display image[0] to see if our model is right. Run the code below and it’s an ankle boot!

plt.figure()
plt.imshow(test_images[0])
plt.colorbar()
plt.show()

 

Watch the video lesson below: