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_4_GradCam.ipynb
Views: 13372
Ungraded Lab: GradCAM
This lab will walk you through generating gradient-weighted class activation maps (GradCAMs) for model predictions.
This is similar to the CAMs you generated before except:
GradCAMs uses gradients instead of the global average pooling weights to weight the activations.
Imports
Download and Prepare the Dataset
You will use the Cats vs Dogs dataset again for this exercise. The following will prepare the train, test, and eval sets.
Modelling
You will use a pre-trained VGG16 network as your base model for the classifier. This will be followed by a global average pooling (GAP) and a 2-neuron Dense layer with softmax activation for the output. The earlier VGG blocks will be frozen and we will just fine-tune the final layers during training. These steps are shown in the utility function below.
You can now train the model. This will take around 10 minutes to run.
Model Interpretability
Let's now go through the steps to generate the class activation maps. You will start by specifying the layers you want to visualize.
Class activation maps (GradCAM)
We'll define a few more functions to output the maps. get_CAM()
is the function highlighted in the lectures and takes care of generating the heatmap of gradient weighted features. show_random_sample()
takes care of plotting the results.
Time to visualize the results
Intermediate activations of layers
You can use the utility function below to visualize the activations in the intermediate layers you defined earlier. This plots the feature side by side for each convolution layer starting from the earliest layer all the way to the final convolution layer.
If you scroll all the way down to see the outputs of the final conv layer, you'll see that there are very few active features and these are mostly located in the face of the cat. This is the region of the image that your model looks at when determining the class.