MITB Banner

Tensorboard Tutorial – Visualize the Model Performance During Training

Share

Tensorboard

In image classification when you train a Deep Learning model it takes a lot of time. We are unable to check what all is going on when a model is getting trained. Even some of the models that were trained on the Image Net dataset took many days to get trained. But what do you do when your model is getting trained? How to monitor what is happening inside the model? Tensorboard is helpful in these kinds of scenarios that provide different visualizations for models. You can visualize the model graph, losses, accuracy, etc when your model is under training. It is a very helpful tool that can be used to monitor the model that is getting trained on a large dataset. You can use Tensorboard not only with TensorFlow but also with Keras. It is similar to other visualizations that tell about the performance graphically. 

This article demonstrates the exploration of Tensorboard to visualize and check the performance of the model at the same time when the model is getting trained. We will explore these visualizations while training an image classifier over the MNIST data set. The MNIST data set contains 28X28 greyscale images of Handwritten digits between 0 to 9. It is used for many tasks in computer vision. 

What will you learn from this article? 

  • Image Classification using Deep Learning
  • Exploring the features of Tensorboard
  • Evaluation of performance through Tensorboard

Image Classification model for MNIST data 

We will first import the required libraries and load the data set. Use the below code to the same.

import tensorflow as tf
(X_train, y_train), (X_test, y_test) = tf.keras.datasets.mnist.load_data()
from keras.utils import np_utils

After importing the data set we preprocess the images by normalizing both training and testing images followed by converting them into the structure that is accepted by Keras. After that, we convert both the training and testing labels into categorical.  

X_train=X_train.astype('float32')
X_test=X_test.astype('float32')

X_train/=255
X_test/=255

X_train = X_train.reshape(X_train.shape[0], 28, 28, 1).astype('float32')
X_test = X_test.reshape(X_test.shape[0], 28, 28, 1).astype('float32')

y_train = np_utils.to_categorical(y_train, 10)
y_test = np_utils.to_categorical(y_test, 10)

Model 

We now define the convent network with different layers connected to a fully connected network. We have used ReLu as activation function and for the output layer, we have used softmax having 10 classes as the output.

model = Sequential()
model.add(Convolution2D(32, 3, 3, input_shape=(28, 28, 1)))
model.add(Activation('relu'))
model.add(Dropout(0.25))

model.add(Convolution2D(32, 3, 3))
model.add(Activation('relu'))

model.add(Convolution2D(
32, 3, 3))
model.add(Activation('relu'))
model.add(Dropout(0.25))

model.add(Flatten())

model.add(Dense(128))

model.add(Dense(
128))
model.add(Activation('relu'))

model.add(Dense(10))
model.add(Activation('softmax'))

model.compile(loss='categorical_crossentropy',optimizer='adam',metrics=['accuracy'])

After defining the structure of the model and compiling the model we now import a few libraries that are required and load the tensorboard extension followed by removing the previous logs.

import tensorflow as tf
import datetime

!rm -rf ./logs/
log_dir = "logs/fit/" + datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
tensorboard_callback=tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1)
%tensorboard --logdir logs/fit

You will see no dashboard for the current data set because we have not started the training of the model. Once we will start the training different visualizations will appear here. We now fit the data for the training and validation of the model. Use the below code to do the same. We have set callbacks to tensorboard_callback.

model.fit(x=X_train, y=y_train,epochs=30,validation_data=(X_test, y_test), 

          callbacks=[tensorboard_callback])

As soon the model training starts you need to press the refresh button on the tensorboard dashboard to receive the calls back sent by the model. You will see initially the graph of the model that is shown below in the image

Select drop down option from inactive to scalar as shown in the screenshot below.

Tensorboard
Tensorboard
Tensorboard

The above images show the dashboard visualization for accuracy and loss of the model for training as well testing. You can explore more features that are there in the tensorboard dashboard.

Conclusion

I would conclude the article by saying that Tensorboard can be helpful when you want to evaluate the performance of your model for a long time with continuous monitoring. It gives you several different observations regarding the model which gives you more pictures about your model. You can also explore more about Tensorboard in this article “Hyper Parameter Tuning with Tensorboard in 6 Steps

Share
Picture of Rohit Dwivedi

Rohit Dwivedi

I am currently enrolled in a Post Graduate Program In Artificial Intelligence and Machine learning. Data Science Enthusiast who likes to draw insights from the data. Always amazed with the intelligence of AI. It's really fascinating teaching a machine to see and understand images. Also, the interest gets doubled when the machine can tell you what it just saw. This is where I say I am highly interested in Computer Vision and Natural Language Processing. I love exploring different use cases that can be build with the power of AI. I am the person who first develops something and then explains it to the whole community with my writings.
Related Posts

CORPORATE TRAINING PROGRAMS ON GENERATIVE AI

Generative AI Skilling for Enterprises

Our customized corporate training program on Generative AI provides a unique opportunity to empower, retain, and advance your talent.

Upcoming Large format Conference

May 30 and 31, 2024 | 📍 Bangalore, India

Download the easiest way to
stay informed

Subscribe to The Belamy: Our Weekly Newsletter

Biggest AI stories, delivered to your inbox every week.

AI Forum for India

Our Discord Community for AI Ecosystem, In collaboration with NVIDIA. 

Flagship Events

Rising 2024 | DE&I in Tech Summit

April 4 and 5, 2024 | 📍 Hilton Convention Center, Manyata Tech Park, Bangalore

MachineCon GCC Summit 2024

June 28 2024 | 📍Bangalore, India

MachineCon USA 2024

26 July 2024 | 583 Park Avenue, New York

Cypher India 2024

September 25-27, 2024 | 📍Bangalore, India

Cypher USA 2024

Nov 21-22 2024 | 📍Santa Clara Convention Center, California, USA

Data Engineering Summit 2024

May 30 and 31, 2024 | 📍 Bangalore, India

Subscribe to Our Newsletter

The Belamy, our weekly Newsletter is a rage. Just enter your email below.