# 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.

**Dataset:**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.

#Imports

import tensorflow as tf

import tensorflow.keras as keras

from tensorflow.keras.models import Sequential

from tensorflow.keras.layers import Flatten,Conv2D,MaxPool2D,Dense#Load Dataset

mnist = tf.keras.datasets.fashion_mnist

(training_images, training_labels), (test_images, test_labels) =

fashion_mnist.load_data()#Reshape & Scaletraining_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.

**Build Model:**

`model= Sequential([`

Conv2D(16,(3,3),input_shape(28,28,1),

activation='relu'),

MaxPool2D(2,2),

Flatten(),

Dense(512,activation='relu'),

Dense(10,activation='softmax')

])

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: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.

`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(train_rescale,train_label,epochs=5)`

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

`model.evaluate(test_rescale,test_label)`

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

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 !!!**

References:** Conv2D:**

https://www.tensorflow.org/api_docs/python/tf/keras/layers/Conv2D

**MaxPooling2D:**

https://www.tensorflow.org/api_docs/python/tf/keras/layers/MaxPool2D

**Relu: **

https://www.tensorflow.org/api_docs/python/tf/keras/layers/ReLU

https://www.tensorflow.org/api_docs/python/tf/keras/activations/relu

**Coursera link**:

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

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