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 | // Authors: keir@google.com (Keir Mierle), |
| 30 | // dgossow@google.com (David Gossow) |
| 31 | |
| 32 | #ifndef CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_ |
| 33 | #define CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_ |
| 34 | |
| 35 | #include <mutex> |
| 36 | #include <string> |
| 37 | |
| 38 | #include "ceres/cost_function.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 39 | #include "ceres/internal/port.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 40 | #include "ceres/iteration_callback.h" |
| 41 | #include "ceres/local_parameterization.h" |
| 42 | |
| 43 | namespace ceres { |
| 44 | namespace internal { |
| 45 | |
| 46 | class ProblemImpl; |
| 47 | |
| 48 | // Callback that collects information about gradient checking errors, and |
| 49 | // will abort the solve as soon as an error occurs. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 50 | class CERES_EXPORT_INTERNAL GradientCheckingIterationCallback |
| 51 | : public IterationCallback { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 52 | public: |
| 53 | GradientCheckingIterationCallback(); |
| 54 | |
| 55 | // Will return SOLVER_CONTINUE until a gradient error has been detected, |
| 56 | // then return SOLVER_ABORT. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 57 | CallbackReturnType operator()(const IterationSummary& summary) final; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 58 | |
| 59 | // Notify this that a gradient error has occurred (thread safe). |
| 60 | void SetGradientErrorDetected(std::string& error_log); |
| 61 | |
| 62 | // Retrieve error status (not thread safe). |
| 63 | bool gradient_error_detected() const { return gradient_error_detected_; } |
| 64 | const std::string& error_log() const { return error_log_; } |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 65 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 66 | private: |
| 67 | bool gradient_error_detected_; |
| 68 | std::string error_log_; |
| 69 | std::mutex mutex_; |
| 70 | }; |
| 71 | |
| 72 | // Creates a CostFunction that checks the Jacobians that cost_function computes |
| 73 | // with finite differences. This API is only intended for unit tests that intend |
| 74 | // to check the functionality of the GradientCheckingCostFunction |
| 75 | // implementation directly. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 76 | CERES_EXPORT_INTERNAL CostFunction* CreateGradientCheckingCostFunction( |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 77 | const CostFunction* cost_function, |
| 78 | const std::vector<const LocalParameterization*>* local_parameterizations, |
| 79 | double relative_step_size, |
| 80 | double relative_precision, |
| 81 | const std::string& extra_info, |
| 82 | GradientCheckingIterationCallback* callback); |
| 83 | |
| 84 | // Create a new ProblemImpl object from the input problem_impl, where all |
| 85 | // cost functions are wrapped so that each time their Evaluate method is called, |
| 86 | // an additional check is performed that compares the Jacobians computed by |
| 87 | // the original cost function with alternative Jacobians computed using |
| 88 | // numerical differentiation. If local parameterizations are given for any |
| 89 | // parameters, the Jacobians will be compared in the local space instead of the |
| 90 | // ambient space. For details on the gradient checking procedure, see the |
| 91 | // documentation of the GradientChecker class. If an error is detected in any |
| 92 | // iteration, the respective cost function will notify the |
| 93 | // GradientCheckingIterationCallback. |
| 94 | // |
| 95 | // The caller owns the returned ProblemImpl object. |
| 96 | // |
| 97 | // Note: This is quite inefficient and is intended only for debugging. |
| 98 | // |
| 99 | // relative_step_size and relative_precision are parameters to control |
| 100 | // the numeric differentiation and the relative tolerance between the |
| 101 | // jacobian computed by the CostFunctions in problem_impl and |
| 102 | // jacobians obtained by numerically differentiating them. See the |
| 103 | // documentation of 'numeric_derivative_relative_step_size' in solver.h for a |
| 104 | // better explanation. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame^] | 105 | CERES_EXPORT_INTERNAL ProblemImpl* CreateGradientCheckingProblemImpl( |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 106 | ProblemImpl* problem_impl, |
| 107 | double relative_step_size, |
| 108 | double relative_precision, |
| 109 | GradientCheckingIterationCallback* callback); |
| 110 | |
| 111 | } // namespace internal |
| 112 | } // namespace ceres |
| 113 | |
| 114 | #endif // CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_ |