Error regarding grad cam implementation in model using tensorflow

9 views
Skip to first unread message

Senura Perera

unread,
Aug 10, 2023, 10:16:11 AM8/10/23
to TensorFlow Hub
Hi I have an error regarding adding grad cam to my model.

this is the model summary 
tensor flow version is 2.13.0

Screenshot 2023-08-10 at 7.43.31 PM.png
this is the grad cam code

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()


this is the error I get 

--------------------------------------------------------------------------- FailedPreconditionError Traceback (most recent call last) File ~/anaconda3/envs/flaskenv/lib/python3.9/site-packages/tensorflow/python/client/session.py:1379, in BaseSession._do_call(self, fn, *args) 1378 try: -> 1379 return fn(*args) 1380 except errors.OpError as e: File ~/anaconda3/envs/flaskenv/lib/python3.9/site-packages/tensorflow/python/client/session.py:1362, in BaseSession._do_run.._run_fn(feed_dict, fetch_list, target_list, options, run_metadata) 1361 self._extend_graph() -> 1362 return self._call_tf_sessionrun(options, feed_dict, fetch_list, 1363 target_list, run_metadata) File ~/anaconda3/envs/flaskenv/lib/python3.9/site-packages/tensorflow/python/client/session.py:1455, in BaseSession._call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata) 1453 def _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, 1454 run_metadata): -> 1455 return tf_session.TF_SessionRun_wrapper(self._session, options, feed_dict, 1456 fetch_list, target_list, 1457 run_metadata) FailedPreconditionError: 2 root error(s) found. (0) FAILED_PRECONDITION: Could not find variable block5_conv1_2/kernel. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status error message=Container localhost does not exist. (Could not find resource: localhost/block5_conv1_2/kernel) [[{{node model_11/block5_conv1/Conv2D/ReadVariableOp}}]] [[model_11/block5_conv3/Conv2D/ReadVariableOp/_28]] (1) FAILED_PRECONDITION: Could not find variable block5_conv1_2/kernel. This could mean that the variable has been deleted. In TF1, it can also mean the variable is uninitialized. Debug info: container=localhost, status error message=Container localhost does not exist. (Could not find resource: localhost/block5_conv1_2/kernel) [[{{node model_11/block5_conv1/Conv2D/ReadVariableOp}}]]
...
File "/Users/senu2k/anaconda3/envs/flaskenv/lib/python3.9/site-packages/tensorflow/python/framework/op_def_library.py", line 795, in _apply_op_helper op = g._create_op_internal(op_type_name, inputs, dtypes=None, File "/Users/senu2k/anaconda3/envs/flaskenv/lib/python3.9/site-packages/tensorflow/python/framework/ops.py", line 3381, in _create_op_internal ret = Operation.from_node_def(


Reply all
Reply to author
Forward
0 new messages