123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726 |
- #include "ceres/evaluator.h"
- #include <memory>
- #include <string>
- #include <vector>
- #include "ceres/casts.h"
- #include "ceres/cost_function.h"
- #include "ceres/crs_matrix.h"
- #include "ceres/evaluator_test_utils.h"
- #include "ceres/internal/eigen.h"
- #include "ceres/manifold.h"
- #include "ceres/problem_impl.h"
- #include "ceres/program.h"
- #include "ceres/sized_cost_function.h"
- #include "ceres/sparse_matrix.h"
- #include "ceres/stringprintf.h"
- #include "ceres/types.h"
- #include "gtest/gtest.h"
- namespace ceres {
- namespace internal {
- template <int kFactor, int kNumResiduals, int... Ns>
- class ParameterIgnoringCostFunction
- : public SizedCostFunction<kNumResiduals, Ns...> {
- using Base = SizedCostFunction<kNumResiduals, Ns...>;
- public:
- explicit ParameterIgnoringCostFunction(bool succeeds = true)
- : succeeds_(succeeds) {}
- bool Evaluate(double const* const* parameters,
- double* residuals,
- double** jacobians) const final {
- for (int i = 0; i < Base::num_residuals(); ++i) {
- residuals[i] = i + 1;
- }
- if (jacobians) {
- for (int k = 0; k < Base::parameter_block_sizes().size(); ++k) {
-
-
-
-
-
-
-
-
-
-
-
-
- if (jacobians[k] != nullptr) {
- MatrixRef jacobian(jacobians[k],
- Base::num_residuals(),
- Base::parameter_block_sizes()[k]);
- for (int j = 0; j < Base::parameter_block_sizes()[k]; ++j) {
- jacobian.col(j).setConstant(kFactor * (j + 1));
- }
- }
- }
- }
- return succeeds_;
- }
- private:
- bool succeeds_;
- };
- struct EvaluatorTestOptions {
- EvaluatorTestOptions(LinearSolverType linear_solver_type,
- int num_eliminate_blocks,
- bool dynamic_sparsity = false)
- : linear_solver_type(linear_solver_type),
- num_eliminate_blocks(num_eliminate_blocks),
- dynamic_sparsity(dynamic_sparsity) {}
- LinearSolverType linear_solver_type;
- int num_eliminate_blocks;
- bool dynamic_sparsity;
- };
- struct EvaluatorTest : public ::testing::TestWithParam<EvaluatorTestOptions> {
- std::unique_ptr<Evaluator> CreateEvaluator(Program* program) {
-
-
- program->SetParameterOffsetsAndIndex();
- if (VLOG_IS_ON(1)) {
- std::string report;
- StringAppendF(&report,
- "Creating evaluator with type: %d",
- GetParam().linear_solver_type);
- if (GetParam().linear_solver_type == SPARSE_NORMAL_CHOLESKY) {
- StringAppendF(
- &report, ", dynamic_sparsity: %d", GetParam().dynamic_sparsity);
- }
- StringAppendF(&report,
- " and num_eliminate_blocks: %d",
- GetParam().num_eliminate_blocks);
- VLOG(1) << report;
- }
- Evaluator::Options options;
- options.linear_solver_type = GetParam().linear_solver_type;
- options.num_eliminate_blocks = GetParam().num_eliminate_blocks;
- options.dynamic_sparsity = GetParam().dynamic_sparsity;
- options.context = problem.context();
- std::string error;
- return Evaluator::Create(options, program, &error);
- }
- void EvaluateAndCompare(ProblemImpl* problem,
- int expected_num_rows,
- int expected_num_cols,
- double expected_cost,
- const double* expected_residuals,
- const double* expected_gradient,
- const double* expected_jacobian) {
- std::unique_ptr<Evaluator> evaluator =
- CreateEvaluator(problem->mutable_program());
- int num_residuals = expected_num_rows;
- int num_parameters = expected_num_cols;
- double cost = -1;
- Vector residuals(num_residuals);
- residuals.setConstant(-2000);
- Vector gradient(num_parameters);
- gradient.setConstant(-3000);
- std::unique_ptr<SparseMatrix> jacobian(evaluator->CreateJacobian());
- ASSERT_EQ(expected_num_rows, evaluator->NumResiduals());
- ASSERT_EQ(expected_num_cols, evaluator->NumEffectiveParameters());
- ASSERT_EQ(expected_num_rows, jacobian->num_rows());
- ASSERT_EQ(expected_num_cols, jacobian->num_cols());
- std::vector<double> state(evaluator->NumParameters());
-
- ASSERT_TRUE(evaluator->Evaluate(
- &state[0],
- &cost,
- expected_residuals != nullptr ? &residuals[0] : nullptr,
- expected_gradient != nullptr ? &gradient[0] : nullptr,
- expected_jacobian != nullptr ? jacobian.get() : nullptr));
-
- Matrix actual_jacobian;
- if (expected_jacobian != nullptr) {
- jacobian->ToDenseMatrix(&actual_jacobian);
- }
- CompareEvaluations(expected_num_rows,
- expected_num_cols,
- expected_cost,
- expected_residuals,
- expected_gradient,
- expected_jacobian,
- cost,
- &residuals[0],
- &gradient[0],
- actual_jacobian.data());
- }
-
- void CheckAllEvaluationCombinations(const ExpectedEvaluation& expected) {
- for (int i = 0; i < 8; ++i) {
- EvaluateAndCompare(&problem,
- expected.num_rows,
- expected.num_cols,
- expected.cost,
- (i & 1) ? expected.residuals : nullptr,
- (i & 2) ? expected.gradient : nullptr,
- (i & 4) ? expected.jacobian : nullptr);
- }
- }
-
- double x[2];
- double y[3];
- double z[4];
- ProblemImpl problem;
- };
- static void SetSparseMatrixConstant(SparseMatrix* sparse_matrix, double value) {
- VectorRef(sparse_matrix->mutable_values(), sparse_matrix->num_nonzeros())
- .setConstant(value);
- }
- TEST_P(EvaluatorTest, SingleResidualProblem) {
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<1, 3, 2, 3, 4>, nullptr, x, y, z);
-
- ExpectedEvaluation expected = {
-
- 3, 9,
-
- 7.0,
-
- { 1.0, 2.0, 3.0 },
-
- { 6.0, 12.0,
- 6.0, 12.0, 18.0,
- 6.0, 12.0, 18.0, 24.0,
- },
-
-
- { 1, 2, 1, 2, 3, 1, 2, 3, 4,
- 1, 2, 1, 2, 3, 1, 2, 3, 4,
- 1, 2, 1, 2, 3, 1, 2, 3, 4
- }
- };
-
- CheckAllEvaluationCombinations(expected);
- }
- TEST_P(EvaluatorTest, SingleResidualProblemWithPermutedParameters) {
-
- problem.AddParameterBlock(x, 2);
- problem.AddParameterBlock(y, 3);
- problem.AddParameterBlock(z, 4);
-
-
-
-
-
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<1, 3, 4, 3, 2>, nullptr, z, y, x);
-
- ExpectedEvaluation expected = {
-
- 3, 9,
-
- 7.0,
-
- { 1.0, 2.0, 3.0 },
-
- { 6.0, 12.0,
- 6.0, 12.0, 18.0,
- 6.0, 12.0, 18.0, 24.0,
- },
-
-
- { 1, 2, 1, 2, 3, 1, 2, 3, 4,
- 1, 2, 1, 2, 3, 1, 2, 3, 4,
- 1, 2, 1, 2, 3, 1, 2, 3, 4
- }
- };
-
- CheckAllEvaluationCombinations(expected);
- }
- TEST_P(EvaluatorTest, SingleResidualProblemWithNuisanceParameters) {
-
- double a[2];
- double b[1];
- double c[1];
- double d[3];
-
-
- problem.AddParameterBlock(a, 2);
- problem.AddParameterBlock(x, 2);
- problem.AddParameterBlock(b, 1);
- problem.AddParameterBlock(y, 3);
- problem.AddParameterBlock(c, 1);
- problem.AddParameterBlock(z, 4);
- problem.AddParameterBlock(d, 3);
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<1, 3, 2, 3, 4>, nullptr, x, y, z);
-
- ExpectedEvaluation expected = {
-
- 3, 16,
-
- 7.0,
-
- { 1.0, 2.0, 3.0 },
-
- { 0.0, 0.0,
- 6.0, 12.0,
- 0.0,
- 6.0, 12.0, 18.0,
- 0.0,
- 6.0, 12.0, 18.0, 24.0,
- 0.0, 0.0, 0.0,
- },
-
-
- { 0, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 0, 0,
- 0, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 0, 0,
- 0, 0, 1, 2, 0, 1, 2, 3, 0, 1, 2, 3, 4, 0, 0, 0
- }
- };
-
- CheckAllEvaluationCombinations(expected);
- }
- TEST_P(EvaluatorTest, MultipleResidualProblem) {
-
- problem.AddParameterBlock(x, 2);
- problem.AddParameterBlock(y, 3);
- problem.AddParameterBlock(z, 4);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<1, 2, 2, 3>, nullptr, x, y);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<2, 3, 2, 4>, nullptr, x, z);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<3, 4, 3, 4>, nullptr, y, z);
-
- ExpectedEvaluation expected = {
-
- 9, 9,
-
-
- ( 1 + 4 + 1 + 4 + 9 + 1 + 4 + 9 + 16) / 2.0,
-
- { 1.0, 2.0,
- 1.0, 2.0, 3.0,
- 1.0, 2.0, 3.0, 4.0
- },
-
- { 15.0, 30.0,
- 33.0, 66.0, 99.0,
- 42.0, 84.0, 126.0, 168.0
- },
-
-
- { 1, 2, 1, 2, 3, 0, 0, 0, 0,
- 1, 2, 1, 2, 3, 0, 0, 0, 0,
- 2, 4, 0, 0, 0, 2, 4, 6, 8,
- 2, 4, 0, 0, 0, 2, 4, 6, 8,
- 2, 4, 0, 0, 0, 2, 4, 6, 8,
- 0, 0, 3, 6, 9, 3, 6, 9, 12,
- 0, 0, 3, 6, 9, 3, 6, 9, 12,
- 0, 0, 3, 6, 9, 3, 6, 9, 12,
- 0, 0, 3, 6, 9, 3, 6, 9, 12
- }
- };
-
- CheckAllEvaluationCombinations(expected);
- }
- TEST_P(EvaluatorTest, MultipleResidualsWithManifolds) {
-
- problem.AddParameterBlock(x, 2);
-
- std::vector<int> y_fixed;
- y_fixed.push_back(0);
- problem.AddParameterBlock(y, 3, new SubsetManifold(3, y_fixed));
-
- std::vector<int> z_fixed;
- z_fixed.push_back(1);
- problem.AddParameterBlock(z, 4, new SubsetManifold(4, z_fixed));
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<1, 2, 2, 3>, nullptr, x, y);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<2, 3, 2, 4>, nullptr, x, z);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<3, 4, 3, 4>, nullptr, y, z);
-
- ExpectedEvaluation expected = {
-
- 9, 7,
-
-
- ( 1 + 4 + 1 + 4 + 9 + 1 + 4 + 9 + 16) / 2.0,
-
- { 1.0, 2.0,
- 1.0, 2.0, 3.0,
- 1.0, 2.0, 3.0, 4.0
- },
-
- { 15.0, 30.0,
- 66.0, 99.0,
- 42.0, 126.0, 168.0
- },
-
-
- { 1, 2, 2, 3, 0, 0, 0,
- 1, 2, 2, 3, 0, 0, 0,
- 2, 4, 0, 0, 2, 6, 8,
- 2, 4, 0, 0, 2, 6, 8,
- 2, 4, 0, 0, 2, 6, 8,
- 0, 0, 6, 9, 3, 9, 12,
- 0, 0, 6, 9, 3, 9, 12,
- 0, 0, 6, 9, 3, 9, 12,
- 0, 0, 6, 9, 3, 9, 12
- }
- };
-
- CheckAllEvaluationCombinations(expected);
- }
- TEST_P(EvaluatorTest, MultipleResidualProblemWithSomeConstantParameters) {
-
- double x[2];
- double y[3];
- double z[4];
-
- problem.AddParameterBlock(x, 2);
- problem.AddParameterBlock(y, 3);
- problem.AddParameterBlock(z, 4);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<1, 2, 2, 3>, nullptr, x, y);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<2, 3, 2, 4>, nullptr, x, z);
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<3, 4, 3, 4>, nullptr, y, z);
-
- problem.SetParameterBlockConstant(z);
-
-
-
- Program reduced_program;
- std::vector<ParameterBlock*>* parameter_blocks =
- problem.mutable_program()->mutable_parameter_blocks();
-
-
-
- ParameterBlock* parameter_block_z = parameter_blocks->back();
- parameter_blocks->pop_back();
-
- ExpectedEvaluation expected = {
-
- 9, 5,
-
-
- ( 1 + 4 + 1 + 4 + 9 + 1 + 4 + 9 + 16) / 2.0,
-
- { 1.0, 2.0,
- 1.0, 2.0, 3.0,
- 1.0, 2.0, 3.0, 4.0
- },
-
- { 15.0, 30.0,
- 33.0, 66.0, 99.0,
- },
-
-
- { 1, 2, 1, 2, 3,
- 1, 2, 1, 2, 3,
- 2, 4, 0, 0, 0,
- 2, 4, 0, 0, 0,
- 2, 4, 0, 0, 0,
- 0, 0, 3, 6, 9,
- 0, 0, 3, 6, 9,
- 0, 0, 3, 6, 9,
- 0, 0, 3, 6, 9
- }
- };
-
- CheckAllEvaluationCombinations(expected);
-
- parameter_blocks->push_back(parameter_block_z);
- }
- TEST_P(EvaluatorTest, EvaluatorAbortsForResidualsThatFailToEvaluate) {
-
- problem.AddResidualBlock(
- new ParameterIgnoringCostFunction<20, 3, 2, 3, 4>(false),
- nullptr,
- x,
- y,
- z);
-
- double state[9];
- std::unique_ptr<Evaluator> evaluator =
- CreateEvaluator(problem.mutable_program());
- std::unique_ptr<SparseMatrix> jacobian(evaluator->CreateJacobian());
- double cost;
- EXPECT_FALSE(evaluator->Evaluate(state, &cost, nullptr, nullptr, nullptr));
- }
- INSTANTIATE_TEST_SUITE_P(
- LinearSolvers,
- EvaluatorTest,
- ::testing::Values(EvaluatorTestOptions(DENSE_QR, 0),
- EvaluatorTestOptions(DENSE_SCHUR, 0),
- EvaluatorTestOptions(DENSE_SCHUR, 1),
- EvaluatorTestOptions(DENSE_SCHUR, 2),
- EvaluatorTestOptions(DENSE_SCHUR, 3),
- EvaluatorTestOptions(DENSE_SCHUR, 4),
- EvaluatorTestOptions(SPARSE_SCHUR, 0),
- EvaluatorTestOptions(SPARSE_SCHUR, 1),
- EvaluatorTestOptions(SPARSE_SCHUR, 2),
- EvaluatorTestOptions(SPARSE_SCHUR, 3),
- EvaluatorTestOptions(SPARSE_SCHUR, 4),
- EvaluatorTestOptions(ITERATIVE_SCHUR, 0),
- EvaluatorTestOptions(ITERATIVE_SCHUR, 1),
- EvaluatorTestOptions(ITERATIVE_SCHUR, 2),
- EvaluatorTestOptions(ITERATIVE_SCHUR, 3),
- EvaluatorTestOptions(ITERATIVE_SCHUR, 4),
- EvaluatorTestOptions(SPARSE_NORMAL_CHOLESKY, 0, false),
- EvaluatorTestOptions(SPARSE_NORMAL_CHOLESKY, 0, true)));
- class ParameterSensitiveCostFunction : public SizedCostFunction<2, 2> {
- public:
- bool Evaluate(double const* const* parameters,
- double* residuals,
- double** jacobians) const final {
- double x1 = parameters[0][0];
- double x2 = parameters[0][1];
- residuals[0] = x1 * x1;
- residuals[1] = x2 * x2;
- if (jacobians != nullptr) {
- double* jacobian = jacobians[0];
- if (jacobian != nullptr) {
- jacobian[0] = 2.0 * x1;
- jacobian[1] = 0.0;
- jacobian[2] = 0.0;
- jacobian[3] = 2.0 * x2;
- }
- }
- return true;
- }
- };
- TEST(Evaluator, EvaluatorRespectsParameterChanges) {
- ProblemImpl problem;
- double x[2];
- x[0] = 1.0;
- x[1] = 1.0;
- problem.AddResidualBlock(new ParameterSensitiveCostFunction(), nullptr, x);
- Program* program = problem.mutable_program();
- program->SetParameterOffsetsAndIndex();
- Evaluator::Options options;
- options.linear_solver_type = DENSE_QR;
- options.num_eliminate_blocks = 0;
- options.context = problem.context();
- std::string error;
- std::unique_ptr<Evaluator> evaluator(
- Evaluator::Create(options, program, &error));
- std::unique_ptr<SparseMatrix> jacobian(evaluator->CreateJacobian());
- ASSERT_EQ(2, jacobian->num_rows());
- ASSERT_EQ(2, jacobian->num_cols());
- double state[2];
- state[0] = 2.0;
- state[1] = 3.0;
-
-
-
-
-
- {
- double cost = -1;
- ASSERT_TRUE(evaluator->Evaluate(state, &cost, nullptr, nullptr, nullptr));
- EXPECT_EQ(48.5, cost);
- }
-
- {
- double cost = -1;
- double residuals[2] = {-2, -2};
- ASSERT_TRUE(evaluator->Evaluate(state, &cost, residuals, nullptr, nullptr));
- EXPECT_EQ(48.5, cost);
- EXPECT_EQ(4, residuals[0]);
- EXPECT_EQ(9, residuals[1]);
- }
-
- {
- double cost = -1;
- double residuals[2] = {-2, -2};
- SetSparseMatrixConstant(jacobian.get(), -1);
- ASSERT_TRUE(
- evaluator->Evaluate(state, &cost, residuals, nullptr, jacobian.get()));
- EXPECT_EQ(48.5, cost);
- EXPECT_EQ(4, residuals[0]);
- EXPECT_EQ(9, residuals[1]);
- Matrix actual_jacobian;
- jacobian->ToDenseMatrix(&actual_jacobian);
- Matrix expected_jacobian(2, 2);
- expected_jacobian << 2 * state[0], 0, 0, 2 * state[1];
- EXPECT_TRUE((actual_jacobian.array() == expected_jacobian.array()).all())
- << "Actual:\n"
- << actual_jacobian << "\nExpected:\n"
- << expected_jacobian;
- }
- }
- class HugeCostFunction : public SizedCostFunction<46341, 46345> {
- bool Evaluate(double const* const* parameters,
- double* residuals,
- double** jacobians) const override {
- return true;
- }
- };
- TEST(Evaluator, LargeProblemDoesNotCauseCrashBlockJacobianWriter) {
- ProblemImpl problem;
- std::vector<double> x(46345);
- problem.AddResidualBlock(new HugeCostFunction, nullptr, x.data());
- Evaluator::Options options;
- options.linear_solver_type = SPARSE_NORMAL_CHOLESKY;
- options.context = problem.context();
- options.num_eliminate_blocks = 0;
- options.dynamic_sparsity = false;
- std::string error;
- auto program = problem.mutable_program();
- program->SetParameterOffsetsAndIndex();
- auto evaluator = Evaluator::Create(options, program, &error);
- auto jacobian = evaluator->CreateJacobian();
- EXPECT_EQ(jacobian, nullptr);
- }
- TEST(Evaluator, LargeProblemDoesNotCauseCrashCompressedRowJacobianWriter) {
- ProblemImpl problem;
- std::vector<double> x(46345);
- problem.AddResidualBlock(new HugeCostFunction, nullptr, x.data());
- Evaluator::Options options;
-
-
- options.linear_solver_type = CGNR;
- options.sparse_linear_algebra_library_type = CUDA_SPARSE;
- options.context = problem.context();
- options.num_eliminate_blocks = 0;
- std::string error;
- auto program = problem.mutable_program();
- program->SetParameterOffsetsAndIndex();
- auto evaluator = Evaluator::Create(options, program, &error);
- auto jacobian = evaluator->CreateJacobian();
- EXPECT_EQ(jacobian, nullptr);
- }
- }
- }
|