blob: f59fe16da0394c97a83c331889f7fad81a084c3f [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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//
Austin Schuh3de38b02024-06-25 18:25:10 -070031// Class for loading the data required for describing a Fields of Experts (FoE)
Austin Schuh70cc9552019-01-21 19:46:48 -080032// model.
33
34#include "fields_of_experts.h"
35
Austin Schuh70cc9552019-01-21 19:46:48 -080036#include <cmath>
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080037#include <fstream>
Austin Schuh70cc9552019-01-21 19:46:48 -080038
39#include "pgm_image.h"
40
Austin Schuh3de38b02024-06-25 18:25:10 -070041namespace ceres::examples {
Austin Schuh70cc9552019-01-21 19:46:48 -080042
43FieldsOfExpertsCost::FieldsOfExpertsCost(const std::vector<double>& filter)
44 : filter_(filter) {
45 set_num_residuals(1);
Austin Schuh3de38b02024-06-25 18:25:10 -070046 for (int64_t i = 0; i < filter_.size(); ++i) {
Austin Schuh70cc9552019-01-21 19:46:48 -080047 mutable_parameter_block_sizes()->push_back(1);
48 }
49}
50
51// This is a dot product between a the scalar parameters and a vector of filter
52// coefficients.
53bool FieldsOfExpertsCost::Evaluate(double const* const* parameters,
54 double* residuals,
55 double** jacobians) const {
Austin Schuh3de38b02024-06-25 18:25:10 -070056 const int64_t num_variables = filter_.size();
Austin Schuh70cc9552019-01-21 19:46:48 -080057 residuals[0] = 0;
Austin Schuh3de38b02024-06-25 18:25:10 -070058 for (int64_t i = 0; i < num_variables; ++i) {
Austin Schuh70cc9552019-01-21 19:46:48 -080059 residuals[0] += filter_[i] * parameters[i][0];
60 }
61
Austin Schuh3de38b02024-06-25 18:25:10 -070062 if (jacobians != nullptr) {
63 for (int64_t i = 0; i < num_variables; ++i) {
64 if (jacobians[i] != nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -080065 jacobians[i][0] = filter_[i];
66 }
67 }
68 }
69
70 return true;
71}
72
73// This loss function builds the FoE terms and is equal to
74//
75// f(x) = alpha_i * log(1 + (1/2)s)
76//
77void FieldsOfExpertsLoss::Evaluate(double sq_norm, double rho[3]) const {
78 const double c = 0.5;
79 const double sum = 1.0 + sq_norm * c;
80 const double inv = 1.0 / sum;
81 // 'sum' and 'inv' are always positive, assuming that 's' is.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080082 rho[0] = alpha_ * log(sum);
Austin Schuh70cc9552019-01-21 19:46:48 -080083 rho[1] = alpha_ * c * inv;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080084 rho[2] = -alpha_ * c * c * inv * inv;
Austin Schuh70cc9552019-01-21 19:46:48 -080085}
86
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080087FieldsOfExperts::FieldsOfExperts() : size_(0), num_filters_(0) {}
Austin Schuh70cc9552019-01-21 19:46:48 -080088
89bool FieldsOfExperts::LoadFromFile(const std::string& filename) {
90 std::ifstream foe_file(filename.c_str());
91 foe_file >> size_;
92 foe_file >> num_filters_;
93 if (size_ < 0 || num_filters_ < 0) {
94 return false;
95 }
96 const int num_variables = NumVariables();
97
98 x_delta_indices_.resize(num_variables);
99 for (int i = 0; i < num_variables; ++i) {
100 foe_file >> x_delta_indices_[i];
101 }
102
103 y_delta_indices_.resize(NumVariables());
104 for (int i = 0; i < num_variables; ++i) {
105 foe_file >> y_delta_indices_[i];
106 }
107
108 alpha_.resize(num_filters_);
109 for (int i = 0; i < num_filters_; ++i) {
110 foe_file >> alpha_[i];
111 }
112
113 filters_.resize(num_filters_);
114 for (int i = 0; i < num_filters_; ++i) {
115 filters_[i].resize(num_variables);
116 for (int j = 0; j < num_variables; ++j) {
117 foe_file >> filters_[i][j];
118 }
119 }
120
121 // If any read failed, return failure.
122 if (!foe_file) {
123 size_ = 0;
124 return false;
125 }
126
127 // There cannot be anything else in the file. Try reading another number and
128 // return failure if that succeeded.
129 double temp;
130 foe_file >> temp;
131 if (foe_file) {
132 size_ = 0;
133 return false;
134 }
135
136 return true;
137}
138
139ceres::CostFunction* FieldsOfExperts::NewCostFunction(int alpha_index) const {
140 return new FieldsOfExpertsCost(filters_[alpha_index]);
141}
142
143ceres::LossFunction* FieldsOfExperts::NewLossFunction(int alpha_index) const {
144 return new FieldsOfExpertsLoss(alpha_[alpha_index]);
145}
146
Austin Schuh3de38b02024-06-25 18:25:10 -0700147} // namespace ceres::examples