介绍

Ceres可以解决下列形式的边界约束鲁棒非线性最小二乘问题

(1)

$\min\limits_{x}\quad \frac{1}{2} \sum\limits_{i}\rho_{i}\left( \left\| f_{i}\left( x_{i1},\ldots,x_{ik}\right)\right\|^{2} \right)$

$s.t. \quad l_{j} \leqslant x_{j} \leqslant u_{j}$

这种形式的问题广泛地出现在科学工程领域——从拟合统计曲线到计算机视觉中通过图像重建3D模型。

我们将通过Ceres求解器解决问题(1),本章给所有示例都提供了完整可行的代码。

表达式$\rho_{i}\left( \left\| f_{i}\left( x_{i1},\ldots,x_{ik}\right)\right\|^{2} \right)$作为ResidualBlock,其中$f_{i}\left(\cdot\right)$是取决于参数块$\left[ x_{i1},\ldots,x_{ik} \right]$的CostFunction。在大多数优化问题中,小群体的标量一起出现。比如,平移向量的三个组成部分和定义相机姿态四元数的四个分量。我们将这样一组小标量称为ParameterBlock。当然ParameterBlock也可以只有一个参数。$l_{j}$和$u_{j}$是参数块$x_{j}$的边界。

$\rho_{i}$是LossFunction,LossFunction是一个标量函数,用于减少异常值对非线性最小二乘解的影响。

一个特殊情况,当$\rho_{i}\left( x \right)=x$,i.e.恒等函数,并且$l_{j}=-\infty$和$u_{j}=\infty$,我们得到了一更熟悉的非线性最小二乘问题。

(2)

$\frac{1}{2} \sum\limits_{i} \left\| f_{i}\left( x_{i1},\ldots,x_{ik}\right)\right\|^{2}$

Hello World!

首先,考虑函数最小值的问题

$\frac{1}{2}\left(10-x\right)^{2}$

这是一个很简单的问题,其最小值为x=10,但是它非常适合用来解释如何通过Ceres解决问题。

第一步是编写一个函数来评估函数$f\left(x\right)=10-x$;

struct CostFunctor {
template <typename T>
bool operator()(const T* const x, T* residual) const {
residual[] = T(10.0) - x[];
return true;
}
};

这里需要注意的一点是operator()是一个模板方法,它假定所有的输入和输出都是T类型的。此处使用模板允许调用CostFunction::operator<T>()。当只有残差被用到时T=double,当用到雅可比时T=Jet

一旦我们有了计算残差函数的方法,就可以用它构造一个非线性最小二乘问题,并让Ceres解决它。

int main(int argc, char** argv) {
google::InitGoogleLogging(argv[]); // The variable to solve for with its initial value.
double initial_x = 5.0;
double x = initial_x; // Build the problem.
Problem problem; // Set up the only cost function (also known as residual). This uses
// auto-differentiation to obtain the derivative (jacobian).
CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, , >(new CostFunctor);//CostFunctor结构
problem.AddResidualBlock(cost_function, NULL, &x); // Run the solver!
Solver::Options options;
options.linear_solver_type = ceres::DENSE_QR;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary); std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n";
return ;
}

AutoDiffCostFunctionCostFunction作为输入,并且自动求导,并为其提供CostFunction接口。

编译运行examples/helloworld.cc(在下载的Ceres的examples文件夹下)有以下结果

iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
4.512500e+01 0.00e+00 9.50e+00 0.00e+00 0.00e+00 1.00e+04 5.33e-04 3.46e-03
4.511598e-07 4.51e+01 9.50e-04 9.50e+00 1.00e+00 3.00e+04 5.00e-04 4.05e-03
5.012552e-16 4.51e-07 3.17e-08 9.50e-04 1.00e+00 9.00e+04 1.60e-05 4.09e-03
Ceres Solver Report: Iterations: , Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
x : 5.0 ->
 
一开始X =5,两次迭代中的求解器将得到了10 。仔细的读者会注意到这是一个线性问题,一个线性的解决方案应该足以获得最优值。而求解器的默认配置是针对非线性问题的,为了简单起见,我们在本例中没有改变它。 确实有可能在一次迭代中就可以使用Ceres来解决这个问题。 还要注意的时,求解器在第一次迭代中确实已经得到非常接近0的最优函数值。 当我们谈论Ceres的收敛和参数设置时,我们将更详细地讨论这些问题。
 
