|
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 |
// Copyright 2007 Google Inc. All Rights Reserved.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Authors: wjr@google.com (William Rucklidge),
|
|
Packit |
ea1746 |
// keir@google.com (Keir Mierle),
|
|
Packit |
ea1746 |
// dgossow@google.com (David Gossow)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_PUBLIC_GRADIENT_CHECKER_H_
|
|
Packit |
ea1746 |
#define CERES_PUBLIC_GRADIENT_CHECKER_H_
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
#include <string>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/cost_function.h"
|
|
Packit |
ea1746 |
#include "ceres/dynamic_numeric_diff_cost_function.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/eigen.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/fixed_array.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/macros.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/local_parameterization.h"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// GradientChecker compares the Jacobians returned by a cost function against
|
|
Packit |
ea1746 |
// derivatives estimated using finite differencing.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The condition enforced is that
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// (J_actual(i, j) - J_numeric(i, j))
|
|
Packit |
ea1746 |
// ------------------------------------ < relative_precision
|
|
Packit |
ea1746 |
// max(J_actual(i, j), J_numeric(i, j))
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// where J_actual(i, j) is the jacobian as computed by the supplied cost
|
|
Packit |
ea1746 |
// function (by the user) multiplied by the local parameterization Jacobian
|
|
Packit |
ea1746 |
// and J_numeric is the jacobian as computed by finite differences, multiplied
|
|
Packit |
ea1746 |
// by the local parameterization Jacobian as well.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// How to use: Fill in an array of pointers to parameter blocks for your
|
|
Packit |
ea1746 |
// CostFunction, and then call Probe(). Check that the return value is 'true'.
|
|
Packit |
ea1746 |
class CERES_EXPORT GradientChecker {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
// This will not take ownership of the cost function or local
|
|
Packit |
ea1746 |
// parameterizations.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// function: The cost function to probe.
|
|
Packit |
ea1746 |
// local_parameterization: A vector of local parameterizations for each
|
|
Packit |
ea1746 |
// parameter. May be NULL or contain NULL pointers to indicate that the
|
|
Packit |
ea1746 |
// respective parameter does not have a local parameterization.
|
|
Packit |
ea1746 |
// options: Options to use for numerical differentiation.
|
|
Packit |
ea1746 |
GradientChecker(
|
|
Packit |
ea1746 |
const CostFunction* function,
|
|
Packit |
ea1746 |
const std::vector<const LocalParameterization*>* local_parameterizations,
|
|
Packit |
ea1746 |
const NumericDiffOptions& options);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Contains results from a call to Probe for later inspection.
|
|
Packit |
ea1746 |
struct CERES_EXPORT ProbeResults {
|
|
Packit |
ea1746 |
// The return value of the cost function.
|
|
Packit |
ea1746 |
bool return_value;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Computed residual vector.
|
|
Packit |
ea1746 |
Vector residuals;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// The sizes of the Jacobians below are dictated by the cost function's
|
|
Packit |
ea1746 |
// parameter block size and residual block sizes. If a parameter block
|
|
Packit |
ea1746 |
// has a local parameterization associated with it, the size of the "local"
|
|
Packit |
ea1746 |
// Jacobian will be determined by the local parameterization dimension and
|
|
Packit |
ea1746 |
// residual block size, otherwise it will be identical to the regular
|
|
Packit |
ea1746 |
// Jacobian.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Derivatives as computed by the cost function.
|
|
Packit |
ea1746 |
std::vector<Matrix> jacobians;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Derivatives as computed by the cost function in local space.
|
|
Packit |
ea1746 |
std::vector<Matrix> local_jacobians;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Derivatives as computed by nuerical differentiation in local space.
|
|
Packit |
ea1746 |
std::vector<Matrix> numeric_jacobians;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Derivatives as computed by nuerical differentiation in local space.
|
|
Packit |
ea1746 |
std::vector<Matrix> local_numeric_jacobians;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Contains the maximum relative error found in the local Jacobians.
|
|
Packit |
ea1746 |
double maximum_relative_error;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// If an error was detected, this will contain a detailed description of
|
|
Packit |
ea1746 |
// that error.
|
|
Packit |
ea1746 |
std::string error_log;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Call the cost function, compute alternative Jacobians using finite
|
|
Packit |
ea1746 |
// differencing and compare results. If local parameterizations are given,
|
|
Packit |
ea1746 |
// the Jacobians will be multiplied by the local parameterization Jacobians
|
|
Packit |
ea1746 |
// before performing the check, which effectively means that all errors along
|
|
Packit |
ea1746 |
// the null space of the local parameterization will be ignored.
|
|
Packit |
ea1746 |
// Returns false if the Jacobians don't match, the cost function return false,
|
|
Packit |
ea1746 |
// or if the cost function returns different residual when called with a
|
|
Packit |
ea1746 |
// Jacobian output argument vs. calling it without. Otherwise returns true.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// parameters: The parameter values at which to probe.
|
|
Packit |
ea1746 |
// relative_precision: A threshold for the relative difference between the
|
|
Packit |
ea1746 |
// Jacobians. If the Jacobians differ by more than this amount, then the
|
|
Packit |
ea1746 |
// probe fails.
|
|
Packit |
ea1746 |
// results: On return, the Jacobians (and other information) will be stored
|
|
Packit |
ea1746 |
// here. May be NULL.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Returns true if no problems are detected and the difference between the
|
|
Packit |
ea1746 |
// Jacobians is less than error_tolerance.
|
|
Packit |
ea1746 |
bool Probe(double const* const* parameters,
|
|
Packit |
ea1746 |
double relative_precision,
|
|
Packit |
ea1746 |
ProbeResults* results) const;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
CERES_DISALLOW_IMPLICIT_CONSTRUCTORS(GradientChecker);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
std::vector<const LocalParameterization*> local_parameterizations_;
|
|
Packit |
ea1746 |
const CostFunction* function_;
|
|
Packit |
ea1746 |
internal::scoped_ptr<CostFunction> finite_diff_cost_function_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace ceres
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif // CERES_PUBLIC_GRADIENT_CHECKER_H_
|