Deep Learning using TensorFlow — Part1
By: Sailaja Karra
Tags: Image Classification, Convolution Neural Net, Keras, TensorFlow, Deep Learning

Hi All, this is a series of blogs that I intend to write about how to use TensorFlow 2.0 for deep learning.

In this blog, I will go over how to classify Fashion Minst dataset using TensorFlow 2.0. Before we dig into this task I would like to add why TensorFlow 2.0. Before I took Laurence Moroney class on Coursera I did look at TensorFlow once before. I have to say I was scared and didn’t quite understand anything in the program, with its session scope, variables, etc this seemed extremely foreign to me and honestly turned off my zeal to learn this. Instead, I spent my time learning SciKit-Learn, Pandas, and other python libraries.

After watching a video on youtube by Laurence Moroney about TensorFlow 2.0 and having spent some time on other libraries I thought I will give this a try again. Boy, I was surprised how much user friendly this got from version 1.4 to 2.0, the change is incredible. This is such a leap forward that I now feel a lot more comfortable writing a few simple programs and wanted to share with everyone how easy it got. I honestly don’t know if this is advertised as such or not but TensorFlow 2.0 is nothing like TF 1.4 and is genuinely much more novice-friendly.

Now, let’s get into this classification to show how easy this has become. There are five steps in the process.

1. Dataset: Load the data set, do some feature engineering if needed.

2. Build Model: Build a TensorFlow model with various layers.

3. Compile Model: Here we compile the model, select the loss & Optimizer functions.

4. Fit Model: Here we finally train the model using the training data and get some metrics.

5. Evaluate Model: We check our model performance on the validation data.

Dataset:

Fashion Minst: This is a dataset of 70,000 images. These are small grayscale images with a standard size of 28x28 pixels. Here are a few examples.

First, let’s look at how to load data. This is going to use TensorFlow Datasets to do this.

#Imports

import tensorflow as tf #Load Dataset

mnist = tf.keras.datasets.fashion_mnist

(training_images, training_labels), (test_images, test_labels) = mnist.load_data() #Reshape & Scale

training_images=training_images.reshape(60000, 28, 28, 1)

training_images=training_images / 255.0

test_images = test_images.reshape(10000, 28, 28, 1)

test_images=test_images/255.0

As you can see this function loads all the 70k images and splits it into two parts 60k for training and 10k for testing. Now that we have loaded the data, let’s see how to use a simple Dense layers-based model to classify the images.

Build Model:

model = tf.keras.models.Sequential([Flatten(),

Dense(1024,activation='relu'),

Dense(activation='softmax')])

This is a very simple model that use Flatten as the first layer. As you might have guessed this takes the 28x28 matrix image and Flattens that to a 784 vector. Once this is done then we can use the Dense layers to train the model.

One last thing I would like to point out is that the last Dense layer is different from the two above. As in the last step we need to get as output one number that represents the one class out of 10 classes. Also, we use the activation ‘SoftMax’ to make sure we get the maximum probability class as the answer.

Compile Model:

After we build the model we need to compile it. Here we need to select the loss functions and the optimizers. As you can see from the below code snippet this is very easy in TensorFlow. You can either just give the name as we have done for loss function or you can specify the full function to change any of the default parameters as we did with the learning rate for Optimizer here.

model.compile(optimizer = 'Adam',

loss = 'sparse_categorical_crossentropy',

metrics=['accuracy'])

Fit Model:

Without further ado, here is the simple fit line used to train the model.

model.fit(training_images, training_labels, epochs=5)

We can specify various metrics if we want to see how our training is performing. Here is the accuracy being calculated at the end of every epoch.

We will discuss callbacks in next few blog posts as I would like to keep this simple.

Evaluate Model:

Now the final test to see how the model performs on our test dataset, do remember this is the data the model never saw before so this is a true test.

model.evaluate(test_images, test_labels)

As you can see we got a decent 87.22% accuracy from such a simple model. We can improve on this further but what I want to show in the blog is easy of use of TensorFlow 2.0. I hope this blog inspires you to give you the confidence to start fiddling around with TensorFlow 2.0.

Good luck !!!

References:

Coursera link:

https://www.coursera.org/specializations/tensorflow-in-practice

Book: Hands-On Machine Learning with Scikit-Learn, Keras & TensorFlow by Aurelien Geron