From ebd50af044e7eb335e2598fff87ee506b0ff028e Mon Sep 17 00:00:00 2001 From: Linfei Pan <36349740+lpanaf@users.noreply.github.com> Date: Fri, 13 Dec 2024 15:10:26 +0100 Subject: [PATCH] fix the bug for threshold update (#148) * fix the bug that the update of threshold from command line is not reflected in the structure * f * update * refactor the loss_function * d --- glomap/estimators/bundle_adjustment.cc | 3 ++- glomap/estimators/bundle_adjustment.h | 6 +++++- glomap/estimators/global_positioning.cc | 14 +++++++------- glomap/estimators/global_positioning.h | 6 +++++- glomap/estimators/gravity_refinement.cc | 5 +++-- glomap/estimators/gravity_refinement.h | 7 +++++-- glomap/estimators/optimization_base.h | 3 --- glomap/estimators/view_graph_calibration.cc | 7 +++---- glomap/estimators/view_graph_calibration.h | 5 +++++ pybind11 | 1 + pyglomap/pybind11 | 1 + 11 files changed, 37 insertions(+), 21 deletions(-) create mode 160000 pybind11 create mode 160000 pyglomap/pybind11 diff --git a/glomap/estimators/bundle_adjustment.cc b/glomap/estimators/bundle_adjustment.cc index f9aa51c..ee2be7a 100644 --- a/glomap/estimators/bundle_adjustment.cc +++ b/glomap/estimators/bundle_adjustment.cc @@ -54,6 +54,7 @@ void BundleAdjuster::Reset() { ceres::Problem::Options problem_options; problem_options.loss_function_ownership = ceres::DO_NOT_TAKE_OWNERSHIP; problem_ = std::make_unique(problem_options); + loss_function_ = options_.CreateLossFunction(); } void BundleAdjuster::AddPointToCameraConstraints( @@ -77,7 +78,7 @@ void BundleAdjuster::AddPointToCameraConstraints( if (cost_function != nullptr) { problem_->AddResidualBlock( cost_function, - options_.loss_function.get(), + loss_function_.get(), image.cam_from_world.rotation.coeffs().data(), image.cam_from_world.translation.data(), tracks[track_id].xyz.data(), diff --git a/glomap/estimators/bundle_adjustment.h b/glomap/estimators/bundle_adjustment.h index 97419a5..76a81b9 100644 --- a/glomap/estimators/bundle_adjustment.h +++ b/glomap/estimators/bundle_adjustment.h @@ -21,9 +21,12 @@ struct BundleAdjusterOptions : public OptimizationBaseOptions { BundleAdjusterOptions() : OptimizationBaseOptions() { thres_loss_function = 1.; - loss_function = std::make_shared(thres_loss_function); solver_options.max_num_iterations = 200; } + + std::shared_ptr CreateLossFunction() { + return std::make_shared(thres_loss_function); + } }; class BundleAdjuster { @@ -65,6 +68,7 @@ class BundleAdjuster { BundleAdjusterOptions options_; std::unique_ptr problem_; + std::shared_ptr loss_function_; }; } // namespace glomap diff --git a/glomap/estimators/global_positioning.cc b/glomap/estimators/global_positioning.cc index b18fa80..8a9b064 100644 --- a/glomap/estimators/global_positioning.cc +++ b/glomap/estimators/global_positioning.cc @@ -87,6 +87,8 @@ void GlobalPositioner::SetupProblem( ceres::Problem::Options problem_options; problem_options.loss_function_ownership = ceres::DO_NOT_TAKE_OWNERSHIP; problem_ = std::make_unique(problem_options); + loss_function_ = options_.CreateLossFunction(); + // Allocate enough memory for the scales. One for each residual. // Due to possibly invalid image pairs or tracks, the actual number of // residuals may be smaller. @@ -169,7 +171,7 @@ void GlobalPositioner::AddCameraToCameraConstraints( BATAPairwiseDirectionError::Create(translation); problem_->AddResidualBlock( cost_function, - options_.loss_function.get(), + loss_function_.get(), images[image_id1].cam_from_world.translation.data(), images[image_id2].cam_from_world.translation.data(), &scale); @@ -212,19 +214,17 @@ void GlobalPositioner::AddPointToCameraConstraints( if (loss_function_ptcam_uncalibrated_ == nullptr) { loss_function_ptcam_uncalibrated_ = - std::make_shared(options_.loss_function.get(), + std::make_shared(loss_function_.get(), 0.5 * weight_scale_pt, ceres::DO_NOT_TAKE_OWNERSHIP); } if (options_.constraint_type == GlobalPositionerOptions::POINTS_AND_CAMERAS_BALANCED) { - loss_function_ptcam_calibrated_ = - std::make_shared(options_.loss_function.get(), - weight_scale_pt, - ceres::DO_NOT_TAKE_OWNERSHIP); + loss_function_ptcam_calibrated_ = std::make_shared( + loss_function_.get(), weight_scale_pt, ceres::DO_NOT_TAKE_OWNERSHIP); } else { - loss_function_ptcam_calibrated_ = options_.loss_function; + loss_function_ptcam_calibrated_ = loss_function_; } for (auto& [track_id, track] : tracks) { diff --git a/glomap/estimators/global_positioning.h b/glomap/estimators/global_positioning.h index 0eb1185..3926ff7 100644 --- a/glomap/estimators/global_positioning.h +++ b/glomap/estimators/global_positioning.h @@ -42,7 +42,10 @@ struct GlobalPositionerOptions : public OptimizationBaseOptions { GlobalPositionerOptions() : OptimizationBaseOptions() { thres_loss_function = 1e-1; - loss_function = std::make_shared(thres_loss_function); + } + + std::shared_ptr CreateLossFunction() { + return std::make_shared(thres_loss_function); } }; @@ -104,6 +107,7 @@ class GlobalPositioner { std::unique_ptr problem_; // Loss functions for reweighted terms. + std::shared_ptr loss_function_; std::shared_ptr loss_function_ptcam_uncalibrated_; std::shared_ptr loss_function_ptcam_calibrated_; diff --git a/glomap/estimators/gravity_refinement.cc b/glomap/estimators/gravity_refinement.cc index 6015d66..d1f96fa 100644 --- a/glomap/estimators/gravity_refinement.cc +++ b/glomap/estimators/gravity_refinement.cc @@ -23,6 +23,8 @@ void GravityRefiner::RefineGravity(const ViewGraph& view_graph, return; } + loss_function_ = options_.CreateLossFunction(); + int counter_progress = 0; // Iterate through the error prone images for (auto image_id : error_prone_images) { @@ -66,8 +68,7 @@ void GravityRefiner::RefineGravity(const ViewGraph& view_graph, ceres::CostFunction* coor_cost = GravError::CreateCost(gravities[counter]); - problem.AddResidualBlock( - coor_cost, options_.loss_function.get(), gravity.data()); + problem.AddResidualBlock(coor_cost, loss_function_.get(), gravity.data()); counter++; } diff --git a/glomap/estimators/gravity_refinement.h b/glomap/estimators/gravity_refinement.h index 581b434..b966715 100644 --- a/glomap/estimators/gravity_refinement.h +++ b/glomap/estimators/gravity_refinement.h @@ -17,8 +17,10 @@ struct GravityRefinerOptions : public OptimizationBaseOptions { // Only refine the gravity of the images with more than min_neighbors int min_num_neighbors = 7; - GravityRefinerOptions() : OptimizationBaseOptions() { - loss_function = std::make_shared( + GravityRefinerOptions() : OptimizationBaseOptions() {} + + std::shared_ptr CreateLossFunction() { + return std::make_shared( 1 - std::cos(DegToRad(max_gravity_error))); } }; @@ -35,6 +37,7 @@ class GravityRefiner { const std::unordered_map& images, std::unordered_set& error_prone_images); GravityRefinerOptions options_; + std::shared_ptr loss_function_; }; } // namespace glomap diff --git a/glomap/estimators/optimization_base.h b/glomap/estimators/optimization_base.h index 2b43e06..c6a67ab 100644 --- a/glomap/estimators/optimization_base.h +++ b/glomap/estimators/optimization_base.h @@ -12,9 +12,6 @@ struct OptimizationBaseOptions { // The threshold for the loss function double thres_loss_function = 1e-1; - // The loss function for the calibration - std::shared_ptr loss_function; - // The options for the solver ceres::Solver::Options solver_options; diff --git a/glomap/estimators/view_graph_calibration.cc b/glomap/estimators/view_graph_calibration.cc index 7d5b457..30c7d34 100644 --- a/glomap/estimators/view_graph_calibration.cc +++ b/glomap/estimators/view_graph_calibration.cc @@ -61,8 +61,7 @@ void ViewGraphCalibrator::Reset( ceres::Problem::Options problem_options; problem_options.loss_function_ownership = ceres::DO_NOT_TAKE_OWNERSHIP; problem_ = std::make_unique(problem_options); - options_.loss_function = - std::make_shared(options_.thres_loss_function); + loss_function_ = options_.CreateLossFunction(); } void ViewGraphCalibrator::AddImagePairsToProblem( @@ -90,14 +89,14 @@ void ViewGraphCalibrator::AddImagePair( problem_->AddResidualBlock( FetzerFocalLengthSameCameraCost::Create( image_pair.F, cameras.at(camera_id1).PrincipalPoint()), - options_.loss_function.get(), + loss_function_.get(), &(focals_[camera_id1])); } else { problem_->AddResidualBlock( FetzerFocalLengthCost::Create(image_pair.F, cameras.at(camera_id1).PrincipalPoint(), cameras.at(camera_id2).PrincipalPoint()), - options_.loss_function.get(), + loss_function_.get(), &(focals_[camera_id1]), &(focals_[camera_id2])); } diff --git a/glomap/estimators/view_graph_calibration.h b/glomap/estimators/view_graph_calibration.h index f7f5104..c1cebd6 100644 --- a/glomap/estimators/view_graph_calibration.h +++ b/glomap/estimators/view_graph_calibration.h @@ -21,6 +21,10 @@ struct ViewGraphCalibratorOptions : public OptimizationBaseOptions { ViewGraphCalibratorOptions() : OptimizationBaseOptions() { thres_loss_function = 1e-2; } + + std::shared_ptr CreateLossFunction() { + return std::make_shared(thres_loss_function); + } }; class ViewGraphCalibrator { @@ -61,6 +65,7 @@ class ViewGraphCalibrator { ViewGraphCalibratorOptions options_; std::unique_ptr problem_; std::unordered_map focals_; + std::shared_ptr loss_function_; }; } // namespace glomap diff --git a/pybind11 b/pybind11 new file mode 160000 index 0000000..3ebdc50 --- /dev/null +++ b/pybind11 @@ -0,0 +1 @@ +Subproject commit 3ebdc503d29c7f089b9a0bc1823add0dda76f40d diff --git a/pyglomap/pybind11 b/pyglomap/pybind11 new file mode 160000 index 0000000..3ebdc50 --- /dev/null +++ b/pyglomap/pybind11 @@ -0,0 +1 @@ +Subproject commit 3ebdc503d29c7f089b9a0bc1823add0dda76f40d