Approach CNN using Tensorflow V2.0 and Analyse with Tensorboard

Seshadri Senthamaraikannan
6 min readMar 22, 2020

--

In this article, we will go through the basic approach of CNN and extend the details with its usage in new updated framework called tensorflow V2.0 which includes the interesting high level abstraction of Keras to it. This enables the coding python to be more short and precise. We will also see how Tensorboard can be used to analyse the model performance. Expecting the reader to have preliminary knowledge of Neural Nets and its working procedure.

High Level information on CNN

Fig.1 Sample architecture for CNN (img Src: Google Images)

Convolutional neural network (CNN, or ConvNet) is a class of deep neural networks, most commonly applied to analyzing visual imagery. They are also known as shift invariant or space invariant artificial neural networks (SIANN), based on their shared-weights architecture and translation invariant characteristics. We will be going through the basic components of ConvNet and its implementation using tensorflow V2.0 against with the 101 Categorical Image dataset.

Components of CNN:

  1. Convolutional layers: This layer is so compared to a flashlight shown over all parts of the image. It captures different features like a straight line, curve etc., present in the image using a filter. This filter is initialized randomly and updated during the training process. Every filter corresponds to a different feature and gathers a new information about the image.

Activation Map: During the process of convolution, we need to activate the respective heat maps around the image using the activation function. Perhaps, the most common activation function used is RELU. It looks as below:

Fig2 : Rectified Linear Unit Graph

And can be represented as follows:

f(x)={0, if x<0 or x otherwise

Or, in plain English, it produces a zero output for all inputs smaller than zero; and x for all other inputs. Hence, for all inputs<=0, it produces zero outputs. So by this means, we can get the features activated which the filters are bound to from the image rest all becomes zero.

2. Pooling Layers: After the convolution layer we will have the size reduced, but it was not enough to save our computational resources while training pretty large image dataset. Hence pooling layers comes into picture to reduce the size of the image still holding all the essentials of the image.

Fig 3: Max pooling

As seen above the image with (4,4,1) dimensions is now reduce to (2,2,1).

Max pooling is widely used and it reduces the size of image to have much more ease during the training process.

Fully connected Layer: Now, all the above layers helps us to get the pattern of the image, but the actual classification happens in Fully connected layer. Its a densely connected layer which is fits for classification and regression tasks. We will mostly be using the softmax activation function to translate our output to a specific class.

This is with the general introduction with the components of CNN. We will now see how we implement the CNN using our tensorflow V2.0 which is the primary goal of our article.

We will see now how we can approach CNN with our own data set and analyse the model with tensorboard.

Imported the necessary libraries. We can see callbacks which invokes the tensorboard. Tensorboard function is used to capture all the logs during the run which shall be visualised later in tensorboard.

Now let’s say we have a data set which can be used for our model. We have to preprocess them, reshape them to a common format, separate some data sets for validation purpose and make an one-hot encoding on the target labels for final classification. For all these process we have library provided by tensorflow “tf.keras.preprocessing.image.ImageDataGenerator” which make our job simpler.

For representation purpose we have used a sample dataset downloaded in tar format. It is a classification dataset with 102 classes and our job is to build a classifier using CNN. We have coded everything in our Google Colab environment.

Since my dataset was a tar file the below code untar the image folders and files and keep them under the root directory.

The above code shows we split 10 percent of data for testing and remaining for training by separating them into different folders.
The above code shows the initialization of ImageDataGenerator from tensorflow V2 and other necessary variables for invoking the data generator.
The above image shows image data generator used to create a training data and test data. We have provided the target label count, Standard shape of image, batch size, and directory to look for the dataset.

Now we have the test data and train data prepared with our own data set of images. This have the labels, batch size inbuilt already since we have given the information during data generation. Now we can go ahead and create the model.

CNN model Architecture used

Model run with all the necessary training and test data. As mentioned, we don’t specify the target labels and batch size since its already equipped withing the train_data and test_data. And we can notice the callbacks option in mentioned to visualize the results in Tensorboard.

Now that we have trained our model for classifying the images we will now visualize its measures in tensorboard.

Tensorboard can be invoked in Google colab as below:

%load_ext tensorboard

%tensorboard — logdir {‘logs/’}

Tensorboard Fig1
Tensorboard Fig2

It shows our model measures in a graphical representation and we could see the gradual increase in the accuracy and gradual decrease in the loss during our training process. We could also see our validation against the test data set needs more improvement hence we can collect more data set and update our model parameters accordingly for better results. So, we can also have addition measure as shown below which will help us to investigate more and improve our CNN classifier.

  • The Scalars dashboard shows how the loss and metrics change with every epoch. You can use it to also track training speed, learning rate, and other scalar values.
  • The Graphs dashboard helps you visualize your model. In this case, the Keras graph of layers is shown which can help you ensure it is built correctly.
  • The Distributions and Histograms dashboards show the distribution of a Tensor over time. This can be useful to visualize weights and biases and verify that they are changing in an expected way.

Try this CNN with your own datasets and explore more!!!. Happy Modelling.

References:

Tensforflow V2 Documentation

https://www.tensorflow.org/api_docs/python/tf

--

--