8000 Add E2E hierarchical mapper tests by ahojnnes · Pull Request #2033 · colmap/colmap · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Add E2E hierarchical mapper tests #2033

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 1 addition & 15 deletions src/colmap/base/reconstruction_manager.cc
Original file line number Diff line number Diff line change
Expand Up 10000 @@ -36,28 +36,14 @@

namespace colmap {

ReconstructionManager::ReconstructionManager(
ReconstructionManager&& other) noexcept
: ReconstructionManager() {
reconstructions_ = std::move(other.reconstructions_);
}

ReconstructionManager& ReconstructionManager::operator=(
ReconstructionManager&& other) noexcept {
if (this != &other) {
reconstructions_ = std::move(other.reconstructions_);
}
return *this;
}

size_t ReconstructionManager::Size() const { return reconstructions_.size(); }

std::shared_ptr<const Reconstruction> ReconstructionManager::Get(
const size_t idx) const {
return reconstructions_.at(idx);
}

std::shared_ptr<Reconstruction> ReconstructionManager::Get(const size_t idx) {
std::shared_ptr<Reconstruction>& ReconstructionManager::Get(const size_t idx) {
return reconstructions_.at(idx);
}

Expand Down
10 changes: 1 addition & 9 deletions src/colmap/base/reconstruction_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,12 @@ class OptionManager;

class ReconstructionManager {
public:
ReconstructionManager() = default;

// Move constructor and assignment.
ReconstructionManager(ReconstructionManager&& other) noexcept;
ReconstructionManager& operator=(ReconstructionManager&& other) noexcept;

// The number of reconstructions managed.
size_t Size() const;

// Get a reference to a specific reconstruction.
std::shared_ptr<const Reconstruction> Get(size_t idx) const;
std::shared_ptr<Reconstruction> Get(size_t idx);
std::shared_ptr<Reconstruction>& Get(size_t idx);

// Add a new empty reconstruction and return its index.
size_t Add();
Expand All @@ -72,8 +66,6 @@ class ReconstructionManager {
void Write(const std::string& path, const OptionManager* options) const;

private:
NON_COPYABLE(ReconstructionManager)

std::vector<std::shared_ptr<Reconstruction>> reconstructions_;
};

Expand Down
1 change: 1 addition & 0 deletions src/colmap/controllers/CMakeLists.txt
10000
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,5 @@ COLMAP_ADD_SOURCES(
incremental_mapper.h incremental_mapper.cc
)

COLMAP_ADD_TEST(hierarchical_mapper_test hierarchical_mapper_test.cc)
COLMAP_ADD_TEST(incremental_mapper_test incremental_mapper_test.cc)
63 changes: 35 additions & 28 deletions src/colmap/controllers/hierarchical_mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,20 @@ void MergeClusters(const SceneClustering::Cluster& cluster,
while (reconstructions.size() > 1) {
bool merge_success = false;
for (size_t i = 0; i < reconstructions.size(); ++i) {
const int num_reg_images_i = reconstructions[i]->NumRegImages();
for (size_t j = 0; j < i; ++j) {
const double kMaxReprojError = 8.0;
const int num_reg_images_j = reconstructions[j]->NumRegImages();
if (MergeReconstructions(kMaxReprojError,
*reconstructions[j],
reconstructions[i].get())) {
std::cout
<< StringPrintf(
" => Merged clusters with %d and %d images into %d images",
num_reg_images_i,
num_reg_images_j,
reconstructions[i]->NumRegImages())
<< std::endl;
reconstructions.erase(reconstructions.begin() + j);
merge_success = true;
break;
Expand All @@ -82,10 +91,9 @@ void MergeClusters(const SceneClustering::Cluster& cluster,

// Insert a new reconstruction manager for merged cluster.
auto& reconstruction_manager = (*reconstruction_managers)[&cluster];
reconstruction_manager = std::make_shared<ReconstructionManager>();
for (const auto& reconstruction : reconstructions) {
reconstruction_manager->Add();
reconstruction_manager->Get(reconstruction_manager->Size() - 1) =
reconstruction;
reconstruction_manager->Get(reconstruction_manager->Add()) = reconstruction;
}

// Delete all merged child cluster reconstruction managers.
Expand All @@ -99,43 +107,39 @@ void MergeClusters(const SceneClustering::Cluster& cluster,
bool HierarchicalMapperController::Options::Check() const {
CHECK_OPTION_GT(init_num_trials, -1);
CHECK_OPTION_GE(num_workers, -1);
clustering_options.Check();
CHECK_EQ(clustering_options.branching, 2);
incremental_options.Check();
return true;
}

HierarchicalMapperController::HierarchicalMapperController(
const Options& options,
const SceneClustering::Options& clustering_options,
std::shared_ptr<const IncrementalMapperOptions> mapper_options,
std::shared_ptr<ReconstructionManager> reconstruction_manager)
: options_(options),
clustering_options_(clustering_options),
mapper_options_(std::move(mapper_options)),
reconstruction_manager_(std::move(reconstruction_manager)) {
CHECK(options_.Check());
CHECK(clustering_options_.Check());
CHECK_EQ(clustering_options_.branching, 2);
CHECK(mapper_options_->Check());
}

void HierarchicalMapperController::Run() {
PrintHeading1("Partitioning the scene");
PrintHeading1("Partitioning s DDB0 cene");

//////////////////////////////////////////////////////////////////////////////
// Cluster scene
// Cluster scene graph
//////////////////////////////////////////////////////////////////////////////

std::unordered_map<image_t, std::string> image_id_to_name;

Database database(options_.database_path);
const Database database(options_.database_path);

std::cout << "Reading images..." << std::endl;
const auto images = database.ReadAllImages();
std::unordered_map<image_t, std::string> image_id_to_name;
image_id_to_name.reserve(images.size());
for (const auto& image : images) {
image_id_to_name.emplace(image.ImageId(), image.Name());
}

SceneClustering scene_clustering =
SceneClustering::Create(clustering_options_, database);
SceneClustering::Create(options_.clustering_options, database);

auto leaf_clusters = scene_clustering.GetLeafClusters();

Expand Down Expand Up @@ -177,44 +181,46 @@ void HierarchicalMapperController::Run() {
return;
}

auto custom_mapper_options =
std::make_shared<IncrementalMapperOptions>(*mapper_options_);
custom_mapper_options->max_model_overlap = 3;
custom_mapper_options->init_num_trials = options_.init_num_trials;
if (custom_mapper_options->num_threads < 0) {
custom_mapper_options->num_threads = num_threads_per_worker;
auto incremental_options = std::make_shared<IncrementalMapperOptions>(
options_.incremental_options);
incremental_options->max_model_overlap = 3;
incremental_options->init_num_trials = options_.init_num_trials;
if (incremental_options->num_threads < 0) {
incremental_options->num_threads = num_threads_per_worker;
}

for (const auto image_id : cluster.image_ids) {
custom_mapper_options->image_names.insert(
incremental_options->image_names.insert(
image_id_to_name.at(image_id));
}

IncrementalMapperController mapper(std::move(custom_mapper_options),
IncrementalMapperController mapper(std::move(incremental_options),
options_.image_path,
options_.database_path,
std::move(reconstruction_manager));
mapper.Start();
mapper.Wait();
};

// Start reconstructing the bigger clusters first for resource usage.
// Start reconstructing the bigger clusters first for better resource usage.
std::sort(leaf_clusters.begin(),
leaf_clusters.end(),
[](const SceneClustering::Cluster* cluster1,
const SceneClustering::Cluster* cluster2) {
return cluster1->image_ids.size() > cluster2->image_ids.size();
});

// Start the reconstruction workers.

// Start the reconstruction workers. Use a separate reconstruction manager per
// thread to avoid race conditions.
std::unordered_map<const SceneClustering::Cluster*,
std::shared_ptr<ReconstructionManager>>
reconstruction_managers;
reconstruction_managers.reserve(leaf_clusters.size());

ThreadPool thread_pool(num_eff_workers);
for (const auto& cluster : leaf_clusters) {
reconstruction_managers[cluster] =
std::make_shared<ReconstructionManager>();
thread_pool.AddTask(
ReconstructCluster, *cluster, reconstruction_managers[cluster]);
}
Expand All @@ -229,7 +235,8 @@ void HierarchicalMapperController::Run() {
MergeClusters(*scene_clustering.GetRootCluster(), &reconstruction_managers);

CHECK_EQ(reconstruction_managers.size(), 1);
reconstruction_manager_ = std::move(reconstruction_managers.begin()->second);
CHECK_GT(reconstruction_managers.begin()->second->Get(0)->NumRegImages(), 0);
*reconstruction_manager_ = *reconstruction_managers.begin()->second;

std::cout << std::endl;
GetTimer().PrintMinutes();
Expand Down
8 changes: 6 additions & 2 deletions src/colmap/controllers/hierarchical_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,17 @@ class HierarchicalMapperController : public Thread {
// The number of workers used to reconstruct clusters in parallel.
int num_workers = -1;

// Options for clustering the scene graph.
SceneClustering::Options clustering_options;

// Options used to reconstruction each cluster individually.
IncrementalMapperOptions incremental_options;

bool Check() const;
};

HierarchicalMapperController(
const Options& options,
const SceneClustering::Options& clustering_options,
std::shared_ptr<const IncrementalMapperOptions> mapper_options,
std::shared_ptr<ReconstructionManager> reconstruction_manager);

private:
Expand Down
143 changes: 143 additions & 0 deletions src/colmap/controllers/hierarchical_mapper_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// Copyright (c) 2023, ETH Zurich and UNC Chapel Hill.
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
//
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
//
// * Neither the name of ETH Zurich and UNC Chapel Hill nor the names of
// its contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR CONTRIBUTORS BE
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
// POSSIBILITY OF SUCH DAMAGE.
//
// Author: Johannes L. Schoenberger (jsch-at-demuc-dot-de)

#include "colmap/controllers/hierarchical_mapper.h"

#include "colmap/base/alignment.h"
#include "colmap/base/synthetic.h"
#include "colmap/util/testing.h"

#include <gtest/gtest.h>

namespace colmap {

void ExpectEqualReconstructions(const Reconstruction& gt,
const Reconstruction& computed,
const double max_rotation_error_deg,
const double max_proj_center_error,
const double num_obs_tolerance) {
EXPECT_EQ(computed.NumCameras(), gt.NumCameras());
EXPECT_EQ(computed.NumImages(), gt.NumImages());
EXPECT_EQ(computed.NumRegImages(), gt.NumRegImages());
EXPECT_GE(computed.ComputeNumObservations(),
(1 - num_obs_tolerance) * gt.ComputeNumObservations());

SimilarityTransform3 gtFromComputed;
AlignReconstructions(computed,
gt,
/*max_proj_center_error=*/0.1,
&gtFromComputed);

const std::vector<ImageAlignmentError> errors =
ComputeImageAlignmentError(computed, gt, gtFromComputed);
EXPECT_EQ(errors.size(), gt.NumImages());
for (const auto& error : errors) {
EXPECT_LT(error.rotation_error_deg, max_rotation_error_deg);
EXPECT_LT(error.proj_center_error, max_proj_center_error);
}
}

TEST(HierarchicalMapperController, WithoutNoise) {
const std::string database_path = CreateTestDir() + "/database.db";

Database database(database_path);
Reconstruction gt_reconstruction;
SyntheticDatasetOptions synthetic_dataset_options;
synthetic_dataset_options.num_cameras = 2;
synthetic_dataset_options.num_images = 20;
synthetic_dataset_options.num_points3D = 50;
synthetic_dataset_options.point2D_stddev = 0;
SynthesizeDataset(synthetic_dataset_options, &gt_reconstruction, &database);

auto reconstruction_manager = std::make_shared<ReconstructionManager>();
HierarchicalMapperController::Options mapper_options;
mapper_options.database_path = database_path;
mapper_options.clustering_options.leaf_max_num_images = 5;
mapper_options.clustering_options.image_overlap = 3;
HierarchicalMapperController mapper(mapper_options, reconstruction_manager);
mapper.Start();
mapper.Wait();

ASSERT_EQ(reconstruction_manager->Size(), 1);
ExpectEqualReconstructions(gt_reconstruction,
*reconstruction_manager->Get(0),
/*max_rotation_error_deg=*/1e-2,
/*max_proj_center_error=*/1e-4,
/*num_obs_tolerance=*/0);
}

TEST(IncrementalMapperController, MultiReconstruction) {
const std::string database_path = CreateTestDir() + "/database.db";

Database database(database_path);
Reconstruction gt_reconstruction1;
Reconstruction gt_reconstruction2;
SyntheticDatasetOptions synthetic_dataset_options;
synthetic_dataset_options.num_cameras = 1;
synthetic_dataset_options.num_images = 5;
synthetic_dataset_options.num_points3D = 50;
synthetic_dataset_options.point2D_stddev = 0;
SynthesizeDataset(synthetic_dataset_options, &gt_reconstruction1, &database);
synthetic_dataset_options.num_images = 4;
SynthesizeDataset(synthetic_dataset_options, &gt_reconstruction2, &database);

auto reconstruction_manager = std::make_shared<ReconstructionManager>();
HierarchicalMapperController::Options mapper_options;
mapper_options.database_path = database_path;
mapper_options.clustering_options.leaf_max_num_images = 5;
mapper_options.clustering_options.image_overlap = 3;
HierarchicalMapperController mapper(mapper_options, reconstruction_manager);
mapper.Start();
mapper.Wait();

ASSERT_EQ(reconstruction_manager->Size(), 2);
Reconstruction* computed_reconstruction1 = nullptr;
Reconstruction* computed_reconstruction2 = nullptr;
if (reconstruction_manager->Get(0)->NumRegImages() == 5) {
computed_reconstruction1 = reconstruction_manager->Get(0).get();
computed_reconstruction2 = reconstruction_manager->Get(1).get();
} else {
computed_reconstruction1 = reconstruction_manager->Get(1).get();
computed_reconstruction2 = reconstruction_manager->Get(0).get();
}
ExpectEqualReconstructions(gt_reconstruction1,
*computed_reconstruction1,
/*max_rotation_error_deg=*/1e-2,
/*max_proj_center_error=*/1e-4,
/*num_obs_tolerance=*/0);
ExpectEqualReconstructions(gt_reconstruction2,
*computed_reconstruction2,
/*max_rotation_error_deg=*/1e-2,
/*max_proj_center_error=*/1e-4,
/*num_obs_tolerance=*/0);
}

} // namespace colmap
Loading
0