[Bug] convert_points_from_homogeneous - NaN gradients in backward pass
See original GitHub issueI 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:
- Created 4 years ago
- Reactions:10
- Comments:19 (10 by maintainers)
Top 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 >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
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
https://discuss.pytorch.org/t/gradients-of-torch-where/26835/2
my current workaround is:
thank you, @edgarriba