blob: c193735f1060fa26959067a03676b6f02f7652ae [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 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: David Gallup (dgallup@google.com)
30// Sameer Agarwal (sameeragarwal@google.com)
31
32#include "ceres/canonical_views_clustering.h"
33
Austin Schuh70cc9552019-01-21 19:46:48 -080034#include <unordered_map>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080035#include <unordered_set>
Austin Schuh70cc9552019-01-21 19:46:48 -080036
37#include "ceres/graph.h"
38#include "ceres/map_util.h"
39#include "glog/logging.h"
40
41namespace ceres {
42namespace internal {
43
44using std::vector;
45
46typedef std::unordered_map<int, int> IntMap;
47typedef std::unordered_set<int> IntSet;
48
49class CanonicalViewsClustering {
50 public:
51 CanonicalViewsClustering() {}
52
53 // Compute the canonical views clustering of the vertices of the
54 // graph. centers will contain the vertices that are the identified
55 // as the canonical views/cluster centers, and membership is a map
56 // from vertices to cluster_ids. The i^th cluster center corresponds
57 // to the i^th cluster. It is possible depending on the
58 // configuration of the clustering algorithm that some of the
59 // vertices may not be assigned to any cluster. In this case they
60 // are assigned to a cluster with id = kInvalidClusterId.
61 void ComputeClustering(const CanonicalViewsClusteringOptions& options,
62 const WeightedGraph<int>& graph,
63 vector<int>* centers,
64 IntMap* membership);
65
66 private:
67 void FindValidViews(IntSet* valid_views) const;
68 double ComputeClusteringQualityDifference(const int candidate,
69 const vector<int>& centers) const;
70 void UpdateCanonicalViewAssignments(const int canonical_view);
71 void ComputeClusterMembership(const vector<int>& centers,
72 IntMap* membership) const;
73
74 CanonicalViewsClusteringOptions options_;
75 const WeightedGraph<int>* graph_;
76 // Maps a view to its representative canonical view (its cluster
77 // center).
78 IntMap view_to_canonical_view_;
79 // Maps a view to its similarity to its current cluster center.
80 std::unordered_map<int, double> view_to_canonical_view_similarity_;
81};
82
83void ComputeCanonicalViewsClustering(
84 const CanonicalViewsClusteringOptions& options,
85 const WeightedGraph<int>& graph,
86 vector<int>* centers,
87 IntMap* membership) {
88 time_t start_time = time(NULL);
89 CanonicalViewsClustering cv;
90 cv.ComputeClustering(options, graph, centers, membership);
91 VLOG(2) << "Canonical views clustering time (secs): "
92 << time(NULL) - start_time;
93}
94
95// Implementation of CanonicalViewsClustering
96void CanonicalViewsClustering::ComputeClustering(
97 const CanonicalViewsClusteringOptions& options,
98 const WeightedGraph<int>& graph,
99 vector<int>* centers,
100 IntMap* membership) {
101 options_ = options;
102 CHECK(centers != nullptr);
103 CHECK(membership != nullptr);
104 centers->clear();
105 membership->clear();
106 graph_ = &graph;
107
108 IntSet valid_views;
109 FindValidViews(&valid_views);
110 while (valid_views.size() > 0) {
111 // Find the next best canonical view.
112 double best_difference = -std::numeric_limits<double>::max();
113 int best_view = 0;
114
115 // TODO(sameeragarwal): Make this loop multi-threaded.
116 for (const auto& view : valid_views) {
117 const double difference =
118 ComputeClusteringQualityDifference(view, *centers);
119 if (difference > best_difference) {
120 best_difference = difference;
121 best_view = view;
122 }
123 }
124
125 CHECK_GT(best_difference, -std::numeric_limits<double>::max());
126
127 // Add canonical view if quality improves, or if minimum is not
128 // yet met, otherwise break.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800129 if ((best_difference <= 0) && (centers->size() >= options_.min_views)) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800130 break;
131 }
132
133 centers->push_back(best_view);
134 valid_views.erase(best_view);
135 UpdateCanonicalViewAssignments(best_view);
136 }
137
138 ComputeClusterMembership(*centers, membership);
139}
140
141// Return the set of vertices of the graph which have valid vertex
142// weights.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800143void CanonicalViewsClustering::FindValidViews(IntSet* valid_views) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800144 const IntSet& views = graph_->vertices();
145 for (const auto& view : views) {
146 if (graph_->VertexWeight(view) != WeightedGraph<int>::InvalidWeight()) {
147 valid_views->insert(view);
148 }
149 }
150}
151
152// Computes the difference in the quality score if 'candidate' were
153// added to the set of canonical views.
154double CanonicalViewsClustering::ComputeClusteringQualityDifference(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800155 const int candidate, const vector<int>& centers) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800156 // View score.
157 double difference =
158 options_.view_score_weight * graph_->VertexWeight(candidate);
159
160 // Compute how much the quality score changes if the candidate view
161 // was added to the list of canonical views and its nearest
162 // neighbors became members of its cluster.
163 const IntSet& neighbors = graph_->Neighbors(candidate);
164 for (const auto& neighbor : neighbors) {
165 const double old_similarity =
166 FindWithDefault(view_to_canonical_view_similarity_, neighbor, 0.0);
167 const double new_similarity = graph_->EdgeWeight(neighbor, candidate);
168 if (new_similarity > old_similarity) {
169 difference += new_similarity - old_similarity;
170 }
171 }
172
173 // Number of views penalty.
174 difference -= options_.size_penalty_weight;
175
176 // Orthogonality.
177 for (int i = 0; i < centers.size(); ++i) {
178 difference -= options_.similarity_penalty_weight *
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800179 graph_->EdgeWeight(centers[i], candidate);
Austin Schuh70cc9552019-01-21 19:46:48 -0800180 }
181
182 return difference;
183}
184
185// Reassign views if they're more similar to the new canonical view.
186void CanonicalViewsClustering::UpdateCanonicalViewAssignments(
187 const int canonical_view) {
188 const IntSet& neighbors = graph_->Neighbors(canonical_view);
189 for (const auto& neighbor : neighbors) {
190 const double old_similarity =
191 FindWithDefault(view_to_canonical_view_similarity_, neighbor, 0.0);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800192 const double new_similarity = graph_->EdgeWeight(neighbor, canonical_view);
Austin Schuh70cc9552019-01-21 19:46:48 -0800193 if (new_similarity > old_similarity) {
194 view_to_canonical_view_[neighbor] = canonical_view;
195 view_to_canonical_view_similarity_[neighbor] = new_similarity;
196 }
197 }
198}
199
200// Assign a cluster id to each view.
201void CanonicalViewsClustering::ComputeClusterMembership(
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800202 const vector<int>& centers, IntMap* membership) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800203 CHECK(membership != nullptr);
204 membership->clear();
205
206 // The i^th cluster has cluster id i.
207 IntMap center_to_cluster_id;
208 for (int i = 0; i < centers.size(); ++i) {
209 center_to_cluster_id[centers[i]] = i;
210 }
211
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800212 static constexpr int kInvalidClusterId = -1;
Austin Schuh70cc9552019-01-21 19:46:48 -0800213
214 const IntSet& views = graph_->vertices();
215 for (const auto& view : views) {
216 auto it = view_to_canonical_view_.find(view);
217 int cluster_id = kInvalidClusterId;
218 if (it != view_to_canonical_view_.end()) {
219 cluster_id = FindOrDie(center_to_cluster_id, it->second);
220 }
221
222 InsertOrDie(membership, view, cluster_id);
223 }
224}
225
226} // namespace internal
227} // namespace ceres