Commit Graph

306 Commits

Author SHA1 Message Date
Sameer Agarwal 2145c10539 Improve ExecutionSummary
1. Replace two maps by 1.
2. Update number of calls and the time for the call at the cost
   of a single map lookup.
3. Add Solver::Summary::num_linear_solves.

Fixes https://github.com/ceres-solver/ceres-solver/issues/340

Change-Id: I71eb9be7fb363a8cb066591c4c1761f256c81677
2018-02-05 16:37:27 -08:00
Sameer Agarwal 0251359830 A bunch of minor changes.
1. Fix a typo in auto_diff_cost_function.h
2. Fix and update Solver::Summary::FullReport() text labels.
3. Add logging of the number of residual and jacobian evaluations
   to the full report. The GradientProblemSolver already does this.

Change-Id: I41059af5f0ebe0417accbbc30b0808a4b04b9edb
2018-02-05 15:51:08 -08:00
Sameer Agarwal 78d49f4682 Update EigenTypes to deal with 1 column matrices
Most matrices in Ceres are row-major. Eigen does now
allow statically sized row-major matrices with a single
column.

This CL changes the EigenTypes struct to change the definition
of typedefs to change to ColMajor if this is the case.

This will allow Schur specializations (2,1,6) for example.

Change-Id: I24e7b62d4e1cb5660541062336225bbc9c41c6bf
2018-01-31 04:57:54 -08:00
Sameer Agarwal 202dd9f3a1 Add GradientProblemSolver::Options::update_state_every_iteration
The non-linear least squares solver had the ability to update the
user's parameters every iteration. Now GradientProblemSolver can
do the same.

Also a few minor Sphinx markup related cleanup to the documentation
which were found in the process of updating Sphinx on my machine
and adding the docs for this feature.

This fixes https://github.com/ceres-solver/ceres-solver/issues/246

Change-Id: Ib6b90ac22be8bfb60b14f25ad52082ba371af164
2018-01-25 21:30:09 -08:00
Rodrigo Queiro 237eb17bdd Consistently use quotes to include Eigen
This makes these files match the convention in the rest of the codebase.

Change-Id: I3a0634274e0e604c575dd3c78fa87f485770e7ff
2018-01-12 18:25:16 +00:00
Janick Martinez Esturo c2da96082b Fix Eigen >= 3.3 compilation if EIGEN_DONT_VECTORIZE set
Would result in compilation error

include/ceres/jet.h:296:59: error: requested alignment is not a positive power of 2
   alignas(kAlignment) Eigen::Matrix<T, N, 1, kAlignOrNot> v;

because CERES_JET_ALIGN_BYTES = EIGEN_MAX_ALIGN_BYTES = 0 in that case.

Change-Id: I247653d3b7555e80891d3fb78676acfa302a9e1d
2018-01-12 17:50:01 +01:00
Alex Stewart e9009829b5 Fix use of unnamed type as template argument warnings on Clang.
- When compiling Ceres without C++11 but with glog, then the unnamed
  enum results in a compiler warning when
  google::GetReferenceableValue(.) is invoked with the unnamed enum as a
  template parameter (unnamed-type-template-args) which was not legal
  prior to C++11.

Change-Id: Iddbed05e2199865ab915f2c5817ce917bb0609b5
2017-12-01 10:12:54 +00:00
Keir Mierle cb497df619 Add link for CLA in docs; minor fixes
Also fixes a typo in cost_function.h

Change-Id: I45f08fd27fd2f8e5cb5b3f08e0d4f9a3e8822a15
2017-10-24 17:18:02 -07:00
Sameer Agarwal 030b41dd0e Improve compatibility with ceres::Solver
1. Default linear solver is Eigen::LDLT
2. Options::max_iterations -> Options::max_num_iterations
3. Options::error_threshold -> Options::cost_threshold
4. Options::relative_step_threshold -> Options::parameter_threshold
5. Options::initial_scale_factor -> Options::initial_trust_region_radius
6. The default values of the above parameters have been changed
   to match those in ceres::Solver::Options
