blob: eaeb2d8b54b2eba02301c1d50115e41ca9f4b0fe [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
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#include "ceres/loss_function.h"
32
33#include <cstddef>
34
35#include <glog/logging.h>
36#include "gtest/gtest.h"
37
38namespace ceres {
39namespace internal {
40namespace {
41
42// Helper function for testing a LossFunction callback.
43//
44// Compares the values of rho'(s) and rho''(s) computed by the
45// callback with estimates obtained by symmetric finite differencing
46// of rho(s).
47void AssertLossFunctionIsValid(const LossFunction& loss, double s) {
48 CHECK_GT(s, 0);
49
50 // Evaluate rho(s), rho'(s) and rho''(s).
51 double rho[3];
52 loss.Evaluate(s, rho);
53
54 // Use symmetric finite differencing to estimate rho'(s) and
55 // rho''(s).
56 const double kH = 1e-4;
57 // Values at s + kH.
58 double fwd[3];
59 // Values at s - kH.
60 double bwd[3];
61 loss.Evaluate(s + kH, fwd);
62 loss.Evaluate(s - kH, bwd);
63
64 // First derivative.
65 const double fd_1 = (fwd[0] - bwd[0]) / (2 * kH);
66 ASSERT_NEAR(fd_1, rho[1], 1e-6);
67
68 // Second derivative.
69 const double fd_2 = (fwd[0] - 2*rho[0] + bwd[0]) / (kH * kH);
70 ASSERT_NEAR(fd_2, rho[2], 1e-6);
71}
72} // namespace
73
74// Try two values of the scaling a = 0.7 and 1.3
75// (where scaling makes sense) and of the squared norm
76// s = 0.357 and 1.792
77//
78// Note that for the Huber loss the test exercises both code paths
79// (i.e. both small and large values of s).
80
81TEST(LossFunction, TrivialLoss) {
82 AssertLossFunctionIsValid(TrivialLoss(), 0.357);
83 AssertLossFunctionIsValid(TrivialLoss(), 1.792);
84}
85
86TEST(LossFunction, HuberLoss) {
87 AssertLossFunctionIsValid(HuberLoss(0.7), 0.357);
88 AssertLossFunctionIsValid(HuberLoss(0.7), 1.792);
89 AssertLossFunctionIsValid(HuberLoss(1.3), 0.357);
90 AssertLossFunctionIsValid(HuberLoss(1.3), 1.792);
91}
92
93TEST(LossFunction, SoftLOneLoss) {
94 AssertLossFunctionIsValid(SoftLOneLoss(0.7), 0.357);
95 AssertLossFunctionIsValid(SoftLOneLoss(0.7), 1.792);
96 AssertLossFunctionIsValid(SoftLOneLoss(1.3), 0.357);
97 AssertLossFunctionIsValid(SoftLOneLoss(1.3), 1.792);
98}
99
100TEST(LossFunction, CauchyLoss) {
101 AssertLossFunctionIsValid(CauchyLoss(0.7), 0.357);
102 AssertLossFunctionIsValid(CauchyLoss(0.7), 1.792);
103 AssertLossFunctionIsValid(CauchyLoss(1.3), 0.357);
104 AssertLossFunctionIsValid(CauchyLoss(1.3), 1.792);
105}
106
107TEST(LossFunction, ScaledLoss) {
108 // Wrap a few loss functions, and a few scale factors. This can't combine
109 // construction with the call to AssertLossFunctionIsValid() because Apple's
110 // GCC is unable to eliminate the copy of ScaledLoss, which is not copyable.
111 {
112 ScaledLoss scaled_loss(NULL, 6, TAKE_OWNERSHIP);
113 AssertLossFunctionIsValid(scaled_loss, 0.323);
114 }
115 {
116 ScaledLoss scaled_loss(new TrivialLoss(), 10, TAKE_OWNERSHIP);
117 AssertLossFunctionIsValid(scaled_loss, 0.357);
118 }
119 {
120 ScaledLoss scaled_loss(new HuberLoss(0.7), 0.1, TAKE_OWNERSHIP);
121 AssertLossFunctionIsValid(scaled_loss, 1.792);
122 }
123 {
124 ScaledLoss scaled_loss(new SoftLOneLoss(1.3), 0.1, TAKE_OWNERSHIP);
125 AssertLossFunctionIsValid(scaled_loss, 1.792);
126 }
127 {
128 ScaledLoss scaled_loss(new CauchyLoss(1.3), 10, TAKE_OWNERSHIP);
129 AssertLossFunctionIsValid(scaled_loss, 1.792);
130 }
131}
132
133TEST(LossFunction, LossFunctionWrapper) {
134 // Initialization
135 HuberLoss loss_function1(1.0);
136 LossFunctionWrapper loss_function_wrapper(new HuberLoss(1.0),
137 TAKE_OWNERSHIP);
138
139 double s = 0.862;
140 double rho_gold[3];
141 double rho[3];
142 loss_function1.Evaluate(s, rho_gold);
143 loss_function_wrapper.Evaluate(s, rho);
144 for (int i = 0; i < 3; ++i) {
145 EXPECT_NEAR(rho[i], rho_gold[i], 1e-12);
146 }
147
148 // Resetting
149 HuberLoss loss_function2(0.5);
150 loss_function_wrapper.Reset(new HuberLoss(0.5), TAKE_OWNERSHIP);
151 loss_function_wrapper.Evaluate(s, rho);
152 loss_function2.Evaluate(s, rho_gold);
153 for (int i = 0; i < 3; ++i) {
154 EXPECT_NEAR(rho[i], rho_gold[i], 1e-12);
155 }
156
157 // Not taking ownership.
158 HuberLoss loss_function3(0.3);
159 loss_function_wrapper.Reset(&loss_function3, DO_NOT_TAKE_OWNERSHIP);
160 loss_function_wrapper.Evaluate(s, rho);
161 loss_function3.Evaluate(s, rho_gold);
162 for (int i = 0; i < 3; ++i) {
163 EXPECT_NEAR(rho[i], rho_gold[i], 1e-12);
164 }
165}
166
167} // namespace internal
168} // namespace ceres