blob: ea6e9b31c8cb8e7abd981683235197cb85e3f46e [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"
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080039#include "ceres/internal/port.h"
Austin Schuh70cc9552019-01-21 19:46:48 -080040#include "ceres/iteration_callback.h"
41#include "ceres/local_parameterization.h"
42
43namespace ceres {
44namespace internal {
45
46class ProblemImpl;
47
48// Callback that collects information about gradient checking errors, and
49// will abort the solve as soon as an error occurs.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080050class CERES_EXPORT_INTERNAL GradientCheckingIterationCallback
51 : public IterationCallback {
Austin Schuh70cc9552019-01-21 19:46:48 -080052 public:
53 GradientCheckingIterationCallback();
54
55 // Will return SOLVER_CONTINUE until a gradient error has been detected,
56 // then return SOLVER_ABORT.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080057 CallbackReturnType operator()(const IterationSummary& summary) final;
Austin Schuh70cc9552019-01-21 19:46:48 -080058
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 Schuh1d1e6ea2020-12-23 21:56:30 -080065
Austin Schuh70cc9552019-01-21 19:46:48 -080066 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 Schuh1d1e6ea2020-12-23 21:56:30 -080076CERES_EXPORT_INTERNAL CostFunction* CreateGradientCheckingCostFunction(
Austin Schuh70cc9552019-01-21 19:46:48 -080077 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 Schuh1d1e6ea2020-12-23 21:56:30 -0800105CERES_EXPORT_INTERNAL ProblemImpl* CreateGradientCheckingProblemImpl(
Austin Schuh70cc9552019-01-21 19:46:48 -0800106 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_