Path: blob/master/site/en-snapshot/tensorboard/image_summaries.ipynb
25115 views
Copyright 2019 The TensorFlow Authors.
Displaying image data in TensorBoard
Overview
Using the TensorFlow Image Summary API, you can easily log tensors and arbitrary images and view them in TensorBoard. This can be extremely helpful to sample and examine your input data, or to visualize layer weights and generated tensors. You can also log diagnostic data as images that can be helpful in the course of your model development.
In this tutorial, you will learn how to use the Image Summary API to visualize tensors as images. You will also learn how to take an arbitrary image, convert it to a tensor, and visualize it in TensorBoard. You will work through a simple but real example that uses Image Summaries to help you understand how your model is performing.
Setup
Download the Fashion-MNIST dataset
You're going to construct a simple neural network to classify images in the the Fashion-MNIST dataset. This dataset consist of 70,000 28x28 grayscale images of fashion products from 10 categories, with 7,000 images per category.
First, download the data:
Visualizing a single image
To understand how the Image Summary API works, you're now going to simply log the first training image in your training set in TensorBoard.
Before you do that, examine the shape of your training data:
Notice that the shape of each image in the data set is a rank-2 tensor of shape (28, 28), representing the height and the width.
However, tf.summary.image()
expects a rank-4 tensor containing (batch_size, height, width, channels)
. Therefore, the tensors need to be reshaped.
You're logging only one image, so batch_size
is 1. The images are grayscale, so set channels
to 1.
You're now ready to log this image and view it in TensorBoard.
Now, use TensorBoard to examine the image. Wait a few seconds for the UI to spin up.
The "Time Series" dashboard displays the image you just logged. It's an "ankle boot".
The image is scaled to a default size for easier viewing. If you want to view the unscaled original image, check "Show actual image size" at the bottom of the "Settings" panel on the right.
Play with the brightness and contrast sliders to see how they affect the image pixels.
Visualizing multiple images
Logging one tensor is great, but what if you wanted to log multiple training examples?
Simply specify the number of images you want to log when passing data to tf.summary.image()
.
Logging arbitrary image data
What if you want to visualize an image that's not a tensor, such as an image generated by matplotlib?
You need some boilerplate code to convert the plot to a tensor, but after that, you're good to go.
In the code below, you'll log the first 25 images as a nice grid using matplotlib's subplot()
function. You'll then view the grid in TensorBoard:
Building an image classifier
Now put this all together with a real example. After all, you're here to do machine learning and not plot pretty pictures!
You're going to use image summaries to understand how well your model is doing while training a simple classifier for the Fashion-MNIST dataset.
First, create a very simple model and compile it, setting up the optimizer and loss function. The compile step also specifies that you want to log the accuracy of the classifier along the way.
When training a classifier, it's useful to see the confusion matrix. The confusion matrix gives you detailed knowledge of how your classifier is performing on test data.
Define a function that calculates the confusion matrix. You'll use a convenient Scikit-learn function to do this, and then plot it using matplotlib.
You're now ready to train the classifier and regularly log the confusion matrix along the way.
Here's what you'll do:
Create the Keras TensorBoard callback to log basic metrics
Create a Keras LambdaCallback to log the confusion matrix at the end of every epoch
Train the model using Model.fit(), making sure to pass both callbacks
As training progresses, scroll down to see TensorBoard start up.
Notice that accuracy is climbing on both train and validation sets. That's a good sign. But how is the model performing on specific subsets of the data?
Scroll down the "Time Series" dashboard to visualize your logged confusion matrices. Check "Show actual image size" at the bottom of the "Settings" panel to see the confusion matrix at full size.
By default the dashboard shows the image summary for the last logged step or epoch. Use the slider to view earlier confusion matrices. Notice how the matrix changes significantly as training progresses, with darker squares coalescing along the diagonal, and the rest of the matrix tending toward 0 and white. This means that your classifier is improving as training progresses! Great work!
The confusion matrix shows that this simple model has some problems. Despite the great progress, Shirts, T-Shirts, and Pullovers are getting confused with each other. The model needs more work.
If you're interested, try to improve this model with a convolutional network (CNN).