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