Skip to content

Commit ebd50af

Browse files
authored
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
1 parent e7e8a41 commit ebd50af

File tree

11 files changed

+37
-21
lines changed

11 files changed

+37
-21
lines changed

glomap/estimators/bundle_adjustment.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ void BundleAdjuster::Reset() {
5454
ceres::Problem::Options problem_options;
5555
problem_options.loss_function_ownership = ceres::DO_NOT_TAKE_OWNERSHIP;
5656
problem_ = std::make_unique<ceres::Problem>(problem_options);
57+
loss_function_ = options_.CreateLossFunction();
5758
}
5859

5960
void BundleAdjuster::AddPointToCameraConstraints(
@@ -77,7 +78,7 @@ void BundleAdjuster::AddPointToCameraConstraints(
7778
if (cost_function != nullptr) {
7879
problem_->AddResidualBlock(
7980
cost_function,
80-
options_.loss_function.get(),
81+
loss_function_.get(),
8182
image.cam_from_world.rotation.coeffs().data(),
8283
image.cam_from_world.translation.data(),
8384
tracks[track_id].xyz.data(),

glomap/estimators/bundle_adjustment.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,12 @@ struct BundleAdjusterOptions : public OptimizationBaseOptions {
2121

2222
BundleAdjusterOptions() : OptimizationBaseOptions() {
2323
thres_loss_function = 1.;
24-
loss_function = std::make_shared<ceres::HuberLoss>(thres_loss_function);
2524
solver_options.max_num_iterations = 200;
2625
}
26+
27+
std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
28+
return std::make_shared<ceres::HuberLoss>(thres_loss_function);
29+
}
2730
};
2831

2932
class BundleAdjuster {
@@ -65,6 +68,7 @@ class BundleAdjuster {
6568
BundleAdjusterOptions options_;
6669

6770
std::unique_ptr<ceres::Problem> problem_;
71+
std::shared_ptr<ceres::LossFunction> loss_function_;
6872
};
6973

7074
} // namespace glomap

glomap/estimators/global_positioning.cc

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,8 @@ void GlobalPositioner::SetupProblem(
8787
ceres::Problem::Options problem_options;
8888
problem_options.loss_function_ownership = ceres::DO_NOT_TAKE_OWNERSHIP;
8989
problem_ = std::make_unique<ceres::Problem>(problem_options);
90+
loss_function_ = options_.CreateLossFunction();
91+
9092
// Allocate enough memory for the scales. One for each residual.
9193
// Due to possibly invalid image pairs or tracks, the actual number of
9294
// residuals may be smaller.
@@ -169,7 +171,7 @@ void GlobalPositioner::AddCameraToCameraConstraints(
169171
BATAPairwiseDirectionError::Create(translation);
170172
problem_->AddResidualBlock(
171173
cost_function,
172-
options_.loss_function.get(),
174+
loss_function_.get(),
173175
images[image_id1].cam_from_world.translation.data(),
174176
images[image_id2].cam_from_world.translation.data(),
175177
&scale);
@@ -212,19 +214,17 @@ void GlobalPositioner::AddPointToCameraConstraints(
212214

213215
if (loss_function_ptcam_uncalibrated_ == nullptr) {
214216
loss_function_ptcam_uncalibrated_ =
215-
std::make_shared<ceres::ScaledLoss>(options_.loss_function.get(),
217+
std::make_shared<ceres::ScaledLoss>(loss_function_.get(),
216218
0.5 * weight_scale_pt,
217219
ceres::DO_NOT_TAKE_OWNERSHIP);
218220
}
219221

220222
if (options_.constraint_type ==
221223
GlobalPositionerOptions::POINTS_AND_CAMERAS_BALANCED) {
222-
loss_function_ptcam_calibrated_ =
223-
std::make_shared<ceres::ScaledLoss>(options_.loss_function.get(),
224-
weight_scale_pt,
225-
ceres::DO_NOT_TAKE_OWNERSHIP);
224+
loss_function_ptcam_calibrated_ = std::make_shared<ceres::ScaledLoss>(
225+
loss_function_.get(), weight_scale_pt, ceres::DO_NOT_TAKE_OWNERSHIP);
226226
} else {
227-
loss_function_ptcam_calibrated_ = options_.loss_function;
227+
loss_function_ptcam_calibrated_ = loss_function_;
228228
}
229229

230230
for (auto& [track_id, track] : tracks) {

glomap/estimators/global_positioning.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,10 @@ struct GlobalPositionerOptions : public OptimizationBaseOptions {
4242

4343
GlobalPositionerOptions() : OptimizationBaseOptions() {
4444
thres_loss_function = 1e-1;
45-
loss_function = std::make_shared<ceres::HuberLoss>(thres_loss_function);
45+
}
46+
47+
std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
48+
return std::make_shared<ceres::HuberLoss>(thres_loss_function);
4649
}
4750
};
4851

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

106109
// Loss functions for reweighted terms.
110+
std::shared_ptr<ceres::LossFunction> loss_function_;
107111
std::shared_ptr<ceres::LossFunction> loss_function_ptcam_uncalibrated_;
108112
std::shared_ptr<ceres::LossFunction> loss_function_ptcam_calibrated_;
109113

glomap/estimators/gravity_refinement.cc

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ void GravityRefiner::RefineGravity(const ViewGraph& view_graph,
2323
return;
2424
}
2525

26+
loss_function_ = options_.CreateLossFunction();
27+
2628
int counter_progress = 0;
2729
// Iterate through the error prone images
2830
for (auto image_id : error_prone_images) {
@@ -66,8 +68,7 @@ void GravityRefiner::RefineGravity(const ViewGraph& view_graph,
6668

6769
ceres::CostFunction* coor_cost =
6870
GravError::CreateCost(gravities[counter]);
69-
problem.AddResidualBlock(
70-
coor_cost, options_.loss_function.get(), gravity.data());
71+
problem.AddResidualBlock(coor_cost, loss_function_.get(), gravity.data());
7172
counter++;
7273
}
7374

glomap/estimators/gravity_refinement.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,10 @@ struct GravityRefinerOptions : public OptimizationBaseOptions {
1717
// Only refine the gravity of the images with more than min_neighbors
1818
int min_num_neighbors = 7;
1919

20-
GravityRefinerOptions() : OptimizationBaseOptions() {
21-
loss_function = std::make_shared<ceres::ArctanLoss>(
20+
GravityRefinerOptions() : OptimizationBaseOptions() {}
21+
22+
std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
23+
return std::make_shared<ceres::ArctanLoss>(
2224
1 - std::cos(DegToRad(max_gravity_error)));
2325
}
2426
};
@@ -35,6 +37,7 @@ class GravityRefiner {
3537
const std::unordered_map<image_t, Image>& images,
3638
std::unordered_set<image_t>& error_prone_images);
3739
GravityRefinerOptions options_;
40+
std::shared_ptr<ceres::LossFunction> loss_function_;
3841
};
3942

4043
} // namespace glomap

glomap/estimators/optimization_base.h

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,6 @@ struct OptimizationBaseOptions {
1212
// The threshold for the loss function
1313
double thres_loss_function = 1e-1;
1414

15-
// The loss function for the calibration
16-
std::shared_ptr<ceres::LossFunction> loss_function;
17-
1815
// The options for the solver
1916
ceres::Solver::Options solver_options;
2017

glomap/estimators/view_graph_calibration.cc

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ void ViewGraphCalibrator::Reset(
6161
ceres::Problem::Options problem_options;
6262
problem_options.loss_function_ownership = ceres::DO_NOT_TAKE_OWNERSHIP;
6363
problem_ = std::make_unique<ceres::Problem>(problem_options);
64-
options_.loss_function =
65-
std::make_shared<ceres::CauchyLoss>(options_.thres_loss_function);
64+
loss_function_ = options_.CreateLossFunction();
6665
}
6766

6867
void ViewGraphCalibrator::AddImagePairsToProblem(
@@ -90,14 +89,14 @@ void ViewGraphCalibrator::AddImagePair(
9089
problem_->AddResidualBlock(
9190
FetzerFocalLengthSameCameraCost::Create(
9291
image_pair.F, cameras.at(camera_id1).PrincipalPoint()),
93-
options_.loss_function.get(),
92+
loss_function_.get(),
9493
&(focals_[camera_id1]));
9594
} else {
9695
problem_->AddResidualBlock(
9796
FetzerFocalLengthCost::Create(image_pair.F,
9897
cameras.at(camera_id1).PrincipalPoint(),
9998
cameras.at(camera_id2).PrincipalPoint()),
100-
options_.loss_function.get(),
99+
loss_function_.get(),
101100
&(focals_[camera_id1]),
102101
&(focals_[camera_id2]));
103102
}

glomap/estimators/view_graph_calibration.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ struct ViewGraphCalibratorOptions : public OptimizationBaseOptions {
2121
ViewGraphCalibratorOptions() : OptimizationBaseOptions() {
2222
thres_loss_function = 1e-2;
2323
}
24+
25+
std::shared_ptr<ceres::LossFunction> CreateLossFunction() {
26+
return std::make_shared<ceres::CauchyLoss>(thres_loss_function);
27+
}
2428
};
2529

2630
class ViewGraphCalibrator {
@@ -61,6 +65,7 @@ class ViewGraphCalibrator {
6165
ViewGraphCalibratorOptions options_;
6266
std::unique_ptr<ceres::Problem> problem_;
6367
std::unordered_map<camera_t, double> focals_;
68+
std::shared_ptr<ceres::LossFunction> loss_function_;
6469
};
6570

6671
} // namespace glomap

pybind11

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Subproject commit 3ebdc503d29c7f089b9a0bc1823add0dda76f40d

0 commit comments

Comments
 (0)