| 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/bundle_adjustment.h" |
| 31 |
|
| 32 |
#include "colmap/estimators/bundle_adjustment_ceres.h" |
| 33 |
#include "colmap/scene/reconstruction.h" |
| 34 |
#include "colmap/scene/synthetic.h" |
| 35 |
|
| 36 |
#include <benchmark/benchmark.h> |
| 37 |
|
| 38 |
using namespace colmap; |
| 39 |
|
| 40 |
static void AddArguments(::benchmark::Benchmark* b) { |
| 41 |
for (const int track_length : {5, 20, 100}) { |
| 42 |
for (const int num_rigs : {1, 5}) { |
| 43 |
for (const int num_cameras_per_rig : {1, 3}) { |
| 44 |
for (const int num_frames_per_rig : {10, 50}) { |
| 45 |
const int num_images = |
| 46 |
num_rigs * num_cameras_per_rig * num_frames_per_rig; |
| 47 |
if (track_length > num_images) continue; |
| 48 |
for (const int num_points3D : {1000, 10000}) { |
| 49 |
b->Args({track_length, |
| 50 |
num_rigs, |
| 51 |
num_cameras_per_rig, |
| 52 |
num_frames_per_rig, |
| 53 |
num_points3D}); |
| 54 |
} |
| 55 |
} |
| 56 |
} |
| 57 |
} |
| 58 |
} |
| 59 |
} |
| 60 |
|
| 61 |
class BM_BundleAdjustment : public benchmark::Fixture { |
| 62 |
public: |
| 63 |
void SetUp(::benchmark::State& state) { |
| 64 |
SetPRNGSeed(42); |
| 65 |
|
| 66 |
SyntheticDatasetOptions dataset_options; |
| 67 |
dataset_options.track_length = state.range(0); |
| 68 |
dataset_options.num_rigs = state.range(1); |
| 69 |
dataset_options.num_cameras_per_rig = state.range(2); |
| 70 |
dataset_options.num_frames_per_rig = state.range(3); |
| 71 |
dataset_options.num_points3D = state.range(4); |
| 72 |
|
| 73 |
reconstruction_ = std::make_unique<Reconstruction>(); |
| 74 |
SynthesizeDataset(dataset_options, reconstruction_.get()); |
| 75 |
|
| 76 |
SyntheticNoiseOptions noise_options; |
| 77 |
noise_options.point2D_stddev = 1.0; |
| 78 |
noise_options.point3D_stddev = 0.05; |
| 79 |
noise_options.rig_from_world_translation_stddev = 0.01; |
| 80 |
noise_options.rig_from_world_rotation_stddev = 1.0; |
| 81 |
SynthesizeNoise(noise_options, reconstruction_.get()); |
| 82 |
|
| 83 |
for (const image_t image_id : reconstruction_->RegImageIds()) { |
| 84 |
config_.AddImage(image_id); |
| 85 |
} |
| 86 |
config_.FixGauge(BundleAdjustmentGauge::TWO_CAMS_FROM_WORLD); |
| 87 |
|
| 88 |
options_.print_summary = false; |
| 89 |
} |
| 90 |
|
| 91 |
void TearDown(::benchmark::State& /*state*/) { |
| 92 |
reconstruction_.reset(); |
| 93 |
options_ = BundleAdjustmentOptions(); |
| 94 |
config_ = BundleAdjustmentConfig(); |
| 95 |
} |
| 96 |
|
| 97 |
protected: |
| 98 |
void ReportSceneCounters(benchmark::State& state) const { |
| 99 |
state.counters["track_length"] = reconstruction_->ComputeMeanTrackLength(); |
| 100 |
state.counters["num_images"] = reconstruction_->NumRegImages(); |
| 101 |
state.counters["num_rigs"] = reconstruction_->NumRigs(); |
| 102 |
state.counters["num_cameras"] = reconstruction_->NumCameras(); |
| 103 |
state.counters["num_frames"] = reconstruction_->NumRegFrames(); |
| 104 |
state.counters["num_points3d"] = reconstruction_->NumPoints3D(); |
| 105 |
} |
| 106 |
|
| 107 |
std::unique_ptr<Reconstruction> reconstruction_; |
| 108 |
BundleAdjustmentConfig config_; |
| 109 |
BundleAdjustmentOptions options_; |
| 110 |
}; |
| 111 |
|
| 112 |
// Time column reports wall-clock time (ms) per full BA solve. |
| 113 |
BENCHMARK_DEFINE_F(BM_BundleAdjustment, Ceres)(benchmark::State& state) { |
| 114 |
BundleAdjustmentOptions opts = options_; |
| 115 |
opts.backend = BundleAdjustmentBackend::CERES; |
| 116 |
|
| 117 |
int total_lm_steps = 0; |
| 118 |
double total_ceres_time_s = 0.0; |
| 119 |
|
| 120 |
for (auto _ : state) { |
| 121 |
state.PauseTiming(); |
| 122 |
Reconstruction copy = *reconstruction_; |
| 123 |
state.ResumeTiming(); |
| 124 |
|
| 125 |
auto ba = CreateDefaultBundleAdjuster(opts, config_, copy); |
| 126 |
const auto summary = ba->Solve(); |
| 127 |
|
| 128 |
state.PauseTiming(); |
| 129 |
if (summary->termination_type == |
| 130 |
BundleAdjustmentTerminationType::NO_CONVERGENCE) { |
| 131 |
state.SkipWithError("Bundle adjustment did not converge"); |
| 132 |
break; |
| 133 |
} |
| 134 |
const auto* ceres_sum = |
| 135 |
dynamic_cast<const CeresBundleAdjustmentSummary*>(summary.get()); |
| 136 |
if (ceres_sum == nullptr) { |
| 137 |
state.SkipWithError("Unexpected summary type for Ceres backend"); |
| 138 |
break; |
| 139 |
} |
| 140 |
total_lm_steps += ceres_sum->ceres_summary.num_successful_steps + |
| 141 |
ceres_sum->ceres_summary.num_unsuccessful_steps; |
| 142 |
total_ceres_time_s += ceres_sum->ceres_summary.total_time_in_seconds; |
| 143 |
state.ResumeTiming(); |
| 144 |
} |
| 145 |
|
| 146 |
state.PauseTiming(); |
| 147 |
ReportSceneCounters(state); |
| 148 |
const int64_t num_iters = state.iterations(); |
| 149 |
if (num_iters > 0) { |
| 150 |
state.counters["avg_lm_steps"] = |
| 151 |
static_cast<double>(total_lm_steps) / num_iters; |
| 152 |
} |
| 153 |
if (total_lm_steps > 0) { |
| 154 |
state.counters["avg_ms_per_lm_step"] = |
| 155 |
total_ceres_time_s * 1000.0 / total_lm_steps; |
| 156 |
} |
| 157 |
state.ResumeTiming(); |
| 158 |
} |
| 159 |
|
| 160 |
#ifdef CASPAR_ENABLED |
| 161 |
// Time column reports wall-clock time (ms) per full BA solve. |
| 162 |
BENCHMARK_DEFINE_F(BM_BundleAdjustment, Caspar)(benchmark::State& state) { |
| 163 |
BundleAdjustmentOptions opts = options_; |
| 164 |
opts.backend = BundleAdjustmentBackend::CASPAR; |
| 165 |
|
| 166 |
for (auto _ : state) { |
| 167 |
state.PauseTiming(); |
| 168 |
Reconstruction copy = *reconstruction_; |
| 169 |
state.ResumeTiming(); |
| 170 |
|
| 171 |
auto ba = CreateDefaultBundleAdjuster(opts, config_, copy); |
| 172 |
const auto summary = ba->Solve(); |
| 173 |
|
| 174 |
state.PauseTiming(); |
| 175 |
if (summary->termination_type == |
| 176 |
BundleAdjustmentTerminationType::NO_CONVERGENCE) { |
| 177 |
state.SkipWithError("Bundle adjustment did not converge"); |
| 178 |
break; |
| 179 |
} |
| 180 |
state.ResumeTiming(); |
| 181 |
} |
| 182 |
|
| 183 |
state.PauseTiming(); |
| 184 |
ReportSceneCounters(state); |
| 185 |
state.ResumeTiming(); |
| 186 |
} |
| 187 |
#endif |
| 188 |
|
| 189 |
BENCHMARK_REGISTER_F(BM_BundleAdjustment, Ceres) |
| 190 |
->Apply(AddArguments) |
| 191 |
->Unit(benchmark::kMillisecond); |
| 192 |
|
| 193 |
#ifdef CASPAR_ENABLED |
| 194 |
BENCHMARK_REGISTER_F(BM_BundleAdjustment, Caspar) |
| 195 |
->Apply(AddArguments) |
| 196 |
->Unit(benchmark::kMillisecond); |
| 197 |
#endif |
| 198 |
|
| 199 |
int main(int argc, char** argv) { |
| 200 |
benchmark::Initialize(&argc, argv); |
| 201 |
if (benchmark::ReportUnrecognizedArguments(argc, argv)) return 1; |
| 202 |
benchmark::RunSpecifiedBenchmarks(); |
| 203 |
benchmark::Shutdown(); |
| 204 |
return 0; |
| 205 |
} |