% matplotlib inline
import os
import numpy as np
from keras.preprocessing import image
from keras.applications.vgg16 import preprocess_input, decode_predictions
import keras.backend as K
import matplotlib.pyplot as plt
import cv2
from keras.applications import VGG16
model = VGG16(weights='imagenet')
img_path = os.path.join('jo_feline','jo','jo2.jpg')
img = image.load_img(img_path, target_size = (224,224))
x = image.img_to_array(img)
y = x.copy()
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
plt.imshow(y * 255)
preds = model.predict(x)
print('Predicted:', decode_predictions(preds, top=3)[0])
np.argmax(preds)
blenheim_spaniel_output = model.output[:,156]
last_conv_layer = model.get_layer('block5_conv3')
grads = K.gradients(blenheim_spaniel_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)
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
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
plt.imshow(superimposed_img)
plt.show()
plt.imshow(img)