Visualizing outputs of each layer of a Convolutional Neural Network (CNN) can provide a good understanding of its behavior during the training phase. Here, I show you how you can achieve that by writing a few lines of code using Keras library.
First, let’s start by defining function get_kernels_output. This function returns a list that contains the output of each kernel of a layer identified by its index, i.e. layer_index:
The inputs to the built Keras function, i.e. get_outputs, are the input to the first layer of the model as well as the value that the learning_phase flag should be set to. It should be noted that the behavior of many layers like Dropout and BatchNormalization are different during the training phase and testing phase. So, it is important to set the value of learning_phase to True when the model is training. This value should be set to False when a trained model is used.
Now, let’s define a function that combines all outputs generated by all kernels of a layer and creates a tiled image:
The if statement, makes sure that kernels_output is the not the output of a fully-connected layer.
Then, we define a function that goes through each layer of the model and plots the tiled image generated by all kernels of that layer:
Here, we assumed that all inputs to the model are normalized and they have a value between 0 and 1.
Now, in order to plot the output of kernels at each layer during the training phase, we need to define a customized Callback:
This allows us to call get_model_layers_output_combined_image function at the end of every 50 epochs.
Now, we are ready to use the OutputImages callback:
Please note that I used data generators to be able to deal with my large volume of data. If this is not your case, you can simply call the model’s fit function and pass your training and cv data sets to the model.
Finally, don’t forget to import all the libraries that we need:
The below figure shows an example of generated tiled images for the first few layers of a CNN model after first epoch of training: