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