2012-04-30 23:09:08 -07:00
|
|
|
// Ceres Solver - A fast non-linear least squares minimizer
|
2015-03-17 22:30:16 -07:00
|
|
|
// Copyright 2015 Google Inc. All rights reserved.
|
|
|
|
|
// http://ceres-solver.org/
|
2012-04-30 23:09:08 -07:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without
|
|
|
|
|
// modification, are permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// * Redistributions of source code must retain the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer.
|
|
|
|
|
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
|
// this list of conditions and the following disclaimer in the documentation
|
|
|
|
|
// and/or other materials provided with the distribution.
|
|
|
|
|
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
|
|
|
// used to endorse or promote products derived from this software without
|
|
|
|
|
// specific prior written permission.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
|
|
|
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
|
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
|
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
|
|
|
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
|
|
|
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
|
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
|
|
|
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
|
|
|
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
|
|
|
// POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
2016-04-29 16:07:11 +02:00
|
|
|
// Authors: keir@google.com (Keir Mierle),
|
|
|
|
|
// dgossow@google.com (David Gossow)
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
#include "ceres/gradient_checking_cost_function.h"
|
|
|
|
|
|
|
|
|
|
#include <algorithm>
|
|
|
|
|
#include <cmath>
|
2018-08-08 04:27:24 -07:00
|
|
|
#include <cstdint>
|
2022-02-12 08:11:33 -08:00
|
|
|
#include <memory>
|
2012-04-30 23:09:08 -07:00
|
|
|
#include <numeric>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <vector>
|
|
|
|
|
|
2020-09-20 21:45:24 +02:00
|
|
|
#include "ceres/dynamic_numeric_diff_cost_function.h"
|
2016-04-29 16:07:11 +02:00
|
|
|
#include "ceres/gradient_checker.h"
|
2012-08-13 15:12:01 -07:00
|
|
|
#include "ceres/internal/eigen.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/parameter_block.h"
|
2012-08-13 15:12:01 -07:00
|
|
|
#include "ceres/problem.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
#include "ceres/problem_impl.h"
|
|
|
|
|
#include "ceres/program.h"
|
|
|
|
|
#include "ceres/residual_block.h"
|
|
|
|
|
#include "ceres/stringprintf.h"
|
|
|
|
|
#include "ceres/types.h"
|
2012-08-13 15:12:01 -07:00
|
|
|
#include "glog/logging.h"
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
namespace ceres {
|
|
|
|
|
namespace internal {
|
2014-12-17 07:35:09 -08:00
|
|
|
|
2015-01-08 11:45:15 -08:00
|
|
|
using std::abs;
|
|
|
|
|
using std::max;
|
2015-01-07 15:10:46 -08:00
|
|
|
using std::string;
|
2014-12-17 07:35:09 -08:00
|
|
|
using std::vector;
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
namespace {
|
|
|
|
|
|
2022-02-17 17:06:49 -08:00
|
|
|
class GradientCheckingCostFunction final : public CostFunction {
|
2012-04-30 23:09:08 -07:00
|
|
|
public:
|
2021-12-28 07:05:03 -08:00
|
|
|
GradientCheckingCostFunction(const CostFunction* function,
|
|
|
|
|
const std::vector<const Manifold*>* manifolds,
|
|
|
|
|
const NumericDiffOptions& options,
|
|
|
|
|
double relative_precision,
|
|
|
|
|
const string& extra_info,
|
|
|
|
|
GradientCheckingIterationCallback* callback)
|
2012-04-30 23:09:08 -07:00
|
|
|
: function_(function),
|
2021-12-28 07:05:03 -08:00
|
|
|
gradient_checker_(function, manifolds, options),
|
2012-04-30 23:09:08 -07:00
|
|
|
relative_precision_(relative_precision),
|
2016-04-29 16:07:11 +02:00
|
|
|
extra_info_(extra_info),
|
|
|
|
|
callback_(callback) {
|
2018-09-09 14:14:13 -07:00
|
|
|
CHECK(callback_ != nullptr);
|
2018-08-08 04:27:24 -07:00
|
|
|
const vector<int32_t>& parameter_block_sizes =
|
2013-10-09 10:12:43 -07:00
|
|
|
function->parameter_block_sizes();
|
|
|
|
|
*mutable_parameter_block_sizes() = parameter_block_sizes;
|
2012-04-30 23:09:08 -07:00
|
|
|
set_num_residuals(function->num_residuals());
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-14 00:16:13 +02:00
|
|
|
bool Evaluate(double const* const* parameters,
|
|
|
|
|
double* residuals,
|
|
|
|
|
double** jacobians) const final {
|
2012-04-30 23:09:08 -07:00
|
|
|
if (!jacobians) {
|
|
|
|
|
// Nothing to check in this case; just forward.
|
2022-02-02 13:17:29 -08:00
|
|
|
return function_->Evaluate(parameters, residuals, nullptr);
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
GradientChecker::ProbeResults results;
|
2020-09-20 21:45:24 +02:00
|
|
|
bool okay =
|
|
|
|
|
gradient_checker_.Probe(parameters, relative_precision_, &results);
|
2012-04-30 23:09:08 -07:00
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
// If the cost function returned false, there's nothing we can say about
|
|
|
|
|
// the gradients.
|
|
|
|
|
if (results.return_value == false) {
|
2012-04-30 23:09:08 -07:00
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
// Copy the residuals.
|
|
|
|
|
const int num_residuals = function_->num_residuals();
|
|
|
|
|
MatrixRef(residuals, num_residuals, 1) = results.residuals;
|
2012-04-30 23:09:08 -07:00
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
// Copy the original jacobian blocks into the jacobians array.
|
2018-08-08 04:27:24 -07:00
|
|
|
const vector<int32_t>& block_sizes = function_->parameter_block_sizes();
|
2012-04-30 23:09:08 -07:00
|
|
|
for (int k = 0; k < block_sizes.size(); k++) {
|
2022-02-02 13:17:29 -08:00
|
|
|
if (jacobians[k] != nullptr) {
|
2012-04-30 23:09:08 -07:00
|
|
|
MatrixRef(jacobians[k],
|
2016-04-29 16:07:11 +02:00
|
|
|
results.jacobians[k].rows(),
|
|
|
|
|
results.jacobians[k].cols()) = results.jacobians[k];
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
if (!okay) {
|
2020-09-20 21:45:24 +02:00
|
|
|
std::string error_log =
|
|
|
|
|
"Gradient Error detected!\nExtra info for this residual: " +
|
|
|
|
|
extra_info_ + "\n" + results.error_log;
|
2016-04-29 16:07:11 +02:00
|
|
|
callback_->SetGradientErrorDetected(error_log);
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private:
|
|
|
|
|
const CostFunction* function_;
|
2016-04-29 16:07:11 +02:00
|
|
|
GradientChecker gradient_checker_;
|
2012-04-30 23:09:08 -07:00
|
|
|
double relative_precision_;
|
|
|
|
|
string extra_info_;
|
2016-04-29 16:07:11 +02:00
|
|
|
GradientCheckingIterationCallback* callback_;
|
2012-04-30 23:09:08 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
GradientCheckingIterationCallback::GradientCheckingIterationCallback()
|
2020-09-20 21:45:24 +02:00
|
|
|
: gradient_error_detected_(false) {}
|
2016-04-29 16:07:11 +02:00
|
|
|
|
|
|
|
|
CallbackReturnType GradientCheckingIterationCallback::operator()(
|
|
|
|
|
const IterationSummary& summary) {
|
|
|
|
|
if (gradient_error_detected_) {
|
2020-09-20 21:45:24 +02:00
|
|
|
LOG(ERROR) << "Gradient error detected. Terminating solver.";
|
2016-04-29 16:07:11 +02:00
|
|
|
return SOLVER_ABORT;
|
|
|
|
|
}
|
|
|
|
|
return SOLVER_CONTINUE;
|
|
|
|
|
}
|
2022-02-10 16:16:37 -08:00
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
void GradientCheckingIterationCallback::SetGradientErrorDetected(
|
|
|
|
|
std::string& error_log) {
|
2018-04-02 06:03:06 -07:00
|
|
|
std::lock_guard<std::mutex> l(mutex_);
|
2016-04-29 16:07:11 +02:00
|
|
|
gradient_error_detected_ = true;
|
|
|
|
|
error_log_ += "\n" + error_log;
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-10 16:16:37 -08:00
|
|
|
std::unique_ptr<CostFunction> CreateGradientCheckingCostFunction(
|
2016-04-29 16:07:11 +02:00
|
|
|
const CostFunction* cost_function,
|
2021-12-28 07:05:03 -08:00
|
|
|
const std::vector<const Manifold*>* manifolds,
|
2012-04-30 23:09:08 -07:00
|
|
|
double relative_step_size,
|
|
|
|
|
double relative_precision,
|
2016-04-29 16:07:11 +02:00
|
|
|
const std::string& extra_info,
|
|
|
|
|
GradientCheckingIterationCallback* callback) {
|
2015-05-13 15:43:51 +03:00
|
|
|
NumericDiffOptions numeric_diff_options;
|
|
|
|
|
numeric_diff_options.relative_step_size = relative_step_size;
|
|
|
|
|
|
2022-02-10 16:16:37 -08:00
|
|
|
return std::make_unique<GradientCheckingCostFunction>(cost_function,
|
|
|
|
|
manifolds,
|
|
|
|
|
numeric_diff_options,
|
|
|
|
|
relative_precision,
|
|
|
|
|
extra_info,
|
|
|
|
|
callback);
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2022-02-02 13:17:29 -08:00
|
|
|
std::unique_ptr<ProblemImpl> CreateGradientCheckingProblemImpl(
|
2016-04-29 16:07:11 +02:00
|
|
|
ProblemImpl* problem_impl,
|
|
|
|
|
double relative_step_size,
|
|
|
|
|
double relative_precision,
|
|
|
|
|
GradientCheckingIterationCallback* callback) {
|
2018-08-27 07:12:43 -07:00
|
|
|
CHECK(callback != nullptr);
|
2021-12-28 07:05:03 -08:00
|
|
|
// We create new CostFunctions by wrapping the original CostFunction in a
|
|
|
|
|
// gradient checking CostFunction. So its okay for the ProblemImpl to take
|
|
|
|
|
// ownership of it and destroy it. The LossFunctions and Manifolds are reused
|
|
|
|
|
// and since they are owned by problem_impl, gradient_checking_problem_impl
|
2012-04-30 23:09:08 -07:00
|
|
|
// should not take ownership of it.
|
|
|
|
|
Problem::Options gradient_checking_problem_options;
|
|
|
|
|
gradient_checking_problem_options.cost_function_ownership = TAKE_OWNERSHIP;
|
|
|
|
|
gradient_checking_problem_options.loss_function_ownership =
|
|
|
|
|
DO_NOT_TAKE_OWNERSHIP;
|
2021-12-28 07:05:03 -08:00
|
|
|
gradient_checking_problem_options.manifold_ownership = DO_NOT_TAKE_OWNERSHIP;
|
2018-02-22 10:28:39 -08:00
|
|
|
gradient_checking_problem_options.context = problem_impl->context();
|
2012-04-30 23:09:08 -07:00
|
|
|
|
2016-04-29 16:07:11 +02:00
|
|
|
NumericDiffOptions numeric_diff_options;
|
|
|
|
|
numeric_diff_options.relative_step_size = relative_step_size;
|
|
|
|
|
|
2022-02-02 13:17:29 -08:00
|
|
|
auto gradient_checking_problem_impl =
|
|
|
|
|
std::make_unique<ProblemImpl>(gradient_checking_problem_options);
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
Program* program = problem_impl->mutable_program();
|
|
|
|
|
|
2021-12-28 07:05:03 -08:00
|
|
|
// For every ParameterBlock in problem_impl, create a new parameter block with
|
|
|
|
|
// the same manifold and constancy.
|
2015-01-08 11:45:15 -08:00
|
|
|
const vector<ParameterBlock*>& parameter_blocks = program->parameter_blocks();
|
2012-04-30 23:09:08 -07:00
|
|
|
for (int i = 0; i < parameter_blocks.size(); ++i) {
|
|
|
|
|
ParameterBlock* parameter_block = parameter_blocks[i];
|
|
|
|
|
gradient_checking_problem_impl->AddParameterBlock(
|
|
|
|
|
parameter_block->mutable_user_state(),
|
|
|
|
|
parameter_block->Size(),
|
2021-12-28 07:05:03 -08:00
|
|
|
parameter_block->mutable_manifold());
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
if (parameter_block->IsConstant()) {
|
|
|
|
|
gradient_checking_problem_impl->SetParameterBlockConstant(
|
|
|
|
|
parameter_block->mutable_user_state());
|
|
|
|
|
}
|
2018-07-07 17:11:38 -07:00
|
|
|
|
2020-09-20 21:45:24 +02:00
|
|
|
for (int i = 0; i < parameter_block->Size(); ++i) {
|
2018-07-07 17:11:38 -07:00
|
|
|
gradient_checking_problem_impl->SetParameterUpperBound(
|
|
|
|
|
parameter_block->mutable_user_state(),
|
|
|
|
|
i,
|
|
|
|
|
parameter_block->UpperBound(i));
|
|
|
|
|
gradient_checking_problem_impl->SetParameterLowerBound(
|
|
|
|
|
parameter_block->mutable_user_state(),
|
|
|
|
|
i,
|
|
|
|
|
parameter_block->LowerBound(i));
|
|
|
|
|
}
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// For every ResidualBlock in problem_impl, create a new
|
|
|
|
|
// ResidualBlock by wrapping its CostFunction inside a
|
|
|
|
|
// GradientCheckingCostFunction.
|
2015-01-08 11:45:15 -08:00
|
|
|
const vector<ResidualBlock*>& residual_blocks = program->residual_blocks();
|
2012-04-30 23:09:08 -07:00
|
|
|
for (int i = 0; i < residual_blocks.size(); ++i) {
|
|
|
|
|
ResidualBlock* residual_block = residual_blocks[i];
|
|
|
|
|
|
|
|
|
|
// Build a human readable string which identifies the
|
|
|
|
|
// ResidualBlock. This is used by the GradientCheckingCostFunction
|
|
|
|
|
// when logging debugging information.
|
2020-09-20 21:45:24 +02:00
|
|
|
string extra_info =
|
|
|
|
|
StringPrintf("Residual block id %d; depends on parameters [", i);
|
2012-04-30 23:09:08 -07:00
|
|
|
vector<double*> parameter_blocks;
|
2021-12-28 07:05:03 -08:00
|
|
|
vector<const Manifold*> manifolds;
|
2016-04-29 16:07:11 +02:00
|
|
|
parameter_blocks.reserve(residual_block->NumParameterBlocks());
|
2021-12-28 07:05:03 -08:00
|
|
|
manifolds.reserve(residual_block->NumParameterBlocks());
|
2012-04-30 23:09:08 -07:00
|
|
|
for (int j = 0; j < residual_block->NumParameterBlocks(); ++j) {
|
|
|
|
|
ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
|
|
|
|
|
parameter_blocks.push_back(parameter_block->mutable_user_state());
|
|
|
|
|
StringAppendF(&extra_info, "%p", parameter_block->mutable_user_state());
|
|
|
|
|
extra_info += (j < residual_block->NumParameterBlocks() - 1) ? ", " : "]";
|
2021-12-28 07:05:03 -08:00
|
|
|
manifolds.push_back(
|
|
|
|
|
problem_impl->GetManifold(parameter_block->mutable_user_state()));
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Wrap the original CostFunction in a GradientCheckingCostFunction.
|
|
|
|
|
CostFunction* gradient_checking_cost_function =
|
2016-04-29 16:07:11 +02:00
|
|
|
new GradientCheckingCostFunction(residual_block->cost_function(),
|
2021-12-28 07:05:03 -08:00
|
|
|
&manifolds,
|
2016-04-29 16:07:11 +02:00
|
|
|
numeric_diff_options,
|
|
|
|
|
relative_precision,
|
|
|
|
|
extra_info,
|
|
|
|
|
callback);
|
2012-04-30 23:09:08 -07:00
|
|
|
|
|
|
|
|
// The const_cast is necessary because
|
|
|
|
|
// ProblemImpl::AddResidualBlock can potentially take ownership of
|
|
|
|
|
// the LossFunction, but in this case we are guaranteed that this
|
|
|
|
|
// will not be the case, so this const_cast is harmless.
|
|
|
|
|
gradient_checking_problem_impl->AddResidualBlock(
|
|
|
|
|
gradient_checking_cost_function,
|
|
|
|
|
const_cast<LossFunction*>(residual_block->loss_function()),
|
2018-10-15 21:10:05 +02:00
|
|
|
parameter_blocks.data(),
|
|
|
|
|
static_cast<int>(parameter_blocks.size()));
|
2012-04-30 23:09:08 -07:00
|
|
|
}
|
|
|
|
|
|
2014-08-07 12:19:10 -07:00
|
|
|
// Normally, when a problem is given to the solver, we guarantee
|
|
|
|
|
// that the state pointers for each parameter block point to the
|
|
|
|
|
// user provided data. Since we are creating this new problem from a
|
|
|
|
|
// problem given to us at an arbitrary stage of the solve, we cannot
|
|
|
|
|
// depend on this being the case, so we explicitly call
|
|
|
|
|
// SetParameterBlockStatePtrsToUserStatePtrs to ensure that this is
|
|
|
|
|
// the case.
|
2020-09-20 21:45:24 +02:00
|
|
|
gradient_checking_problem_impl->mutable_program()
|
2014-08-07 12:19:10 -07:00
|
|
|
->SetParameterBlockStatePtrsToUserStatePtrs();
|
|
|
|
|
|
2012-04-30 23:09:08 -07:00
|
|
|
return gradient_checking_problem_impl;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
} // namespace internal
|
|
|
|
|
} // namespace ceres
|