K.gradients(loss, input_img)[0] return "None". (Keras CNN visualization with tensorflow backend)
If you have a Model instance, then to take the gradient of the loss with respect to the input, you should do:
grads = K.gradients(loss, model.input)[0]
model.input
contains the symbolic tensor that represents the input to the model. Using a plain numpy array makes no sense because TensorFlow then has no idea how this connects to the computational graph, and returns None as the gradient.
Then you should also rewrite the iterate
function as:
iterate = K.function([model.input], [loss, grads])
Below, it's my example. Hope to help someone.
gradient = keras.backend.gradients(model.output, model.input)[2]
iterate = keras.backend.function(model.input, [gradient])
grad = iterate([patches, depthes, poses])
[patches, depthes, poses] is my model.input