canonical_views_clustering.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 Google Inc. All rights reserved.
  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. // An implementation of the Canonical Views clustering algorithm from
  32. // "Scene Summarization for Online Image Collections", Ian Simon, Noah
  33. // Snavely, Steven M. Seitz, ICCV 2007.
  34. //
  35. // More details can be found at
  36. // http://grail.cs.washington.edu/projects/canonview/
  37. //
  38. // Ceres uses this algorithm to perform view clustering for
  39. // constructing visibility based preconditioners.
  40. #ifndef CERES_INTERNAL_CANONICAL_VIEWS_CLUSTERING_H_
  41. #define CERES_INTERNAL_CANONICAL_VIEWS_CLUSTERING_H_
  42. #include <unordered_map>
  43. #include <vector>
  44. #include "ceres/graph.h"
  45. #include "ceres/internal/disable_warnings.h"
  46. #include "ceres/internal/export.h"
  47. namespace ceres::internal {
  48. struct CanonicalViewsClusteringOptions;
  49. // Compute a partitioning of the vertices of the graph using the
  50. // canonical views clustering algorithm.
  51. //
  52. // In the following we will use the terms vertices and views
  53. // interchangeably. Given a weighted Graph G(V,E), the canonical views
  54. // of G are the set of vertices that best "summarize" the content
  55. // of the graph. If w_ij i s the weight connecting the vertex i to
  56. // vertex j, and C is the set of canonical views. Then the objective
  57. // of the canonical views algorithm is
  58. //
  59. // E[C] = sum_[i in V] max_[j in C] w_ij
  60. // - size_penalty_weight * |C|
  61. // - similarity_penalty_weight * sum_[i in C, j in C, j > i] w_ij
  62. //
  63. // alpha is the size penalty that penalizes large number of canonical
  64. // views.
  65. //
  66. // beta is the similarity penalty that penalizes canonical views that
  67. // are too similar to other canonical views.
  68. //
  69. // Thus the canonical views algorithm tries to find a canonical view
  70. // for each vertex in the graph which best explains it, while trying
  71. // to minimize the number of canonical views and the overlap between
  72. // them.
  73. //
  74. // We further augment the above objective function by allowing for per
  75. // vertex weights, higher weights indicating a higher preference for
  76. // being chosen as a canonical view. Thus if w_i is the vertex weight
  77. // for vertex i, the objective function is then
  78. //
  79. // E[C] = sum_[i in V] max_[j in C] w_ij
  80. // - size_penalty_weight * |C|
  81. // - similarity_penalty_weight * sum_[i in C, j in C, j > i] w_ij
  82. // + view_score_weight * sum_[i in C] w_i
  83. //
  84. // centers will contain the vertices that are the identified
  85. // as the canonical views/cluster centers, and membership is a map
  86. // from vertices to cluster_ids. The i^th cluster center corresponds
  87. // to the i^th cluster.
  88. //
  89. // It is possible depending on the configuration of the clustering
  90. // algorithm that some of the vertices may not be assigned to any
  91. // cluster. In this case they are assigned to a cluster with id = -1;
  92. CERES_NO_EXPORT void ComputeCanonicalViewsClustering(
  93. const CanonicalViewsClusteringOptions& options,
  94. const WeightedGraph<int>& graph,
  95. std::vector<int>* centers,
  96. std::unordered_map<int, int>* membership);
  97. struct CERES_NO_EXPORT CanonicalViewsClusteringOptions {
  98. // The minimum number of canonical views to compute.
  99. int min_views = 3;
  100. // Penalty weight for the number of canonical views. A higher
  101. // number will result in fewer canonical views.
  102. double size_penalty_weight = 5.75;
  103. // Penalty weight for the diversity (orthogonality) of the
  104. // canonical views. A higher number will encourage less similar
  105. // canonical views.
  106. double similarity_penalty_weight = 100;
  107. // Weight for per-view scores. Lower weight places less
  108. // confidence in the view scores.
  109. double view_score_weight = 0.0;
  110. };
  111. } // namespace ceres::internal
  112. #include "ceres/internal/reenable_warnings.h"
  113. #endif // CERES_INTERNAL_CANONICAL_VIEWS_CLUSTERING_H_