Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
356 changes: 356 additions & 0 deletions examples/vision/grad_cam.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,356 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"# Grad-CAM class activation visualization\n",
"\n",
"**Author:** [fchollet](https://twitter.com/fchollet)<br>\n",
"**Date created:** 2020/04/26<br>\n",
"**Last modified:** 2021/03/07<br>\n",
"**Description:** How to obtain a class activation heatmap for an image classification model."
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"Adapted from Deep Learning with Python (2017).\n",
"\n",
"## Setup"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"import os\n",
"\n",
"os.environ[\"KERAS_BACKEND\"] = \"tensorflow\"\n",
"\n",
"import numpy as np\n",
"import tensorflow as tf\n",
"import keras\n",
"\n",
"# Display\n",
"from IPython.display import Image, display\n",
"import matplotlib as mpl\n",
"import matplotlib.pyplot as plt\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"## Configurable parameters\n",
"\n",
"You can change these to another model.\n",
"\n",
"To get the values for `last_conv_layer_name` use `model.summary()`\n",
"to see the names of all layers in the model."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"model_builder = keras.applications.xception.Xception\n",
"img_size = (299, 299)\n",
"preprocess_input = keras.applications.xception.preprocess_input\n",
"decode_predictions = keras.applications.xception.decode_predictions\n",
"\n",
"last_conv_layer_name = \"block14_sepconv2_act\"\n",
"\n",
"# The local path to our target image\n",
"img_path = keras.utils.get_file(\n",
" \"african_elephant.jpg\", \"https://i.imgur.com/Bvro0YD.png\"\n",
")\n",
"\n",
"display(Image(img_path))\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"## The Grad-CAM algorithm"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"\n",
"def get_img_array(img_path, size):\n",
" # `img` is a PIL image of size 299x299\n",
" img = keras.utils.load_img(img_path, target_size=size)\n",
" # `array` is a float32 Numpy array of shape (299, 299, 3)\n",
" array = keras.utils.img_to_array(img)\n",
" # We add a dimension to transform our array into a \"batch\"\n",
" # of size (1, 299, 299, 3)\n",
" array = np.expand_dims(array, axis=0)\n",
" return array\n",
"\n",
"\n",
"def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None):\n",
" # First, we create a model that maps the input image to the activations\n",
" # of the last conv layer as well as the output predictions\n",
" grad_model = keras.models.Model(\n",
" model.input, [model.get_layer(last_conv_layer_name).output, model.output]\n",
" )\n",
"\n",
" # Then, we compute the gradient of the top predicted class for our input image\n",
" # with respect to the activations of the last conv layer\n",
"\n",
"\n",
"with tf.GradientTape() as tape:\n",
" last_conv_layer_output, preds = grad_model(img_array)\n",
" if pred_index is None:\n",
" pred_index = tf.argmax(preds[0])\n",
" class_channel = preds[:, pred_index]\n",
"\n",
" # This is the gradient of the output neuron (top predicted or chosen)\n",
" # with regard to the output feature map of the last conv layer\n",
" grads = tape.gradient(class_channel, last_conv_layer_output)\n",
"\n",
" # This is a vector where each entry is the mean intensity of the gradient\n",
" # over a specific feature map channel\n",
" pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))\n",
"\n",
" # We multiply each channel in the feature map array\n",
" # by \"how important this channel is\" with regard to the top predicted class\n",
" # then sum all the channels to obtain the heatmap class activation\n",
" last_conv_layer_output = last_conv_layer_output[0]\n",
" heatmap = last_conv_layer_output @ pooled_grads[..., tf.newaxis]\n",
" heatmap = tf.squeeze(heatmap)\n",
"\n",
" # For visualization purpose, we will also normalize the heatmap between 0 & 1\n",
" heatmap = tf.maximum(heatmap, 0) / tf.math.reduce_max(heatmap)\n",
" return heatmap.numpy()\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"## Let's test-drive it"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"# Prepare image\n",
"img_array = preprocess_input(get_img_array(img_path, size=img_size))\n",
"\n",
"# Make model\n",
"model = model_builder(weights=\"imagenet\")\n",
"\n",
"# Remove last layer's softmax\n",
"model.layers[-1].activation = None\n",
"\n",
"# Print what the top predicted class is\n",
"preds = model.predict(img_array)\n",
"print(\"Predicted:\", decode_predictions(preds, top=1)[0])\n",
"\n",
"# Generate class activation heatmap\n",
"heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name)\n",
"\n",
"# Display heatmap\n",
"plt.matshow(heatmap)\n",
"plt.show()\n",
""
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"## Create a superimposed visualization"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"\n",
"def save_and_display_gradcam(img_path, heatmap, cam_path=\"cam.jpg\", alpha=0.4):\n",
" # Load the original image\n",
" img = keras.utils.load_img(img_path)\n",
" img = keras.utils.img_to_array(img)\n",
"\n",
" # Rescale heatmap to a range 0-255\n",
" heatmap = np.uint8(255 * heatmap)\n",
"\n",
" # Use jet colormap to colorize heatmap\n",
" jet = mpl.colormaps[\"jet\"]\n",
"\n",
" # Use RGB values of the colormap\n",
" jet_colors = jet(np.arange(256))[:, :3]\n",
" jet_heatmap = jet_colors[heatmap]\n",
"\n",
" # Create an image with RGB colorized heatmap\n",
" jet_heatmap = keras.utils.array_to_img(jet_heatmap)\n",
" jet_heatmap = jet_heatmap.resize((img.shape[1], img.shape[0]))\n",
" jet_heatmap = keras.utils.img_to_array(jet_heatmap)\n",
"\n",
" # Superimpose the heatmap on original image\n",
" superimposed_img = jet_heatmap * alpha + img\n",
" superimposed_img = keras.utils.array_to_img(superimposed_img)\n",
"\n",
" # Save the superimposed image\n",
" superimposed_img.save(cam_path)\n",
"\n",
" # Display Grad CAM\n",
" display(Image(cam_path))\n",
"\n",
"\n",
"save_and_display_gradcam(img_path, heatmap)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"## Let's try another image\n",
"\n",
"We will see how the grad cam explains the model's outputs for a multi-label image. Let's\n",
"try an image with a cat and a dog together, and see how the grad cam behaves."
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"img_path = keras.utils.get_file(\n",
" \"cat_and_dog.jpg\",\n",
" \"https://storage.googleapis.com/petbacker/images/blog/2017/dog-and-cat-cover.jpg\",\n",
")\n",
"\n",
"display(Image(img_path))\n",
"\n",
"# Prepare image\n",
"img_array = preprocess_input(get_img_array(img_path, size=img_size))\n",
"\n",
"# Print what the two top predicted classes are\n",
"preds = model.predict(img_array)\n",
"print(\"Predicted:\", decode_predictions(preds, top=2)[0])"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"We generate class activation heatmap for \"chow,\" the class index is 260"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=260)\n",
"\n",
"save_and_display_gradcam(img_path, heatmap)"
]
},
{
"cell_type": "markdown",
"metadata": {
"colab_type": "text"
},
"source": [
"We generate class activation heatmap for \"egyptian cat,\" the class index is 285"
]
},
{
"cell_type": "code",
"execution_count": 0,
"metadata": {
"colab_type": "code"
},
"outputs": [],
"source": [
"heatmap = make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=285)\n",
"\n",
"save_and_display_gradcam(img_path, heatmap)"
]
}
],
"metadata": {
"accelerator": "GPU",
"colab": {
"collapsed_sections": [],
"name": "grad_cam",
"private_outputs": false,
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
15 changes: 8 additions & 7 deletions examples/vision/grad_cam.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import matplotlib as mpl
import matplotlib.pyplot as plt


"""
## Configurable parameters

Expand Down Expand Up @@ -71,16 +70,18 @@ def make_gradcam_heatmap(img_array, model, last_conv_layer_name, pred_index=None
# First, we create a model that maps the input image to the activations
# of the last conv layer as well as the output predictions
grad_model = keras.models.Model(
model.inputs, [model.get_layer(last_conv_layer_name).output, model.output]
model.input, [model.get_layer(last_conv_layer_name).output, model.output]
)

# Then, we compute the gradient of the top predicted class for our input image
# with respect to the activations of the last conv layer
with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model(img_array)
if pred_index is None:
pred_index = tf.argmax(preds[0])
class_channel = preds[:, pred_index]


with tf.GradientTape() as tape:
last_conv_layer_output, preds = grad_model(img_array)
if pred_index is None:
pred_index = tf.argmax(preds[0])
class_channel = preds[:, pred_index]

# This is the gradient of the output neuron (top predicted or chosen)
# with regard to the output feature map of the last conv layer
Expand Down
Loading