Blame internal/ceres/gradient_checking_cost_function.h

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Authors: keir@google.com (Keir Mierle),
Packit ea1746
//          dgossow@google.com (David Gossow)
Packit ea1746
Packit ea1746
#ifndef CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_
Packit ea1746
#define CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_
Packit ea1746
Packit ea1746
#include <string>
Packit ea1746
Packit ea1746
#include "ceres/cost_function.h"
Packit ea1746
#include "ceres/iteration_callback.h"
Packit ea1746
#include "ceres/local_parameterization.h"
Packit ea1746
#include "ceres/mutex.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
namespace internal {
Packit ea1746
Packit ea1746
class ProblemImpl;
Packit ea1746
Packit ea1746
// Callback that collects information about gradient checking errors, and
Packit ea1746
// will abort the solve as soon as an error occurs.
Packit ea1746
class GradientCheckingIterationCallback : public IterationCallback {
Packit ea1746
 public:
Packit ea1746
  GradientCheckingIterationCallback();
Packit ea1746
Packit ea1746
  // Will return SOLVER_CONTINUE until a gradient error has been detected,
Packit ea1746
  // then return SOLVER_ABORT.
Packit ea1746
  virtual CallbackReturnType operator()(const IterationSummary& summary);
Packit ea1746
Packit ea1746
  // Notify this that a gradient error has occurred (thread safe).
Packit ea1746
  void SetGradientErrorDetected(std::string& error_log);
Packit ea1746
Packit ea1746
  // Retrieve error status (not thread safe).
Packit ea1746
  bool gradient_error_detected() const { return gradient_error_detected_; }
Packit ea1746
  const std::string& error_log() const { return error_log_; }
Packit ea1746
 private:
Packit ea1746
  bool gradient_error_detected_;
Packit ea1746
  std::string error_log_;
Packit ea1746
  // Mutex protecting member variables.
Packit ea1746
  ceres::internal::Mutex mutex_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Creates a CostFunction that checks the Jacobians that cost_function computes
Packit ea1746
// with finite differences. This API is only intended for unit tests that intend
Packit ea1746
// to  check the functionality of the GradientCheckingCostFunction
Packit ea1746
// implementation directly.
Packit ea1746
CostFunction* CreateGradientCheckingCostFunction(
Packit ea1746
    const CostFunction* cost_function,
Packit ea1746
    const std::vector<const LocalParameterization*>* local_parameterizations,
Packit ea1746
    double relative_step_size,
Packit ea1746
    double relative_precision,
Packit ea1746
    const std::string& extra_info,
Packit ea1746
    GradientCheckingIterationCallback* callback);
Packit ea1746
Packit ea1746
// Create a new ProblemImpl object from the input problem_impl, where all
Packit ea1746
// cost functions are wrapped so that each time their Evaluate method is called,
Packit ea1746
// an additional check is performed that compares the Jacobians computed by
Packit ea1746
// the original cost function with alternative Jacobians computed using
Packit ea1746
// numerical differentiation. If local parameterizations are given for any
Packit ea1746
// parameters, the Jacobians will be compared in the local space instead of the
Packit ea1746
// ambient space. For details on the gradient checking procedure, see the
Packit ea1746
// documentation of the GradientChecker class. If an error is detected in any
Packit ea1746
// iteration, the respective cost function will notify the
Packit ea1746
// GradientCheckingIterationCallback.
Packit ea1746
//
Packit ea1746
// The caller owns the returned ProblemImpl object.
Packit ea1746
//
Packit ea1746
// Note: This is quite inefficient and is intended only for debugging.
Packit ea1746
//
Packit ea1746
// relative_step_size and relative_precision are parameters to control
Packit ea1746
// the numeric differentiation and the relative tolerance between the
Packit ea1746
// jacobian computed by the CostFunctions in problem_impl and
Packit ea1746
// jacobians obtained by numerically differentiating them. See the
Packit ea1746
// documentation of 'numeric_derivative_relative_step_size' in solver.h for a
Packit ea1746
// better explanation.
Packit ea1746
ProblemImpl* CreateGradientCheckingProblemImpl(
Packit ea1746
    ProblemImpl* problem_impl,
Packit ea1746
    double relative_step_size,
Packit ea1746
    double relative_precision,
Packit ea1746
    GradientCheckingIterationCallback* callback);
Packit ea1746
Packit ea1746
}  // namespace internal
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#endif  // CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_