base_model = VGG16(weights='imagenet', include_top=False)
model = Sequential([base_model, Dense(5, activation='softmax')])
def visualize_grad_cam(input_model, img_array, layer_name, class_index, colormap=cv2.COLORMAP_VIRIDIS):
grad_model = Model(inputs=input_model.input, outputs=[input_model.get_layer(layer_name).output, input_model.output])
with tf.GradientTape() as tape:
conv_outputs, predictions = grad_model(img_array)
loss = predictions[:, class_index]
grads = tape.gradient(loss, conv_outputs)
pooled_grads = tf.reduce_mean(grads, axis=(0, 1, 2))
conv_outputs = conv_outputs[0]
heatmap = tf.reduce_mean(tf.multiply(pooled_grads, conv_outputs), axis=-1)
heatmap = tf.maximum(heatmap, 0) # Use TensorFlow's maximum function here
# Convert the tensor to a NumPy array using a TensorFlow session
with tf.compat.v1.Session() as sess:
heatmap_array = heatmap.eval()
heatmap_array /= np.max(heatmap_array)
heatmap_array = cv2.resize(heatmap_array, (img_size, img_size))
heatmap_array = np.uint8(255 * heatmap_array)
heatmap_array = cv2.applyColorMap(heatmap_array, colormap)
superimposed_img = cv2.addWeighted(cv2.cvtColor(img_array[0], cv2.COLOR_RGB2BGR), 0.6, heatmap_array, 0.4, 0)
return superimposed_img
import tensorflow as tf
import cv2
import numpy as np
import matplotlib.pyplot as plt
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.applications import VGG16
from tensorflow.keras.models import Sequential, Model
from tensorflow.keras.layers import Dense, Dropout, Flatten
from tensorflow.keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler
from sklearn.metrics import classification_report, confusion_matrix
import seaborn as sns
from lime import lime_image
# ...
# Choose a random image from the test set for visualization
test_img, true_label = test_generator.next()
class_index = 0 # Choose the class index you want to visualize (corresponding to class_names)
# layer_name = 'block5_conv3' # Choose a layer from the base VGG16 model
# Use the functional API model for Grad-CAM visualization
# test_img_with_gradcam = visualize_grad_cam(model, test_img, layer_name, class_index=class_index)
# test_img_with_gradcam = visualize_grad_cam(base_model, test_img, layer_name, class_index=class_index)
layer_name = 'block5_conv2' # Choose a different layer from the base VGG16 model
test_img_with_gradcam = visualize_grad_cam(base_model, test_img, layer_name, class_index=class_index)
# Create the LIME explainer
explainer = lime_image.LimeImageExplainer()
# Display the original image, Grad-CAM visualization, and LIME explanation
fig, axes = plt.subplots(1, 3, figsize=(18, 6))
axes[0].imshow(test_img[0])
axes[0].set_title('Original Image')
axes[0].axis('off')
axes[1].imshow(test_img_with_gradcam)
axes[1].set_title(f'Image with Grad-CAM (Class {class_index})')
axes[1].axis('off')
axes[2].imshow(explanation_img)
axes[2].set_title('LIME Explanation')
axes[2].axis('off')
plt.show()