I have a function that takes a single parameter (alpha) as an input an outputs a N by N image (2048x2048). I want to obtain the gradient of this image with respect to the parameter (alpha). I’m not talking about a sobel filter, I’m looking to see how my image changes as I change alpha. The function takes around 2 seconds to evaluate.
grad_img = torch.autograd.functional.jacobian(render_with_alpha, alpha).squeeze(-1)
This works, and does exactly what I want. However, it takes minutes for a 64x64 image, and I terminated the 1024x1024 after 16h before it finished (I’m looking to compute 2048x2048).
Another approach, which is certainly too slow is use backward()
for each pixel. To get this working, I had to rerun the forward pass every time, which makes this method impractical (is there a way around this?).
Two alternative methods from the pytorch documentation appear to be jacrev and jacfwd. In my case, since I have a single input and a large output, it appears that jacfwd
would be ideal. However, I cannot get it to work with my code. If I understand correctly, it does not use autograd and when I use it, the code has errors about missing storage.
RuntimeError: Cannot access data pointer of Tensor that doesn’t have storage
I have this error every time I use a view, or use detach(). I do both of these quite often in my code.
So how can I efficiently compute the per pixel gradient of the image?