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.

[Bug] convert_points_from_homogeneous - NaN gradients in backward pass

See original GitHub issue

I just experienced a NaN-gradient problem while doing a backward pass here: https://github.com/kornia/kornia/blob/4b0ae70f7f806c5eff5ab87f8b1f2d9ab4ff1e45/kornia/geometry/conversions.py#L99

torch.where works absolutely fine, but if you have zero divisions you find yourself with NaN-gradients for sure 💩

Here is a toy example:

eps = 1e-8

z_vec: torch.Tensor = torch.tensor([4., 6., 0., -3., 1e-9], requires_grad=True)

scale: torch.Tensor = torch.where(
    torch.abs(z_vec) > eps,
    torch.tensor(1.) / z_vec,
    torch.ones_like(z_vec)
)
scale.backward(torch.ones_like(scale))

And these are z_vec gradients: tensor([-0.0625, -0.0278, nan, -0.1111, -0.0000])

For now my little hack is:

...
    # we check for points at infinity
    z_vec: torch.Tensor = points[..., -1:]
    if z_vec.requires_grad:
        def z_vec_backward_hook(grad: torch.Tensor) -> torch.Tensor:
            grad[grad != grad] = 0.
            return grad
        z_vec.register_hook(z_vec_backward_hook)
...

But not sure if it’s good enough.

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:10
  • Comments:19 (10 by maintainers)

github_iconTop GitHub Comments

2reactions
edgarribacommented, Dec 11, 2019

Yes, please open a separated issue. Will close this once we merge #369 Regarding this new issue, I would give a try at this code from tf graphics, https://github.com/tensorflow/graphics/blob/master/tensorflow_graphics/geometry/transformation/rotation_matrix_3d.py since at least it’s more clean

2reactions
poxyucommented, Dec 8, 2019

https://discuss.pytorch.org/t/gradients-of-torch-where/26835/2

my current workaround is:

scale: torch.Tensor = torch.where(
    torch.abs(z_vec) > eps,
#        torch.tensor(1.) / z_vec,
    torch.tensor(1.) / z_vec.masked_fill(z_vec == 0., eps),
    torch.ones_like(z_vec))

thank you, @edgarriba

Read more comments on GitHub >

github_iconTop Results From Across the Web

Evision v0.1.25 - HexDocs
Constructs the image pyramid which can be passed to calcOpticalFlowPyrLK. ... Calculates the minimal eigenvalue of gradient matrices for corner detection.
Read more >
ropencv/opencv.yml at master · D-Alex/ropencv - GitHub
The type `Scalar` is widely used in OpenCV to pass ... Backward conversion from `Mat` to `CvMat` or `IplImage` is provided ... cv::triangulatePoints:...
Read more >
Cv2 Class - GitHub Pages
Finds edges in an image using the Canny algorithm with custom image gradient. Public method Static member, Canny(InputArray, OutputArray, Double, Double, ...
Read more >
OpenCvSharp.Cv2 - FuGet.org
public static void ConvertPointsFromHomogeneous(InputArray src, ... to the gradient field inside the selection and then integrating back ...
Read more >
The OpenCV Reference Manual Release 2.4.9.0
The type Scalar is widely used in OpenCV to pass pixel values. ... Backward conversion from Mat to CvMat or IplImage is provided...
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