Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 descibing a Fields of Experts (FoE) |
| 32 | // model. The Fields of Experts regularization consists of terms of the type |
| 33 | // |
| 34 | // alpha * log(1 + (1/2)*sum(F .* X)^2), |
| 35 | // |
| 36 | // where F is a d-by-d image patch and alpha is a constant. This is implemented |
| 37 | // by a FieldsOfExpertsSum object which represents the dot product between the |
| 38 | // image patches and a FieldsOfExpertsLoss which implements the log(1 + (1/2)s) |
| 39 | // part. |
| 40 | // |
| 41 | // [1] S. Roth and M.J. Black. "Fields of Experts." International Journal of |
| 42 | // Computer Vision, 82(2):205--229, 2009. |
| 43 | |
| 44 | #ifndef CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_ |
| 45 | #define CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_ |
| 46 | |
| 47 | #include <iostream> |
| 48 | #include <vector> |
| 49 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 50 | #include "ceres/cost_function.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 51 | #include "ceres/loss_function.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 52 | #include "ceres/sized_cost_function.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 53 | #include "pgm_image.h" |
| 54 | |
| 55 | namespace ceres { |
| 56 | namespace examples { |
| 57 | |
| 58 | // One sum in the FoE regularizer. This is a dot product between a filter and an |
| 59 | // image patch. It simply calculates the dot product between the filter |
| 60 | // coefficients given in the constructor and the scalar parameters passed to it. |
| 61 | class FieldsOfExpertsCost : public ceres::CostFunction { |
| 62 | public: |
| 63 | explicit FieldsOfExpertsCost(const std::vector<double>& filter); |
| 64 | // The number of scalar parameters passed to Evaluate must equal the number of |
| 65 | // filter coefficients passed to the constructor. |
| 66 | virtual bool Evaluate(double const* const* parameters, |
| 67 | double* residuals, |
| 68 | double** jacobians) const; |
| 69 | |
| 70 | private: |
| 71 | const std::vector<double>& filter_; |
| 72 | }; |
| 73 | |
| 74 | // The loss function used to build the correct regularization. See above. |
| 75 | // |
| 76 | // f(x) = alpha_i * log(1 + (1/2)s) |
| 77 | // |
| 78 | class FieldsOfExpertsLoss : public ceres::LossFunction { |
| 79 | public: |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 80 | explicit FieldsOfExpertsLoss(double alpha) : alpha_(alpha) {} |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 81 | virtual void Evaluate(double, double*) const; |
| 82 | |
| 83 | private: |
| 84 | const double alpha_; |
| 85 | }; |
| 86 | |
| 87 | // This class loads a set of filters and coefficients from file. Then the users |
| 88 | // obtains the correct loss and cost functions through NewCostFunction and |
| 89 | // NewLossFunction. |
| 90 | class FieldsOfExperts { |
| 91 | public: |
| 92 | // Creates an empty object with size() == 0. |
| 93 | FieldsOfExperts(); |
| 94 | // Attempts to load filters from a file. If unsuccessful it returns false and |
| 95 | // sets size() == 0. |
| 96 | bool LoadFromFile(const std::string& filename); |
| 97 | |
| 98 | // Side length of a square filter in this FoE. They are all of the same size. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 99 | int Size() const { return size_; } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 100 | |
| 101 | // Total number of pixels the filter covers. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 102 | int NumVariables() const { return size_ * size_; } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 103 | |
| 104 | // Number of filters used by the FoE. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 105 | int NumFilters() const { return num_filters_; } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 106 | |
| 107 | // Creates a new cost function. The caller is responsible for deallocating the |
| 108 | // memory. alpha_index specifies which filter is used in the cost function. |
| 109 | ceres::CostFunction* NewCostFunction(int alpha_index) const; |
| 110 | // Creates a new loss function. The caller is responsible for deallocating the |
| 111 | // memory. alpha_index specifies which filter this loss function is for. |
| 112 | ceres::LossFunction* NewLossFunction(int alpha_index) const; |
| 113 | |
| 114 | // Gets the delta pixel indices for all pixels in a patch. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 115 | const std::vector<int>& GetXDeltaIndices() const { return x_delta_indices_; } |
| 116 | const std::vector<int>& GetYDeltaIndices() const { return y_delta_indices_; } |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 117 | |
| 118 | private: |
| 119 | // The side length of a square filter. |
| 120 | int size_; |
| 121 | // The number of different filters used. |
| 122 | int num_filters_; |
| 123 | // Pixel offsets for all variables. |
| 124 | std::vector<int> x_delta_indices_, y_delta_indices_; |
| 125 | // The coefficients in front of each term. |
| 126 | std::vector<double> alpha_; |
| 127 | // The filters used for the dot product with image patches. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 128 | std::vector<std::vector<double>> filters_; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 129 | }; |
| 130 | |
| 131 | } // namespace examples |
| 132 | } // namespace ceres |
| 133 | |
| 134 | #endif // CERES_EXAMPLES_FIELDS_OF_EXPERTS_H_ |