canonical_views_clustering_test.cc 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // 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: Sameer Agarwal (sameeragarwal@google.com)
  30. // David Gallup (dgallup@google.com)
  31. #include "ceres/canonical_views_clustering.h"
  32. #include <unordered_map>
  33. #include "ceres/graph.h"
  34. #include "gtest/gtest.h"
  35. namespace ceres::internal {
  36. const int kVertexIds[] = {0, 1, 2, 3};
  37. class CanonicalViewsTest : public ::testing::Test {
  38. protected:
  39. void SetUp() final {
  40. // The graph structure is as follows.
  41. //
  42. // Vertex weights: 0 2 2 0
  43. // V0-----V1-----V2-----V3
  44. // Edge weights: 0.8 0.9 0.3
  45. const double kVertexWeights[] = {0.0, 2.0, 2.0, -1.0};
  46. for (int i = 0; i < 4; ++i) {
  47. graph_.AddVertex(i, kVertexWeights[i]);
  48. }
  49. // Create self edges.
  50. // CanonicalViews requires that every view "sees" itself.
  51. for (int i = 0; i < 4; ++i) {
  52. graph_.AddEdge(i, i, 1.0);
  53. }
  54. // Create three edges.
  55. const double kEdgeWeights[] = {0.8, 0.9, 0.3};
  56. for (int i = 0; i < 3; ++i) {
  57. // The graph interface is directed, so remember to create both
  58. // edges.
  59. graph_.AddEdge(kVertexIds[i], kVertexIds[i + 1], kEdgeWeights[i]);
  60. }
  61. }
  62. void ComputeClustering() {
  63. ComputeCanonicalViewsClustering(options_, graph_, &centers_, &membership_);
  64. }
  65. WeightedGraph<int> graph_;
  66. CanonicalViewsClusteringOptions options_;
  67. std::vector<int> centers_;
  68. std::unordered_map<int, int> membership_;
  69. };
  70. TEST_F(CanonicalViewsTest, ComputeCanonicalViewsTest) {
  71. options_.min_views = 0;
  72. options_.size_penalty_weight = 0.5;
  73. options_.similarity_penalty_weight = 0.0;
  74. options_.view_score_weight = 0.0;
  75. ComputeClustering();
  76. // 2 canonical views.
  77. EXPECT_EQ(centers_.size(), 2);
  78. EXPECT_EQ(centers_[0], kVertexIds[1]);
  79. EXPECT_EQ(centers_[1], kVertexIds[3]);
  80. // Check cluster membership.
  81. EXPECT_EQ(FindOrDie(membership_, kVertexIds[0]), 0);
  82. EXPECT_EQ(FindOrDie(membership_, kVertexIds[1]), 0);
  83. EXPECT_EQ(FindOrDie(membership_, kVertexIds[2]), 0);
  84. EXPECT_EQ(FindOrDie(membership_, kVertexIds[3]), 1);
  85. }
  86. // Increases size penalty so the second canonical view won't be
  87. // chosen.
  88. TEST_F(CanonicalViewsTest, SizePenaltyTest) {
  89. options_.min_views = 0;
  90. options_.size_penalty_weight = 2.0;
  91. options_.similarity_penalty_weight = 0.0;
  92. options_.view_score_weight = 0.0;
  93. ComputeClustering();
  94. // 1 canonical view.
  95. EXPECT_EQ(centers_.size(), 1);
  96. EXPECT_EQ(centers_[0], kVertexIds[1]);
  97. }
  98. // Increases view score weight so vertex 2 will be chosen.
  99. TEST_F(CanonicalViewsTest, ViewScoreTest) {
  100. options_.min_views = 0;
  101. options_.size_penalty_weight = 0.5;
  102. options_.similarity_penalty_weight = 0.0;
  103. options_.view_score_weight = 1.0;
  104. ComputeClustering();
  105. // 2 canonical views.
  106. EXPECT_EQ(centers_.size(), 2);
  107. EXPECT_EQ(centers_[0], kVertexIds[1]);
  108. EXPECT_EQ(centers_[1], kVertexIds[2]);
  109. }
  110. // Increases similarity penalty so vertex 2 won't be chosen despite
  111. // it's view score.
  112. TEST_F(CanonicalViewsTest, SimilarityPenaltyTest) {
  113. options_.min_views = 0;
  114. options_.size_penalty_weight = 0.5;
  115. options_.similarity_penalty_weight = 3.0;
  116. options_.view_score_weight = 1.0;
  117. ComputeClustering();
  118. // 2 canonical views.
  119. EXPECT_EQ(centers_.size(), 1);
  120. EXPECT_EQ(centers_[0], kVertexIds[1]);
  121. }
  122. } // namespace ceres::internal