Skip to content

Instantly share code, notes, and snippets.

@michelkana
Last active July 27, 2022 17:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save michelkana/b8a5176b3ac7db09b48ef4264f4e16fa to your computer and use it in GitHub Desktop.
Save michelkana/b8a5176b3ac7db09b48ef4264f4e16fa to your computer and use it in GitHub Desktop.
# pip install scipy==1.1.0
# pip install keras-vis
from vis.visualization import visualize_saliency
def plot_saliency(img_idx=None):
img_idx = plot_features_map(img_idx)
grads = visualize_saliency(cnn_saliency, -1, filter_indices=ytest[img_idx][0],
seed_input=x_test[img_idx], backprop_modifier=None,
grad_modifier="absolute")
predicted_label = labels[np.argmax(cnn.predict(x_test[img_idx].reshape(1,32,32,3)),1)[0]]
fig, ax = plt.subplots(1,2, figsize=(10,5))
ax[0].imshow(x_test[img_idx])
ax[0].set_title('original img id {} - {}'.format(img_idx, labels[ytest[img_idx][0]]))
ax[1].imshow(grads, cmap='jet')
ax[1].set_title('saliency - predicted {}'.format(predicted_label))
plot_saliency()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment