I am getting this warning when I am trying to create saliency visualizations.
RuntimeError Traceback (most recent call last)
<ipython-input-36-6f13b9abef1d> in <module>()
8 fig, axes = plt.subplots(1, 2)
9 # Generate visualization
---> 10 visualize = visualize_saliency(saved_model, layer_index, filter_indices=input_class, seed_input=input_image)
11 axes[0].imshow(input_image[..., 0])
12 axes[0].set_title('Original image')
tf.gradients is not supported when eager execution is enabled. Use tf.GradientTape instead.
This is my code
layer_index = utils.find_layer_idx(saved_model, 'dense_2')
saved_model.layers[layer_index].activation = activations.linear
saved_model = utils.apply_modifications(saved_model)
indices_to_visualize = [ 0, 12, 38, 83, 112, 74, 190 ]
# Visualize
for index_to_visualize in indices_to_visualize:
# Get input
input_image = X_test[index_to_visualize]
input_class = np.argmax(y_test[index_to_visualize])
# Matplotlib preparations
fig, axes = plt.subplots(1, 2)
# Generate visualization
visualize = visualize_saliency(saved_model, layer_index, filter_indices=input_class, seed_input=input_image)
axes[0].imshow(input_image[..., 0])
axes[0].set_title('Original image')
axes[1].imshow(visualize)
axes[1].set_title('Saliency map')
fig.suptitle(f'MNIST target = {input_class}')
plt.show()
I have tried looking at other similar errors online but I dont understand how those solutions can be implemented in my code. How exactly am I supposed to use tf.GradientTape on my saliency visualization function. I havent come across anyone having the same error for saliency visualization.