loss_function.cc 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. // Ceres Solver - A fast non-linear least squares minimizer
  2. // Copyright 2023 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. //
  31. // Purpose: See .h file.
  32. #include "ceres/loss_function.h"
  33. #include <algorithm>
  34. #include <cmath>
  35. #include <cstddef>
  36. #include <limits>
  37. namespace ceres {
  38. LossFunction::~LossFunction() = default;
  39. void TrivialLoss::Evaluate(double s, double rho[3]) const {
  40. rho[0] = s;
  41. rho[1] = 1.0;
  42. rho[2] = 0.0;
  43. }
  44. void HuberLoss::Evaluate(double s, double rho[3]) const {
  45. if (s > b_) {
  46. // Outlier region.
  47. // 'r' is always positive.
  48. const double r = sqrt(s);
  49. rho[0] = 2.0 * a_ * r - b_;
  50. rho[1] = std::max(std::numeric_limits<double>::min(), a_ / r);
  51. rho[2] = -rho[1] / (2.0 * s);
  52. } else {
  53. // Inlier region.
  54. rho[0] = s;
  55. rho[1] = 1.0;
  56. rho[2] = 0.0;
  57. }
  58. }
  59. void SoftLOneLoss::Evaluate(double s, double rho[3]) const {
  60. const double sum = 1.0 + s * c_;
  61. const double tmp = sqrt(sum);
  62. // 'sum' and 'tmp' are always positive, assuming that 's' is.
  63. rho[0] = 2.0 * b_ * (tmp - 1.0);
  64. rho[1] = std::max(std::numeric_limits<double>::min(), 1.0 / tmp);
  65. rho[2] = -(c_ * rho[1]) / (2.0 * sum);
  66. }
  67. void CauchyLoss::Evaluate(double s, double rho[3]) const {
  68. const double sum = 1.0 + s * c_;
  69. const double inv = 1.0 / sum;
  70. // 'sum' and 'inv' are always positive, assuming that 's' is.
  71. rho[0] = b_ * log(sum);
  72. rho[1] = std::max(std::numeric_limits<double>::min(), inv);
  73. rho[2] = -c_ * (inv * inv);
  74. }
  75. void ArctanLoss::Evaluate(double s, double rho[3]) const {
  76. const double sum = 1 + s * s * b_;
  77. const double inv = 1 / sum;
  78. // 'sum' and 'inv' are always positive.
  79. rho[0] = a_ * atan2(s, a_);
  80. rho[1] = std::max(std::numeric_limits<double>::min(), inv);
  81. rho[2] = -2.0 * s * b_ * (inv * inv);
  82. }
  83. TolerantLoss::TolerantLoss(double a, double b)
  84. : a_(a), b_(b), c_(b * log(1.0 + exp(-a / b))) {
  85. CHECK_GE(a, 0.0);
  86. CHECK_GT(b, 0.0);
  87. }
  88. void TolerantLoss::Evaluate(double s, double rho[3]) const {
  89. const double x = (s - a_) / b_;
  90. // The basic equation is rho[0] = b ln(1 + e^x). However, if e^x is too
  91. // large, it will overflow. Since numerically 1 + e^x == e^x when the
  92. // x is greater than about ln(2^53) for doubles, beyond this threshold
  93. // we substitute x for ln(1 + e^x) as a numerically equivalent approximation.
  94. // ln(MathLimits<double>::kEpsilon).
  95. static constexpr double kLog2Pow53 = 36.7;
  96. if (x > kLog2Pow53) {
  97. rho[0] = s - a_ - c_;
  98. rho[1] = 1.0;
  99. rho[2] = 0.0;
  100. } else {
  101. const double e_x = exp(x);
  102. rho[0] = b_ * log(1.0 + e_x) - c_;
  103. rho[1] = std::max(std::numeric_limits<double>::min(), e_x / (1.0 + e_x));
  104. rho[2] = 0.5 / (b_ * (1.0 + cosh(x)));
  105. }
  106. }
  107. void TukeyLoss::Evaluate(double s, double* rho) const {
  108. if (s <= a_squared_) {
  109. // Inlier region.
  110. const double value = 1.0 - s / a_squared_;
  111. const double value_sq = value * value;
  112. rho[0] = a_squared_ / 3.0 * (1.0 - value_sq * value);
  113. rho[1] = value_sq;
  114. rho[2] = -2.0 / a_squared_ * value;
  115. } else {
  116. // Outlier region.
  117. rho[0] = a_squared_ / 3.0;
  118. rho[1] = 0.0;
  119. rho[2] = 0.0;
  120. }
  121. }
  122. ComposedLoss::ComposedLoss(const LossFunction* f,
  123. Ownership ownership_f,
  124. const LossFunction* g,
  125. Ownership ownership_g)
  126. : f_(f), g_(g), ownership_f_(ownership_f), ownership_g_(ownership_g) {
  127. CHECK(f_ != nullptr);
  128. CHECK(g_ != nullptr);
  129. }
  130. ComposedLoss::~ComposedLoss() {
  131. if (ownership_f_ == DO_NOT_TAKE_OWNERSHIP) {
  132. f_.release();
  133. }
  134. if (ownership_g_ == DO_NOT_TAKE_OWNERSHIP) {
  135. g_.release();
  136. }
  137. }
  138. void ComposedLoss::Evaluate(double s, double rho[3]) const {
  139. double rho_f[3], rho_g[3];
  140. g_->Evaluate(s, rho_g);
  141. f_->Evaluate(rho_g[0], rho_f);
  142. rho[0] = rho_f[0];
  143. // f'(g(s)) * g'(s).
  144. rho[1] = rho_f[1] * rho_g[1];
  145. // f''(g(s)) * g'(s) * g'(s) + f'(g(s)) * g''(s).
  146. rho[2] = rho_f[2] * rho_g[1] * rho_g[1] + rho_f[1] * rho_g[2];
  147. }
  148. void ScaledLoss::Evaluate(double s, double rho[3]) const {
  149. if (rho_.get() == nullptr) {
  150. rho[0] = a_ * s;
  151. rho[1] = a_;
  152. rho[2] = 0.0;
  153. } else {
  154. rho_->Evaluate(s, rho);
  155. rho[0] *= a_;
  156. rho[1] *= a_;
  157. rho[2] *= a_;
  158. }
  159. }
  160. } // namespace ceres