"""VISUALISING HEATMAPS OF CLASS ACTIVATION"""
"""However, to visualize heatmap I used my transfer learning model which is
my main model"""
from keras.applications.vgg16 import VGG16

model = VGG16(weights='imagenet')                        

from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input
import numpy as np


img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
                            
acer_capilipes_output = model.output[:, 386]           
last_conv_layer = model.get_layer('block5_conv3')
  
grads = K.gradients(acer_capilipes_output, last_conv_layer.output)[0]
pooled_grads = K.mean(grads, axis=(0, 1, 2))
iterate = K.function([model.input],[pooled_grads, last_conv_layer.output[0]])
pooled_grads_value, conv_layer_output_value = iterate([x])

for i in range(512):
    conv_layer_output_value[:, :, i] *= pooled_grads_value[i]
    heatmap = np.mean(conv_layer_output_value, axis=-1)
                            
heatmap = np.maximum(heatmap, 0)
heatmap /= np.max(heatmap)
plt.matshow(heatmap)
                            
import cv2
img = cv2.imread(img_path)
heatmap = cv2.resize(heatmap, (img.shape[1], img.shape[0]))
heatmap = np.uint8(255 * heatmap)
heatmap = cv2.applyColorMap(heatmap, cv2.COLORMAP_JET)
superimposed_img = heatmap * 0.4 + img
"""Save the superimposed image into the following directory - img_path"""
cv2.imwrite(img_path, superimposed_img)                            
                            
###############################################################################
###############################END#############################################
###############################################################################                            
 
