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)