求导
像大多数用于优化的软件包一样,Ceres求解器依赖于能够在任意参数下评估目标函数中每个项的值和导数。 正确而有效地做到这一点对于取得好的结果至关重要。 Ceres Solver提供了许多方法。 
我们现在考虑其他两种可能性。 也就是解析求导和数值求导。
数值求导
在某些情况下,无法定义模板损失函数,例如,当残差评估涉及对您无法控制的库函数的调用时。 在这种情况下,可以使用数值区分。 用户定义了一个计算残差的函数,并用它构造一个NumericDiffCostFunction。 例如,$f\left(x\right)=10-x$对应的函数是
struct NumericDiffCostFunctor {
bool operator()(const double* const x, double* residual) const {
residual[] = 10.0 - x[];
return true;
}
};

添加到problem中:

CostFunction* cost_function =
new NumericDiffCostFunction<NumericDiffCostFunctor, ceres::CENTRAL, , >(new NumericDiffCostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);

注意,自动求导时我们用的是

CostFunction* cost_function =
new AutoDiffCostFunction<CostFunctor, , >(new CostFunctor);
problem.AddResidualBlock(cost_function, NULL, &x);

除了一个额外的模板参数,该参数表明用于计算数值导数(该示例在examples/helloworld_numeric_diff.cc)的有限差分格式的种类之外,其结构看起来与用于自动求导的格式几乎完全相同。

一般来说,我们建议自动求导而不是数值求导。 C ++模板的使用使得自动求导变得高效,而数值求导很expensive,容易出现数值错误,并导致收敛速度变慢。

解析求导

在某些情况下,使用自动区分是不可能的。 例如,以封闭形式计算导数比依赖自动求导代码所使用的链式规则更有效。

在这种情况下,可以提供自己写的残差和雅可比计算代码。 为此,如果您知道编译时参数和残差的大小,请定义CostFunctionSizedCostFunction的子类。 这里例如是实现$f\left(x\right)=10-x$的SimpleCostFunction

class QuadraticCostFunction : public ceres::SizedCostFunction<, > {
public:
virtual ~QuadraticCostFunction() {}
virtual bool Evaluate(double const* const* parameters,
double* residuals,
double** jacobians) const {
const double x = parameters[][];
residuals[] = - x; // Compute the Jacobian if asked for.
if (jacobians != NULL && jacobians[] != NULL) {
jacobians[][] = -;//残差的导数为-1,残差函数是线性的。
}
return true;
}
};
SimpleCostFunction :: Evaluate提供输入parameters数组,residuals输出数组残差和jacobians输出数组雅可比。 jacobians数组是可选的,Evaluate需要检查它是否为非空值,如果是这种情况,则用残差函数的导数值填充它。 在这种情况下,由于残差函数是线性的,因此雅可比行列式是恒定的。
从上面的代码片段可以看出,实现CostFunction对象有点乏味。 我们建议,除非您有充分理由来自己管理雅可比计算,否则使用AutoDiffCostFunctionNumericDiffCostFunction来构建残差块。解析求导程序在examples/helloworld_analytic_diff.cc
#include <vector>
#include "ceres/ceres.h"
#include "glog/logging.h" using ceres::CostFunction;
using ceres::SizedCostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve; // A CostFunction implementing analytically derivatives for the
// function f(x) = 10 - x.
class QuadraticCostFunction
: public SizedCostFunction< /* number of residuals */,
/* size of first parameter */> {
public:
virtual ~QuadraticCostFunction() {} virtual bool Evaluate(double const* const* parameters,
double* residuals,
double** jacobians) const {
double x = parameters[][]; // f(x) = 10 - x.
residuals[] = - x; // f'(x) = -1. Since there's only 1 parameter and that parameter
// has 1 dimension, there is only 1 element to fill in the
// jacobians.
//
// Since the Evaluate function can be called with the jacobians
// pointer equal to NULL, the Evaluate function must check to see
// if jacobians need to be computed.
//
// For this simple problem it is overkill to check if jacobians[0]
// is NULL, but in general when writing more complex
// CostFunctions, it is possible that Ceres may only demand the
// derivatives w.r.t. a subset of the parameter blocks.
if (jacobians != NULL && jacobians[] != NULL) {
jacobians[][] = -;
} return true;
}
}; int main(int argc, char** argv) {
google::InitGoogleLogging(argv[]); // The variable to solve for with its initial value. It will be
// mutated in place by the solver.
double x = 0.5;
const double initial_x = x; // Build the problem.
Problem problem; // Set up the only cost function (also known as residual).
CostFunction* cost_function = new QuadraticCostFunction;
problem.AddResidualBlock(cost_function, NULL, &x); //x待估计参数 // Run the solver!
Solver::Options options;
options.minimizer_progress_to_stdout = true;
Solver::Summary summary;
Solve(options, &problem, &summary); std::cout << summary.BriefReport() << "\n";
std::cout << "x : " << initial_x
<< " -> " << x << "\n"; return ;
}
 
 

