Skip to content

Commit

Permalink
fix the bug for threshold update (#148)
Browse files Browse the repository at this point in the history
* fix the bug that the update of threshold from command line is not reflected in the structure

* f

* update

* refactor the loss_function

* d
  • Loading branch information
lpanaf authored Dec 13, 2024
1 parent e7e8a41 commit ebd50af
Show file tree
Hide file tree
Showing 11 changed files with 37 additions and 21 deletions.
3 changes: 2 additions & 1 deletion glomap/estimators/bundle_adjustment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ceres::Problem>(problem_options);
loss_function_ = options_.CreateLossFunction();
}

void BundleAdjuster::AddPointToCameraConstraints(
Expand All @@ -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(),
Expand Down
6 changes: 5 additions & 1 deletion glomap/estimators/bundle_adjustment.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ struct BundleAdjusterOptions : public OptimizationBaseOptions {

BundleAdjusterOptions() : OptimizationBaseOptions() {
thres_loss_function = 1.;
loss_function = std::make_shared<ceres::HuberLoss>(thres_loss_function);
solver_options.max_num_iterations = 200;
}

std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
return std::make_shared<ceres::HuberLoss>(thres_loss_function);
}
};

class BundleAdjuster {
Expand Down Expand Up @@ -65,6 +68,7 @@ class BundleAdjuster {
BundleAdjusterOptions options_;

std::unique_ptr<ceres::Problem> problem_;
std::shared_ptr<ceres::LossFunction> loss_function_;
};

} // namespace glomap
14 changes: 7 additions & 7 deletions glomap/estimators/global_positioning.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ceres::Problem>(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.
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -212,19 +214,17 @@ void GlobalPositioner::AddPointToCameraConstraints(

if (loss_function_ptcam_uncalibrated_ == nullptr) {
loss_function_ptcam_uncalibrated_ =
std::make_shared<ceres::ScaledLoss>(options_.loss_function.get(),
std::make_shared<ceres::ScaledLoss>(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<ceres::ScaledLoss>(options_.loss_function.get(),
weight_scale_pt,
ceres::DO_NOT_TAKE_OWNERSHIP);
loss_function_ptcam_calibrated_ = std::make_shared<ceres::ScaledLoss>(
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) {
Expand Down
6 changes: 5 additions & 1 deletion glomap/estimators/global_positioning.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ struct GlobalPositionerOptions : public OptimizationBaseOptions {

GlobalPositionerOptions() : OptimizationBaseOptions() {
thres_loss_function = 1e-1;
loss_function = std::make_shared<ceres::HuberLoss>(thres_loss_function);
}

std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
return std::make_shared<ceres::HuberLoss>(thres_loss_function);
}
};

Expand Down Expand Up @@ -104,6 +107,7 @@ class GlobalPositioner {
std::unique_ptr<ceres::Problem> problem_;

// Loss functions for reweighted terms.
std::shared_ptr<ceres::LossFunction> loss_function_;
std::shared_ptr<ceres::LossFunction> loss_function_ptcam_uncalibrated_;
std::shared_ptr<ceres::LossFunction> loss_function_ptcam_calibrated_;

Expand Down
5 changes: 3 additions & 2 deletions glomap/estimators/gravity_refinement.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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++;
}

Expand Down
7 changes: 5 additions & 2 deletions glomap/estimators/gravity_refinement.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ceres::ArctanLoss>(
GravityRefinerOptions() : OptimizationBaseOptions() {}

std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
return std::make_shared<ceres::ArctanLoss>(
1 - std::cos(DegToRad(max_gravity_error)));
}
};
Expand All @@ -35,6 +37,7 @@ class GravityRefiner {
const std::unordered_map<image_t, Image>& images,
std::unordered_set<image_t>& error_prone_images);
GravityRefinerOptions options_;
std::shared_ptr<ceres::LossFunction> loss_function_;
};

} // namespace glomap
3 changes: 0 additions & 3 deletions glomap/estimators/optimization_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -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<ceres::LossFunction> loss_function;

// The options for the solver
ceres::Solver::Options solver_options;

Expand Down
7 changes: 3 additions & 4 deletions glomap/estimators/view_graph_calibration.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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<ceres::Problem>(problem_options);
options_.loss_function =
std::make_shared<ceres::CauchyLoss>(options_.thres_loss_function);
loss_function_ = options_.CreateLossFunction();
}

void ViewGraphCalibrator::AddImagePairsToProblem(
Expand Down Expand Up @@ -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]));
}
Expand Down
5 changes: 5 additions & 0 deletions glomap/estimators/view_graph_calibration.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ struct ViewGraphCalibratorOptions : public OptimizationBaseOptions {
ViewGraphCalibratorOptions() : OptimizationBaseOptions() {
thres_loss_function = 1e-2;
}

std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
return std::make_shared<ceres::CauchyLoss>(thres_loss_function);
}
};

class ViewGraphCalibrator {
Expand Down Expand Up @@ -61,6 +65,7 @@ class ViewGraphCalibrator {
ViewGraphCalibratorOptions options_;
std::unique_ptr<ceres::Problem> problem_;
std::unordered_map<camera_t, double> focals_;
std::shared_ptr<ceres::LossFunction> loss_function_;
};

} // namespace glomap
1 change: 1 addition & 0 deletions pybind11
Submodule pybind11 added at 3ebdc5
1 change: 1 addition & 0 deletions pyglomap/pybind11
Submodule pybind11 added at 3ebdc5

0 comments on commit ebd50af

Please sign in to comment.