7. Status::RUNNING has been removed
8. Update now returns a bool instead of a Status enum and
   the status handling has been included in the main loop.
9. Summary::gradient_norm has been changed to Summary::gradient_max_norm
   to match the convergence test
10. A member variable cost_ has been added which is computed by Update
11. The test for parameter_tolerance based convergence is made
    more robust near zero.
12. Use of double has been replaced by Scalar.
13. Minor clang-formatting

Change-Id: I3cb0e2fd0a0204476bb8718761dc740cdf5e42ce
2017-10-22 21:40:49 -07:00
Keir Mierle dc9bf012c4 Fix tiny solver build break
The solver code must rely on the vectors for
sizing, since not all cost functions will have
NumParameters() or NumResiduals().

Change-Id: Id254ce37507443910edb0064de7907d64558851e
2017-10-19 23:46:53 -07:00
Sameer Agarwal ba73ce120c Improve the convergence performance of TinySolver
1. Default constructor and initialization for Summary.
2. Add Jacobi scaling.
3. Add bounds on the lm diagonal
4. Use the diagonal of J'J as the regularizer instead of identity.
5. Update the computation of rho to match the change in regularization.

As a result of these changes, the performance of TinySolver is
now the same as ceres::Solver, solving 53 out of 54 problems.

Change-Id: Ie08c3389ac2e3964ffa04411734c06b65835358a
2017-10-19 13:52:31 -07:00
Keir Mierle b485002bb7 Replace template use of >>
Older compilers do not support >> to terminate
templates, only > >. Ceres supports old compilers.

Change-Id: I7e43dc9fdac06507b32dd0c9bf1a3bc2a544916b
2017-10-14 16:08:17 -07:00
Keir Mierle 40effe3b15 Tiny solver autodiff adapter
Change-Id: I29fe736d53b2be32a101ba128cf557726def9a00
2017-10-14 15:51:14 -07:00
Sameer Agarwal 8beedf5cf6 Add TinySolverCostFunctionAdapter
Change-Id: I1905044d09abe5c927cd7e2cda804cba516fd961
2017-10-14 15:12:01 -07:00
Sameer Agarwal cc0bd492bd A number of minor changes to TinySolver
1. Instead of Core/LU just include Eigen/Dense
2. Rename SolverParameters to Options and params to options.
3. Rename Results to Summary.
4. Summary::error_magnitude -> Summary::final_cost.
5. Add Summary::initial_cost.
6. Change definitions of Summary::initial_cost and Summary::final_cost
   to match those used by Ceres::Solver.

Change-Id: Id64b78398f47810ca25938a15423c514fc8c164d
2017-10-14 14:14:14 -07:00
Sameer Agarwal 4d88f50f6b Two changes to TinySolver
1. Change the ordering from NUM_PARAMETERS, NUM_RESIDUALS to
NUM_RESIDUALS, NUM_PARAMETERS in docs and in code.
2. TinySolver::solve -> TinySolver::Solve

Change-Id: I4dca87b971fd9168f1200b53c362669cffc82c1b
2017-10-11 16:09:40 -07:00
Keir Mierle 7928ca003f Initial commit of tiny solver
Tiny solver is targeted towards small dense least square
solves, where the overhead of calling normal Ceres is too
high. For example, when solving for inverse camera
distortion for every pixel location in a many-megapixel
image. Anecdotally, at one point in the past, tiny solver
was ~20x faster than Ceres for the problems it's intended
for. This is due to two key aspects:

  1. Memory is allocated up front: repeated solves incur no
     allocation overhead beyond a few scalars on the stack.
  2. The cost function is fully inlined into the solver
     loop, removing even the cost function call overhead.

Tiny solver originated many years ago as part of
libmv/Blender, where it is still used for distortion solving
today, but the time has come for it to migrate into Ceres.

This commit is just the initial import into Ceres.  Follow
up patches will add further cleanups, and add CostFunction
and Jet adapters to make it easier to call tiny solver
(though by using adapters, some performance advantages will
be lost).

Change-Id: I8079535cd41382b1e0ac0ca2fca141711c72b7f8
2017-10-10 14:32:19 -07:00
Sameer Agarwal 2a956099d4 Add missing Eigen traits to Jets
Add highest and lowest traits to the Jet implementation.