Ceres入门笔记的更多相关文章

  1. 每天成长一点---WEB前端学习入门笔记

    WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...

  2. ES6入门笔记

    ES6入门笔记 02 Let&Const.md 增加了块级作用域. 常量 避免了变量提升 03 变量的解构赋值.md var [a, b, c] = [1, 2, 3]; var [[a,d] ...

  3. [Java入门笔记] 面向对象编程基础(二):方法详解

    什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...

  4. React.js入门笔记

    # React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...

  5. redis入门笔记(2)

    redis入门笔记(2) 上篇文章介绍了redis的基本情况和支持的数据类型,本篇文章将介绍redis持久化.主从复制.简单的事务支持及发布订阅功能. 持久化 •redis是一个支持持久化的内存数据库 ...

  6. redis入门笔记(1)

    redis入门笔记(1) 1. Redis 简介 •Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure serv ...

  7. OpenGLES入门笔记四

    原文参考地址:http://www.cnblogs.com/zilongshanren/archive/2011/08/08/2131019.html 一.编译Vertex Shaders和Fragm ...

  8. OpenGLES入门笔记三

    在入门笔记一中比较详细的介绍了顶点着色器和片面着色器. 在入门笔记二中讲解了简单的创建OpenGL场景流程的实现,但是如果在场景中渲染任何一种几何图形,还是需要入门笔记一中的知识:Vertex Sha ...

  9. unity入门笔记

    我于2010年4月1日硕士毕业加入完美时空, 至今5年整.刚刚从一家公司的微端(就是端游技术+页游思想, 具体点就是c++开发, directX渲染, 资源采取所需才会下载)项目的前端主程职位离职, ...

随机推荐

  1. 抓取访客ip

    $realip = $_SERVER['REMOTE_ADDR'] $proxy_ip = explode(',',$_SERVER['HTTP_X_FORWARDED_FOR']);//有用到代理 ...

  2. Spring AOP配置

    相关概念有点拗口,我这里简单总结一个,切面,决定做什么,写处理逻辑,比如打日志.切入点,决定在哪些方里拦截,一般填正则表达式查询. 通知,就是连接切面和切入点的桥梁. 其中遇到了配置好,启动服务器没报 ...

  3. 创建一个子进程---vfork

    子.父进程共享数据段与堆栈段 函数原型:pid_t vfork(void) 返回值:子进程中返回0,父进程中返回子进程ID,出错返回-1. 注意: vfork创建的进程是按先子进程后父进程的顺序执行的 ...

  4. 替换SQL执行计划

    Switching two different SQL Plan with SQL Profile in Oracle... 当SQL是业务系统动态生成的,或者是第三方系统产生的,在数据库层面分析发现 ...

  5. PTA第五次作业

    #include<stdio.h> #include<math.h> int main () { int n,m,i,j,a; scanf("%d",&am ...

  6. 2018.09.21 atcoder An Invisible Hand(贪心)

    传送门 简单贪心啊. 这题显然跟t并没有关系,取差量最大的几组买入卖出就行了. 于是我们统计一下有几组差量是最大的就行了. 代码: #include<bits/stdc++.h> #def ...

  7. MySQL通过游标来实现通过查询记录集循环

    /*我们有时候会遇到需要对 从A表查询的结果集S_S 的记录 进行遍历并做一些操作(如插入),且这些操作需要的数据或许部分来自S_S集合*//*临时存储过程,没办法,不能直接在查询窗口做这些事.*/d ...

  8. Paper格式-国际会议版

    Paper Title 论文题目 Authors Name/s per 1st Affiliation (Author) 作者名字/s 每第一作者 line 1 (of Affiliation): d ...

  9. 理解JavaWeb项目中的路径问题——相对路径与绝对路径

    背景: 在刚开始学习javaweb,使用servlet和jsp开发web项目的过程中,一直有一个问题困扰着我:servlet 和 jsp 之间相互跳转,跳转的路径应该如何书写,才能正确的访问到相应的s ...

  10. 安卓添加USB外置UVC摄像头

    实现的方法有很多种,按步骤来看适合哪一种,网上说什么接采集卡,其实就是把AV转成UVC,现在市面上很多摄像头直接就已经是UVC的了,在windows上面即插即用. 安卓也是Linux,这个就好办了. ...