| 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 |
// End-to-end incremental mapping benchmark on synthetic data. Measures the |
| 31 |
// wall-clock runtime of a full reconstruction (registration, triangulation, |
| 32 |
// bundle adjustment) and is useful for evaluating the impact of changes to the |
| 33 |
// mapper or bundle adjustment. Parametrized over camera model and scene size; |
| 34 |
// the camera model determines which cost-function path bundle adjustment |
| 35 |
// exercises. All runs are deterministic (fixed seed, single thread) so the |
| 36 |
// produced reconstruction is stable and only the runtime varies across |
| 37 |
// configurations. |
| 38 |
|
| 39 |
#include "colmap/controllers/incremental_pipeline.h" |
| 40 |
#include "colmap/math/random.h" |
| 41 |
#include "colmap/scene/database.h" |
| 42 |
#include "colmap/scene/database_sqlite.h" |
| 43 |
#include "colmap/scene/reconstruction.h" |
| 44 |
#include "colmap/scene/reconstruction_manager.h" |
| 45 |
#include "colmap/scene/synthetic.h" |
| 46 |
#include "colmap/sensor/models.h" |
| 47 |
#include "colmap/util/logging.h" |
| 48 |
|
| 49 |
#include <memory> |
| 50 |
#include <vector> |
| 51 |
|
| 52 |
#include <benchmark/benchmark.h> |
| 53 |
|
| 54 |
using namespace colmap; |
| 55 |
|
| 56 |
namespace { |
| 57 |
|
| 58 |
constexpr unsigned kSeed = 42; |
| 59 |
|
| 60 |
// Deterministic mapper options so all configurations perform identical work; |
| 61 |
// only the bundle-adjustment cost-function implementation varies between |
| 62 |
// builds. |
| 63 |
std::shared_ptr<IncrementalPipelineOptions> MakeOptions() { |
| 64 |
auto options = std::make_shared<IncrementalPipelineOptions>(); |
| 65 |
options->random_seed = kSeed; |
| 66 |
// Single-threaded to keep timings stable and isolate per-residual cost. |
| 67 |
options->num_threads = 1; |
| 68 |
options->extract_colors = false; |
| 69 |
return options; |
| 70 |
} |
| 71 |
|
| 72 |
void RunPipelineAndReport(benchmark::State& state, |
| 73 |
const std::shared_ptr<Database>& database) { |
| 74 |
std::shared_ptr<const Reconstruction> largest; |
| 75 |
for (auto _ : state) { |
| 76 |
auto reconstruction_manager = std::make_shared<ReconstructionManager>(); |
| 77 |
IncrementalPipeline mapper(MakeOptions(), database, reconstruction_manager); |
| 78 |
mapper.Run(); |
| 79 |
|
| 80 |
state.PauseTiming(); |
| 81 |
largest = nullptr; |
| 82 |
for (size_t i = 0; i < reconstruction_manager->Size(); ++i) { |
| 83 |
const auto& reconstruction = reconstruction_manager->Get(i); |
| 84 |
if (largest == nullptr || |
| 85 |
reconstruction->NumRegImages() > largest->NumRegImages()) { |
| 86 |
largest = reconstruction; |
| 87 |
} |
| 88 |
} |
| 89 |
state.ResumeTiming(); |
| 90 |
} |
| 91 |
|
| 92 |
if (largest != nullptr) { |
| 93 |
state.counters["num_reg_images"] = largest->NumRegImages(); |
| 94 |
state.counters["num_points3d"] = largest->NumPoints3D(); |
| 95 |
state.counters["mean_track_length"] = largest->ComputeMeanTrackLength(); |
| 96 |
} else { |
| 97 |
state.SkipWithError("No reconstruction was produced"); |
| 98 |
} |
| 99 |
} |
| 100 |
|
| 101 |
std::vector<double> DefaultCameraParams(const CameraModelId model_id) { |
| 102 |
// Nominal intrinsics for a 1024x768 image with moderate distortion. |
| 103 |
switch (model_id) { |
| 104 |
case PinholeCameraModel::model_id: |
| 105 |
return {1280, 1280, 512, 384}; |
| 106 |
case OpenCVCameraModel::model_id: |
| 107 |
return {1280, 1280, 512, 384, 0.05, 0.01, 0.001, 0.001}; |
| 108 |
default: |
| 109 |
LOG(FATAL) << "Unsupported camera model"; |
| 110 |
return {}; |
| 111 |
} |
| 112 |
} |
| 113 |
|
| 114 |
void BM_IncrementalMapping(benchmark::State& state, |
| 115 |
const CameraModelId camera_model_id) { |
| 116 |
SetPRNGSeed(kSeed); |
| 117 |
|
| 118 |
SyntheticDatasetOptions dataset_options; |
| 119 |
dataset_options.num_rigs = 1; |
| 120 |
dataset_options.num_cameras_per_rig = 1; |
| 121 |
dataset_options.num_frames_per_rig = static_cast<int>(state.range(0)); |
| 122 |
dataset_options.num_points3D = static_cast<int>(state.range(1)); |
| 123 |
dataset_options.camera_model_id = camera_model_id; |
| 124 |
dataset_options.camera_params = DefaultCameraParams(camera_model_id); |
| 125 |
// Sparse but connected view graph so per-image work stays bounded as the |
| 126 |
// image count grows. |
| 127 |
dataset_options.match_config = SyntheticDatasetOptions::MatchConfig::SPARSE; |
| 128 |
dataset_options.match_sparsity = 0.9; |
| 129 |
|
| 130 |
auto database = Database::Open(kInMemorySqliteDatabasePath); |
| 131 |
Reconstruction gt_reconstruction; |
| 132 |
SynthesizeDataset(dataset_options, >_reconstruction, database.get()); |
| 133 |
|
| 134 |
// Inject realistic observation noise so bundle adjustment performs |
| 135 |
// representative work. |
| 136 |
SyntheticNoiseOptions noise_options; |
| 137 |
noise_options.point2D_stddev = 1.0; |
| 138 |
noise_options.point3D_stddev = 0.05; |
| 139 |
noise_options.rig_from_world_translation_stddev = 0.01; |
| 140 |
noise_options.rig_from_world_rotation_stddev = 1.0; |
| 141 |
SynthesizeNoise(noise_options, >_reconstruction, database.get()); |
| 142 |
|
| 143 |
RunPipelineAndReport(state, database); |
| 144 |
} |
| 145 |
|
| 146 |
void AddArguments(::benchmark::Benchmark* b) { |
| 147 |
b->ArgNames({"num_frames", "num_points3D"}); |
| 148 |
for (const int num_frames : {25, 50, 100}) { |
| 149 |
b->Args({num_frames, /*num_points3D=*/1000}); |
| 150 |
} |
| 151 |
b->Unit(benchmark::kMillisecond) |
| 152 |
->Iterations(1) |
| 153 |
->Repetitions(5) |
| 154 |
->ReportAggregatesOnly(true) |
| 155 |
->UseRealTime(); |
| 156 |
} |
| 157 |
|
| 158 |
} // namespace |
| 159 |
|
| 160 |
BENCHMARK_CAPTURE(BM_IncrementalMapping, PINHOLE, PinholeCameraModel::model_id) |
| 161 |
->Apply(AddArguments); |
| 162 |
BENCHMARK_CAPTURE(BM_IncrementalMapping, OPENCV, OpenCVCameraModel::model_id) |
| 163 |
->Apply(AddArguments); |
| 164 |
|
| 165 |
BENCHMARK_MAIN(); |