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

Support evaluation on residuals and Jacobian from pyceres.Problem #49

Merged
merged 8 commits into from
Jun 26, 2024

Conversation

B1ueber2y
Copy link
Member

Also support partial evaluation.

Example with the NormalCost:

x1 = np.zeros(3)
x2 = np.ones(3)
x1_prior = np.arange(3)
covar = np.eye(3)
prob = pyceres.Problem()
loss = pyceres.TrivialLoss()
prob.add_residual_block(pyceres.factors.NormalPrior(x1_prior, covar), loss, [x1])
prob.add_residual_block(pyceres.factors.NormalError(covar), loss, [x1, x2])
print(prob.evaluate_residuals())
# Output:
# [0.0, -1.0, -2.0, -1.0, -1.0, -1.0]

options = pyceres.SolverOptions()
summary = pyceres.SolverSummary()
pyceres.solve(options, prob, summary)

# Example: full evaluation
eval_options = pyceres.EvaluateOptions()
jacobian = prob.evaluate_jacobian(eval_options)
rows, cols, values = jacobian.to_tuple()
sparse_matrix = scipy.sparse.coo_matrix((values, (rows, cols)), shape=(6, 6))
print(sparse_matrix.toarray())
# Output:
# [[ 1.  0.  0.  0.  0.  0. ]
#  [ 0.  1.  0.  0.  0.  0. ]
#  [ 0.  0.  1.  0.  0.  0. ]
#  [ 1.  0.  0. -1.  0.  0. ]
#  [ 0.  1.  0.  0. -1.  0. ]
#  [ 0.  0.  1.  0.  0. -1. ]]

 # Example: partial evaluation
eval_options.set_parameter_blocks([x2])
jacobian = prob.evaluate_jacobian(eval_options)
rows, cols, values = jacobian.to_tuple()
sparse_matrix = scipy.sparse.coo_matrix((values, (rows, cols)), shape=(6, 3))
print(sparse_matrix.toarray())
# Output:
# [[  0.  0.  0. ]
#  [  0.  0.  0. ]
#  [  0.  0.  0. ]
#  [ -1.  0.  0. ]
#  [  0. -1.  0. ]
#  [  0.  0. -1. ]]

@B1ueber2y B1ueber2y requested a review from sarlinpe June 20, 2024 21:24
_pyceres/core/crs_matrix.h Show resolved Hide resolved
_pyceres/core/crs_matrix.h Outdated Show resolved Hide resolved
_pyceres/core/crs_matrix.h Outdated Show resolved Hide resolved
_pyceres/core/crs_matrix.h Show resolved Hide resolved
_pyceres/core/crs_matrix.h Outdated Show resolved Hide resolved
@B1ueber2y
Copy link
Member Author

Thanks. I have made an update.

@B1ueber2y B1ueber2y requested a review from sarlinpe June 26, 2024 07:50
@sarlinpe sarlinpe merged commit 4fce758 into cvg:main Jun 26, 2024
6 checks passed
@B1ueber2y B1ueber2y deleted the features/evaluate_jacobian branch June 28, 2024 15:10
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

Successfully merging this pull request may close these issues.

2 participants