gradient_checking_cost_function.h 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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. #ifndef CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_
  32. #define CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_
  33. #include <memory>
  34. #include <mutex>
  35. #include <string>
  36. #include "ceres/cost_function.h"
  37. #include "ceres/internal/disable_warnings.h"
  38. #include "ceres/internal/export.h"
  39. #include "ceres/iteration_callback.h"
  40. #include "ceres/manifold.h"
  41. namespace ceres::internal {
  42. class ProblemImpl;
  43. // Callback that collects information about gradient checking errors, and
  44. // will abort the solve as soon as an error occurs.
  45. class CERES_NO_EXPORT GradientCheckingIterationCallback
  46. : public IterationCallback {
  47. public:
  48. GradientCheckingIterationCallback();
  49. // Will return SOLVER_CONTINUE until a gradient error has been detected,
  50. // then return SOLVER_ABORT.
  51. CallbackReturnType operator()(const IterationSummary& summary) final;
  52. // Notify this that a gradient error has occurred (thread safe).
  53. void SetGradientErrorDetected(std::string& error_log);
  54. // Retrieve error status (not thread safe).
  55. bool gradient_error_detected() const { return gradient_error_detected_; }
  56. const std::string& error_log() const { return error_log_; }
  57. private:
  58. bool gradient_error_detected_;
  59. std::string error_log_;
  60. std::mutex mutex_;
  61. };
  62. // Creates a CostFunction that checks the Jacobians that cost_function computes
  63. // with finite differences. This API is only intended for unit tests that intend
  64. // to check the functionality of the GradientCheckingCostFunction
  65. // implementation directly.
  66. CERES_NO_EXPORT std::unique_ptr<CostFunction>
  67. CreateGradientCheckingCostFunction(
  68. const CostFunction* cost_function,
  69. const std::vector<const Manifold*>* manifolds,
  70. double relative_step_size,
  71. double relative_precision,
  72. const std::string& extra_info,
  73. GradientCheckingIterationCallback* callback);
  74. // Create a new ProblemImpl object from the input problem_impl, where all
  75. // cost functions are wrapped so that each time their Evaluate method is called,
  76. // an additional check is performed that compares the Jacobians computed by
  77. // the original cost function with alternative Jacobians computed using
  78. // numerical differentiation. If local parameterizations are given for any
  79. // parameters, the Jacobians will be compared in the local space instead of the
  80. // ambient space. For details on the gradient checking procedure, see the
  81. // documentation of the GradientChecker class. If an error is detected in any
  82. // iteration, the respective cost function will notify the
  83. // GradientCheckingIterationCallback.
  84. //
  85. // Note: This is quite inefficient and is intended only for debugging.
  86. //
  87. // relative_step_size and relative_precision are parameters to control
  88. // the numeric differentiation and the relative tolerance between the
  89. // jacobian computed by the CostFunctions in problem_impl and
  90. // jacobians obtained by numerically differentiating them. See the
  91. // documentation of 'numeric_derivative_relative_step_size' in solver.h for a
  92. // better explanation.
  93. CERES_NO_EXPORT std::unique_ptr<ProblemImpl> CreateGradientCheckingProblemImpl(
  94. ProblemImpl* problem_impl,
  95. double relative_step_size,
  96. double relative_precision,
  97. GradientCheckingIterationCallback* callback);
  98. } // namespace ceres::internal
  99. #include "ceres/internal/reenable_warnings.h"
  100. #endif // CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_