Deep Learning using TensorFlow — Part2

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 Mnist data set using TensorFlow 2.0. Last time in Part1 we used a simple Dense model to classify these images. In this blog Part2, we are going to use Conv2D layers for this task. Again the idea is still the same, using TensorFlow 2.0 inbuilt layers to show how easy it is to use Conv2D layers along with MaxPooling2D layer to show how the classification model can be improved from the prior dense model.

Using the same template as we did before, here are five steps to run this classification.

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.

This part remains the same as part1, as we are still using the same dataset.
Fashion Minst: This is a dataset of 70,000 images. These are small grey scaled 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.

import tensorflow as tf
import tensorflow.keras as keras
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Flatten,Conv2D,MaxPool2D,Dense

As you can see this function loads all the 70k images and splits it into two parts 60k for training and 10k for testing.

Build Model:

model= Sequential([

This is the updated model using Conv2D layer and MaxPooling2D layer. To get a little bit more into the details:

# Here is the model summary:

Conv2D: This convolution layer can be thought of as matrix multiplication using the kernel size matrix in our example (3,3) so if our input size of the image is (28,28) our first Conv2D output would be a matrix of (28–3+1,28–3+1) so (26,26). We also have this process run for each filter so in our example of 16 filters the end dimensions are (26,26,16).

MaxPooling2D: After this Con2D layer, we use MaxPooling2D that dimensions are reduced to (13,13,16) when we use the unit size of (2,2) after the above layer.

One more new parameter we used in the layers is the activation function ‘Relu’. There are also activation functions available from TensorFlow.
Here is a link to all of them.

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. This is the same as we had in the last post.


Fit Model:
Without further ado, here is the simple fit line used to train the model.,train_label,epochs=5)

Evaluate Model:
Now the final test to see how the model performs on our test dataset.


Here is a quick reminder of how the Dense only model performed.

Dense model final output from part1

As you can see adding the 2 more lines in the model with Conv2D and MaxPooling2D improves the overall performance on the classification task from 87.72% to 91.12%.

Good luck !!!




Coursera link:

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