https://github.com/ceres-solver/ceres-solver/issues/310

Change-Id: I7c68fa8e2baa7742880d3faa21f366352e48aacf
2017-09-27 03:57:49 +00:00
Alex Stewart 5eae62348d Add compound with scalar operators for Jets.
Change-Id: Ie47771d8c9df22ddeb6b643ae8a751860d892d67
2017-07-06 19:10:34 +01:00
Sameer Agarwal a9977da3eb Fix and enhance GradientProblemSolver::Summary::FullReport
1. Fix a bug which was causing the cost and gradient evaluation
   time to not be reported.
2. Add the number of times cost and gradients are evaluated to
   the Summary object and to the output of FullReport.

Change-Id: Id0703cd2dafbf437f3e537fbdc30ae81d5f4f540
2017-06-28 06:26:04 -07:00
Sameer Agarwal 3f6d01f313 Add a missing CERES_EXPORT to GradientChecker
Change-Id: Ib6b21354191b7ef5a0781cb3e53488144aa3f7b6
2017-05-14 23:31:13 -07:00
Sameer Agarwal 39388bd2ff Address comments from William Rucklidge
Change-Id: I0c4f686b89798435741c55a5ca45cf000af842dc
2017-05-09 17:23:43 -07:00
Ricardo Sanchez-Saez 08d02d7177 Fix implicit precission loss warning on 64-bit archs
Change-Id: Ia9212be3678ea6c92e9aef0e3f5c14447c9d9f88
2017-04-22 16:25:18 -07:00
Sameer Agarwal 85d276c84d Remove future looking comments from types.h
Change-Id: I2c801980778e2575ef8eaa52eafbdb3724f1ae01
2017-04-18 07:46:06 -07:00
Sameer Agarwal 14d8297cf9 Refactor Covariance::Options::algorithm_type.
THIS IS AN API BREAKING CHANGE.

Decouple the algorithm from the sparse linear algebra
library being used to perform the computation.

Before this change

Covariance::AlgorithmType had values

DENSE_SVD
EIGEN_SPARSE_QR
SUITE_SPARSE_QR

This has been replaced by two enums now.

Covariance::Options::sparse_linear_algebra_library_type
which can take values EIGEN_SPARSE, SUITE_SPARSE or CX_SPARSE.
The last one is currently not supported.

And Covariance::Options::algorithm_type takes values

DENSE_SVD
SPARSE_QR

This sets the stage for future extensions of the covariance
computation algorithm.

Also as part of this change, the covariance computation chapter
has been made a top level chapter on its own instead of being
buried deep inside the Solving Non-linear Least Squares problem.

Change-Id: Ibfbf60902d8d17694d9ff585047a5a57d329ab22
2017-04-17 09:43:22 -07:00
Julien Pilet e5dceb3ab0 Fix Jet/Eigen compatibility for Eigen > 3.3
Honour the digits10 NumTraits member introduced in Eigen 3.3.

Change-Id: If20183332beef8335b7758cdb58859a43fc59970
2017-02-28 10:34:26 +01:00
Sameer Agarwal 5f87f35cec Changes from William Rucklidge
Change-Id: Ia5d724edef947870fe13050a71aa1cba878352a8
2017-02-27 11:09:08 -08:00
Sameer Agarwal 1cfec3c666 Improvements to Schur template specializations
1. Refactor the python code that generates the template specializations
   to remove code duplication.
2. Improved the logic for template specialization selection where
   Eigen::Dynamic now serves as a wildcard.
3. Added schur_templates.h/cc which allows querying the set of available
   template specializations without instantiating a linear solver.
4. Added Solver::Summary::schur_structre_given and
   Solver::Summary::schur_structure_used and expose them in
   Solver::Summary::FullReport for better performance debugging.
5. Updated the templates with newer dates and some minor comments cleanup
   which lead to the the template specializations to be re-generated.

