<jupyter_start><jupyter_text>Grad Cam on Any Deep Network------------------------------------1) Define model2) get predicted class3) Pass layer_name to grad_cam function4) Outputs heatmap overlayed on input image<jupyter_code>from keras.preprocessing.image import load_img, img_to_array
import numpy as np
import scipy
import argparse
import cv2
from keras.applications import inception_resnet_v2
from keras.applications import resnet50
from keras import backend as K
import matplotlib.pyplot as plt
%matplotlib inline
K.set_learning_phase(0)
model = resnet50.ResNet50(weights='imagenet')
model.summary()
layer_dict = dict([(layer.name, layer) for layer in model.layers])

def load_image(image_path):
    # Util function to open, resize and format pictures
    # into appropriate tensors.
    img = load_img(image_path, target_size=(224, 224))
    img = img_to_array(img)
    img = np.expand_dims(img, axis=0)
    img = resnet50.preprocess_input(img)
    return img
preprocessed_image = load_image("/home/nishanth/Documents/index.jpeg")
org_img = cv2.resize(cv2.imread("/home/nishanth/Documents/index.jpeg"),(224,224))
predictions = model.predict(preprocessed_image)
top_1 = resnet50.decode_predictions(predictions)[0][0]
print('Predicted class:')
print('%s (%s) with probability %.2f' % (top_1[1], top_1[0], top_1[2]))
predicted_class = np.argmax(predictions)
def grad_cam(input_model, image, org_img, category_index, layer_name):
    

    mod_outputs = input_model.output[0, category_index]
    conv_output = input_model.get_layer(layer_name).output
    grads = K.gradients(mod_outputs, conv_output)[0]
    gradient_function = K.function([input_model.input], [conv_output, grads])
    output, grads_val = gradient_function([image])
    output, grads_val = output[0, :], grads_val[0, :, :, :]
    weights = np.mean(grads_val, axis = (0, 1))
    cam = np.ones(output.shape[0 : 2], dtype = np.float32)
    cam = np.dot(output, weights)
    cam = cv2.resize(cam, (224, 224))
    cam = np.maximum(cam, 0)
    heatmap = cam / np.max(cam)
    cam = cv2.applyColorMap(np.uint8(255*heatmap), cv2.COLORMAP_JET)
    #cam = np.float32(cam) 
    #grad_cam_out = 0.3 * np.uint8(255 * cam / np.max(cam)) + 0.7 * org_img
    grad_cam_out = 0.3 * cam + 0.7 * org_img
    return grad_cam_out

heatmap_out = grad_cam(model, preprocessed_image, org_img, predicted_class, "res5c_branch2c")
im = cv2.cvtColor(heatmap_out.astype('uint8'), cv2.COLOR_BGR2RGB)
plt.imshow(im)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.13537469782433523, 'fraction_numerical': 0.0346494762288477, 'mean_word_length': 4.668949771689498, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '541295', 'license': 'MIT', 'n_tokens_mistral': 933, 'n_tokens_neox': 883, 'n_words': 190, 'path': 'gradCamALL.ipynb', 'repo_name': 'Nishanth009/Deep-learning-', 'size': '2482'}