Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot generate heatmaps using Gradcam library #20475

Open
jeev0306 opened this issue Nov 8, 2024 · 2 comments
Open

Cannot generate heatmaps using Gradcam library #20475

jeev0306 opened this issue Nov 8, 2024 · 2 comments

Comments

@jeev0306
Copy link

jeev0306 commented Nov 8, 2024

import numpy as np
import tensorflow as tf
from tf_explain.core.grad_cam import GradCAM
import matplotlib.pyplot as plt

Initialize Grad-CAM explainer

explainer = GradCAM()

Specify the layer for Grad-CAM in the VGG16 model (vgg16_tr)

conv_layer_name = 'block5_conv2' # Adjusted to use 'block5_conv2'

Get a batch from the validation generator

X_val, y_val = val_generator.getitem(0)

Process each image in the batch

for i in range(len(X_val)):
# Add batch dimension to match the expected input shape for Grad-CAM
image_to_explain = np.expand_dims(X_val[i], axis=0) # Shape: (1, 128, 128, 3)
label_to_explain = np.expand_dims(y_val[i], axis=0) # Shape: (1, num_classes)
true_class = np.argmax(y_val[i]) # Get the true class index from the label

# Ensure the image is passed as a tf.Tensor (not a NumPy array)
image_to_explain = tf.convert_to_tensor(image_to_explain, dtype=tf.float32)

# Generate the Grad-CAM heatmap
heatmap = explainer.explain(
    validation_data=(image_to_explain, label_to_explain),  # Pass the single image and its label
    model=vgg16_tr,
    class_index=true_class,
    layer_name=conv_layer_name
)

# Plot the original image and Grad-CAM heatmap
fig, ax = plt.subplots(1, 2, figsize=(10, 5))

# Original image
ax[0].imshow(X_val[i])
ax[0].set_title(f'Original Image {i+1}')
ax[0].axis('off')

# Grad-CAM heatmap overlay
ax[1].imshow(X_val[i])  # Show the original image first
ax[1].imshow(heatmap, cmap='jet', alpha=0.3)  # Overlay heatmap with transparency
ax[1].set_title(f'Grad-CAM Heatmap {i+1}')
ax[1].axis('off')

plt.tight_layout()
plt.show()

I'm trying to generate heatmaps using inbuilt GradCam function for the celebA dataset. But I get the below error. I have reviewed the size to the input going to the GradCam it is (1, 128,128,3).

error: When providing inputs as a list/tuple, all values in the list/tuple must be KerasTensors. Received: inputs=[[<KerasTensor shape=(None, 128, 128, 3), dtype=float32, sparse=None, name=keras_tensor>]] including invalid value [<KerasTensor shape=(None, 128, 128, 3), dtype=float32, sparse=None, name=keras_tensor>] of type <class 'list'>

If anyone could help me debug this, it would be much appriciated.

@jeev0306 jeev0306 changed the title Cannot generate heatmaps using Gradcam libraray Cannot generate heatmaps using Gradcam library Nov 8, 2024
@fchollet
Copy link
Member

This looks like you're using an old version of Keras 3, from before the time when we supported nested model inputs.

@dhantule
Copy link

dhantule commented Nov 12, 2024

Hi @jeev0306,
Thanks for reporting the issue. Could you please share the version of Keras you are using and also dummy dataset to reproduce this issue?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants