There are a number of ways to understand how a neural network works, one of them is through visualization.
Consider a classification task, which need to recognize between leopards and Arabian leopards, we can differentiate between these animals using the image background. Both animals live in starkly contrasting habitats. The majority of the snow leopard images will have snow in the background while most of the Arabian leopard images will have a sprawling desert. But, the model will start classifying snow versus desert images. So, to make sure our model has correctly learned the distinguishing features between these two leopard types, we can use visualization.
In here, we will produce the saliency maps. Saliency maps are another visualization technique based on gradients. Saliency maps calculate the effect of every pixel on the output of the model. This involves calculating the gradient of the output with respect to every pixel of the input image. These gradients, which are of the same shape as the image (gradient is calculated with respect to every pixel), provide us with the intuition of attention.
This code demonstrates how to visualize the features from 3D Convolutional network. We use vanilla gradients and guided back propagation to compute these saliency representation. Note, since the I3D is 3D convolution, hence, the input is a set of frames. To visualize the features, we need to modify the axis of visualization.py in the folder /usr/local/lib/python2.7/dist-packages/saliency.
Setup
1. Clone the project
$ git clone https://github.com/didpurwanto/feat_visualization
2. install saliency by
$ pip install saliency
3. edit the axis of visualization into 3, which originally set as 2.
Go to folder /usr/local/lib/python2.7/dist-packages/saliency
line 23: axis = 3, instead of axis = 2.
4. put this folder project inside the I3D/experiments/dataset/ along with the train.py and test.py.
Run the code
$ python feat_visualization.py
The results are stored in .visualization/results/.
Input Output:
This code is inspired by PAIRML Saliency library link
Thanks to 吳侒融.