question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

nan Gradient in kornia.geometry.rotation_matrix_to_quaternion

See original GitHub issue

Describe 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:closed
  • Created 2 years ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
simon-schaefercommented, Mar 26, 2022

I have submitted the PR for adding the eps.
https://github.com/kornia/kornia/pull/1665

0reactions
ducha-aikicommented, Sep 29, 2022

Solved in #1665

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found