Skip to content

Instantly share code, notes, and snippets.

@thunderInfy
Last active April 21, 2020 08:17
Show Gist options
  • Select an option

  • Save thunderInfy/75dfe7059760c8ca7b96f2c71432dffe to your computer and use it in GitHub Desktop.

Select an option

Save thunderInfy/75dfe7059760c8ca7b96f2c71432dffe to your computer and use it in GitHub Desktop.
# preprocess the image
X = preprocess(img)
# we would run the model in evaluation mode
model.eval()
# we need to find the gradient with respect to the input image, so we need to call requires_grad_ on it
X.requires_grad_()
'''
forward pass through the model to get the scores, note that VGG-19 model doesn't perform softmax at the end
and we also don't need softmax, we need scores, so that's perfect for us.
'''
scores = model(X)
# Get the index corresponding to the maximum score and the maximum score itself.
score_max_index = scores.argmax()
score_max = scores[0,score_max_index]
'''
backward function on score_max performs the backward pass in the computation graph and calculates the gradient of
score_max with respect to nodes in the computation graph
'''
score_max.backward()
'''
Saliency would be the gradient with respect to the input image now. But note that the input image has 3 channels,
R, G and B. To derive a single class saliency value for each pixel (i, j), we take the maximum magnitude
across all colour channels.
'''
saliency, _ = torch.max(X.grad.data.abs(),dim=1)
# code to plot the saliency map as a heatmap
plt.imshow(saliency[0], cmap=plt.cm.hot)
plt.axis('off')
plt.show()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment