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 | // Copyright 2007 Google Inc. All Rights Reserved. |
| 29 | // |
| 30 | // Authors: wjr@google.com (William Rucklidge), |
| 31 | // keir@google.com (Keir Mierle), |
| 32 | // dgossow@google.com (David Gossow) |
| 33 | |
| 34 | #ifndef CERES_PUBLIC_GRADIENT_CHECKER_H_ |
| 35 | #define CERES_PUBLIC_GRADIENT_CHECKER_H_ |
| 36 | |
| 37 | #include <memory> |
| 38 | #include <string> |
| 39 | #include <vector> |
| 40 | |
| 41 | #include "ceres/cost_function.h" |
| 42 | #include "ceres/dynamic_numeric_diff_cost_function.h" |
| 43 | #include "ceres/internal/eigen.h" |
| 44 | #include "ceres/internal/fixed_array.h" |
| 45 | #include "ceres/local_parameterization.h" |
| 46 | #include "glog/logging.h" |
| 47 | |
| 48 | namespace ceres { |
| 49 | |
| 50 | // GradientChecker compares the Jacobians returned by a cost function against |
| 51 | // derivatives estimated using finite differencing. |
| 52 | // |
| 53 | // The condition enforced is that |
| 54 | // |
| 55 | // (J_actual(i, j) - J_numeric(i, j)) |
| 56 | // ------------------------------------ < relative_precision |
| 57 | // max(J_actual(i, j), J_numeric(i, j)) |
| 58 | // |
| 59 | // where J_actual(i, j) is the jacobian as computed by the supplied cost |
| 60 | // function (by the user) multiplied by the local parameterization Jacobian |
| 61 | // and J_numeric is the jacobian as computed by finite differences, multiplied |
| 62 | // by the local parameterization Jacobian as well. |
| 63 | // |
| 64 | // How to use: Fill in an array of pointers to parameter blocks for your |
| 65 | // CostFunction, and then call Probe(). Check that the return value is 'true'. |
| 66 | class CERES_EXPORT GradientChecker { |
| 67 | public: |
| 68 | // This will not take ownership of the cost function or local |
| 69 | // parameterizations. |
| 70 | // |
| 71 | // function: The cost function to probe. |
| 72 | // local_parameterizations: A vector of local parameterizations for each |
| 73 | // parameter. May be NULL or contain NULL pointers to indicate that the |
| 74 | // respective parameter does not have a local parameterization. |
| 75 | // options: Options to use for numerical differentiation. |
| 76 | GradientChecker( |
| 77 | const CostFunction* function, |
| 78 | const std::vector<const LocalParameterization*>* local_parameterizations, |
| 79 | const NumericDiffOptions& options); |
| 80 | |
| 81 | // Contains results from a call to Probe for later inspection. |
| 82 | struct CERES_EXPORT ProbeResults { |
| 83 | // The return value of the cost function. |
| 84 | bool return_value; |
| 85 | |
| 86 | // Computed residual vector. |
| 87 | Vector residuals; |
| 88 | |
| 89 | // The sizes of the Jacobians below are dictated by the cost function's |
| 90 | // parameter block size and residual block sizes. If a parameter block |
| 91 | // has a local parameterization associated with it, the size of the "local" |
| 92 | // Jacobian will be determined by the local parameterization dimension and |
| 93 | // residual block size, otherwise it will be identical to the regular |
| 94 | // Jacobian. |
| 95 | |
| 96 | // Derivatives as computed by the cost function. |
| 97 | std::vector<Matrix> jacobians; |
| 98 | |
| 99 | // Derivatives as computed by the cost function in local space. |
| 100 | std::vector<Matrix> local_jacobians; |
| 101 | |
| 102 | // Derivatives as computed by numerical differentiation in local space. |
| 103 | std::vector<Matrix> numeric_jacobians; |
| 104 | |
| 105 | // Derivatives as computed by numerical differentiation in local space. |
| 106 | std::vector<Matrix> local_numeric_jacobians; |
| 107 | |
| 108 | // Contains the maximum relative error found in the local Jacobians. |
| 109 | double maximum_relative_error; |
| 110 | |
| 111 | // If an error was detected, this will contain a detailed description of |
| 112 | // that error. |
| 113 | std::string error_log; |
| 114 | }; |
| 115 | |
| 116 | // Call the cost function, compute alternative Jacobians using finite |
| 117 | // differencing and compare results. If local parameterizations are given, |
| 118 | // the Jacobians will be multiplied by the local parameterization Jacobians |
| 119 | // before performing the check, which effectively means that all errors along |
| 120 | // the null space of the local parameterization will be ignored. |
| 121 | // Returns false if the Jacobians don't match, the cost function return false, |
| 122 | // or if the cost function returns different residual when called with a |
| 123 | // Jacobian output argument vs. calling it without. Otherwise returns true. |
| 124 | // |
| 125 | // parameters: The parameter values at which to probe. |
| 126 | // relative_precision: A threshold for the relative difference between the |
| 127 | // Jacobians. If the Jacobians differ by more than this amount, then the |
| 128 | // probe fails. |
| 129 | // results: On return, the Jacobians (and other information) will be stored |
| 130 | // here. May be NULL. |
| 131 | // |
| 132 | // Returns true if no problems are detected and the difference between the |
| 133 | // Jacobians is less than error_tolerance. |
| 134 | bool Probe(double const* const* parameters, |
| 135 | double relative_precision, |
| 136 | ProbeResults* results) const; |
| 137 | |
| 138 | private: |
| 139 | GradientChecker() = delete; |
| 140 | GradientChecker(const GradientChecker&) = delete; |
| 141 | void operator=(const GradientChecker&) = delete; |
| 142 | |
| 143 | std::vector<const LocalParameterization*> local_parameterizations_; |
| 144 | const CostFunction* function_; |
| 145 | std::unique_ptr<CostFunction> finite_diff_cost_function_; |
| 146 | }; |
| 147 | |
| 148 | } // namespace ceres |
| 149 | |
| 150 | #endif // CERES_PUBLIC_GRADIENT_CHECKER_H_ |