ceres_python_bindings icon indicating copy to clipboard operation
ceres_python_bindings copied to clipboard

parameters values are not updated

Open azzabenfarhat opened this issue 2 years ago • 3 comments

Hi,

I'm having a problem getting the final values of parameters. I defined a custom cost function (PyCeres.CostFunction) and used options.linear_solver_type = PyCeres.LinearSolverType.DENSE_QR, with options = PyCeres.SolverOptions(). I also define an initial_x and parameters = np.array(initial_x). However, once the solver converges, the value of parameters is equal to its initial value. I added a print in the cost function inside the cost function and I could see the different values of parameters evaluated at each iteration.

Is there a way to get the final values of parameters once the problem is solved?

Thanks, Azza

azzabenfarhat avatar Mar 07 '22 16:03 azzabenfarhat

I don't have enough information to go off of. The example https://github.com/Edwinem/ceres_python_bindings/blob/master/examples/ceres_hello_world_analytic_diff.py works on my computer so I would say try to copy that as much as possible.

Some potential problems I can think of:

  • You are overwriting the the jacobian/residuals in the cost function rather than setting the values.
  • You are creating the cost functions parameters in a local scope(e.g. a for loop). Depending on how you do it you might be creating a unique numpy array to that local scope, which will be different then the numpy array you want to optimize. So at the end it ends up optimizing the local scope variable, but when you print the answer it shows a different numpy array.

Maybe something like this?

np_arr_to_optimize = np.array( [0.5])
problem = PyCeres.Problem()
for a in list:
   np_arr_to_optimize = np.array( [a.val])
   problem.AddResidualBlock(cost_function, None, np_arr_to_optimize)

Edwinem avatar Apr 28 '22 06:04 Edwinem

I had the same issue with you. I found it is due to initial_x was set as int type. For example initial_x = 5 Instead, you should set it like initial_x = 5.0

peirongxu avatar Nov 02 '22 01:11 peirongxu

All internal computations expect "double" data type as input.

The author may add some casting into ParseNumpyData. Temporarily, np_params = np_params.astype(np.float64) if np_params.dtype != np.float32 else np_params or np_params = np.array(params, dtype=np.float64)

works for me.

inkyusa avatar Feb 28 '23 06:02 inkyusa