Hi,
input_image in your example should be a Tensor. Otherwise, setting requires_grad on it will have no effect.
Also you should not use .data anymore. You can use .detach() here to replace it.
Are you trying to get the gradient for the input of your network? If that case, since you set require_grad=True on input_image which is a leaf Tensor, after the call to .backward, you can simply access it via input_image.grad.
Finally as you can see in the doc for register_backward_hook, it won’t work with all nn.Modules. So you might want to use register_hook directly on a Tensor if you want the gradients for that Tensor.
Or if you have access to that Tensor during the forward, simply call .retain_grad() on it for its .grad field to be populated during the backward.