Ceres入门笔记
介绍
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 ;
}
AutoDiffCostFunction将CostFunction作为输入,并且自动求导,并为其提供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 ->
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,容易出现数值错误,并导致收敛速度变慢。
解析求导
在某些情况下,使用自动区分是不可能的。 例如,以封闭形式计算导数比依赖自动求导代码所使用的链式规则更有效。
在这种情况下,可以提供自己写的残差和雅可比计算代码。 为此,如果您知道编译时参数和残差的大小,请定义CostFunction或SizedCostFunction的子类。 这里例如是实现$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;
}
};
#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入门笔记的更多相关文章
- 每天成长一点---WEB前端学习入门笔记
WEB前端学习入门笔记 从今天开始,本人就要学习WEB前端了. 经过老师的建议,说到他每天都会记录下来新的知识点,每天都是在围绕着这些问题来度过,很有必要每天抽出半个小时来写一个知识总结,及时对一天工 ...
- ES6入门笔记
ES6入门笔记 02 Let&Const.md 增加了块级作用域. 常量 避免了变量提升 03 变量的解构赋值.md var [a, b, c] = [1, 2, 3]; var [[a,d] ...
- [Java入门笔记] 面向对象编程基础(二):方法详解
什么是方法? 简介 在上一篇的blog中,我们知道了方法是类中的一个组成部分,是类或对象的行为特征的抽象. 无论是从语法和功能上来看,方法都有点类似与函数.但是,方法与传统的函数还是有着不同之处: 在 ...
- React.js入门笔记
# React.js入门笔记 核心提示 这是本人学习react.js的第一篇入门笔记,估计也会是该系列涵盖内容最多的笔记,主要内容来自英文官方文档的快速上手部分和阮一峰博客教程.当然,还有我自己尝试的 ...
- redis入门笔记(2)
redis入门笔记(2) 上篇文章介绍了redis的基本情况和支持的数据类型,本篇文章将介绍redis持久化.主从复制.简单的事务支持及发布订阅功能. 持久化 •redis是一个支持持久化的内存数据库 ...
- redis入门笔记(1)
redis入门笔记(1) 1. Redis 简介 •Redis是一款开源的.高性能的键-值存储(key-value store).它常被称作是一款数据结构服务器(data structure serv ...
- OpenGLES入门笔记四
原文参考地址:http://www.cnblogs.com/zilongshanren/archive/2011/08/08/2131019.html 一.编译Vertex Shaders和Fragm ...
- OpenGLES入门笔记三
在入门笔记一中比较详细的介绍了顶点着色器和片面着色器. 在入门笔记二中讲解了简单的创建OpenGL场景流程的实现,但是如果在场景中渲染任何一种几何图形,还是需要入门笔记一中的知识:Vertex Sha ...
- unity入门笔记
我于2010年4月1日硕士毕业加入完美时空, 至今5年整.刚刚从一家公司的微端(就是端游技术+页游思想, 具体点就是c++开发, directX渲染, 资源采取所需才会下载)项目的前端主程职位离职, ...
随机推荐
- UI设计是青春饭?今天告诉你真相!
最近有学员来问,“我想转行学习UI设计,但是听很多人说,UI设计是吃青春饭的,互联网公司是不是只选择年轻的血液而淘汰年纪大的?”今天,我来统一回答一下. UI设计是不是青春饭? 我们先来思考一个问题: ...
- kbmMWEncodeEscapes 中汉字编码的问题及解决办法
kbmMWEncodeEscapes 是kbmmw 里面的一个函数,用来对URL 中的汉字进行编码,例如 http://127.0.0.1/getname?name=春节,由于'春节'是汉字,浏览器向 ...
- Django模型层(2)
https://www.cnblogs.com/yuanchenqi/articles/8963244.html from django.db import models class Author(m ...
- Linux必须学的东西,鉴于各大公司实际开发都不用Windows系统
Windows安全性比较差,所以各大公司会使用其他的平台,所以像Linux就是很常用的,基于Unix的开源系统,鉴于很多人写的很散,所以自己总结下对于自己有用的重点,现在总结下简单的linxu的命令使 ...
- org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defined
org.springframework.beans.factory.NoSuchBeanDefinitionException: No bean named 'testService' is defi ...
- 对call() apply() 方法的简单理解
真的是非常简单的理解,我知道的并不多,在网上查找了很多的资料,还是只能了解一点皮毛,下面来整理出来,方便以后深入的去学习,也是对目前知道的知识点的巩固. 整理一些网上的经典解答: 1.一句话区分cal ...
- modelsim编译altera的库
http://www.cnblogs.com/LJWJL/p/3515586.html 在modelsim的安装目录下,把配置文件modelsim.ini的只读属性去掉,然后在modelsim中运行T ...
- 2、WindowManager源码分析--最后一战
在最后一站中,几乎所有的UI界面都是这个WindowManager管理的,那么他是如何调度的呢?我们来看看一些项目中的界面. 上面有登陆界面,专门管理登陆.战斗界面,用户界面,专门管理用户属性等等. ...
- 201709012工作日记--activity与service的通信机制
service生命周期 Service主要包含本地类和远程类. Service不是Thread,Service 是android的一种机制,当它运行的时候如果是Local Service,那么对应的 ...
- day11(多线程,唤醒机制,生产消费者模式,多线程的生命周期)
A:进程: 进程指正在运行的程序.确切的来说,当一个程序进入内存运行,即变成一个进程,进程是处于运行过程中的程序,并且具有一定独立功能. B:线程: 线程是进程中的一个执行单元,负责当前进程中程序的执 ...