Change-Id: Iaf3c6f714353597899916c300465da01f151c3de
2017-02-23 14:52:59 -08:00
Sameer Agarwal 62a70bc74c Lint cleanup from William Rucklidge
Change-Id: I11ebf9bdb09cfd465a32a61e0a9a045ab650deac
2017-02-15 11:01:27 -08:00
Alex Stewart c1ebbfc79b Require Eigen >= 3.3 to define ScalarBinaryOpTraits in Jet.
- ScalarBinaryOpTraits was introduced in Eigen 3.3, and thus breaks
  compilation of Ceres for Eigen versions < 3.3.
- Reported as issue #252 by AustinDeric:
  https://github.com/ceres-solver/ceres-solver/issues/252

Change-Id: Ie9b7dace17709c348b18b297f2d5306b054ce4b0
2017-02-13 16:14:40 +00:00
Chris Sweeney 9a195d91de Specify ScalarBinaryOpTraits for Jet types.
This commit extends the use of Jets with Eigen matrices and arrays
by enabling the use of binary operators (such as scalar addition,
multiplication, as well as matrix multiplication) when one Eigen
matrix/array is of type Jet and the other is a scalar type. This
should increase performance since Jet types have optimized
scalar-to-jet binary operations.

Change-Id: Ia756064845d845cefcf0abb16d366331d2824b52
2017-02-10 19:50:33 +00:00
Je Hyeong Hong ccfd7f4dbb CERES_EXPORT fix to compile Ceres as DLL
Below fixes are required to compile Ceres as a dynamic library using
MSVC.
1. Added CERES_EXPORT to EigenQuaternionParameterization.
2. Removed CERES_EXPORT from CubicInterpolator and BicubicInterpolator.

EigenQuaternionParameterization has its source embedded in ceres.dll 
whereas Cubic+BicubicInterpolators are defined in the header file and 
therefore do not require __declspec(dllimport).

Change-Id: Idd1f387890c36aab51a1a4177ea7c06933b96f39
2017-02-02 03:07:52 +00:00
Alex Stewart 9321b620ac Fix detection of deprecated Bessel function names on MSVC.
- MSVC deprecated the standard POSIX names for Bessel functions in
  favour of underscore prefixed versions.
- Previously we were checking for the presence of the newer underscore
  functions via a macro, which fails as the functions are not defined
  as macros.
- Now we check via a check_cxx_source_compiles() if the newer underscore
  prefixed versions exist when compiling on MSVC and use a new Ceres
  configuration #define to switch on their presence in jet.h

Change-Id: I430880bde2981d12f4d03dbc94d903b9842e887e
2017-01-27 22:31:53 +00:00
Sameer Agarwal 2dd907744d Ensure that partial evaluation of residuals triggers an error
ResidualBlock evaluation has logic to ensure that CostFunction
should always fill out the residual and jacobian arrays completely
by using a special value to pre-populate these arrays.

This works for CostFunctions with analytical Jacobians but not for
AutoDiffCostFunction and NumericDiffCostFunction Jacobians.

There is no way to fix this for NumericDiffCostFunctions without
introducing significant performance penalties but the residual
evaluation fails, which should be enough to catch such errors.

For AutoDiffCostFunction the way the Jets are default initialized
was sidestepping this check. So now, the Jet that is used to
capture the output residuals is now initialized with
kImpossibleValue, which will ensure that if the user forgets
to fill all output fields, it triggers an evaluation error.

This change required that ceres::internal::kImpossibleValue be moved
out of array_utils.h/cc to types.h.

Change-Id: I35bb0946cf0785a5d43c7b5459a2272848fb2a9b
2017-01-16 10:00:36 -08:00
Alex Stewart 3fa415bd0c Enable support for AVX instructions for Jets.
- Eigen versions < 3.3 only supported SIMD instructions that required
  16-byte alignment (SSE), whereas Eigen >= 3.3 also supports AVX
  instructions which require 32+-byte alignment.
- Previously we only ever requested 16-byte alignment for Jets (if >=
  C++11 was enabled) which resulted in Eigen assertions being triggered
  on some compilers as reported as Issue #251:
  https://github.com/ceres-solver/ceres-solver/issues/251.
