Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 3 | // http://ceres-solver.org/ |
| 4 | // |
| 5 | // Redistribution and use in source and binary forms, with or without |
| 6 | // modification, are permitted provided that the following conditions are met: |
| 7 | // |
| 8 | // * Redistributions of source code must retain the above copyright notice, |
| 9 | // this list of conditions and the following disclaimer. |
| 10 | // * Redistributions in binary form must reproduce the above copyright notice, |
| 11 | // this list of conditions and the following disclaimer in the documentation |
| 12 | // and/or other materials provided with the distribution. |
| 13 | // * Neither the name of Google Inc. nor the names of its contributors may be |
| 14 | // used to endorse or promote products derived from this software without |
| 15 | // specific prior written permission. |
| 16 | // |
| 17 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 18 | // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 19 | // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 20 | // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE |
| 21 | // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 22 | // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 23 | // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 24 | // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 25 | // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 26 | // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| 27 | // POSSIBILITY OF SUCH DAMAGE. |
| 28 | // |
| 29 | // Author: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | |
| 31 | #include "ceres/visibility_based_preconditioner.h" |
| 32 | |
| 33 | #include <algorithm> |
| 34 | #include <functional> |
| 35 | #include <iterator> |
| 36 | #include <memory> |
| 37 | #include <set> |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 38 | #include <string> |
| 39 | #include <unordered_set> |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | #include <utility> |
| 41 | #include <vector> |
| 42 | |
| 43 | #include "Eigen/Dense" |
| 44 | #include "ceres/block_random_access_sparse_matrix.h" |
| 45 | #include "ceres/block_sparse_matrix.h" |
| 46 | #include "ceres/canonical_views_clustering.h" |
| 47 | #include "ceres/graph.h" |
| 48 | #include "ceres/graph_algorithms.h" |
| 49 | #include "ceres/linear_solver.h" |
| 50 | #include "ceres/schur_eliminator.h" |
| 51 | #include "ceres/single_linkage_clustering.h" |
| 52 | #include "ceres/visibility.h" |
| 53 | #include "glog/logging.h" |
| 54 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 55 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 56 | |
| 57 | // TODO(sameeragarwal): Currently these are magic weights for the |
| 58 | // preconditioner construction. Move these higher up into the Options |
| 59 | // struct and provide some guidelines for choosing them. |
| 60 | // |
| 61 | // This will require some more work on the clustering algorithm and |
| 62 | // possibly some more refactoring of the code. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 63 | static constexpr double kCanonicalViewsSizePenaltyWeight = 3.0; |
| 64 | static constexpr double kCanonicalViewsSimilarityPenaltyWeight = 0.0; |
| 65 | static constexpr double kSingleLinkageMinSimilarity = 0.9; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 66 | |
| 67 | VisibilityBasedPreconditioner::VisibilityBasedPreconditioner( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 68 | const CompressedRowBlockStructure& bs, Preconditioner::Options options) |
| 69 | : options_(std::move(options)), num_blocks_(0), num_clusters_(0) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | CHECK_GT(options_.elimination_groups.size(), 1); |
| 71 | CHECK_GT(options_.elimination_groups[0], 0); |
| 72 | CHECK(options_.type == CLUSTER_JACOBI || options_.type == CLUSTER_TRIDIAGONAL) |
| 73 | << "Unknown preconditioner type: " << options_.type; |
| 74 | num_blocks_ = bs.cols.size() - options_.elimination_groups[0]; |
| 75 | CHECK_GT(num_blocks_, 0) << "Jacobian should have at least 1 f_block for " |
| 76 | << "visibility based preconditioning."; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 77 | CHECK(options_.context != nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 78 | |
| 79 | // Vector of camera block sizes |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 80 | blocks_ = Tail(bs.cols, bs.cols.size() - options_.elimination_groups[0]); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 82 | const time_t start_time = time(nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 83 | switch (options_.type) { |
| 84 | case CLUSTER_JACOBI: |
| 85 | ComputeClusterJacobiSparsity(bs); |
| 86 | break; |
| 87 | case CLUSTER_TRIDIAGONAL: |
| 88 | ComputeClusterTridiagonalSparsity(bs); |
| 89 | break; |
| 90 | default: |
| 91 | LOG(FATAL) << "Unknown preconditioner type"; |
| 92 | } |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 93 | const time_t structure_time = time(nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 94 | InitStorage(bs); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 95 | const time_t storage_time = time(nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 96 | InitEliminator(bs); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 97 | const time_t eliminator_time = time(nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 98 | |
| 99 | LinearSolver::Options sparse_cholesky_options; |
| 100 | sparse_cholesky_options.sparse_linear_algebra_library_type = |
| 101 | options_.sparse_linear_algebra_library_type; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 102 | sparse_cholesky_options.ordering_type = options_.ordering_type; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 103 | sparse_cholesky_ = SparseCholesky::Create(sparse_cholesky_options); |
| 104 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 105 | const time_t init_time = time(nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 106 | VLOG(2) << "init time: " << init_time - start_time |
| 107 | << " structure time: " << structure_time - start_time |
| 108 | << " storage time:" << storage_time - structure_time |
| 109 | << " eliminator time: " << eliminator_time - storage_time; |
| 110 | } |
| 111 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 112 | VisibilityBasedPreconditioner::~VisibilityBasedPreconditioner() = default; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 113 | |
| 114 | // Determine the sparsity structure of the CLUSTER_JACOBI |
| 115 | // preconditioner. It clusters cameras using their scene |
| 116 | // visibility. The clusters form the diagonal blocks of the |
| 117 | // preconditioner matrix. |
| 118 | void VisibilityBasedPreconditioner::ComputeClusterJacobiSparsity( |
| 119 | const CompressedRowBlockStructure& bs) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 120 | std::vector<std::set<int>> visibility; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 121 | ComputeVisibility(bs, options_.elimination_groups[0], &visibility); |
| 122 | CHECK_EQ(num_blocks_, visibility.size()); |
| 123 | ClusterCameras(visibility); |
| 124 | cluster_pairs_.clear(); |
| 125 | for (int i = 0; i < num_clusters_; ++i) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 126 | cluster_pairs_.insert(std::make_pair(i, i)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | } |
| 128 | } |
| 129 | |
| 130 | // Determine the sparsity structure of the CLUSTER_TRIDIAGONAL |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 131 | // preconditioner. It clusters cameras using the scene visibility and |
| 132 | // then finds the strongly interacting pairs of clusters by |
| 133 | // constructing another graph with the clusters as vertices and |
| 134 | // approximating it with a degree-2 maximum spanning forest. The set |
| 135 | // of edges in this forest are the cluster pairs. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 136 | void VisibilityBasedPreconditioner::ComputeClusterTridiagonalSparsity( |
| 137 | const CompressedRowBlockStructure& bs) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 138 | std::vector<std::set<int>> visibility; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 139 | ComputeVisibility(bs, options_.elimination_groups[0], &visibility); |
| 140 | CHECK_EQ(num_blocks_, visibility.size()); |
| 141 | ClusterCameras(visibility); |
| 142 | |
| 143 | // Construct a weighted graph on the set of clusters, where the |
| 144 | // edges are the number of 3D points/e_blocks visible in both the |
| 145 | // clusters at the ends of the edge. Return an approximate degree-2 |
| 146 | // maximum spanning forest of this graph. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 147 | std::vector<std::set<int>> cluster_visibility; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 148 | ComputeClusterVisibility(visibility, &cluster_visibility); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 149 | auto cluster_graph = CreateClusterGraph(cluster_visibility); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 150 | CHECK(cluster_graph != nullptr); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 151 | auto forest = Degree2MaximumSpanningForest(*cluster_graph); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 152 | CHECK(forest != nullptr); |
| 153 | ForestToClusterPairs(*forest, &cluster_pairs_); |
| 154 | } |
| 155 | |
| 156 | // Allocate storage for the preconditioner matrix. |
| 157 | void VisibilityBasedPreconditioner::InitStorage( |
| 158 | const CompressedRowBlockStructure& bs) { |
| 159 | ComputeBlockPairsInPreconditioner(bs); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 160 | m_ = std::make_unique<BlockRandomAccessSparseMatrix>( |
| 161 | blocks_, block_pairs_, options_.context, options_.num_threads); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 162 | } |
| 163 | |
| 164 | // Call the canonical views algorithm and cluster the cameras based on |
| 165 | // their visibility sets. The visibility set of a camera is the set of |
| 166 | // e_blocks/3D points in the scene that are seen by it. |
| 167 | // |
| 168 | // The cluster_membership_ vector is updated to indicate cluster |
| 169 | // memberships for each camera block. |
| 170 | void VisibilityBasedPreconditioner::ClusterCameras( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 171 | const std::vector<std::set<int>>& visibility) { |
| 172 | auto schur_complement_graph = CreateSchurComplementGraph(visibility); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 173 | CHECK(schur_complement_graph != nullptr); |
| 174 | |
| 175 | std::unordered_map<int, int> membership; |
| 176 | |
| 177 | if (options_.visibility_clustering_type == CANONICAL_VIEWS) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 178 | std::vector<int> centers; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 179 | CanonicalViewsClusteringOptions clustering_options; |
| 180 | clustering_options.size_penalty_weight = kCanonicalViewsSizePenaltyWeight; |
| 181 | clustering_options.similarity_penalty_weight = |
| 182 | kCanonicalViewsSimilarityPenaltyWeight; |
| 183 | ComputeCanonicalViewsClustering( |
| 184 | clustering_options, *schur_complement_graph, ¢ers, &membership); |
| 185 | num_clusters_ = centers.size(); |
| 186 | } else if (options_.visibility_clustering_type == SINGLE_LINKAGE) { |
| 187 | SingleLinkageClusteringOptions clustering_options; |
| 188 | clustering_options.min_similarity = kSingleLinkageMinSimilarity; |
| 189 | num_clusters_ = ComputeSingleLinkageClustering( |
| 190 | clustering_options, *schur_complement_graph, &membership); |
| 191 | } else { |
| 192 | LOG(FATAL) << "Unknown visibility clustering algorithm."; |
| 193 | } |
| 194 | |
| 195 | CHECK_GT(num_clusters_, 0); |
| 196 | VLOG(2) << "num_clusters: " << num_clusters_; |
| 197 | FlattenMembershipMap(membership, &cluster_membership_); |
| 198 | } |
| 199 | |
| 200 | // Compute the block sparsity structure of the Schur complement |
| 201 | // matrix. For each pair of cameras contributing a non-zero cell to |
| 202 | // the schur complement, determine if that cell is present in the |
| 203 | // preconditioner or not. |
| 204 | // |
| 205 | // A pair of cameras contribute a cell to the preconditioner if they |
| 206 | // are part of the same cluster or if the two clusters that they |
| 207 | // belong have an edge connecting them in the degree-2 maximum |
| 208 | // spanning forest. |
| 209 | // |
| 210 | // For example, a camera pair (i,j) where i belongs to cluster1 and |
| 211 | // j belongs to cluster2 (assume that cluster1 < cluster2). |
| 212 | // |
| 213 | // The cell corresponding to (i,j) is present in the preconditioner |
| 214 | // if cluster1 == cluster2 or the pair (cluster1, cluster2) were |
| 215 | // connected by an edge in the degree-2 maximum spanning forest. |
| 216 | // |
| 217 | // Since we have already expanded the forest into a set of camera |
| 218 | // pairs/edges, including self edges, the check can be reduced to |
| 219 | // checking membership of (cluster1, cluster2) in cluster_pairs_. |
| 220 | void VisibilityBasedPreconditioner::ComputeBlockPairsInPreconditioner( |
| 221 | const CompressedRowBlockStructure& bs) { |
| 222 | block_pairs_.clear(); |
| 223 | for (int i = 0; i < num_blocks_; ++i) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 224 | block_pairs_.insert(std::make_pair(i, i)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | int r = 0; |
| 228 | const int num_row_blocks = bs.rows.size(); |
| 229 | const int num_eliminate_blocks = options_.elimination_groups[0]; |
| 230 | |
| 231 | // Iterate over each row of the matrix. The block structure of the |
| 232 | // matrix is assumed to be sorted in order of the e_blocks/point |
| 233 | // blocks. Thus all row blocks containing an e_block/point occur |
| 234 | // contiguously. Further, if present, an e_block is always the first |
| 235 | // parameter block in each row block. These structural assumptions |
| 236 | // are common to all Schur complement based solvers in Ceres. |
| 237 | // |
| 238 | // For each e_block/point block we identify the set of cameras |
| 239 | // seeing it. The cross product of this set with itself is the set |
| 240 | // of non-zero cells contributed by this e_block. |
| 241 | // |
| 242 | // The time complexity of this is O(nm^2) where, n is the number of |
| 243 | // 3d points and m is the maximum number of cameras seeing any |
| 244 | // point, which for most scenes is a fairly small number. |
| 245 | while (r < num_row_blocks) { |
| 246 | int e_block_id = bs.rows[r].cells.front().block_id; |
| 247 | if (e_block_id >= num_eliminate_blocks) { |
| 248 | // Skip the rows whose first block is an f_block. |
| 249 | break; |
| 250 | } |
| 251 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 252 | std::set<int> f_blocks; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 253 | for (; r < num_row_blocks; ++r) { |
| 254 | const CompressedRow& row = bs.rows[r]; |
| 255 | if (row.cells.front().block_id != e_block_id) { |
| 256 | break; |
| 257 | } |
| 258 | |
| 259 | // Iterate over the blocks in the row, ignoring the first block |
| 260 | // since it is the one to be eliminated and adding the rest to |
| 261 | // the list of f_blocks associated with this e_block. |
| 262 | for (int c = 1; c < row.cells.size(); ++c) { |
| 263 | const Cell& cell = row.cells[c]; |
| 264 | const int f_block_id = cell.block_id - num_eliminate_blocks; |
| 265 | CHECK_GE(f_block_id, 0); |
| 266 | f_blocks.insert(f_block_id); |
| 267 | } |
| 268 | } |
| 269 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 270 | for (auto block1 = f_blocks.begin(); block1 != f_blocks.end(); ++block1) { |
| 271 | auto block2 = block1; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 272 | ++block2; |
| 273 | for (; block2 != f_blocks.end(); ++block2) { |
| 274 | if (IsBlockPairInPreconditioner(*block1, *block2)) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 275 | block_pairs_.emplace(*block1, *block2); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 276 | } |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | // The remaining rows which do not contain any e_blocks. |
| 282 | for (; r < num_row_blocks; ++r) { |
| 283 | const CompressedRow& row = bs.rows[r]; |
| 284 | CHECK_GE(row.cells.front().block_id, num_eliminate_blocks); |
| 285 | for (int i = 0; i < row.cells.size(); ++i) { |
| 286 | const int block1 = row.cells[i].block_id - num_eliminate_blocks; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 287 | for (const auto& cell : row.cells) { |
| 288 | const int block2 = cell.block_id - num_eliminate_blocks; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 289 | if (block1 <= block2) { |
| 290 | if (IsBlockPairInPreconditioner(block1, block2)) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 291 | block_pairs_.insert(std::make_pair(block1, block2)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 292 | } |
| 293 | } |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | VLOG(1) << "Block pair stats: " << block_pairs_.size(); |
| 299 | } |
| 300 | |
| 301 | // Initialize the SchurEliminator. |
| 302 | void VisibilityBasedPreconditioner::InitEliminator( |
| 303 | const CompressedRowBlockStructure& bs) { |
| 304 | LinearSolver::Options eliminator_options; |
| 305 | eliminator_options.elimination_groups = options_.elimination_groups; |
| 306 | eliminator_options.num_threads = options_.num_threads; |
| 307 | eliminator_options.e_block_size = options_.e_block_size; |
| 308 | eliminator_options.f_block_size = options_.f_block_size; |
| 309 | eliminator_options.row_block_size = options_.row_block_size; |
| 310 | eliminator_options.context = options_.context; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 311 | eliminator_ = SchurEliminatorBase::Create(eliminator_options); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 312 | const bool kFullRankETE = true; |
| 313 | eliminator_->Init( |
| 314 | eliminator_options.elimination_groups[0], kFullRankETE, &bs); |
| 315 | } |
| 316 | |
| 317 | // Update the values of the preconditioner matrix and factorize it. |
| 318 | bool VisibilityBasedPreconditioner::UpdateImpl(const BlockSparseMatrix& A, |
| 319 | const double* D) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 320 | const time_t start_time = time(nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 321 | const int num_rows = m_->num_rows(); |
| 322 | CHECK_GT(num_rows, 0); |
| 323 | |
| 324 | // Compute a subset of the entries of the Schur complement. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 325 | eliminator_->Eliminate( |
| 326 | BlockSparseMatrixData(A), nullptr, D, m_.get(), nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 327 | |
| 328 | // Try factorizing the matrix. For CLUSTER_JACOBI, this should |
| 329 | // always succeed modulo some numerical/conditioning problems. For |
| 330 | // CLUSTER_TRIDIAGONAL, in general the preconditioner matrix as |
| 331 | // constructed is not positive definite. However, we will go ahead |
| 332 | // and try factorizing it. If it works, great, otherwise we scale |
| 333 | // all the cells in the preconditioner corresponding to the edges in |
| 334 | // the degree-2 forest and that guarantees positive |
| 335 | // definiteness. The proof of this fact can be found in Lemma 1 in |
| 336 | // "Visibility Based Preconditioning for Bundle Adjustment". |
| 337 | // |
| 338 | // Doing the factorization like this saves us matrix mass when |
| 339 | // scaling is not needed, which is quite often in our experience. |
| 340 | LinearSolverTerminationType status = Factorize(); |
| 341 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 342 | if (status == LinearSolverTerminationType::FATAL_ERROR) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 343 | return false; |
| 344 | } |
| 345 | |
| 346 | // The scaling only affects the tri-diagonal case, since |
| 347 | // ScaleOffDiagonalBlocks only pays attention to the cells that |
| 348 | // belong to the edges of the degree-2 forest. In the CLUSTER_JACOBI |
| 349 | // case, the preconditioner is guaranteed to be positive |
| 350 | // semidefinite. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 351 | if (status == LinearSolverTerminationType::FAILURE && |
| 352 | options_.type == CLUSTER_TRIDIAGONAL) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 353 | VLOG(1) << "Unscaled factorization failed. Retrying with off-diagonal " |
| 354 | << "scaling"; |
| 355 | ScaleOffDiagonalCells(); |
| 356 | status = Factorize(); |
| 357 | } |
| 358 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 359 | VLOG(2) << "Compute time: " << time(nullptr) - start_time; |
| 360 | return (status == LinearSolverTerminationType::SUCCESS); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 361 | } |
| 362 | |
| 363 | // Consider the preconditioner matrix as meta-block matrix, whose |
| 364 | // blocks correspond to the clusters. Then cluster pairs corresponding |
| 365 | // to edges in the degree-2 forest are off diagonal entries of this |
| 366 | // matrix. Scaling these off-diagonal entries by 1/2 forces this |
| 367 | // matrix to be positive definite. |
| 368 | void VisibilityBasedPreconditioner::ScaleOffDiagonalCells() { |
| 369 | for (const auto& block_pair : block_pairs_) { |
| 370 | const int block1 = block_pair.first; |
| 371 | const int block2 = block_pair.second; |
| 372 | if (!IsBlockPairOffDiagonal(block1, block2)) { |
| 373 | continue; |
| 374 | } |
| 375 | |
| 376 | int r, c, row_stride, col_stride; |
| 377 | CellInfo* cell_info = |
| 378 | m_->GetCell(block1, block2, &r, &c, &row_stride, &col_stride); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 379 | CHECK(cell_info != nullptr) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 380 | << "Cell missing for block pair (" << block1 << "," << block2 << ")" |
| 381 | << " cluster pair (" << cluster_membership_[block1] << " " |
| 382 | << cluster_membership_[block2] << ")"; |
| 383 | |
| 384 | // Ah the magic of tri-diagonal matrices and diagonal |
| 385 | // dominance. See Lemma 1 in "Visibility Based Preconditioning |
| 386 | // For Bundle Adjustment". |
| 387 | MatrixRef m(cell_info->values, row_stride, col_stride); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 388 | m.block(r, c, blocks_[block1].size, blocks_[block2].size) *= 0.5; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 389 | } |
| 390 | } |
| 391 | |
| 392 | // Compute the sparse Cholesky factorization of the preconditioner |
| 393 | // matrix. |
| 394 | LinearSolverTerminationType VisibilityBasedPreconditioner::Factorize() { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 395 | // Extract the BlockSparseMatrix that is used for actually storing |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 396 | // S and convert it into a CompressedRowSparseMatrix. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 397 | const BlockSparseMatrix* bsm = |
| 398 | down_cast<BlockRandomAccessSparseMatrix*>(m_.get())->matrix(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 399 | const CompressedRowSparseMatrix::StorageType storage_type = |
| 400 | sparse_cholesky_->StorageType(); |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 401 | if (storage_type == |
| 402 | CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR) { |
| 403 | if (!m_crs_) { |
| 404 | m_crs_ = bsm->ToCompressedRowSparseMatrix(); |
| 405 | m_crs_->set_storage_type( |
| 406 | CompressedRowSparseMatrix::StorageType::UPPER_TRIANGULAR); |
| 407 | } else { |
| 408 | bsm->UpdateCompressedRowSparseMatrix(m_crs_.get()); |
| 409 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 410 | } else { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 411 | if (!m_crs_) { |
| 412 | m_crs_ = bsm->ToCompressedRowSparseMatrixTranspose(); |
| 413 | m_crs_->set_storage_type( |
| 414 | CompressedRowSparseMatrix::StorageType::LOWER_TRIANGULAR); |
| 415 | } else { |
| 416 | bsm->UpdateCompressedRowSparseMatrixTranspose(m_crs_.get()); |
| 417 | } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 418 | } |
| 419 | |
| 420 | std::string message; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 421 | return sparse_cholesky_->Factorize(m_crs_.get(), &message); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 422 | } |
| 423 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 424 | void VisibilityBasedPreconditioner::RightMultiplyAndAccumulate( |
| 425 | const double* x, double* y) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 426 | CHECK(x != nullptr); |
| 427 | CHECK(y != nullptr); |
| 428 | CHECK(sparse_cholesky_ != nullptr); |
| 429 | std::string message; |
| 430 | sparse_cholesky_->Solve(x, y, &message); |
| 431 | } |
| 432 | |
| 433 | int VisibilityBasedPreconditioner::num_rows() const { return m_->num_rows(); } |
| 434 | |
| 435 | // Classify camera/f_block pairs as in and out of the preconditioner, |
| 436 | // based on whether the cluster pair that they belong to is in the |
| 437 | // preconditioner or not. |
| 438 | bool VisibilityBasedPreconditioner::IsBlockPairInPreconditioner( |
| 439 | const int block1, const int block2) const { |
| 440 | int cluster1 = cluster_membership_[block1]; |
| 441 | int cluster2 = cluster_membership_[block2]; |
| 442 | if (cluster1 > cluster2) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 443 | std::swap(cluster1, cluster2); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 444 | } |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 445 | return (cluster_pairs_.count(std::make_pair(cluster1, cluster2)) > 0); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | bool VisibilityBasedPreconditioner::IsBlockPairOffDiagonal( |
| 449 | const int block1, const int block2) const { |
| 450 | return (cluster_membership_[block1] != cluster_membership_[block2]); |
| 451 | } |
| 452 | |
| 453 | // Convert a graph into a list of edges that includes self edges for |
| 454 | // each vertex. |
| 455 | void VisibilityBasedPreconditioner::ForestToClusterPairs( |
| 456 | const WeightedGraph<int>& forest, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 457 | std::unordered_set<std::pair<int, int>, pair_hash>* cluster_pairs) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 458 | CHECK(cluster_pairs != nullptr); |
| 459 | cluster_pairs->clear(); |
| 460 | const std::unordered_set<int>& vertices = forest.vertices(); |
| 461 | CHECK_EQ(vertices.size(), num_clusters_); |
| 462 | |
| 463 | // Add all the cluster pairs corresponding to the edges in the |
| 464 | // forest. |
| 465 | for (const int cluster1 : vertices) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 466 | cluster_pairs->insert(std::make_pair(cluster1, cluster1)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 467 | const std::unordered_set<int>& neighbors = forest.Neighbors(cluster1); |
| 468 | for (const int cluster2 : neighbors) { |
| 469 | if (cluster1 < cluster2) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 470 | cluster_pairs->insert(std::make_pair(cluster1, cluster2)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 471 | } |
| 472 | } |
| 473 | } |
| 474 | } |
| 475 | |
| 476 | // The visibility set of a cluster is the union of the visibility sets |
| 477 | // of all its cameras. In other words, the set of points visible to |
| 478 | // any camera in the cluster. |
| 479 | void VisibilityBasedPreconditioner::ComputeClusterVisibility( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 480 | const std::vector<std::set<int>>& visibility, |
| 481 | std::vector<std::set<int>>* cluster_visibility) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 482 | CHECK(cluster_visibility != nullptr); |
| 483 | cluster_visibility->resize(0); |
| 484 | cluster_visibility->resize(num_clusters_); |
| 485 | for (int i = 0; i < num_blocks_; ++i) { |
| 486 | const int cluster_id = cluster_membership_[i]; |
| 487 | (*cluster_visibility)[cluster_id].insert(visibility[i].begin(), |
| 488 | visibility[i].end()); |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // Construct a graph whose vertices are the clusters, and the edge |
| 493 | // weights are the number of 3D points visible to cameras in both the |
| 494 | // vertices. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 495 | std::unique_ptr<WeightedGraph<int>> |
| 496 | VisibilityBasedPreconditioner::CreateClusterGraph( |
| 497 | const std::vector<std::set<int>>& cluster_visibility) const { |
| 498 | auto cluster_graph = std::make_unique<WeightedGraph<int>>(); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 499 | |
| 500 | for (int i = 0; i < num_clusters_; ++i) { |
| 501 | cluster_graph->AddVertex(i); |
| 502 | } |
| 503 | |
| 504 | for (int i = 0; i < num_clusters_; ++i) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 505 | const std::set<int>& cluster_i = cluster_visibility[i]; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 506 | for (int j = i + 1; j < num_clusters_; ++j) { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 507 | std::vector<int> intersection; |
| 508 | const std::set<int>& cluster_j = cluster_visibility[j]; |
| 509 | std::set_intersection(cluster_i.begin(), |
| 510 | cluster_i.end(), |
| 511 | cluster_j.begin(), |
| 512 | cluster_j.end(), |
| 513 | std::back_inserter(intersection)); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 514 | |
| 515 | if (intersection.size() > 0) { |
| 516 | // Clusters interact strongly when they share a large number |
| 517 | // of 3D points. The degree-2 maximum spanning forest |
| 518 | // algorithm, iterates on the edges in decreasing order of |
| 519 | // their weight, which is the number of points shared by the |
| 520 | // two cameras that it connects. |
| 521 | cluster_graph->AddEdge(i, j, intersection.size()); |
| 522 | } |
| 523 | } |
| 524 | } |
| 525 | return cluster_graph; |
| 526 | } |
| 527 | |
| 528 | // Canonical views clustering returns a std::unordered_map from vertices to |
| 529 | // cluster ids. Convert this into a flat array for quick lookup. It is |
| 530 | // possible that some of the vertices may not be associated with any |
| 531 | // cluster. In that case, randomly assign them to one of the clusters. |
| 532 | // |
| 533 | // The cluster ids can be non-contiguous integers. So as we flatten |
| 534 | // the membership_map, we also map the cluster ids to a contiguous set |
| 535 | // of integers so that the cluster ids are in [0, num_clusters_). |
| 536 | void VisibilityBasedPreconditioner::FlattenMembershipMap( |
| 537 | const std::unordered_map<int, int>& membership_map, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 538 | std::vector<int>* membership_vector) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 539 | CHECK(membership_vector != nullptr); |
| 540 | membership_vector->resize(0); |
| 541 | membership_vector->resize(num_blocks_, -1); |
| 542 | |
| 543 | std::unordered_map<int, int> cluster_id_to_index; |
| 544 | // Iterate over the cluster membership map and update the |
| 545 | // cluster_membership_ vector assigning arbitrary cluster ids to |
| 546 | // the few cameras that have not been clustered. |
| 547 | for (const auto& m : membership_map) { |
| 548 | const int camera_id = m.first; |
| 549 | int cluster_id = m.second; |
| 550 | |
| 551 | // If the view was not clustered, randomly assign it to one of the |
| 552 | // clusters. This preserves the mathematical correctness of the |
| 553 | // preconditioner. If there are too many views which are not |
| 554 | // clustered, it may lead to some quality degradation though. |
| 555 | // |
| 556 | // TODO(sameeragarwal): Check if a large number of views have not |
| 557 | // been clustered and deal with it? |
| 558 | if (cluster_id == -1) { |
| 559 | cluster_id = camera_id % num_clusters_; |
| 560 | } |
| 561 | |
| 562 | const int index = FindWithDefault( |
| 563 | cluster_id_to_index, cluster_id, cluster_id_to_index.size()); |
| 564 | |
| 565 | if (index == cluster_id_to_index.size()) { |
| 566 | cluster_id_to_index[cluster_id] = index; |
| 567 | } |
| 568 | |
| 569 | CHECK_LT(index, num_clusters_); |
| 570 | membership_vector->at(camera_id) = index; |
| 571 | } |
| 572 | } |
| 573 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 574 | } // namespace ceres::internal |