# TensorBoard with Fashion MNIST

In this week’s exercise you will train a convolutional neural network to classify images of the Fashion MNIST dataset and you will use TensorBoard to explore how it’s confusion matrix evolves over time.

## Setup

TensorFlow version:  2.0.0


We are going to use a CNN to classify images in the the Fashion-MNIST dataset. This dataset consist of 70,000 grayscale images of fashion products from 10 categories, with 7,000 images per category. The images have a size of $28\times28$ pixels.

First, we load the data. Even though these are really images, we will load them as NumPy arrays and not as binary image objects. The data is already divided into training and testing sets.

## Format the Images

train_images is a NumPy array with shape (60000, 28, 28) and test_images is a NumPy array with shape (10000, 28, 28). However, our model expects arrays with shape (batch_size, height, width, channels) . Therefore, we must reshape our NumPy arrays to also include the number of color channels. Since the images are grayscale, we will set channels to 1. We will also normalize the values of our NumPy arrays to be in the range [0,1].

## Build the Model

We will build a simple CNN and compile it.

## Plot Confusion Matrix

When training a classifier, it’s often useful to see the confusion matrix. The confusion matrix gives you detailed knowledge of how your classifier is performing on test data.

In the cell below, we will define a function that returns a Matplotlib figure containing the plotted confusion matrix.

## TensorBoard Callback

We are now ready to train the CNN and regularly log the confusion matrix during the process. In the cell below, you will create a Keras TensorBoard callback to log basic metrics.

rm: cannot remove 'logs/image/20200222-182126/cm': Directory not empty


## Convert Matplotlib Figure to PNG

Unfortunately, the Matplotlib file format cannot be logged as an image, but the PNG file format can be logged. So, you will create a helper function that takes a Matplotlib figure and converts it to PNG format so it can be written.

## Confusion Matrix

In the cell below, you will define a function that calculates the confusion matrix.

## Running TensorBoard

The next step will be to run the code shown below to render the TensorBoard. Unfortunately, TensorBoard cannot be rendered within the Coursera environment. Therefore, we won’t run the code below.

However, you are welcome to download the notebook and run the above code locally on your machine or in Google’s Colab to see TensorBoard in action. Below are some example screenshots that you should see when executing the code: