pyceres icon indicating copy to clipboard operation
pyceres copied to clipboard

ceres GradientChecker and ceres AutoDiffCostFunction

Open xuq9908 opened this issue 1 year ago • 7 comments

Hi,

Does this package bind these 2 functions into python? I failed to find them. if it is not included, is there any replacement methods recommended in python?

Thank you for help!

xuq9908 avatar Feb 25 '24 23:02 xuq9908

  • GradientChecker is not bound yet but this shouldn't be difficult, you are welcome to contribute it. You would likely need to also bind GradientChecker::ProbeResults and NumericDiffOptions.
  • We don't bind AutoDiffCostFunction because Ceres cannot auto-diff through Python code. It is best to perform the auto-diff on the Python side, e.g. with a deep learning framework like PyTorch.

sarlinpe avatar Feb 26 '24 08:02 sarlinpe

i saw this from v1.0 https://github.com/cvg/pyceres/blob/179883ec69b0acff7bb2e96eb22cca9390c1404d/_pyceres/factors/bundle.cc#L80 so the AutoDiffCostFunction is removed becasue of this?

We don't bind AutoDiffCostFunction because Ceres cannot auto-diff through Python code.

Thanks!

colin-de avatar Mar 11 '24 14:03 colin-de

An AutoDiffCostFunction needs to be defined in C++ because Ceres cannot differentiate Python code. The code that you link is irrelevant - the implementation of the main cost functions was simply move from pyceres to pycolmap to reduce the dependencies of the core pyceres library.

sarlinpe avatar Mar 11 '24 14:03 sarlinpe

And is the geometry of Sophus not going to be suported in Pyceres in the future?

colin-de avatar Mar 11 '24 14:03 colin-de

So far we've been relying on the geometry objects available in COLMAP. We could consider adding support for Sophus objects if there is a need for minimal dependencies within pyceres.

sarlinpe avatar Apr 24 '24 14:04 sarlinpe

As @sarlinpe mentioned, it may be useful to use torch's autograd for some cases. Here is a simple example for curve fitting. https://github.com/inkyusa/pyceres/blob/main/examples/test_curve_fitting_torch_autograd.ipynb Hope this help or useful somehow. Cheers,

PS: Haven't tested for a large scale problem (i.e., large parameter block size), maybe using GPU benefical in this case?

inkyusa avatar Jul 26 '24 05:07 inkyusa

Nice, thanks for sharing! For larger problems, you would likely need to pre-compute the forward and backward passes for all residuals and parameters in parallel (e.g. in an evaluation callback) and simply lookup their values in CostFunction.Evaluate. It would be nice to have an example on how to do this.

sarlinpe avatar Jul 31 '24 14:07 sarlinpe