Blame include/ceres/loss_function.h

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: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
//
Packit ea1746
// The LossFunction interface is the way users describe how residuals
Packit ea1746
// are converted to cost terms for the overall problem cost function.
Packit ea1746
// For the exact manner in which loss functions are converted to the
Packit ea1746
// overall cost for a problem, see problem.h.
Packit ea1746
//
Packit ea1746
// For least squares problem where there are no outliers and standard
Packit ea1746
// squared loss is expected, it is not necessary to create a loss
Packit ea1746
// function; instead passing a NULL to the problem when adding
Packit ea1746
// residuals implies a standard squared loss.
Packit ea1746
//
Packit ea1746
// For least squares problems where the minimization may encounter
Packit ea1746
// input terms that contain outliers, that is, completely bogus
Packit ea1746
// measurements, it is important to use a loss function that reduces
Packit ea1746
// their associated penalty.
Packit ea1746
//
Packit ea1746
// Consider a structure from motion problem. The unknowns are 3D
Packit ea1746
// points and camera parameters, and the measurements are image
Packit ea1746
// coordinates describing the expected reprojected position for a
Packit ea1746
// point in a camera. For example, we want to model the geometry of a
Packit ea1746
// street scene with fire hydrants and cars, observed by a moving
Packit ea1746
// camera with unknown parameters, and the only 3D points we care
Packit ea1746
// about are the pointy tippy-tops of the fire hydrants. Our magic
Packit ea1746
// image processing algorithm, which is responsible for producing the
Packit ea1746
// measurements that are input to Ceres, has found and matched all
Packit ea1746
// such tippy-tops in all image frames, except that in one of the
Packit ea1746
// frame it mistook a car's headlight for a hydrant. If we didn't do
Packit ea1746
// anything special (i.e. if we used a basic quadratic loss), the
Packit ea1746
// residual for the erroneous measurement will result in extreme error
Packit ea1746
// due to the quadratic nature of squared loss. This results in the
Packit ea1746
// entire solution getting pulled away from the optimimum to reduce
Packit ea1746
// the large error that would otherwise be attributed to the wrong
Packit ea1746
// measurement.
Packit ea1746
//
Packit ea1746
// Using a robust loss function, the cost for large residuals is
Packit ea1746
// reduced. In the example above, this leads to outlier terms getting
Packit ea1746
// downweighted so they do not overly influence the final solution.
Packit ea1746
//
Packit ea1746
// What cost function is best?
Packit ea1746
//
Packit ea1746
// In general, there isn't a principled way to select a robust loss
Packit ea1746
// function. The authors suggest starting with a non-robust cost, then
Packit ea1746
// only experimenting with robust loss functions if standard squared
Packit ea1746
// loss doesn't work.
Packit ea1746
Packit ea1746
#ifndef CERES_PUBLIC_LOSS_FUNCTION_H_
Packit ea1746
#define CERES_PUBLIC_LOSS_FUNCTION_H_
Packit ea1746
Packit ea1746
#include "glog/logging.h"
Packit ea1746
#include "ceres/internal/macros.h"
Packit ea1746
#include "ceres/internal/scoped_ptr.h"
Packit ea1746
#include "ceres/types.h"
Packit ea1746
#include "ceres/internal/disable_warnings.h"
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
class CERES_EXPORT LossFunction {
Packit ea1746
 public:
Packit ea1746
  virtual ~LossFunction() {}
Packit ea1746
Packit ea1746
  // For a residual vector with squared 2-norm 'sq_norm', this method
Packit ea1746
  // is required to fill in the value and derivatives of the loss
Packit ea1746
  // function (rho in this example):
Packit ea1746
  //
Packit ea1746
  //   out[0] = rho(sq_norm),
Packit ea1746
  //   out[1] = rho'(sq_norm),
Packit ea1746
  //   out[2] = rho''(sq_norm),
Packit ea1746
  //
Packit ea1746
  // Here the convention is that the contribution of a term to the
Packit ea1746
  // cost function is given by 1/2 rho(s),  where
Packit ea1746
  //
Packit ea1746
  //   s = ||residuals||^2.
Packit ea1746
  //
Packit ea1746
  // Calling the method with a negative value of 's' is an error and
Packit ea1746
  // the implementations are not required to handle that case.
Packit ea1746
  //
Packit ea1746
  // Most sane choices of rho() satisfy:
Packit ea1746
  //
Packit ea1746
  //   rho(0) = 0,
Packit ea1746
  //   rho'(0) = 1,
Packit ea1746
  //   rho'(s) < 1 in outlier region,
Packit ea1746
  //   rho''(s) < 0 in outlier region,
Packit ea1746
  //
Packit ea1746
  // so that they mimic the least squares cost for small residuals.
Packit ea1746
  virtual void Evaluate(double sq_norm, double out[3]) const = 0;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Some common implementations follow below.
Packit ea1746
//
Packit ea1746
// Note: in the region of interest (i.e. s < 3) we have:
Packit ea1746
//   TrivialLoss >= HuberLoss >= SoftLOneLoss >= CauchyLoss
Packit ea1746
Packit ea1746
Packit ea1746
// This corresponds to no robustification.
Packit ea1746
//
Packit ea1746
//   rho(s) = s
Packit ea1746
//
Packit ea1746
// At s = 0: rho = [0, 1, 0].
Packit ea1746
//
Packit ea1746
// It is not normally necessary to use this, as passing NULL for the
Packit ea1746
// loss function when building the problem accomplishes the same
Packit ea1746
// thing.
Packit ea1746
class CERES_EXPORT TrivialLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Scaling
Packit ea1746
// -------
Packit ea1746
// Given one robustifier
Packit ea1746
//   s -> rho(s)
Packit ea1746
// one can change the length scale at which robustification takes
Packit ea1746
// place, by adding a scale factor 'a' as follows:
Packit ea1746
//
Packit ea1746
//   s -> a^2 rho(s / a^2).
Packit ea1746
//
Packit ea1746
// The first and second derivatives are:
Packit ea1746
//
Packit ea1746
//   s -> rho'(s / a^2),
Packit ea1746
//   s -> (1 / a^2) rho''(s / a^2),
Packit ea1746
//
Packit ea1746
// but the behaviour near s = 0 is the same as the original function,
Packit ea1746
// i.e.
Packit ea1746
//
Packit ea1746
//   rho(s) = s + higher order terms,
Packit ea1746
//   a^2 rho(s / a^2) = s + higher order terms.
Packit ea1746
//
Packit ea1746
// The scalar 'a' should be positive.
Packit ea1746
//
Packit ea1746
// The reason for the appearance of squaring is that 'a' is in the
Packit ea1746
// units of the residual vector norm whereas 's' is a squared
Packit ea1746
// norm. For applications it is more convenient to specify 'a' than
Packit ea1746
// its square. The commonly used robustifiers below are described in
Packit ea1746
// un-scaled format (a = 1) but their implementations work for any
Packit ea1746
// non-zero value of 'a'.
Packit ea1746
Packit ea1746
// Huber.
Packit ea1746
//
Packit ea1746
//   rho(s) = s               for s <= 1,
Packit ea1746
//   rho(s) = 2 sqrt(s) - 1   for s >= 1.
Packit ea1746
//
Packit ea1746
// At s = 0: rho = [0, 1, 0].
Packit ea1746
//
Packit ea1746
// The scaling parameter 'a' corresponds to 'delta' on this page:
Packit ea1746
//   http://en.wikipedia.org/wiki/Huber_Loss_Function
Packit ea1746
class CERES_EXPORT HuberLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit HuberLoss(double a) : a_(a), b_(a * a) { }
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const double a_;
Packit ea1746
  // b = a^2.
Packit ea1746
  const double b_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Soft L1, similar to Huber but smooth.
Packit ea1746
//
Packit ea1746
//   rho(s) = 2 (sqrt(1 + s) - 1).
Packit ea1746
//
Packit ea1746
// At s = 0: rho = [0, 1, -1/2].
Packit ea1746
class CERES_EXPORT SoftLOneLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit SoftLOneLoss(double a) : b_(a * a), c_(1 / b_) { }
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  // b = a^2.
Packit ea1746
  const double b_;
Packit ea1746
  // c = 1 / a^2.
Packit ea1746
  const double c_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Inspired by the Cauchy distribution
Packit ea1746
//
Packit ea1746
//   rho(s) = log(1 + s).
Packit ea1746
//
Packit ea1746
// At s = 0: rho = [0, 1, -1].
Packit ea1746
class CERES_EXPORT CauchyLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit CauchyLoss(double a) : b_(a * a), c_(1 / b_) { }
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  // b = a^2.
Packit ea1746
  const double b_;
Packit ea1746
  // c = 1 / a^2.
Packit ea1746
  const double c_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Loss that is capped beyond a certain level using the arc-tangent function.
Packit ea1746
// The scaling parameter 'a' determines the level where falloff occurs.
Packit ea1746
// For costs much smaller than 'a', the loss function is linear and behaves like
Packit ea1746
// TrivialLoss, and for values much larger than 'a' the value asymptotically
Packit ea1746
// approaches the constant value of a * PI / 2.
Packit ea1746
//
Packit ea1746
//   rho(s) = a atan(s / a).
Packit ea1746
//
Packit ea1746
// At s = 0: rho = [0, 1, 0].
Packit ea1746
class CERES_EXPORT ArctanLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit ArctanLoss(double a) : a_(a), b_(1 / (a * a)) { }
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const double a_;
Packit ea1746
  // b = 1 / a^2.
Packit ea1746
  const double b_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Loss function that maps to approximately zero cost in a range around the
Packit ea1746
// origin, and reverts to linear in error (quadratic in cost) beyond this range.
Packit ea1746
// The tolerance parameter 'a' sets the nominal point at which the
Packit ea1746
// transition occurs, and the transition size parameter 'b' sets the nominal
Packit ea1746
// distance over which most of the transition occurs. Both a and b must be
Packit ea1746
// greater than zero, and typically b will be set to a fraction of a.
Packit ea1746
// The slope rho'[s] varies smoothly from about 0 at s <= a - b to
Packit ea1746
// about 1 at s >= a + b.
Packit ea1746
//
Packit ea1746
// The term is computed as:
Packit ea1746
//
Packit ea1746
//   rho(s) = b log(1 + exp((s - a) / b)) - c0.
Packit ea1746
//
Packit ea1746
// where c0 is chosen so that rho(0) == 0
Packit ea1746
//
Packit ea1746
//   c0 = b log(1 + exp(-a / b)
Packit ea1746
//
Packit ea1746
// This has the following useful properties:
Packit ea1746
//
Packit ea1746
//   rho(s) == 0               for s = 0
Packit ea1746
//   rho'(s) ~= 0              for s << a - b
Packit ea1746
//   rho'(s) ~= 1              for s >> a + b
Packit ea1746
//   rho''(s) > 0              for all s
Packit ea1746
//
Packit ea1746
// In addition, all derivatives are continuous, and the curvature is
Packit ea1746
// concentrated in the range a - b to a + b.
Packit ea1746
//
Packit ea1746
// At s = 0: rho = [0, ~0, ~0].
Packit ea1746
class CERES_EXPORT TolerantLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit TolerantLoss(double a, double b);
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const double a_, b_, c_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// This is the Tukey biweight loss function which aggressively
Packit ea1746
// attempts to suppress large errors.
Packit ea1746
//
Packit ea1746
// The term is computed as:
Packit ea1746
//
Packit ea1746
//   rho(s) = a^2 / 6 * (1 - (1 - s / a^2)^3 )   for s <= a^2,
Packit ea1746
//   rho(s) = a^2 / 6                            for s >  a^2.
Packit ea1746
//
Packit ea1746
// At s = 0: rho = [0, 0.5, -1 / a^2]
Packit ea1746
class CERES_EXPORT TukeyLoss : public ceres::LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit TukeyLoss(double a) : a_squared_(a * a) { }
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  const double a_squared_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// Composition of two loss functions.  The error is the result of first
Packit ea1746
// evaluating g followed by f to yield the composition f(g(s)).
Packit ea1746
// The loss functions must not be NULL.
Packit ea1746
class CERES_EXPORT ComposedLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  explicit ComposedLoss(const LossFunction* f, Ownership ownership_f,
Packit ea1746
                        const LossFunction* g, Ownership ownership_g);
Packit ea1746
  virtual ~ComposedLoss();
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  internal::scoped_ptr<const LossFunction> f_, g_;
Packit ea1746
  const Ownership ownership_f_, ownership_g_;
Packit ea1746
};
Packit ea1746
Packit ea1746
// The discussion above has to do with length scaling: it affects the space
Packit ea1746
// in which s is measured. Sometimes you want to simply scale the output
Packit ea1746
// value of the robustifier. For example, you might want to weight
Packit ea1746
// different error terms differently (e.g., weight pixel reprojection
Packit ea1746
// errors differently from terrain errors).
Packit ea1746
//
Packit ea1746
// If rho is the wrapped robustifier, then this simply outputs
Packit ea1746
// s -> a * rho(s)
Packit ea1746
//
Packit ea1746
// The first and second derivatives are, not surprisingly
Packit ea1746
// s -> a * rho'(s)
Packit ea1746
// s -> a * rho''(s)
Packit ea1746
//
Packit ea1746
// Since we treat the a NULL Loss function as the Identity loss
Packit ea1746
// function, rho = NULL is a valid input and will result in the input
Packit ea1746
// being scaled by a. This provides a simple way of implementing a
Packit ea1746
// scaled ResidualBlock.
Packit ea1746
class CERES_EXPORT ScaledLoss : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  // Constructs a ScaledLoss wrapping another loss function. Takes
Packit ea1746
  // ownership of the wrapped loss function or not depending on the
