I want to implement the code to get Grad-CAM map with pytorch(1.10.0). Most of implementation specify the target class to extract the gradients (This is a natural approach). But instead of this, I want to use gradients from loss to target layer output.
The following code is an example of a keras implementation of what I want to do. How can I implement it with pytorch?
loss = K.mean(K.square(y_pred - y_true), axis=-1)
output_conv = model.get_layer(layer_name).output
gradients = K.gradients(loss, output_conv)[0]
output, grads = K.function([model.input], [output_conv, gradients])([input_image])