Gradiant jacobian matrix output image with respect to input image

Hi,
i try to compute jacobian gradient output image with respect to input image(for each pixels) but i have some errors .
does anyone have an idea about the right code?

Thanks

image = Image.open(’/Users/lionardo/Desktop/3.png’)
x = TF.to_tensor(image)
x = Variable(x, requires_grad = True)

image = Image.open(’/Users/lionardo/Desktop/4.png’)
y = TF.to_tensor(image)
y = Variable(y, requires_grad = True)
def jacobian(y, x, create_graph=False):

jac = []                                                                                          
flat_y = y.reshape(-1)                                                                            
grad_y = torch.zeros_like(flat_y)
flat_x = x.reshape(-1)
grad_x = torch.zeros_like(flat_x)

for i in range(len(flat_y)) :                                                                    

for j in range (len(flat_x)) : 
    grad_y[i] = 1. 
    grad_x[j]=1.
    grad_x, = torch.autograd.grad(flat_y, flat_x, grad_y,grad_x, retain_graph=True, create_graph=create_graph)

    jac.append(grad_x.reshape(x.shape))
    jac.append(grad_y.reshape(y.shape)) 
    grad_y[i] = 0.  
    grad_x[j] =0.
return torch.stack(jac).reshape(y.shape + x.shape)

You can find correct code in this gist to get the jacobian of the output y wrt an input x.

1 Like

thank you so much :blush: