Debian Codegraph @

benchmark/runtime/global_positioning.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 #include "colmap/estimators/global_positioning.h"
31
32 #include "colmap/math/random.h"
33 #include "colmap/scene/database_cache.h"
34 #include "colmap/scene/database_sqlite.h"
35 #include "colmap/scene/pose_graph.h"
36 #include "colmap/scene/reconstruction.h"
37 #include "colmap/scene/synthetic.h"
38
39 #include <algorithm>
40 #include <array>
41
42 #include <benchmark/benchmark.h>
43 #include <glog/logging.h>
44
45 using namespace colmap;
46
47 struct CachedData {
48 std::array<int64_t, 5> dataset_args;
49 Reconstruction reconstruction;
50 colmap::PoseGraph pose_graph;
51 };
52
53 static void BM_GlobalPositioning(benchmark::State& state) {
54 FLAGS_minloglevel = 2; // Suppress INFO and WARNING logs.
55
56 const std::array<int64_t, 5> dataset_args = {state.range(0),
57 state.range(1),
58 state.range(2),
59 state.range(3),
60 state.range(4)};
61 const bool use_parameter_block_ordering = state.range(5);
62
63 // Cache dataset to avoid resynthesizing for different ordering options.
64 static std::unique_ptr<CachedData> cached;
65 if (!cached || cached->dataset_args != dataset_args) {
66 SetPRNGSeed(42);
67
68 SyntheticDatasetOptions dataset_options;
69 dataset_options.num_rigs = dataset_args[0];
70 dataset_options.num_cameras_per_rig = dataset_args[1];
71 dataset_options.num_frames_per_rig = dataset_args[2];
72 dataset_options.num_points3D = dataset_args[3];
73
74 // Compute sparsity from target number of neighbors per image.
75 // sparsity ≈ 1 - num_neighbors / (num_images - 1)
76 const int num_neighbors = dataset_args[4];
77 const int num_images = dataset_options.num_rigs *
78 dataset_options.num_cameras_per_rig *
79 dataset_options.num_frames_per_rig;
80 dataset_options.match_sparsity = std::max(
81 0.0, 1.0 - static_cast<double>(num_neighbors) / (num_images - 1));
82 dataset_options.match_config = SyntheticDatasetOptions::MatchConfig::SPARSE;
83 dataset_options.two_view_geometry_has_relative_pose = true;
84
85 cached = std::make_unique<CachedData>();
86 cached->dataset_args = dataset_args;
87
88 auto database = Database::Open(kInMemorySqliteDatabasePath);
89 Reconstruction gt_reconstruction;
90 SynthesizeDataset(dataset_options, &gt_reconstruction, database.get());
91
92 DatabaseCache database_cache;
93 DatabaseCache::Options cache_options;
94 database_cache.Load(*database, cache_options);
95 database.reset(); // Close database connection.
96
97 cached->pose_graph.Load(*database_cache.CorrespondenceGraph());
98
99 cached->reconstruction = gt_reconstruction;
100 for (const auto& [frame_id, _] : cached->reconstruction.Frames()) {
101 Frame& frame = cached->reconstruction.Frame(frame_id);
102 frame.SetRigFromWorld(
103 Rigid3d(frame.RigFromWorld().rotation(), Eigen::Vector3d::Zero()));
104 }
105 }
106
107 const Reconstruction& reconstruction = cached->reconstruction;
108 const colmap::PoseGraph& pose_graph = cached->pose_graph;
109 const int num_neighbors = dataset_args[4];
110
111 colmap::GlobalPositionerOptions base_options;
112 base_options.use_gpu = false;
113 base_options.random_seed = 42;
114 base_options.solver_options.max_num_iterations = 50;
115 base_options.solver_options.minimizer_progress_to_stdout = false;
116
117 for (auto _ : state) {
118 state.PauseTiming();
119 Reconstruction reconstruction_copy = reconstruction;
120 colmap::GlobalPositionerOptions options = base_options;
121 options.use_parameter_block_ordering = use_parameter_block_ordering;
122 state.ResumeTiming();
123
124 colmap::GlobalPositioner positioner(options);
125 positioner.Solve(pose_graph, reconstruction_copy);
126 }
127 state.counters["ord"] = use_parameter_block_ordering;
128 state.counters["imgs"] = reconstruction.NumRegImages();
129 state.counters["rigs"] = reconstruction.NumRigs();
130 state.counters["cams"] = reconstruction.NumCameras();
131 state.counters["frms"] = reconstruction.NumRegFrames();
132 state.counters["pnts"] = reconstruction.NumPoints3D();
133 state.counters["nbrs"] = num_neighbors;
134 }
135
136 static void GenerateArguments(benchmark::Benchmark* b) {
137 // Args: {num_rigs, num_cameras_per_rig, num_frames_per_rig, num_points3D,
138 // num_neighbors, use_parameter_block_ordering}
139 for (const int num_rigs : {1, 5}) {
140 for (const int num_cameras_per_rig : {1, 3}) {
141 for (const int num_frames_per_rig : {10, 50}) {
142 for (const int num_points3D : {1000, 10000}) {
143 for (const int num_neighbors : {10, 20}) {
144 for (const bool use_parameter_block_ordering : {true, false}) {
145 b->Args({num_rigs,
146 num_cameras_per_rig,
147 num_frames_per_rig,
148 num_points3D,
149 num_neighbors,
150 use_parameter_block_ordering});
151 }
152 }
153 }
154 }
155 }
156 }
157 }
158
159 BENCHMARK(BM_GlobalPositioning)
160 ->Apply(GenerateArguments)
161 ->Unit(benchmark::kMillisecond)
162 ->UseRealTime();
163
164 BENCHMARK_MAIN();