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