Files
ceres-solver/internal/ceres/visibility_based_preconditioner.h
T

202 lines
8.7 KiB
C++
Raw Normal View History

2012-04-30 23:09:08 -07:00
// Ceres Solver - A fast non-linear least squares minimizer
2017-04-12 12:48:44 -07:00
// Copyright 2017 Google Inc. All rights reserved.
// http://ceres-solver.org/
2012-04-30 23:09:08 -07:00
//
// 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 Google Inc. 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 OWNER 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: sameeragarwal@google.com (Sameer Agarwal)
//
// Preconditioners for linear systems that arise in Structure from
2013-02-17 16:50:37 -08:00
// Motion problems. VisibilityBasedPreconditioner implements:
2012-04-30 23:09:08 -07:00
//
// CLUSTER_JACOBI
// CLUSTER_TRIDIAGONAL
//
// Detailed descriptions of these preconditions beyond what is
// documented here can be found in
//
// Visibility Based Preconditioning for Bundle Adjustment
// A. Kushal & S. Agarwal, CVPR 2012.
//
2012-04-30 23:09:08 -07:00
// http://www.cs.washington.edu/homes/sagarwal/vbp.pdf
//
2013-02-17 16:50:37 -08:00
// The two preconditioners share enough code that its most efficient
2012-04-30 23:09:08 -07:00
// to implement them as part of the same code base.
#ifndef CERES_INTERNAL_VISIBILITY_BASED_PRECONDITIONER_H_
#define CERES_INTERNAL_VISIBILITY_BASED_PRECONDITIONER_H_
2018-03-30 16:16:59 -07:00
#include <memory>
2012-04-30 23:09:08 -07:00
#include <set>
2018-03-29 22:01:29 -07:00
#include <unordered_map>
#include <unordered_set>
2012-04-30 23:09:08 -07:00
#include <utility>
2017-04-12 12:48:44 -07:00
#include <vector>
2018-03-30 16:16:59 -07:00
2012-04-30 23:09:08 -07:00
#include "ceres/graph.h"
#include "ceres/linear_solver.h"
2018-03-29 22:01:29 -07:00
#include "ceres/pair_hash.h"
2013-02-17 16:50:37 -08:00
#include "ceres/preconditioner.h"
2017-04-12 12:48:44 -07:00
#include "ceres/sparse_cholesky.h"
2012-04-30 23:09:08 -07:00
namespace ceres {
namespace internal {
class BlockRandomAccessSparseMatrix;
2013-04-24 11:58:24 -07:00
class BlockSparseMatrix;
2012-09-23 18:19:49 -07:00
struct CompressedRowBlockStructure;
2012-04-30 23:09:08 -07:00
class SchurEliminatorBase;
2013-02-17 16:50:37 -08:00
// This class implements visibility based preconditioners for
// Structure from Motion/Bundle Adjustment problems. The name
2012-04-30 23:09:08 -07:00
// VisibilityBasedPreconditioner comes from the fact that the sparsity
// structure of the preconditioner matrix is determined by analyzing
// the visibility structure of the scene, i.e. which cameras see which
// points.
//
// The key idea of visibility based preconditioning is to identify
// cameras that we expect have strong interactions, and then using the
// entries in the Schur complement matrix corresponding to these
// camera pairs as an approximation to the full Schur complement.
//
// CLUSTER_JACOBI identifies these camera pairs by clustering cameras,
// and considering all non-zero camera pairs within each cluster. The
// clustering in the current implementation is done using the
// Canonical Views algorithm of Simon et al. (see
// canonical_views_clustering.h). For the purposes of clustering, the
// similarity or the degree of interaction between a pair of cameras
// is measured by counting the number of points visible in both the
// cameras. Thus the name VisibilityBasedPreconditioner. Further, if we
// were to permute the parameter blocks such that all the cameras in
// the same cluster occur contiguously, the preconditioner matrix will
// be a block diagonal matrix with blocks corresponding to the
// clusters. Thus in analogy with the Jacobi preconditioner we refer
// to this as the CLUSTER_JACOBI preconditioner.
//
// CLUSTER_TRIDIAGONAL adds more mass to the CLUSTER_JACOBI
// preconditioner by considering the interaction between clusters and
// identifying strong interactions between cluster pairs. This is done
// by constructing a weighted graph on the clusters, with the weight
// on the edges connecting two clusters proportional to the number of
// 3D points visible to cameras in both the clusters. A degree-2
// maximum spanning forest is identified in this graph and the camera
// pairs contained in the edges of this forest are added to the
// preconditioner. The detailed reasoning for this construction is
// explained in the paper mentioned above.
//
// Degree-2 spanning trees and forests have the property that they
// correspond to tri-diagonal matrices. Thus there exist a permutation
// of the camera blocks under which the CLUSTER_TRIDIAGONAL
// preconditioner matrix is a block tridiagonal matrix, and thus the
// name for the preconditioner.
//
// Thread Safety: This class is NOT thread safe.
//
// Example usage:
//
// LinearSolver::Options options;
// options.preconditioner_type = CLUSTER_JACOBI;
2013-02-17 16:50:37 -08:00
// options.elimination_groups.push_back(num_points);
// options.elimination_groups.push_back(num_cameras);
2012-04-30 23:09:08 -07:00
// VisibilityBasedPreconditioner preconditioner(
// *A.block_structure(), options);
// preconditioner.Update(A, NULL);
2012-04-30 23:09:08 -07:00
// preconditioner.RightMultiply(x, y);
2013-06-13 23:34:46 -07:00
class VisibilityBasedPreconditioner : public BlockSparseMatrixPreconditioner {
2012-04-30 23:09:08 -07:00
public:
// Initialize the symbolic structure of the preconditioner. bs is
// the block structure of the linear system to be solved. It is used
// to determine the sparsity structure of the preconditioner matrix.
//
// It has the same structural requirement as other Schur complement
// based solvers. Please see schur_eliminator.h for more details.
VisibilityBasedPreconditioner(const CompressedRowBlockStructure& bs,
2013-02-17 16:50:37 -08:00
const Preconditioner::Options& options);
2018-04-12 20:44:56 -07:00
VisibilityBasedPreconditioner(const VisibilityBasedPreconditioner&) = delete;
void operator=(const VisibilityBasedPreconditioner&) = delete;
2012-04-30 23:09:08 -07:00
virtual ~VisibilityBasedPreconditioner();
2013-02-17 16:50:37 -08:00
// Preconditioner interface
void RightMultiply(const double* x, double* y) const final;
int num_rows() const final;
2012-04-30 23:09:08 -07:00
friend class VisibilityBasedPreconditionerTest;
2013-06-13 23:34:46 -07:00
2012-04-30 23:09:08 -07:00
private:
bool UpdateImpl(const BlockSparseMatrix& A, const double* D) final;
2012-04-30 23:09:08 -07:00
void ComputeClusterJacobiSparsity(const CompressedRowBlockStructure& bs);
void ComputeClusterTridiagonalSparsity(const CompressedRowBlockStructure& bs);
void InitStorage(const CompressedRowBlockStructure& bs);
void InitEliminator(const CompressedRowBlockStructure& bs);
LinearSolverTerminationType Factorize();
2012-04-30 23:09:08 -07:00
void ScaleOffDiagonalCells();
2018-04-03 10:41:01 -07:00
void ClusterCameras(const std::vector<std::set<int>>& visibility);
2018-03-29 22:01:29 -07:00
void FlattenMembershipMap(const std::unordered_map<int, int>& membership_map,
2014-12-17 07:35:09 -08:00
std::vector<int>* membership_vector) const;
void ComputeClusterVisibility(
2018-04-03 10:41:01 -07:00
const std::vector<std::set<int>>& visibility,
std::vector<std::set<int>>* cluster_visibility) const;
2014-08-28 13:03:40 -07:00
WeightedGraph<int>* CreateClusterGraph(
2018-04-03 10:41:01 -07:00
const std::vector<std::set<int>>& visibility) const;
2014-08-17 13:14:50 -07:00
void ForestToClusterPairs(const WeightedGraph<int>& forest,
2018-03-29 22:01:29 -07:00
std::unordered_set<std::pair<int, int>, pair_hash>* cluster_pairs) const;
2012-04-30 23:09:08 -07:00
void ComputeBlockPairsInPreconditioner(const CompressedRowBlockStructure& bs);
bool IsBlockPairInPreconditioner(int block1, int block2) const;
bool IsBlockPairOffDiagonal(int block1, int block2) const;
2013-02-17 16:50:37 -08:00
Preconditioner::Options options_;
2012-04-30 23:09:08 -07:00
// Number of parameter blocks in the schur complement.
int num_blocks_;
int num_clusters_;
// Sizes of the blocks in the schur complement.
2014-12-17 07:35:09 -08:00
std::vector<int> block_size_;
2012-04-30 23:09:08 -07:00
// Mapping from cameras to clusters.
2014-12-17 07:35:09 -08:00
std::vector<int> cluster_membership_;
2012-04-30 23:09:08 -07:00
// Non-zero camera pairs from the schur complement matrix that are
// present in the preconditioner, sorted by row (first element of
// each pair), then column (second).
2018-04-03 10:41:01 -07:00
std::set<std::pair<int, int>> block_pairs_;
2012-04-30 23:09:08 -07:00
// Set of cluster pairs (including self pairs (i,i)) in the
// preconditioner.
2018-03-29 22:01:29 -07:00
std::unordered_set<std::pair<int, int>, pair_hash> cluster_pairs_;
2018-03-30 16:16:59 -07:00
std::unique_ptr<SchurEliminatorBase> eliminator_;
2012-04-30 23:09:08 -07:00
// Preconditioner matrix.
2018-03-30 16:16:59 -07:00
std::unique_ptr<BlockRandomAccessSparseMatrix> m_;
std::unique_ptr<SparseCholesky> sparse_cholesky_;
2012-04-30 23:09:08 -07:00
};
} // namespace internal
} // namespace ceres
#endif // CERES_INTERNAL_VISIBILITY_BASED_PRECONDITIONER_H_