- Now we use Eigen’s EIGEN_MAX_ALIGN_BYTES macro, defined in Eigen >=
  3.3 to specify the byte alignment for Jets when C++11 is enabled for
  Eigen >= 3.3.  For Eigen versions < 3.3, we maintain the previous
  behaviour of 16.

Change-Id: I749af7a70ae794e0c2a59301128db781e338422c
2017-01-05 19:12:45 +00:00
Alex Stewart 6519e78920 Fix use of alignas(0) which is not ignored on GCC.
- alignas(0) should be ignored, however it results in a build error on
  GCC, so instead default to the alignment of double in Jets if
  we cannot align to 16-byte boundaries on the platform, but are
  compiling with C++11.

Change-Id: I2e54c69516ea2e1447a8bdc138b2dd70050c6dad
2016-11-12 21:45:57 +00:00
Alex Stewart b4e27240b2 Use default alignment if alignof(std::max_align_t) < 16 with C++11.
- As per Andrew Hunter’s comments in the commit which added Jet
  alignment when using C++11 here:
  https://ceres-solver-review.googlesource.com/#/c/7100, there is wide
  lattitude in the standard about what the maximum supported alignment
  can be.
- Previously, we were forcing the alignment to 1, if the value of
  alignof(std::max_align_t), which we use as a proxy for the maximum
  supported alignment on the platform, was < 16.
- An alignment of 1 is not valid for Jets, as it would weaken the
  natural alignment of the types within a Jet, which would typically be
  4 (32-bit systems) or 8 (64-bit systems), thus resulting in a compiler
  error.
- This was reported as issue 235 for Clang 3.8 on i386:
  https://github.com/ceres-solver/ceres-solver/issues/235.

Change-Id: Ie39e5499c64f9231f29ebf4392992b5c9ce2e385
2016-11-12 17:18:25 +00:00
Sameer Agarwal 78abf0cdfe Introduce a common base class for DynamicAutoDiffCostFunction and DynamicNumericDiffCostFunction.
In the process also move some documentation from the file header to
just above the class declaration.

This change is in response to https://github.com/ceres-solver/ceres-solver/issues/233

Change-Id: I08cff1a94c57b67dd2bd8be4dba8c3fce46b68ab
2016-11-11 07:35:22 -08:00
Sameer Agarwal f21c17064b Add GradientProblemSolver::Options::parameter_tolerance.
Support for parameter tolerance was added to the line search
minimizer was added a while ago, and calling Solve on a
non-linear least squares problem supported it but for some reason
the GradientProblemSolver::Options struct was missing this
option even though the documentation suggested that it was present!

Thanks to Noah Snavely for reporting this bug.

Change-Id: I57cf4ab396bc822c19fa298529e113b89664a349
2016-11-01 12:56:19 +00:00
Rob Carroll e69bd2a206 Add missing T() wrappers for constants.
Needed for instantiation with Jet<float,N>.

Change-Id: I8ec07a844cc911a3c7f24c25797bd21c0bee07b5
2016-10-31 12:51:44 -07:00
Sameer Agarwal 8590e6e8e0 Remove two checks from rotation.h
This allows rotation.h to remove its dependency on glog.

Change-Id: Ia6aede93ee51a4bd4039570dc8edd100a7045329
2016-10-27 12:36:13 -07:00
Sameer Agarwal 195d8d13a6 Remove two DCHECKs from CubicHermiteSpline.
They were present as debugging checks but were causing problems
with the build on 32bit i386 due to numerical cancellation issues,
where x ~ -epsilon.

Removing these checks only changes the behaviour in Debug mode.
We are already handling such small negative numbers in production
if they occur. All that this change does is to remove the crash.

https://github.com/ceres-solver/ceres-solver/issues/212

Thanks to @NeroBurner and @debalance for reporting this.

Change-Id: I66480e86d4fa0a4b621204f2ff44cc3ff8d01c04
2016-09-06 07:15:20 -07:00
David Gossow 46c5ce89dd Fix compiler errors on some systems
This fixes some signed-unsigned comparisons and a missing header
include.

