Skip to content
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

Add to IncrementalMapper reusage of Ceres internal context #1593

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
9 changes: 6 additions & 3 deletions src/estimators/pose.cc
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ bool RefineAbsolutePose(const AbsolutePoseRefinementOptions& options,
const std::vector<Eigen::Vector2d>& points2D,
const std::vector<Eigen::Vector3d>& points3D,
Eigen::Vector4d* qvec, Eigen::Vector3d* tvec,
Camera* camera, Eigen::Matrix6d* rot_tvec_covariance) {
Camera* camera, Eigen::Matrix6d* rot_tvec_covariance,
ceres::Context* ceres_context) {
CHECK_EQ(inlier_mask.size(), points2D.size());
CHECK_EQ(points2D.size(), points3D.size());
options.Check();
Expand All @@ -214,7 +215,9 @@ bool RefineAbsolutePose(const AbsolutePoseRefinementOptions& options,

std::vector<Eigen::Vector3d> points3D_copy = points3D;

ceres::Problem problem;
ceres::Problem::Options problem_options;
problem_options.context = ceres_context;
ceres::Problem problem(problem_options);

for (size_t i = 0; i < points2D.size(); ++i) {
// Skip outlier observations
Expand Down Expand Up @@ -287,7 +290,7 @@ bool RefineAbsolutePose(const AbsolutePoseRefinementOptions& options,
solver_options.linear_solver_type = ceres::DENSE_QR;

// The overhead of creating threads is too large.
solver_options.num_threads = 1;
solver_options.num_threads = GetEffectiveNumThreads(options.num_threads);
#if CERES_VERSION_MAJOR < 2
solver_options.num_linear_solver_threads = 1;
#endif // CERES_VERSION_MAJOR
Expand Down
7 changes: 6 additions & 1 deletion src/estimators/pose.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ struct AbsolutePoseRefinementOptions {
// Whether to print final summary.
bool print_summary = true;

// Number of threads to use during refinement.
int num_threads = 1;

void Check() const {
CHECK_GE(gradient_tolerance, 0.0);
CHECK_GE(max_num_iterations, 0);
Expand Down Expand Up @@ -162,6 +165,7 @@ size_t EstimateRelativePose(const RANSACOptions& ransac_options,
// @param rot_tvec_covariance Estimated 6x6 covariance matrix of
// the rotation (as axis-angle, in tangent space)
// and translation terms (optional).
// @param ceres_context Ceres Solver internal context (optional).
//
// @return Whether the solution is usable.
bool RefineAbsolutePose(const AbsolutePoseRefinementOptions& options,
Expand All @@ -170,7 +174,8 @@ bool RefineAbsolutePose(const AbsolutePoseRefinementOptions& options,
const std::vector<Eigen::Vector3d>& points3D,
Eigen::Vector4d* qvec, Eigen::Vector3d* tvec,
Camera* camera,
Eigen::Matrix6d* rot_tvec_covariance = nullptr);
Eigen::Matrix6d* rot_tvec_covariance = nullptr,
ceres::Context* ceres_context = nullptr);

// Refine relative pose of two cameras.
//
Expand Down
13 changes: 11 additions & 2 deletions src/optim/bundle_adjustment.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,10 @@ bool BundleAdjustmentOptions::Check() const {
// BundleAdjustmentConfig
////////////////////////////////////////////////////////////////////////////////

BundleAdjustmentConfig::BundleAdjustmentConfig() {}
BundleAdjustmentConfig::BundleAdjustmentConfig() : ceres_context_(nullptr) {}

BundleAdjustmentConfig::BundleAdjustmentConfig(ceres::Context* ceres_context)
: ceres_context_(ceres_context) {}

size_t BundleAdjustmentConfig::NumImages() const { return image_ids_.size(); }

Expand Down Expand Up @@ -245,6 +248,10 @@ void BundleAdjustmentConfig::RemoveConstantPoint(const point3D_t point3D_id) {
constant_point3D_ids_.erase(point3D_id);
}

ceres::Context* BundleAdjustmentConfig::GetCeresContext() const {
return ceres_context_;
}

////////////////////////////////////////////////////////////////////////////////
// BundleAdjuster
////////////////////////////////////////////////////////////////////////////////
Expand All @@ -259,7 +266,9 @@ bool BundleAdjuster::Solve(Reconstruction* reconstruction) {
CHECK_NOTNULL(reconstruction);
CHECK(!problem_) << "Cannot use the same BundleAdjuster multiple times";

problem_.reset(new ceres::Problem());
ceres::Problem::Options problem_options;
problem_options.context = config_.GetCeresContext();
problem_.reset(new ceres::Problem(problem_options));

ceres::LossFunction* loss_function = options_.CreateLossFunction();
SetUp(reconstruction, loss_function);
Expand Down
3 changes: 3 additions & 0 deletions src/optim/bundle_adjustment.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ struct BundleAdjustmentOptions {
class BundleAdjustmentConfig {
public:
BundleAdjustmentConfig();
explicit BundleAdjustmentConfig(ceres::Context *ceres_context);

size_t NumImages() const;
size_t NumPoints() const;
Expand Down Expand Up @@ -157,13 +158,15 @@ class BundleAdjustmentConfig {
const std::unordered_set<point3D_t>& ConstantPoints() const;
const std::vector<int>& ConstantTvec(const image_t image_id) const;

ceres::Context* GetCeresContext() const;
private:
std::unordered_set<camera_t> constant_camera_ids_;
std::unordered_set<image_t> image_ids_;
std::unordered_set<point3D_t> variable_point3D_ids_;
std::unordered_set<point3D_t> constant_point3D_ids_;
std::unordered_set<image_t> constant_poses_;
std::unordered_map<image_t, std::vector<int>> constant_tvecs_;
ceres::Context* ceres_context_;
};

// Bundle adjustment based on Ceres-Solver. Enables most flexible configurations
Expand Down
11 changes: 7 additions & 4 deletions src/sfm/incremental_mapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ IncrementalMapper::IncrementalMapper(const DatabaseCache* database_cache)
triangulator_(nullptr),
num_total_reg_images_(0),
num_shared_reg_images_(0),
prev_init_image_pair_id_(kInvalidImagePairId) {}
prev_init_image_pair_id_(kInvalidImagePairId),
ceres_context_(ceres::Context::Create()) {}

void IncrementalMapper::BeginReconstruction(Reconstruction* reconstruction) {
CHECK(reconstruction_ == nullptr);
Expand Down Expand Up @@ -450,6 +451,7 @@ bool IncrementalMapper::RegisterNextImage(const Options& options,
abs_pose_options.ransac_options.confidence = 0.99999;

AbsolutePoseRefinementOptions abs_pose_refinement_options;
abs_pose_refinement_options.num_threads = options.num_threads;
if (num_reg_images_per_camera_[image.CameraId()] > 0) {
// Camera already refined from another image with the same camera.
if (camera.HasBogusParams(options.min_focal_length_ratio,
Expand Down Expand Up @@ -504,7 +506,8 @@ bool IncrementalMapper::RegisterNextImage(const Options& options,

if (!RefineAbsolutePose(abs_pose_refinement_options, inlier_mask,
tri_points2D, tri_points3D, &image.Qvec(),
&image.Tvec(), &camera)) {
&image.Tvec(), &camera, nullptr,
ceres_context_.get())) {
return false;
}

Expand Down Expand Up @@ -571,7 +574,7 @@ IncrementalMapper::AdjustLocalBundle(

// Do the bundle adjustment only if there is any connected images.
if (local_bundle.size() > 0) {
BundleAdjustmentConfig ba_config;
BundleAdjustmentConfig ba_config(ceres_context_.get());
ba_config.AddImage(image_id);
for (const image_t local_image_id : local_bundle) {
ba_config.AddImage(local_image_id);
Expand Down Expand Up @@ -682,7 +685,7 @@ bool IncrementalMapper::AdjustGlobalBundle(
reconstruction_->FilterObservationsWithNegativeDepth();

// Configure bundle adjustment.
BundleAdjustmentConfig ba_config;
BundleAdjustmentConfig ba_config(ceres_context_.get());
for (const image_t image_id : reg_image_ids) {
ba_config.AddImage(image_id);
}
Expand Down
4 changes: 4 additions & 0 deletions src/sfm/incremental_mapper.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#ifndef COLMAP_SRC_SFM_INCREMENTAL_MAPPER_H_
#define COLMAP_SRC_SFM_INCREMENTAL_MAPPER_H_

#include <ceres/context.h>

#include "base/database.h"
#include "base/database_cache.h"
#include "base/reconstruction.h"
Expand Down Expand Up @@ -306,6 +308,8 @@ class IncrementalMapper {
// This image list will be non-empty, if the reconstruction is continued from
// an existing reconstruction.
std::unordered_set<image_t> existing_image_ids_;

std::unique_ptr<ceres::Context> ceres_context_;
};

} // namespace colmap
Expand Down