fields_of_experts.cc 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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: strandmark@google.com (Petter Strandmark)
  30. //
  31. // Class for loading the data required for describing a Fields of Experts (FoE)
  32. // model.
  33. #include "fields_of_experts.h"
  34. #include <cmath>
  35. #include <fstream>
  36. #include "pgm_image.h"
  37. namespace ceres::examples {
  38. FieldsOfExpertsCost::FieldsOfExpertsCost(const std::vector<double>& filter)
  39. : filter_(filter) {
  40. set_num_residuals(1);
  41. for (int64_t i = 0; i < filter_.size(); ++i) {
  42. mutable_parameter_block_sizes()->push_back(1);
  43. }
  44. }
  45. // This is a dot product between a the scalar parameters and a vector of filter
  46. // coefficients.
  47. bool FieldsOfExpertsCost::Evaluate(double const* const* parameters,
  48. double* residuals,
  49. double** jacobians) const {
  50. const int64_t num_variables = filter_.size();
  51. residuals[0] = 0;
  52. for (int64_t i = 0; i < num_variables; ++i) {
  53. residuals[0] += filter_[i] * parameters[i][0];
  54. }
  55. if (jacobians != nullptr) {
  56. for (int64_t i = 0; i < num_variables; ++i) {
  57. if (jacobians[i] != nullptr) {
  58. jacobians[i][0] = filter_[i];
  59. }
  60. }
  61. }
  62. return true;
  63. }
  64. // This loss function builds the FoE terms and is equal to
  65. //
  66. // f(x) = alpha_i * log(1 + (1/2)s)
  67. //
  68. void FieldsOfExpertsLoss::Evaluate(double sq_norm, double rho[3]) const {
  69. const double c = 0.5;
  70. const double sum = 1.0 + sq_norm * c;
  71. const double inv = 1.0 / sum;
  72. // 'sum' and 'inv' are always positive, assuming that 's' is.
  73. rho[0] = alpha_ * log(sum);
  74. rho[1] = alpha_ * c * inv;
  75. rho[2] = -alpha_ * c * c * inv * inv;
  76. }
  77. FieldsOfExperts::FieldsOfExperts() : size_(0), num_filters_(0) {}
  78. bool FieldsOfExperts::LoadFromFile(const std::string& filename) {
  79. std::ifstream foe_file(filename.c_str());
  80. foe_file >> size_;
  81. foe_file >> num_filters_;
  82. if (size_ < 0 || num_filters_ < 0) {
  83. return false;
  84. }
  85. const int num_variables = NumVariables();
  86. x_delta_indices_.resize(num_variables);
  87. for (int i = 0; i < num_variables; ++i) {
  88. foe_file >> x_delta_indices_[i];
  89. }
  90. y_delta_indices_.resize(NumVariables());
  91. for (int i = 0; i < num_variables; ++i) {
  92. foe_file >> y_delta_indices_[i];
  93. }
  94. alpha_.resize(num_filters_);
  95. for (int i = 0; i < num_filters_; ++i) {
  96. foe_file >> alpha_[i];
  97. }
  98. filters_.resize(num_filters_);
  99. for (int i = 0; i < num_filters_; ++i) {
  100. filters_[i].resize(num_variables);
  101. for (int j = 0; j < num_variables; ++j) {
  102. foe_file >> filters_[i][j];
  103. }
  104. }
  105. // If any read failed, return failure.
  106. if (!foe_file) {
  107. size_ = 0;
  108. return false;
  109. }
  110. // There cannot be anything else in the file. Try reading another number and
  111. // return failure if that succeeded.
  112. double temp;
  113. foe_file >> temp;
  114. if (foe_file) {
  115. size_ = 0;
  116. return false;
  117. }
  118. return true;
  119. }
  120. ceres::CostFunction* FieldsOfExperts::NewCostFunction(int alpha_index) const {
  121. return new FieldsOfExpertsCost(filters_[alpha_index]);
  122. }
  123. ceres::LossFunction* FieldsOfExperts::NewLossFunction(int alpha_index) const {
  124. return new FieldsOfExpertsLoss(alpha_[alpha_index]);
  125. }
  126. } // namespace ceres::examples