Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parameters values are not updated #7

Open
azzabenfarhat opened this issue Mar 7, 2022 · 3 comments
Open

parameters values are not updated #7

azzabenfarhat opened this issue Mar 7, 2022 · 3 comments

Comments

@azzabenfarhat
Copy link

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

@Edwinem
Copy link
Owner

Edwinem commented Apr 28, 2022

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)

@peirongxu
Copy link

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

@inkyusa
Copy link

inkyusa commented Feb 28, 2023

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants