Tensor Board For Model Debugging and Visualization
Tensor Board For Model Debugging and Visualization
Here’s an example provided in the TensorFlow Github which involves training the mnist
dataset and capturing NANs then analyzing our results on TensorBoard debugger 2.
First, Navigate to project root on your terminal and run the below command.
print(X_train[0].shape)
Copy
Since we are going to use `tf.summary.image()` which expects rank-4 tensor, we have to
reshape using the `numpy` reshape method. Our output should contain (batch_size, height,
width, channels) .
Since we are logging a single image and our image is grayscale, we are setting both batch_size
and ‘channels’ values as 1.
rm -rf ./logs/
logdir = "logs/single-image/"
file_writer = tf.summary.create_file_writer(logdir)
Copy
Next, log the image to TensorBoard
import numpy as np
with file_writer.as_default():
image = np.reshape(X_train[4], (-1, 28, 28, 1))
tf.summary.image("Single Image", image, step=0)
Copy
Launch tensorboard
import numpy as np
with file_writer.as_default():
images = np.reshape(X_train[5:20], (-1, 28, 28, 1))
tf.summary.image("Multiple Digits", images, max_outputs=16,
step=0)
Copy
Visualizing actual images in TensorBoard
From the above two examples, you have been visualizing mnist tensors. However, using
`matplotlib`, you can visualize the actual images by logging them in TensorBoard.
rm -rf logs
Copy
Import `matplotlib` library and create class names and initiate ‘tf.summary.create_file_writer’.
import io
import matplotlib.pyplot as plt
class_names =
['Zero','One','Two','Three','Four','Five','Six','Seven','Eight','Nine
']
logdir = "logs/actual-images/"
file_writer = tf.summary.create_file_writer(logdir)
Copy
Write a function that will create grid of mnist images
def image_grid():
figure = plt.figure(figsize=(12,8))
for i in range(25):
plt.subplot(5, 5, i + 1)
plt.xlabel(class_names[y_train[i]])
plt.xticks([])
plt.yticks([])
plt.grid(False)
plt.imshow(X_train[i], cmap=plt.cm.coolwarm)
return figure
Copy
Next, let’s write a function that will convert this 5X5 grid into a single image that will be
logged in to Tensorboard logs
def plot_to_image(figure):
buf = io.BytesIO()
plt.savefig(buf, format='png')
plt.close(figure)
buf.seek(0)
image = tf.image.decode_png(buf.getvalue(), channels=4)
image = tf.expand_dims(image, 0)
return image
Copy
Launch Tensorboard