Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Real-time collaboration for Jupyter Notebooks, Linux Terminals, LaTeX, VS Code, R IDE, and more,
all in one place. Commercial Alternative to JupyterHub.
Path: blob/master/Advanced Computer Vision with TensorFlow/Week 4 - Visualization and Interpretability/Copy of C3_W4_Lab_3_Saliency.ipynb
Views: 13371
Ungraded Lab: Saliency
Like class activation maps, saliency maps also tells us what parts of the image the model is focusing on when making its predictions.
The main difference is in saliency maps, we are just shown the relevant pixels instead of the learned features.
You can generate saliency maps by getting the gradient of the loss with respect to the image pixels.
This means that changes in certain pixels that strongly affect the loss will be shown brightly in your saliency map.
Let's see how this is implemented in the following sections.
Imports
Build the model
For the classifier, you will use the Inception V3 model available in Tensorflow Hub. This has pre-trained weights that is able to detect 1001 classes. You can read more here.
Get a sample image
You will download a photo of a Siberian Husky that our model will classify. We left the option to download a Tabby Cat image instead if you want.
Preprocess the image
The image needs to be preprocessed before being fed to the model. This is done in the following steps:
We can now preview our input image.
Compute Gradients
You will now get the gradients of the loss with respect to the input image pixels. This is the key step to generate the map later.
Visualize the results
Now that you have the gradients, you will do some postprocessing to generate the saliency maps and overlay it on the image.
Let's do a little sanity check to see the results of the conversion.
You should get something like:
Now let's see what this looks like when plotted. The white pixels show the parts the model focused on when classifying the image.
Let's superimpose the normalized tensor to the input image to get more context. You can see that the strong pixels are over the husky and that is a good indication that the model is looking at the correct part of the image.