Change-Id: Ieb2bf6e905faa74851bc4ac4658d2f1da24b6ecc
2016-08-31 20:26:53 +00:00
Sameer Agarwal 79a28d1e49 Rename a confusingly named member of Solver::Options
Solver::Options::numeric_derivative_relative_step_size to
Solver::Options::gradient_check_numeric_derivative_relative_step_size

Change-Id: Ib89ae3f87e588d4aba2a75361770d2cec26f07aa
2016-08-31 07:47:29 -07:00
Sameer Agarwal 358ae741c8 Note that Problem::Evaluate cannot be called from an IterationCallback
Change-Id: Ieabdc2d40715e6b547ab22156ba32e9c8444b7ed
2016-08-31 07:42:00 -07:00
Sameer Agarwal 44044e25b1 Update the NumTraits for Jets
1. Use AVX if EIGEN_VECTORIZE_AVX is defined.
2. Make the cost of division same as the cost of multiplication.

These are updates to the original numtraits update needed for eigen 3.3
that Shaheen Gandhi sent out.

Change-Id: Ic1e3ed7d05a659c7badc79a894679b2dd61c51b9
2016-08-31 06:19:08 -07:00
Shaheen Gandhi bfc916cf1c Allow ceres to be used with the latest version of Eigen
Change-Id: Ief3b0f6b405484ec04ecd9ab6a1e1e5409a594c2
2016-08-31 12:45:47 +00:00
David Gossow ac3b8e8217 Gradient checking cleanup and local parameterization bugfix
Change the Ceres gradient checking API to make is useful for
unit testing, clean up code duplication and fix interaction between
gradient checking and local parameterizations.

There were two gradient checking implementations, one being used
when using the check_gradients flag in the Solver, the other
being a standalone class. The standalone version was restricted
to cost functions with fixed parameter sizes at compile time, which
is being lifted here. This enables it to be used inside the
GradientCheckingCostFunction as well.

In addition, this installs new hooks in the Solver to ensure
that Solve will fail if any incorrect gradients are detected. This
way, you can set the check_gradient flags to true and detect
errors in an automated way, instead of just printing error information
to the log. The error log is now also returned in the Solver summary
instead of being printed directly. The user can then decide what to
do with it. The existing hooks for user callbacks are used for
this purpose to keep the internal API changes minimal and non-invasive.

The last and biggest change is the way the the interaction between
local parameterizations and the gradient checker works. Before,
local parameterizations would be ignored by the checker. However,
if a cost function does not compute its Jacobian along the null
space of the local parameterization, this wil not have any effect
on the solver, but would result in a gradient checker error.
With this change, the Jacobians are multiplied by the Jacobians
of the respective local parameterization and thus being compared
in the tangent space only.

The typical use case for this are quaternion parameters, where
a cost function will typically assume that the quaternion is
always normalized, skipping the correct computation of the Jacobian
along the normal to save computation cost.

Change-Id: I5e1bb97b8a899436cea25101efe5011b0bb13282
2016-08-18 06:18:43 +00:00
Mike Vitus d4264ec10d Add a quaternion local parameterization for Eigen's quaternion element convention.
Change-Id: I7046e8b24805313c5fb6a767de581d0054fcdb83
2016-08-17 18:28:54 -07:00
Alex Stewart ff11d0e63d Use _j[0,1,n]() Bessel functions on MSVC to avoid deprecation errors.
- Microsoft deprecated the POSIX Bessel functions: j[0,1,n]() in favour
  of _j[0,1,n](), it appears since at least MSVC 2005:
  https://msdn.microsoft.com/en-us/library/ms235384(v=vs.100).aspx.
- As this occurs in jet.h (templated public header), although Ceres
  suppresses the warning when it itself is built (to suppress a warning
  about the insecurity of using std::copy), it will crop up again in
  client code (without this fix) unless it is explicitly suppressed
  there also.
- Raised as Issue #190:
  https://github.com/ceres-solver/ceres-solver/issues/190.

Change-Id: If7ac5dbb856748f9900be93ec0452a40c0b00524
2016-06-29 09:33:45 +01:00