corrector.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2015 Google Inc. All rights reserved.
  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: sameeragarwal@google.com (Sameer Agarwal)
  30. #include "ceres/corrector.h"
  31. #include <cmath>
  32. #include <cstddef>
  33. #include "ceres/internal/eigen.h"
  34. #include "glog/logging.h"
  35. namespace ceres::internal {
  36. Corrector::Corrector(const double sq_norm, const double rho[3]) {
  37. CHECK_GE(sq_norm, 0.0);
  38. sqrt_rho1_ = sqrt(rho[1]);
  39. // If sq_norm = 0.0, the correction becomes trivial, the residual
  40. // and the jacobian are scaled by the square root of the derivative
  41. // of rho. Handling this case explicitly avoids the divide by zero
  42. // error that would occur below.
  43. //
  44. // The case where rho'' < 0 also gets special handling. Technically
  45. // it shouldn't, and the computation of the scaling should proceed
  46. // as below, however we found in experiments that applying the
  47. // curvature correction when rho'' < 0, which is the case when we
  48. // are in the outlier region slows down the convergence of the
  49. // algorithm significantly.
  50. //
  51. // Thus, we have divided the action of the robustifier into two
  52. // parts. In the inliner region, we do the full second order
  53. // correction which re-wights the gradient of the function by the
  54. // square root of the derivative of rho, and the Gauss-Newton
  55. // Hessian gets both the scaling and the rank-1 curvature
  56. // correction. Normally, alpha is upper bounded by one, but with this
  57. // change, alpha is bounded above by zero.
  58. //
  59. // Empirically we have observed that the full Triggs correction and
  60. // the clamped correction both start out as very good approximations
  61. // to the loss function when we are in the convex part of the
  62. // function, but as the function starts transitioning from convex to
  63. // concave, the Triggs approximation diverges more and more and
  64. // ultimately becomes linear. The clamped Triggs model however
  65. // remains quadratic.
  66. //
  67. // The reason why the Triggs approximation becomes so poor is
  68. // because the curvature correction that it applies to the gauss
  69. // newton hessian goes from being a full rank correction to a rank
  70. // deficient correction making the inversion of the Hessian fraught
  71. // with all sorts of misery and suffering.
  72. //
  73. // The clamped correction retains its quadratic nature and inverting it
  74. // is always well formed.
  75. if ((sq_norm == 0.0) || (rho[2] <= 0.0)) {
  76. residual_scaling_ = sqrt_rho1_;
  77. alpha_sq_norm_ = 0.0;
  78. return;
  79. }
  80. // We now require that the first derivative of the loss function be
  81. // positive only if the second derivative is positive. This is
  82. // because when the second derivative is non-positive, we do not use
  83. // the second order correction suggested by BAMS and instead use a
  84. // simpler first order strategy which does not use a division by the
  85. // gradient of the loss function.
  86. CHECK_GT(rho[1], 0.0);
  87. // Calculate the smaller of the two solutions to the equation
  88. //
  89. // 0.5 * alpha^2 - alpha - rho'' / rho' * z'z = 0.
  90. //
  91. // Start by calculating the discriminant D.
  92. const double D = 1.0 + 2.0 * sq_norm * rho[2] / rho[1];
  93. // Since both rho[1] and rho[2] are guaranteed to be positive at
  94. // this point, we know that D > 1.0.
  95. const double alpha = 1.0 - sqrt(D);
  96. // Calculate the constants needed by the correction routines.
  97. residual_scaling_ = sqrt_rho1_ / (1 - alpha);
  98. alpha_sq_norm_ = alpha / sq_norm;
  99. }
  100. void Corrector::CorrectResiduals(const int num_rows, double* residuals) {
  101. DCHECK(residuals != nullptr);
  102. // Equation 11 in BAMS.
  103. VectorRef(residuals, num_rows) *= residual_scaling_;
  104. }
  105. void Corrector::CorrectJacobian(const int num_rows,
  106. const int num_cols,
  107. double* residuals,
  108. double* jacobian) {
  109. DCHECK(residuals != nullptr);
  110. DCHECK(jacobian != nullptr);
  111. // The common case (rho[2] <= 0).
  112. if (alpha_sq_norm_ == 0.0) {
  113. VectorRef(jacobian, num_rows * num_cols) *= sqrt_rho1_;
  114. return;
  115. }
  116. // Equation 11 in BAMS.
  117. //
  118. // J = sqrt(rho) * (J - alpha^2 r * r' J)
  119. //
  120. // In days gone by this loop used to be a single Eigen expression of
  121. // the form
  122. //
  123. // J = sqrt_rho1_ * (J - alpha_sq_norm_ * r* (r.transpose() * J));
  124. //
  125. // Which turns out to about 17x slower on bal problems. The reason
  126. // is that Eigen is unable to figure out that this expression can be
  127. // evaluated columnwise and ends up creating a temporary.
  128. for (int c = 0; c < num_cols; ++c) {
  129. double r_transpose_j = 0.0;
  130. for (int r = 0; r < num_rows; ++r) {
  131. r_transpose_j += jacobian[r * num_cols + c] * residuals[r];
  132. }
  133. for (int r = 0; r < num_rows; ++r) {
  134. jacobian[r * num_cols + c] =
  135. sqrt_rho1_ * (jacobian[r * num_cols + c] -
  136. alpha_sq_norm_ * residuals[r] * r_transpose_j);
  137. }
  138. }
  139. }
  140. } // namespace ceres::internal