Debian Codegraph @

benchmark/runtime/bundle_adjustment_convergence.cc

raw
1 // Copyright (c), ETH Zurich and UNC Chapel Hill.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright
8 // notice, this list of conditions and the following disclaimer.
9 //
10 // * Redistributions in binary form must reproduce the above copyright
11 // notice, this list of conditions and the following disclaimer in the
12 // documentation and/or other materials provided with the distribution.
13 //
14 // * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
15 // its contributors may be used to endorse or promote products derived
16 // from this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
22 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 // POSSIBILITY OF SUCH DAMAGE.
29
30 // Outputs per-LM-iteration MSE convergence as CSV to stdout.
31 // Columns: solver,iteration,time_ms,mse_px2
32 //
33 // MSE convention matches the existing COLMAP reporting (sqrt(cost/n) = RMSE):
34 // Ceres: mse = cost / num_residuals
35 // Caspar: mse = score_best / num_residuals
36 // Usage:
37 // bundle_adjustment_convergence \
38 // [--track_length=N] [--num_frames=N] [--num_points3D=N] \
39 // [--max_ceres_iterations=N] [--max_caspar_iterations=N]
40
41 #include "colmap/controllers/base_option_manager.h"
42 #include "colmap/estimators/bundle_adjustment.h"
43 #include "colmap/estimators/bundle_adjustment_caspar.h"
44 #include "colmap/estimators/bundle_adjustment_ceres.h"
45 #include "colmap/scene/reconstruction.h"
46 #include "colmap/scene/synthetic.h"
47 #include "colmap/sensor/models.h"
48
49 #include <chrono>
50 #include <iostream>
51 #include <string>
52
53 using namespace colmap;
54
55 int main(int argc, char** argv) {
56 // -1 means "use solver default". Override only when flag is provided.
57 int max_ceres_iterations = -1;
58 int max_caspar_iterations = -1;
59 int track_length = 20;
60 int num_frames = 50;
61 int num_points3D = 5000;
62 int num_cameras_per_rig = 1;
63 bool refine_focal_length = false;
64 std::string label = "default";
65
66 BaseOptionManager args(/*add_project_options=*/false);
67 args.AddDefaultOption("max_ceres_iterations", &max_ceres_iterations);
68 args.AddDefaultOption("max_caspar_iterations", &max_caspar_iterations);
69 args.AddDefaultOption("track_length", &track_length);
70 args.AddDefaultOption("num_frames", &num_frames);
71 args.AddDefaultOption("num_points3D", &num_points3D);
72 args.AddDefaultOption("num_cameras_per_rig", &num_cameras_per_rig);
73 args.AddDefaultOption("refine_focal_length", &refine_focal_length);
74 args.AddDefaultOption("label", &label);
75 if (!args.Parse(argc, argv)) {
76 return EXIT_FAILURE;
77 }
78
79 SetPRNGSeed(42);
80
81 SyntheticDatasetOptions dataset_options;
82 dataset_options.track_length = track_length;
83 dataset_options.num_cameras_per_rig = num_cameras_per_rig;
84 dataset_options.num_points3D = num_points3D;
85 // Use PINHOLE so fx≠fy refinement is testable without distortion coupling.
86 dataset_options.camera_model_id = PinholeCameraModel::model_id;
87 dataset_options.camera_params = {1280, 1280, 512, 384};
88
89 dataset_options.num_rigs = 1;
90 dataset_options.num_frames_per_rig = num_frames;
91
92 Reconstruction reconstruction;
93 SynthesizeDataset(dataset_options, &reconstruction);
94
95 SyntheticNoiseOptions noise_options;
96 noise_options.point2D_stddev = 1.0;
97 noise_options.point3D_stddev = 0.05;
98 noise_options.rig_from_world_translation_stddev = 0.01;
99 noise_options.rig_from_world_rotation_stddev = 1.0;
100 SynthesizeNoise(noise_options, &reconstruction);
101
102 BundleAdjustmentConfig config;
103 for (const image_t image_id : reconstruction.RegImageIds()) {
104 config.AddImage(image_id);
105 }
106 config.FixGauge(BundleAdjustmentGauge::TWO_CAMS_FROM_WORLD);
107
108 BundleAdjustmentOptions options;
109 options.print_summary = false;
110 options.refine_focal_length = refine_focal_length;
111 options.refine_sensor_from_rig = false;
112 if (max_ceres_iterations >= 0) {
113 options.ceres->solver_options.max_num_iterations = max_ceres_iterations;
114 }
115 if (max_caspar_iterations >= 0) {
116 options.caspar->solver_iter_max = max_caspar_iterations;
117 }
118 options.caspar->collect_iteration_data = true;
119
120 std::cerr << "Scene: track_length=" << track_length
121 << " num_frames=" << num_frames << " num_points3D=" << num_points3D
122 << " num_cameras_per_rig=" << num_cameras_per_rig
123 << " refine_focal_length=" << refine_focal_length
124 << " label=" << label << " max_ceres_iterations="
125 << options.ceres->solver_options.max_num_iterations
126 << " max_caspar_iterations=" << options.caspar->solver_iter_max
127 << "\n";
128
129 std::cout << "solver,scenario,iteration,time_ms,mse_px2\n";
130 // Ceres
131 {
132 Reconstruction copy = reconstruction;
133 BundleAdjustmentOptions opts = options;
134 opts.backend = BundleAdjustmentBackend::CERES;
135
136 // Measure setup/build time.
137 const auto t_setup_start = std::chrono::steady_clock::now();
138 auto ba = CreateDefaultBundleAdjuster(opts, config, copy);
139 const auto t_setup_end = std::chrono::steady_clock::now();
140
141 const double setup_time_ms =
142 std::chrono::duration<double, std::milli>(t_setup_end - t_setup_start)
143 .count();
144
145 // Measure total Solve() wall time.
146 const auto t_solve_start = std::chrono::steady_clock::now();
147 const auto summary = ba->Solve();
148 const auto t_solve_end = std::chrono::steady_clock::now();
149
150 const double solve_wall_ms =
151 std::chrono::duration<double, std::milli>(t_solve_end - t_solve_start)
152 .count();
153
154 const auto* s =
155 dynamic_cast<const CeresBundleAdjustmentSummary*>(summary.get());
156 if (!s) {
157 std::cerr << "ERROR: unexpected Ceres summary type\n";
158 return 1;
159 }
160
161 const double n = static_cast<double>(s->num_residuals);
162
163 // Ceres cumulative iteration time reported internally.
164 const double iter_total_ms =
165 s->ceres_summary.iterations.empty()
166 ? 0.0
167 : s->ceres_summary.iterations.back().cumulative_time_in_seconds *
168 1000.0;
169
170 // Solve() wall time not accounted for by iteration timing.
171 const double solve_overhead_ms =
172 std::max(0.0, solve_wall_ms - iter_total_ms);
173
174 // Emit t=0 anchor so the plot origin is consistent with Caspar.
175 if (!s->ceres_summary.iterations.empty()) {
176 std::cout << "ceres," << label << ",-1,0,"
177 << s->ceres_summary.iterations.front().cost / n << "\n";
178 }
179
180 for (const auto& iter : s->ceres_summary.iterations) {
181 const double mse = iter.cost / n;
182
183 // Include:
184 // setup time
185 // + Solve() dispatch/overhead
186 // + cumulative iteration time (includes initial eval — same convention
187 // as Caspar's dt_tot which includes DoResJacFirst)
188 const double time_ms = setup_time_ms + solve_overhead_ms +
189 iter.cumulative_time_in_seconds * 1000.0;
190
191 std::cout << "ceres," << label << "," << iter.iteration << "," << time_ms
192 << "," << mse << "\n";
193 }
194
195 std::cerr << "Ceres: " << s->ceres_summary.iterations.size()
196 << " iterations, final mse=" << s->ceres_summary.final_cost / n
197 << " px^2"
198 << ", setup_ms=" << setup_time_ms
199 << ", solve_overhead_ms=" << solve_overhead_ms
200 << ", iter_ms=" << iter_total_ms
201 << ", solve_wall_ms=" << solve_wall_ms << "\n";
202 }
203 #ifdef CASPAR_ENABLED
204 // Caspar
205 {
206 Reconstruction copy = reconstruction;
207 BundleAdjustmentOptions opts = options;
208 opts.backend = BundleAdjustmentBackend::CASPAR;
209
210 // Measure setup/build time.
211 const auto t_setup_start = std::chrono::steady_clock::now();
212 auto ba = CreateDefaultBundleAdjuster(opts, config, copy);
213 const auto t_setup_end = std::chrono::steady_clock::now();
214
215 const double setup_time_ms =
216 std::chrono::duration<double, std::milli>(t_setup_end - t_setup_start)
217 .count();
218
219 // Measure total Solve() wall time.
220 const auto t_solve_start = std::chrono::steady_clock::now();
221 const auto summary = ba->Solve();
222 const auto t_solve_end = std::chrono::steady_clock::now();
223
224 const double solve_wall_ms =
225 std::chrono::duration<double, std::milli>(t_solve_end - t_solve_start)
226 .count();
227
228 const auto* s =
229 dynamic_cast<const CasparBundleAdjustmentSummary*>(summary.get());
230 if (!s) {
231 std::cerr << "ERROR: unexpected Caspar summary type\n";
232 return 1;
233 }
234
235 const double n = static_cast<double>(s->num_residuals);
236
237 // Caspar internal measured iteration time.
238 const double iter_total_ms =
239 s->iterations.empty() ? 0.0 : s->iterations.back().dt_tot * 1000.0;
240
241 // Everything inside Solve() that is NOT accounted for by iteration timing.
242 const double solve_overhead_ms =
243 std::max(0.0, solve_wall_ms - iter_total_ms);
244
245 // Emit initial state (iteration -1) at t=0
246 std::cout << "caspar," << label << ",-1,0," << (s->initial_score / n)
247 << "\n";
248
249 for (const auto& iter : s->iterations) {
250 const double mse = iter.score_best / n;
251
252 // Include:
253 // setup time
254 // + Solve() dispatch/overhead
255 // + cumulative iteration time
256 const double time_ms =
257 setup_time_ms + solve_overhead_ms + iter.dt_tot * 1000.0;
258
259 std::cout << "caspar," << label << "," << iter.solver_iter << ","
260 << time_ms << "," << mse << "\n";
261 }
262
263 const double final_mse = s->iterations.empty()
264 ? s->initial_score / n
265 : s->iterations.back().score_best / n;
266
267 std::cerr << "Caspar: " << s->iteration_count
268 << " iterations, final mse=" << final_mse << " px^2"
269 << ", setup_ms=" << setup_time_ms
270 << ", solve_overhead_ms=" << solve_overhead_ms
271 << ", iter_ms=" << iter_total_ms
272 << ", solve_wall_ms=" << solve_wall_ms << "\n";
273 }
274 #endif
275
276 return 0;
277 }