kornia / kornia

Geometric Computer Vision Library for Spatial AI
https://kornia.readthedocs.io
Apache License 2.0
9.94k stars 969 forks source link

kornia.warp_affine - Autograd Issue #522

Closed mowolf closed 4 years ago

mowolf commented 4 years ago

I need the gradient of this warping operation. The results of the warp are correct, just the gradient seems to be wrong.

// - tensor   :  output of my neural network 
//               tensor.requires_grad = True
// - M      : transformation Matrix prev created with kornia

warped_tensor: torch.tensor = kornia.warp_affine(tensor, M, dsize=(h_original, w_original))

This operation returns a gradient, however it seems to be not correct. I used the warped_tensor and just the tensor for my loss and with the warped_tensor my network does not correctly optimise the weights.

Is this an autograd issue or is there some other issue I am not seeing here?

edgarriba commented 4 years ago

@mowolf thanks for the report. Do you have any code with your test so that we can take a look at it ? we recently updated this function using native torch operators like affine_grid. What version of kornia do you use ?

code reference https://github.com/kornia/kornia/blob/20a9acca2636512522116601ae09c6be0408b486/kornia/geometry/transform/imgwarp.py#L180

mowolf commented 4 years ago

@edgarriba Thanks for your fast answer! I now think that the gradient is correct but just loosing too much precision due to the transformation. Do you know which parameters must be in which range to prevent that?


I closed this issue, as this is just the problem of the operation in general not of kornia.

edgarriba commented 4 years ago

are you predicting directly the affine ? you can also parametrise from xy, scale , rotation ,shear, and then compose. Try to have normalized values, this will help