nan Gradient in kornia.geometry.rotation_matrix_to_quaternion
See original GitHub issueDescribe the bug
When using torch.float32
the transformation from rotation matrices to quaternions outputs nan valued gradients. This is caused by trace + 1.0
(in sq = ...
) which might be slightly below zero, for single precision. It should be sq = torch.sqrt(trace + 1.0 + eps) * 2.0
instead.
def trace_positive_cond():
sq = torch.sqrt(trace + 1.0) * 2.0 # sq = 4 * qw.
qw = 0.25 * sq
qx = safe_zero_division(m21 - m12, sq)
qy = safe_zero_division(m02 - m20, sq)
qz = safe_zero_division(m10 - m01, sq)
if order == QuaternionCoeffOrder.XYZW:
return torch.cat((qx, qy, qz, qw), dim=-1)
return torch.cat((qw, qx, qy, qz), dim=-1)
Reproduction steps
import kornia
import torch
def main():
R = torch.tensor([[1, 0, 0],
[0, -1, 0],
[0, 0, -1]], dtype=torch.float32, requires_grad=True)
q = kornia.geometry.rotation_matrix_to_quaternion(R)
print(torch.autograd.grad(q.sum(), R))
if __name__ == '__main__':
main()
Expected behavior
The code above outputs a gradient including nan values. As the rotation matrix to quaternion transform includes safe divisions, it should not yield nan values.
(tensor([[ nan, 0.2500, 0.2500],
[ 0.2500, nan, -0.2500],
[ 0.2500, 0.2500, nan]]),)
Environment
python 3.8.10
pytorch 1.11.0
kornia 0.6.2
Additional context
No response
Issue Analytics
- State:
- Created 2 years ago
- Comments:6 (4 by maintainers)
Top Results From Across the Web
kornia.geometry.conversions - Read the Docs
Function that converts angles from radians to degrees. Parameters. tensor ( Tensor ) – Tensor of arbitrary shape. Return type. Tensor.
Read more >Untitled
Letras punto de cruz con flores, Theorem 3.7 geometry, Long island city real estate ... Hitched meaning in urdu, Gradients of perpendicular lines...
Read more >RoMa: A lightweight library to deal with 3D rotations in PyTorch.
While Kornia provides some utility functions to deal with 3D rotations, ... Not-a-Number only for 0.0 angle where gradient is mathematically undefined.
Read more >Pytorch sobel
Sobel Edges ¶ Once with the gradients in the two directions we can. ... development by creating an account on GitHub. class kornia.filters....
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
I have submitted the PR for adding the eps.
https://github.com/kornia/kornia/pull/1665
Solved in #1665