Computing Image Saliency via Neural Network Classifier

☆樱花仙子☆ 提交于 2019-12-04 11:05:50

To compute the gradients, you don't need to use an optimizer, and you can directly use tf.gradients.
With this function, you can directly compute the gradient of output with respect to the image input, whereas the optimizer compute_gradients method can only compute gradients with respect to Variables.

The other advantage of tf.gradients is that you can specify the gradients of the output you want to backpropagate.


So here is how to get the gradients of an input image with respect to output[1, 1]:

  • we have to set the output gradients to 0 everywhere except at indice [1, 1]
input = tf.ones([1, 4, 4, 1])
filter = tf.ones([3, 3, 1, 1])
output = tf.nn.conv2d(input, filter, [1, 1, 1, 1], 'SAME')

grad_output = np.zeros((1, 4, 4, 1), dtype=np.float32)
grad_output[0, 1, 1, 0] = 1.

grads = tf.gradients(output, input, grad_output)

sess = tf.Session()
print sess.run(grads[0]).reshape((4, 4))
# prints [[ 1.  1.  1.  0.]
#         [ 1.  1.  1.  0.]
#         [ 1.  1.  1.  0.]
#         [ 0.  0.  0.  0.]]
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!