|
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 |
// Author: keir@google.com (Keir Mierle)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/compressed_row_jacobian_writer.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <iterator>
|
|
Packit |
ea1746 |
#include <utility>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/casts.h"
|
|
Packit |
ea1746 |
#include "ceres/compressed_row_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/parameter_block.h"
|
|
Packit |
ea1746 |
#include "ceres/program.h"
|
|
Packit |
ea1746 |
#include "ceres/residual_block.h"
|
|
Packit |
ea1746 |
#include "ceres/scratch_evaluate_preparer.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
using std::make_pair;
|
|
Packit |
ea1746 |
using std::pair;
|
|
Packit |
ea1746 |
using std::vector;
|
|
Packit |
ea1746 |
using std::adjacent_find;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowJacobianWriter::PopulateJacobianRowAndColumnBlockVectors(
|
|
Packit |
ea1746 |
const Program* program, CompressedRowSparseMatrix* jacobian) {
|
|
Packit |
ea1746 |
const vector<ParameterBlock*>& parameter_blocks =
|
|
Packit |
ea1746 |
program->parameter_blocks();
|
|
Packit |
ea1746 |
vector<int>& col_blocks = *(jacobian->mutable_col_blocks());
|
|
Packit |
ea1746 |
col_blocks.resize(parameter_blocks.size());
|
|
Packit |
ea1746 |
for (int i = 0; i < parameter_blocks.size(); ++i) {
|
|
Packit |
ea1746 |
col_blocks[i] = parameter_blocks[i]->LocalSize();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const vector<ResidualBlock*>& residual_blocks =
|
|
Packit |
ea1746 |
program->residual_blocks();
|
|
Packit |
ea1746 |
vector<int>& row_blocks = *(jacobian->mutable_row_blocks());
|
|
Packit |
ea1746 |
row_blocks.resize(residual_blocks.size());
|
|
Packit |
ea1746 |
for (int i = 0; i < residual_blocks.size(); ++i) {
|
|
Packit |
ea1746 |
row_blocks[i] = residual_blocks[i]->NumResiduals();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowJacobianWriter::GetOrderedParameterBlocks(
|
|
Packit |
ea1746 |
const Program* program,
|
|
Packit |
ea1746 |
int residual_id,
|
|
Packit |
ea1746 |
vector<pair<int, int> >* evaluated_jacobian_blocks) {
|
|
Packit |
ea1746 |
const ResidualBlock* residual_block =
|
|
Packit |
ea1746 |
program->residual_blocks()[residual_id];
|
|
Packit |
ea1746 |
const int num_parameter_blocks = residual_block->NumParameterBlocks();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int j = 0; j < num_parameter_blocks; ++j) {
|
|
Packit |
ea1746 |
const ParameterBlock* parameter_block =
|
|
Packit |
ea1746 |
residual_block->parameter_blocks()[j];
|
|
Packit |
ea1746 |
if (!parameter_block->IsConstant()) {
|
|
Packit |
ea1746 |
evaluated_jacobian_blocks->push_back(
|
|
Packit |
ea1746 |
make_pair(parameter_block->index(), j));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
sort(evaluated_jacobian_blocks->begin(), evaluated_jacobian_blocks->end());
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
SparseMatrix* CompressedRowJacobianWriter::CreateJacobian() const {
|
|
Packit |
ea1746 |
const vector<ResidualBlock*>& residual_blocks =
|
|
Packit |
ea1746 |
program_->residual_blocks();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int total_num_residuals = program_->NumResiduals();
|
|
Packit |
ea1746 |
int total_num_effective_parameters = program_->NumEffectiveParameters();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Count the number of jacobian nonzeros.
|
|
Packit |
ea1746 |
int num_jacobian_nonzeros = 0;
|
|
Packit |
ea1746 |
for (int i = 0; i < residual_blocks.size(); ++i) {
|
|
Packit |
ea1746 |
ResidualBlock* residual_block = residual_blocks[i];
|
|
Packit |
ea1746 |
const int num_residuals = residual_block->NumResiduals();
|
|
Packit |
ea1746 |
const int num_parameter_blocks = residual_block->NumParameterBlocks();
|
|
Packit |
ea1746 |
for (int j = 0; j < num_parameter_blocks; ++j) {
|
|
Packit |
ea1746 |
ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
|
|
Packit |
ea1746 |
if (!parameter_block->IsConstant()) {
|
|
Packit |
ea1746 |
num_jacobian_nonzeros += num_residuals * parameter_block->LocalSize();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Allocate storage for the jacobian with some extra space at the end.
|
|
Packit |
ea1746 |
// Allocate more space than needed to store the jacobian so that when the LM
|
|
Packit |
ea1746 |
// algorithm adds the diagonal, no reallocation is necessary. This reduces
|
|
Packit |
ea1746 |
// peak memory usage significantly.
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* jacobian =
|
|
Packit |
ea1746 |
new CompressedRowSparseMatrix(
|
|
Packit |
ea1746 |
total_num_residuals,
|
|
Packit |
ea1746 |
total_num_effective_parameters,
|
|
Packit |
ea1746 |
num_jacobian_nonzeros + total_num_effective_parameters);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// At this stage, the CompressedRowSparseMatrix is an invalid state. But this
|
|
Packit |
ea1746 |
// seems to be the only way to construct it without doing a memory copy.
|
|
Packit |
ea1746 |
int* rows = jacobian->mutable_rows();
|
|
Packit |
ea1746 |
int* cols = jacobian->mutable_cols();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
int row_pos = 0;
|
|
Packit |
ea1746 |
rows[0] = 0;
|
|
Packit |
ea1746 |
for (int i = 0; i < residual_blocks.size(); ++i) {
|
|
Packit |
ea1746 |
const ResidualBlock* residual_block = residual_blocks[i];
|
|
Packit |
ea1746 |
const int num_parameter_blocks = residual_block->NumParameterBlocks();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Count the number of derivatives for a row of this residual block and
|
|
Packit |
ea1746 |
// build a list of active parameter block indices.
|
|
Packit |
ea1746 |
int num_derivatives = 0;
|
|
Packit |
ea1746 |
vector<int> parameter_indices;
|
|
Packit |
ea1746 |
for (int j = 0; j < num_parameter_blocks; ++j) {
|
|
Packit |
ea1746 |
ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
|
|
Packit |
ea1746 |
if (!parameter_block->IsConstant()) {
|
|
Packit |
ea1746 |
parameter_indices.push_back(parameter_block->index());
|
|
Packit |
ea1746 |
num_derivatives += parameter_block->LocalSize();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Sort the parameters by their position in the state vector.
|
|
Packit |
ea1746 |
sort(parameter_indices.begin(), parameter_indices.end());
|
|
Packit |
ea1746 |
if (adjacent_find(parameter_indices.begin(), parameter_indices.end()) !=
|
|
Packit |
ea1746 |
parameter_indices.end()) {
|
|
Packit |
ea1746 |
std::string parameter_block_description;
|
|
Packit |
ea1746 |
for (int j = 0; j < num_parameter_blocks; ++j) {
|
|
Packit |
ea1746 |
ParameterBlock* parameter_block = residual_block->parameter_blocks()[j];
|
|
Packit |
ea1746 |
parameter_block_description +=
|
|
Packit |
ea1746 |
parameter_block->ToString() + "\n";
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
LOG(FATAL) << "Ceres internal error: "
|
|
Packit |
ea1746 |
<< "Duplicate parameter blocks detected in a cost function. "
|
|
Packit |
ea1746 |
<< "This should never happen. Please report this to "
|
|
Packit |
ea1746 |
<< "the Ceres developers.\n"
|
|
Packit |
ea1746 |
<< "Residual Block: " << residual_block->ToString() << "\n"
|
|
Packit |
ea1746 |
<< "Parameter Blocks: " << parameter_block_description;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Update the row indices.
|
|
Packit |
ea1746 |
const int num_residuals = residual_block->NumResiduals();
|
|
Packit |
ea1746 |
for (int j = 0; j < num_residuals; ++j) {
|
|
Packit |
ea1746 |
rows[row_pos + j + 1] = rows[row_pos + j] + num_derivatives;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Iterate over parameter blocks in the order which they occur in the
|
|
Packit |
ea1746 |
// parameter vector. This code mirrors that in Write(), where jacobian
|
|
Packit |
ea1746 |
// values are updated.
|
|
Packit |
ea1746 |
int col_pos = 0;
|
|
Packit |
ea1746 |
for (int j = 0; j < parameter_indices.size(); ++j) {
|
|
Packit |
ea1746 |
ParameterBlock* parameter_block =
|
|
Packit |
ea1746 |
program_->parameter_blocks()[parameter_indices[j]];
|
|
Packit |
ea1746 |
const int parameter_block_size = parameter_block->LocalSize();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int r = 0; r < num_residuals; ++r) {
|
|
Packit |
ea1746 |
// This is the position in the values array of the jacobian where this
|
|
Packit |
ea1746 |
// row of the jacobian block should go.
|
|
Packit |
ea1746 |
const int column_block_begin = rows[row_pos + r] + col_pos;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int c = 0; c < parameter_block_size; ++c) {
|
|
Packit |
ea1746 |
cols[column_block_begin + c] = parameter_block->delta_offset() + c;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
col_pos += parameter_block_size;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
row_pos += num_residuals;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
CHECK_EQ(num_jacobian_nonzeros, rows[total_num_residuals]);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
PopulateJacobianRowAndColumnBlockVectors(program_, jacobian);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
return jacobian;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void CompressedRowJacobianWriter::Write(int residual_id,
|
|
Packit |
ea1746 |
int residual_offset,
|
|
Packit |
ea1746 |
double **jacobians,
|
|
Packit |
ea1746 |
SparseMatrix* base_jacobian) {
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* jacobian =
|
|
Packit |
ea1746 |
down_cast<CompressedRowSparseMatrix*>(base_jacobian);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double* jacobian_values = jacobian->mutable_values();
|
|
Packit |
ea1746 |
const int* jacobian_rows = jacobian->rows();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const ResidualBlock* residual_block =
|
|
Packit |
ea1746 |
program_->residual_blocks()[residual_id];
|
|
Packit |
ea1746 |
const int num_residuals = residual_block->NumResiduals();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
vector<pair<int, int> > evaluated_jacobian_blocks;
|
|
Packit |
ea1746 |
GetOrderedParameterBlocks(program_, residual_id, &evaluated_jacobian_blocks);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Where in the current row does the jacobian for a parameter block begin.
|
|
Packit |
ea1746 |
int col_pos = 0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Iterate over the jacobian blocks in increasing order of their
|
|
Packit |
ea1746 |
// positions in the reduced parameter vector.
|
|
Packit |
ea1746 |
for (int i = 0; i < evaluated_jacobian_blocks.size(); ++i) {
|
|
Packit |
ea1746 |
const ParameterBlock* parameter_block =
|
|
Packit |
ea1746 |
program_->parameter_blocks()[evaluated_jacobian_blocks[i].first];
|
|
Packit |
ea1746 |
const int argument = evaluated_jacobian_blocks[i].second;
|
|
Packit |
ea1746 |
const int parameter_block_size = parameter_block->LocalSize();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Copy one row of the jacobian block at a time.
|
|
Packit |
ea1746 |
for (int r = 0; r < num_residuals; ++r) {
|
|
Packit |
ea1746 |
// Position of the r^th row of the current jacobian block.
|
|
Packit |
ea1746 |
const double* block_row_begin =
|
|
Packit |
ea1746 |
jacobians[argument] + r * parameter_block_size;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Position in the values array of the jacobian where this
|
|
Packit |
ea1746 |
// row of the jacobian block should go.
|
|
Packit |
ea1746 |
double* column_block_begin =
|
|
Packit |
ea1746 |
jacobian_values + jacobian_rows[residual_offset + r] + col_pos;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
std::copy(block_row_begin,
|
|
Packit |
ea1746 |
block_row_begin + parameter_block_size,
|
|
Packit |
ea1746 |
column_block_begin);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
col_pos += parameter_block_size;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|