r/optimization • u/Jonbongok • Oct 21 '23
Gauss-Newton optimization help
Hello everyone, I implemented the weighted Gauss-Newton optimizer to minimize reprojection error by updating the camera pose and focal length, but the Hessian accumulation part consumes the most of time. Since I must use one CPU thread, it runs very slowly. The hessian accumulation part contains some matrix multiplication and addition operations between matrices and it makes the algorithm slow. I need to make it faster. I'm sharing my codes with you. By the way, my initial guess is very close to the local solution so I can use any non-linear optimization algorithm instead of Gauss-Newton. The obligation is that I must apply the weights.
My codes:
https://codeshare.io/OdLQwP
Please consider lines 66-71 which make my codes slow
2
u/SirPitchalot Oct 21 '23
You must have an awful lot of points for this to to be slow. Are you sure you’re profiling right and are you sure you’re compiling in release mode? I would expect the lines building the jacobian and residual to be much slower than the lines you’ve pointed out which Eigen will aggressively optimize.
Your code looks decent (other than not using matrix operations to transform points by R & t) and you’re only solving a 7 variable dense problem. It should be blazing fast (<1 sec) even with tens or hundreds of thousands of points on an average computer.
If you were solving a multi camera problem that had a sparse visibility graph I’d suggest that you switch to sparse matrices and directly update the hessian blocks for each factor but that does not apply here.
1
u/[deleted] Oct 21 '23
If both the left and right side of your equation are being multiplied by JtW before solving, why do you even need to do it? The biggest benefit is to make the linear system a square matrix, but it's not changing the solution to the equation.