Packit ea1746
  // ownership parameter.
Packit ea1746
  ScaledLoss(const LossFunction* rho, double a, Ownership ownership) :
Packit ea1746
      rho_(rho), a_(a), ownership_(ownership) { }
Packit ea1746
Packit ea1746
  virtual ~ScaledLoss() {
Packit ea1746
    if (ownership_ == DO_NOT_TAKE_OWNERSHIP) {
Packit ea1746
      rho_.release();
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
  virtual void Evaluate(double, double*) const;
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  internal::scoped_ptr<const LossFunction> rho_;
Packit ea1746
  const double a_;
Packit ea1746
  const Ownership ownership_;
Packit ea1746
  CERES_DISALLOW_COPY_AND_ASSIGN(ScaledLoss);
Packit ea1746
};
Packit ea1746
Packit ea1746
// Sometimes after the optimization problem has been constructed, we
Packit ea1746
// wish to mutate the scale of the loss function. For example, when
Packit ea1746
// performing estimation from data which has substantial outliers,
Packit ea1746
// convergence can be improved by starting out with a large scale,
Packit ea1746
// optimizing the problem and then reducing the scale. This can have
Packit ea1746
// better convergence behaviour than just using a loss function with a
Packit ea1746
// small scale.
Packit ea1746
//
Packit ea1746
// This templated class allows the user to implement a loss function
Packit ea1746
// whose scale can be mutated after an optimization problem has been
Packit ea1746
// constructed.
Packit ea1746
//
Packit ea1746
// Since we treat the a NULL Loss function as the Identity loss
Packit ea1746
// function, rho = NULL is a valid input.
Packit ea1746
//
Packit ea1746
// Example usage
Packit ea1746
//
Packit ea1746
//  Problem problem;
Packit ea1746
//
Packit ea1746
//  // Add parameter blocks
Packit ea1746
//
Packit ea1746
//  CostFunction* cost_function =
Packit ea1746
//    new AutoDiffCostFunction < UW_Camera_Mapper, 2, 9, 3>(
Packit ea1746
//      new UW_Camera_Mapper(feature_x, feature_y));
Packit ea1746
//
Packit ea1746
//  LossFunctionWrapper* loss_function(new HuberLoss(1.0), TAKE_OWNERSHIP);
Packit ea1746
//
Packit ea1746
//  problem.AddResidualBlock(cost_function, loss_function, parameters);
Packit ea1746
//
Packit ea1746
//  Solver::Options options;
Packit ea1746
//  Solger::Summary summary;
Packit ea1746
//
Packit ea1746
//  Solve(options, &problem, &summary)
Packit ea1746
//
Packit ea1746
//  loss_function->Reset(new HuberLoss(1.0), TAKE_OWNERSHIP);
Packit ea1746
//
Packit ea1746
//  Solve(options, &problem, &summary)
Packit ea1746
//
Packit ea1746
class CERES_EXPORT LossFunctionWrapper : public LossFunction {
Packit ea1746
 public:
Packit ea1746
  LossFunctionWrapper(LossFunction* rho, Ownership ownership)
Packit ea1746
      : rho_(rho), ownership_(ownership) {
Packit ea1746
  }
Packit ea1746
Packit ea1746
  virtual ~LossFunctionWrapper() {
Packit ea1746
    if (ownership_ == DO_NOT_TAKE_OWNERSHIP) {
Packit ea1746
      rho_.release();
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  virtual void Evaluate(double sq_norm, double out[3]) const {
Packit ea1746
    if (rho_.get() == NULL) {
Packit ea1746
      out[0] = sq_norm;
Packit ea1746
      out[1] = 1.0;
Packit ea1746
      out[2] = 0.0;
Packit ea1746
    }
Packit ea1746
    else {
Packit ea1746
      rho_->Evaluate(sq_norm, out);
Packit ea1746
    }
Packit ea1746
  }
Packit ea1746
Packit ea1746
  void Reset(LossFunction* rho, Ownership ownership) {
Packit ea1746
    if (ownership_ == DO_NOT_TAKE_OWNERSHIP) {
Packit ea1746
      rho_.release();
Packit ea1746
    }
Packit ea1746
    rho_.reset(rho);
Packit ea1746
    ownership_ = ownership;
Packit ea1746
  }
Packit ea1746
Packit ea1746
 private:
Packit ea1746
  internal::scoped_ptr<const LossFunction> rho_;
Packit ea1746
  Ownership ownership_;
Packit ea1746
  CERES_DISALLOW_COPY_AND_ASSIGN(LossFunctionWrapper);
Packit ea1746
};
Packit ea1746
Packit ea1746
}  // namespace ceres
Packit ea1746
Packit ea1746
#include "ceres/internal/reenable_warnings.h"
Packit ea1746
Packit ea1746
#endif  // CERES_PUBLIC_LOSS_FUNCTION_H_