Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 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 | // Author: keir@google.com (Keir Mierle) |
| 30 | // sameeragarwal@google.com (Sameer Agarwal) |
| 31 | |
| 32 | #include "ceres/residual_block.h" |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <cstddef> |
| 36 | #include <vector> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 37 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 38 | #include "ceres/corrector.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 39 | #include "ceres/cost_function.h" |
| 40 | #include "ceres/internal/eigen.h" |
| 41 | #include "ceres/internal/fixed_array.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 42 | #include "ceres/loss_function.h" |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 43 | #include "ceres/manifold.h" |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 44 | #include "ceres/parameter_block.h" |
| 45 | #include "ceres/residual_block_utils.h" |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 46 | #include "ceres/small_blas.h" |
| 47 | |
| 48 | using Eigen::Dynamic; |
| 49 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 50 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 51 | |
| 52 | ResidualBlock::ResidualBlock( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 53 | const CostFunction* cost_function, |
| 54 | const LossFunction* loss_function, |
| 55 | const std::vector<ParameterBlock*>& parameter_blocks, |
| 56 | int index) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 57 | : cost_function_(cost_function), |
| 58 | loss_function_(loss_function), |
| 59 | parameter_blocks_( |
| 60 | new ParameterBlock*[cost_function->parameter_block_sizes().size()]), |
| 61 | index_(index) { |
| 62 | CHECK(cost_function_ != nullptr); |
| 63 | std::copy(parameter_blocks.begin(), |
| 64 | parameter_blocks.end(), |
| 65 | parameter_blocks_.get()); |
| 66 | } |
| 67 | |
| 68 | bool ResidualBlock::Evaluate(const bool apply_loss_function, |
| 69 | double* cost, |
| 70 | double* residuals, |
| 71 | double** jacobians, |
| 72 | double* scratch) const { |
| 73 | const int num_parameter_blocks = NumParameterBlocks(); |
| 74 | const int num_residuals = cost_function_->num_residuals(); |
| 75 | |
| 76 | // Collect the parameters from their blocks. This will rarely allocate, since |
| 77 | // residuals taking more than 8 parameter block arguments are rare. |
| 78 | FixedArray<const double*, 8> parameters(num_parameter_blocks); |
| 79 | for (int i = 0; i < num_parameter_blocks; ++i) { |
| 80 | parameters[i] = parameter_blocks_[i]->state(); |
| 81 | } |
| 82 | |
| 83 | // Put pointers into the scratch space into global_jacobians as appropriate. |
| 84 | FixedArray<double*, 8> global_jacobians(num_parameter_blocks); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 85 | if (jacobians != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 86 | for (int i = 0; i < num_parameter_blocks; ++i) { |
| 87 | const ParameterBlock* parameter_block = parameter_blocks_[i]; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 88 | if (jacobians[i] != nullptr && |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 89 | parameter_block->PlusJacobian() != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 90 | global_jacobians[i] = scratch; |
| 91 | scratch += num_residuals * parameter_block->Size(); |
| 92 | } else { |
| 93 | global_jacobians[i] = jacobians[i]; |
| 94 | } |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // If the caller didn't request residuals, use the scratch space for them. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 99 | bool outputting_residuals = (residuals != nullptr); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 100 | if (!outputting_residuals) { |
| 101 | residuals = scratch; |
| 102 | } |
| 103 | |
| 104 | // Invalidate the evaluation buffers so that we can check them after |
| 105 | // the CostFunction::Evaluate call, to see if all the return values |
| 106 | // that were required were written to and that they are finite. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 107 | double** eval_jacobians = |
| 108 | (jacobians != nullptr) ? global_jacobians.data() : nullptr; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 109 | |
| 110 | InvalidateEvaluation(*this, cost, residuals, eval_jacobians); |
| 111 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 112 | if (!cost_function_->Evaluate(parameters.data(), residuals, eval_jacobians)) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 113 | return false; |
| 114 | } |
| 115 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 116 | if (!IsEvaluationValid(*this, parameters.data(), residuals, eval_jacobians)) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 117 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 118 | std::string message = |
| 119 | "\n\n" |
| 120 | "Error in evaluating the ResidualBlock.\n\n" |
| 121 | "There are two possible reasons. Either the CostFunction did not evaluate and fill all \n" // NOLINT |
| 122 | "residual and jacobians that were requested or there was a non-finite value (nan/infinite)\n" // NOLINT |
| 123 | "generated during the or jacobian computation. \n\n" + |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 124 | EvaluationToString( |
| 125 | *this, parameters.data(), cost, residuals, eval_jacobians); |
| 126 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | LOG(WARNING) << message; |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | double squared_norm = VectorRef(residuals, num_residuals).squaredNorm(); |
| 132 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 133 | // Update the plus_jacobian for the manifolds. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 134 | if (jacobians != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 135 | for (int i = 0; i < num_parameter_blocks; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 136 | if (jacobians[i] != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 137 | const ParameterBlock* parameter_block = parameter_blocks_[i]; |
| 138 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 139 | // Apply the Manifold::PlusJacobian to the ambient jacobians. |
| 140 | if (parameter_block->PlusJacobian() != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 141 | // jacobians[i] = global_jacobians[i] * global_to_local_jacobian. |
| 142 | MatrixMatrixMultiply<Dynamic, Dynamic, Dynamic, Dynamic, 0>( |
| 143 | global_jacobians[i], |
| 144 | num_residuals, |
| 145 | parameter_block->Size(), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 146 | parameter_block->PlusJacobian(), |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 147 | parameter_block->Size(), |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 148 | parameter_block->TangentSize(), |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 149 | jacobians[i], |
| 150 | 0, |
| 151 | 0, |
| 152 | num_residuals, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 153 | parameter_block->TangentSize()); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 154 | } |
| 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 159 | if (loss_function_ == nullptr || !apply_loss_function) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 160 | *cost = 0.5 * squared_norm; |
| 161 | return true; |
| 162 | } |
| 163 | |
| 164 | double rho[3]; |
| 165 | loss_function_->Evaluate(squared_norm, rho); |
| 166 | *cost = 0.5 * rho[0]; |
| 167 | |
| 168 | // No jacobians and not outputting residuals? All done. Doing an early exit |
| 169 | // here avoids constructing the "Corrector" object below in a common case. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 170 | if (jacobians == nullptr && !outputting_residuals) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 171 | return true; |
| 172 | } |
| 173 | |
| 174 | // Correct for the effects of the loss function. The jacobians need to be |
| 175 | // corrected before the residuals, since they use the uncorrected residuals. |
| 176 | Corrector correct(squared_norm, rho); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 177 | if (jacobians != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 178 | for (int i = 0; i < num_parameter_blocks; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 179 | if (jacobians[i] != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 180 | const ParameterBlock* parameter_block = parameter_blocks_[i]; |
| 181 | |
| 182 | // Correct the jacobians for the loss function. |
| 183 | correct.CorrectJacobian(num_residuals, |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 184 | parameter_block->TangentSize(), |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 185 | residuals, |
| 186 | jacobians[i]); |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | // Correct the residuals with the loss function. |
| 192 | if (outputting_residuals) { |
| 193 | correct.CorrectResiduals(num_residuals, residuals); |
| 194 | } |
| 195 | return true; |
| 196 | } |
| 197 | |
| 198 | int ResidualBlock::NumScratchDoublesForEvaluate() const { |
| 199 | // Compute the amount of scratch space needed to store the full-sized |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 200 | // jacobians. For parameters that have no manifold no storage is needed and |
| 201 | // the passed-in jacobian array is used directly. Also include space to store |
| 202 | // the residuals, which is needed for cost-only evaluations. This is slightly |
| 203 | // pessimistic, since both won't be needed all the time, but the amount of |
| 204 | // excess should not cause problems for the caller. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 205 | int num_parameters = NumParameterBlocks(); |
| 206 | int scratch_doubles = 1; |
| 207 | for (int i = 0; i < num_parameters; ++i) { |
| 208 | const ParameterBlock* parameter_block = parameter_blocks_[i]; |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 209 | if (parameter_block->PlusJacobian() != nullptr) { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 210 | scratch_doubles += parameter_block->Size(); |
| 211 | } |
| 212 | } |
| 213 | scratch_doubles *= NumResiduals(); |
| 214 | return scratch_doubles; |
| 215 | } |
| 216 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 217 | } // namespace ceres::internal |