| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | #include <algorithm> |
| | #include <cmath> |
| | #include <cstdio> |
| | #include <cstdlib> |
| | #include <string> |
| | #include <vector> |
| |
|
| | #include "bal_problem.h" |
| | #include "ceres/ceres.h" |
| | #include "gflags/gflags.h" |
| | #include "glog/logging.h" |
| | #include "snavely_reprojection_error.h" |
| |
|
| | |
| | |
| |
|
| | DEFINE_string(input, "", "Input File name"); |
| | DEFINE_string(trust_region_strategy, "levenberg_marquardt", |
| | "Options are: levenberg_marquardt, dogleg."); |
| | DEFINE_string(dogleg, "traditional_dogleg", "Options are: traditional_dogleg," |
| | "subspace_dogleg."); |
| |
|
| | DEFINE_bool(inner_iterations, false, "Use inner iterations to non-linearly " |
| | "refine each successful trust region step."); |
| |
|
| | DEFINE_string(blocks_for_inner_iterations, "automatic", "Options are: " |
| | "automatic, cameras, points, cameras,points, points,cameras"); |
| |
|
| | DEFINE_string(linear_solver, "sparse_schur", "Options are: " |
| | "sparse_schur, dense_schur, iterative_schur, sparse_normal_cholesky, " |
| | "dense_qr, dense_normal_cholesky and cgnr."); |
| | DEFINE_bool(explicit_schur_complement, false, "If using ITERATIVE_SCHUR " |
| | "then explicitly compute the Schur complement."); |
| | DEFINE_string(preconditioner, "jacobi", "Options are: " |
| | "identity, jacobi, schur_jacobi, cluster_jacobi, " |
| | "cluster_tridiagonal."); |
| | DEFINE_string(visibility_clustering, "canonical_views", |
| | "single_linkage, canonical_views"); |
| |
|
| | DEFINE_string(sparse_linear_algebra_library, "suite_sparse", |
| | "Options are: suite_sparse and cx_sparse."); |
| | DEFINE_string(dense_linear_algebra_library, "eigen", |
| | "Options are: eigen, lapack, and cuda"); |
| | DEFINE_string(ordering, "automatic", "Options are: automatic, user."); |
| |
|
| | DEFINE_bool(use_quaternions, false, "If true, uses quaternions to represent " |
| | "rotations. If false, angle axis is used."); |
| | DEFINE_bool(use_manifolds, false, "For quaternions, use a manifold."); |
| | DEFINE_bool(robustify, false, "Use a robust loss function."); |
| |
|
| | DEFINE_double(eta, 1e-2, "Default value for eta. Eta determines the " |
| | "accuracy of each linear solve of the truncated newton step. " |
| | "Changing this parameter can affect solve performance."); |
| |
|
| | DEFINE_int32(num_threads, 1, "Number of threads."); |
| | DEFINE_int32(num_iterations, 5, "Number of iterations."); |
| | DEFINE_double(max_solver_time, 1e32, "Maximum solve time in seconds."); |
| | DEFINE_bool(nonmonotonic_steps, false, "Trust region algorithm can use" |
| | " nonmonotic steps."); |
| |
|
| | DEFINE_double(rotation_sigma, 0.0, "Standard deviation of camera rotation " |
| | "perturbation."); |
| | DEFINE_double(translation_sigma, 0.0, "Standard deviation of the camera " |
| | "translation perturbation."); |
| | DEFINE_double(point_sigma, 0.0, "Standard deviation of the point " |
| | "perturbation."); |
| | DEFINE_int32(random_seed, 38401, "Random seed used to set the state " |
| | "of the pseudo random number generator used to generate " |
| | "the pertubations."); |
| | DEFINE_bool(line_search, false, "Use a line search instead of trust region " |
| | "algorithm."); |
| | DEFINE_bool(mixed_precision_solves, false, "Use mixed precision solves."); |
| | DEFINE_int32(max_num_refinement_iterations, 0, "Iterative refinement iterations"); |
| | DEFINE_string(initial_ply, "", "Export the BAL file data as a PLY file."); |
| | DEFINE_string(final_ply, "", "Export the refined BAL file data as a PLY " |
| | "file."); |
| |
|
| | |
| |
|
| | namespace ceres { |
| | namespace examples { |
| | namespace { |
| |
|
| | void SetLinearSolver(Solver::Options* options) { |
| | CHECK(StringToLinearSolverType(CERES_GET_FLAG(FLAGS_linear_solver), |
| | &options->linear_solver_type)); |
| | CHECK(StringToPreconditionerType(CERES_GET_FLAG(FLAGS_preconditioner), |
| | &options->preconditioner_type)); |
| | CHECK(StringToVisibilityClusteringType( |
| | CERES_GET_FLAG(FLAGS_visibility_clustering), |
| | &options->visibility_clustering_type)); |
| | CHECK(StringToSparseLinearAlgebraLibraryType( |
| | CERES_GET_FLAG(FLAGS_sparse_linear_algebra_library), |
| | &options->sparse_linear_algebra_library_type)); |
| | CHECK(StringToDenseLinearAlgebraLibraryType( |
| | CERES_GET_FLAG(FLAGS_dense_linear_algebra_library), |
| | &options->dense_linear_algebra_library_type)); |
| | options->use_explicit_schur_complement = |
| | CERES_GET_FLAG(FLAGS_explicit_schur_complement); |
| | options->use_mixed_precision_solves = |
| | CERES_GET_FLAG(FLAGS_mixed_precision_solves); |
| | options->max_num_refinement_iterations = |
| | CERES_GET_FLAG(FLAGS_max_num_refinement_iterations); |
| | } |
| |
|
| | void SetOrdering(BALProblem* bal_problem, Solver::Options* options) { |
| | const int num_points = bal_problem->num_points(); |
| | const int point_block_size = bal_problem->point_block_size(); |
| | double* points = bal_problem->mutable_points(); |
| |
|
| | const int num_cameras = bal_problem->num_cameras(); |
| | const int camera_block_size = bal_problem->camera_block_size(); |
| | double* cameras = bal_problem->mutable_cameras(); |
| |
|
| | if (options->use_inner_iterations) { |
| | if (CERES_GET_FLAG(FLAGS_blocks_for_inner_iterations) == "cameras") { |
| | LOG(INFO) << "Camera blocks for inner iterations"; |
| | options->inner_iteration_ordering.reset(new ParameterBlockOrdering); |
| | for (int i = 0; i < num_cameras; ++i) { |
| | options->inner_iteration_ordering->AddElementToGroup( |
| | cameras + camera_block_size * i, 0); |
| | } |
| | } else if (CERES_GET_FLAG(FLAGS_blocks_for_inner_iterations) == "points") { |
| | LOG(INFO) << "Point blocks for inner iterations"; |
| | options->inner_iteration_ordering.reset(new ParameterBlockOrdering); |
| | for (int i = 0; i < num_points; ++i) { |
| | options->inner_iteration_ordering->AddElementToGroup( |
| | points + point_block_size * i, 0); |
| | } |
| | } else if (CERES_GET_FLAG(FLAGS_blocks_for_inner_iterations) == |
| | "cameras,points") { |
| | LOG(INFO) << "Camera followed by point blocks for inner iterations"; |
| | options->inner_iteration_ordering.reset(new ParameterBlockOrdering); |
| | for (int i = 0; i < num_cameras; ++i) { |
| | options->inner_iteration_ordering->AddElementToGroup( |
| | cameras + camera_block_size * i, 0); |
| | } |
| | for (int i = 0; i < num_points; ++i) { |
| | options->inner_iteration_ordering->AddElementToGroup( |
| | points + point_block_size * i, 1); |
| | } |
| | } else if (CERES_GET_FLAG(FLAGS_blocks_for_inner_iterations) == |
| | "points,cameras") { |
| | LOG(INFO) << "Point followed by camera blocks for inner iterations"; |
| | options->inner_iteration_ordering.reset(new ParameterBlockOrdering); |
| | for (int i = 0; i < num_cameras; ++i) { |
| | options->inner_iteration_ordering->AddElementToGroup( |
| | cameras + camera_block_size * i, 1); |
| | } |
| | for (int i = 0; i < num_points; ++i) { |
| | options->inner_iteration_ordering->AddElementToGroup( |
| | points + point_block_size * i, 0); |
| | } |
| | } else if (CERES_GET_FLAG(FLAGS_blocks_for_inner_iterations) == |
| | "automatic") { |
| | LOG(INFO) << "Choosing automatic blocks for inner iterations"; |
| | } else { |
| | LOG(FATAL) << "Unknown block type for inner iterations: " |
| | << CERES_GET_FLAG(FLAGS_blocks_for_inner_iterations); |
| | } |
| | } |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | if (CERES_GET_FLAG(FLAGS_ordering) == "automatic") { |
| | return; |
| | } |
| |
|
| | auto* ordering = new ceres::ParameterBlockOrdering; |
| |
|
| | |
| | for (int i = 0; i < num_points; ++i) { |
| | ordering->AddElementToGroup(points + point_block_size * i, 0); |
| | } |
| |
|
| | for (int i = 0; i < num_cameras; ++i) { |
| | |
| | |
| | ordering->AddElementToGroup(cameras + camera_block_size * i, 1); |
| | } |
| |
|
| | options->linear_solver_ordering.reset(ordering); |
| | } |
| |
|
| | void SetMinimizerOptions(Solver::Options* options) { |
| | options->max_num_iterations = CERES_GET_FLAG(FLAGS_num_iterations); |
| | options->minimizer_progress_to_stdout = true; |
| | options->num_threads = CERES_GET_FLAG(FLAGS_num_threads); |
| | options->eta = CERES_GET_FLAG(FLAGS_eta); |
| | options->max_solver_time_in_seconds = CERES_GET_FLAG(FLAGS_max_solver_time); |
| | options->use_nonmonotonic_steps = CERES_GET_FLAG(FLAGS_nonmonotonic_steps); |
| | if (CERES_GET_FLAG(FLAGS_line_search)) { |
| | options->minimizer_type = ceres::LINE_SEARCH; |
| | } |
| |
|
| | CHECK(StringToTrustRegionStrategyType( |
| | CERES_GET_FLAG(FLAGS_trust_region_strategy), |
| | &options->trust_region_strategy_type)); |
| | CHECK( |
| | StringToDoglegType(CERES_GET_FLAG(FLAGS_dogleg), &options->dogleg_type)); |
| | options->use_inner_iterations = CERES_GET_FLAG(FLAGS_inner_iterations); |
| | } |
| |
|
| | void SetSolverOptionsFromFlags(BALProblem* bal_problem, |
| | Solver::Options* options) { |
| | SetMinimizerOptions(options); |
| | SetLinearSolver(options); |
| | SetOrdering(bal_problem, options); |
| | } |
| |
|
| | void BuildProblem(BALProblem* bal_problem, Problem* problem) { |
| | const int point_block_size = bal_problem->point_block_size(); |
| | const int camera_block_size = bal_problem->camera_block_size(); |
| | double* points = bal_problem->mutable_points(); |
| | double* cameras = bal_problem->mutable_cameras(); |
| |
|
| | |
| | |
| | |
| | const double* observations = bal_problem->observations(); |
| | for (int i = 0; i < bal_problem->num_observations(); ++i) { |
| | CostFunction* cost_function; |
| | |
| | |
| | cost_function = (CERES_GET_FLAG(FLAGS_use_quaternions)) |
| | ? SnavelyReprojectionErrorWithQuaternions::Create( |
| | observations[2 * i + 0], observations[2 * i + 1]) |
| | : SnavelyReprojectionError::Create( |
| | observations[2 * i + 0], observations[2 * i + 1]); |
| |
|
| | |
| | LossFunction* loss_function = |
| | CERES_GET_FLAG(FLAGS_robustify) ? new HuberLoss(1.0) : nullptr; |
| |
|
| | |
| | |
| | |
| | double* camera = |
| | cameras + camera_block_size * bal_problem->camera_index()[i]; |
| | double* point = points + point_block_size * bal_problem->point_index()[i]; |
| | problem->AddResidualBlock(cost_function, loss_function, camera, point); |
| | } |
| |
|
| | if (CERES_GET_FLAG(FLAGS_use_quaternions) && |
| | CERES_GET_FLAG(FLAGS_use_manifolds)) { |
| | Manifold* camera_manifold = |
| | new ProductManifold<QuaternionManifold, EuclideanManifold<6>>{}; |
| | for (int i = 0; i < bal_problem->num_cameras(); ++i) { |
| | problem->SetManifold(cameras + camera_block_size * i, camera_manifold); |
| | } |
| | } |
| | } |
| |
|
| | void SolveProblem(const char* filename) { |
| | BALProblem bal_problem(filename, CERES_GET_FLAG(FLAGS_use_quaternions)); |
| |
|
| | if (!CERES_GET_FLAG(FLAGS_initial_ply).empty()) { |
| | bal_problem.WriteToPLYFile(CERES_GET_FLAG(FLAGS_initial_ply)); |
| | } |
| |
|
| | Problem problem; |
| |
|
| | srand(CERES_GET_FLAG(FLAGS_random_seed)); |
| | bal_problem.Normalize(); |
| | bal_problem.Perturb(CERES_GET_FLAG(FLAGS_rotation_sigma), |
| | CERES_GET_FLAG(FLAGS_translation_sigma), |
| | CERES_GET_FLAG(FLAGS_point_sigma)); |
| |
|
| | BuildProblem(&bal_problem, &problem); |
| | Solver::Options options; |
| | SetSolverOptionsFromFlags(&bal_problem, &options); |
| | options.gradient_tolerance = 1e-16; |
| | options.function_tolerance = 1e-16; |
| | Solver::Summary summary; |
| | Solve(options, &problem, &summary); |
| | std::cout << summary.FullReport() << "\n"; |
| |
|
| | if (!CERES_GET_FLAG(FLAGS_final_ply).empty()) { |
| | bal_problem.WriteToPLYFile(CERES_GET_FLAG(FLAGS_final_ply)); |
| | } |
| | } |
| |
|
| | } |
| | } |
| | } |
| |
|
| | int main(int argc, char** argv) { |
| | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
| | google::InitGoogleLogging(argv[0]); |
| | if (CERES_GET_FLAG(FLAGS_input).empty()) { |
| | LOG(ERROR) << "Usage: bundle_adjuster --input=bal_problem"; |
| | return 1; |
| | } |
| |
|
| | CHECK(CERES_GET_FLAG(FLAGS_use_quaternions) || |
| | !CERES_GET_FLAG(FLAGS_use_manifolds)) |
| | << "--use_manifolds can only be used with --use_quaternions."; |
| | ceres::examples::SolveProblem(CERES_GET_FLAG(FLAGS_input).c_str()); |
| | return 0; |
| | } |
| |
|