Ceres Solver中文详细易懂版学习笔记

1.Ceres Slover简介

Ceres Solver是谷歌开发的一种高效的非线性优化开源库,应用场景广泛,可以解决常见数据拟合,最小二乘,非线性最小二乘的问题,类似于图像处理中的OpenCv。

大多数人接触此库,往往是因为SLAM中的图优化,归根到底最后也是一个非线性的最小二乘问题,Ceres在数值处理方面有着更加广泛的应用。

本笔记主要参考官方教程,内容为官方教程的再理解和补充。

笔者将大多数学习理解都写入了代码的中文注释中,如有参考请细读。

官方教程链接:http://www.ceres-solver.org/nnls_tutorial.html.
官方网站:http://www.ceres-solver.org/

2.CeresSlover之Helloworld实现

不论是什么语言,经典的Helloworld总是必不可少的,在某种意义上,如果你的python或者java能输出Helloworld,那代表着你的环境配置和程序的基本使用已经完善,下面我们用一个非常简单的例子进行Ceres使用的Helloworld讲解。

2.1 目标问题

我们使用ceres求取下列函数的最小值(为方便教学,函数选取的非常简单):
1 2 ( 10 − x ) 2 \frac{1}{2}(10-x)^2 21(10x)2
我们很容易算出这个最小值在x等于10处,但是简单的问题更适合用来说来用法。

2.2 代码实现

第一步:建立起f(x)= 10- x 的数学函数模型。

struct CostFunctor {
   template <typename T>
   //operators是一种模板方法,其假定所的输入输出都变为T的格式
   bool operator()(const T* const x, T* residual) const 
	//其中x为带估算的参数,residual是残差
	{
     residual[0] = T(10.0) - x[0];//这里的T[10。0],可以将10 转换位所需的T格式,如double,Jet等
     return true;
   }
};

这样我们就建立起了一个残差的计算模型,接下来:
第二步:就要构建最小二乘的问题框架并解决它。

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);

  // 设置迭代的初值
  double initial_x = 5.0;
  double x = initial_x;

  // 申明构建的问题,用Problem类
  Problem problem;

  // 设置好的残差计算的公式,使用auto-differentiation选项去获得导数(雅克比). 
  CostFunction* cost_function =
  //注意:costFunctor是前面定义的f(x)=10-x。这里的第一个1是待测参量的维数,第二个1是每个待测参量的size
  // 比如有两个参量。第一个为m,大小9;第二个c,大小为3。那么就是<CostFunctor, 2, 9,3>
      new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
  problem.AddResidualBlock(cost_function, NULL, &x);

  // 运行solver
  Solver::Options options;//为Solver的设置参数类,使用options可以对Solver类型使用方法等进行设置
  options.linear_solver_type = ceres::DENSE_QR;//使用得是稠密的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 0;
}

本部分的官方参照代码在examples/helloworld.cc
笔者在ubuntumate 18.04下进行验证,结果为:

iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time
   0  4.512500e+01    0.00e+00    9.50e+00   0.00e+00   0.00e+00  1.00e+04       0    5.33e-04    3.46e-03
   1  4.511598e-07    4.51e+01    9.50e-04   9.50e+00   1.00e+00  3.00e+04       1    5.00e-04    4.05e-03
   2  5.012552e-16    4.51e-07    3.17e-08   9.50e-04   1.00e+00  9.00e+04       1    1.60e-05    4.09e-03
Ceres Solver Report: Iterations: 2, Initial cost: 4.512500e+01, Final cost: 5.012552e-16, Termination: CONVERGENCE
x : 0.5 -> 10

如果你得到同样的结果,那么说明环境的配置和基础使用已经OK

2.3 Helloworld小结

学习完本部分后,依托着简单的函数,我们可以大致总结出ceres解决的问题的框架和步骤:

1.建立constFuntions 1
2.设置初值,构建出最小二乘问题(使用Probolem类)
3.构建solver,设置使用的方法如线性最小二乘等
4.输出程序结果

3.ceres在powell法上的实现

接下来介绍一个较为复杂的问题,鲍威尔优化算法。有兴趣的可以查询其算法细节,本文主要是针对ceres的使用。
参数为 x = [ x 1 , x 2 , x 3 , x 4 ] x=[x_1,x_2,x_3,x_4] x=[x1,x2,x3,x4],具体的函数为:
f 1 ( x ) = x 1 + 10 x 2 f 2 ( x ) = 5 ( x 3 − x 4 ) f 3 ( x ) = ( x 2 − 2 x 3 ) 2 f 4 ( x ) = 1 0 ( x 1 − x 4 ) 2 F ( x ) = [ f 1 ( x ) , f 2 ( x ) , f 3 ( x ) , f 4 ( x ) ] f_1(x)=x1+10x_2\\ f_2(x)=\sqrt5(x_3−x_4)\\ f_3(x)=(x_2−2x_3)^2\\ f_4(x)=\sqrt10(x_1−x_4)^2\\ F(x)=[f_1(x), f_2(x), f_3(x), f_4(x)] f1(x)=x1+10x2f2(x)=5 (x3x4)f3(x)=(x22x3)2f4(x)=1 0(x1x4)2F(x)=[f1(x),f2(x),f3(x),f4(x)]
F ( x ) F(x) F(x)有四个参数,所有四个残差,找到一个 x x x 1 2 ∣ ∣ F ( x ) ∣ ∣ 2 \frac{1}{2}||F(x)||^2 21F(x)2的值最小。

3.1 构建costFunctior

对于单独的 f 4 ( x ) f_4(x) f4(x)来说:

struct F4 {
  template <typename T>
  //f_4(x)的相关参量为x_1和x_4
  bool operator()(const T* const x1, const T* const x4, T* residual) const {
  //按照f_4(x)构建残差公式
    residual[0] = T(sqrt(10.0)) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
    return true;
  }
};

完整的构建为:

struct F1 {
  template <typename T> bool operator()(const T* const x1,
                                        const T* const x2,
                                        T* residual) const {
    // f1 = x1 + 10 * x2;
    residual[0] = x1[0] + 10.0 * x2[0];
    return true;
  }
};

struct F2 {
  template <typename T> bool operator()(const T* const x3,
                                        const T* const x4,
                                        T* residual) const {
    // f2 = sqrt(5) (x3 - x4)
    residual[0] = sqrt(5.0) * (x3[0] - x4[0]);
    return true;
  }
};

struct F3 {
  template <typename T> bool operator()(const T* const x2,
                                        const T* const x3,
                                        T* residual) const {
    // f3 = (x2 - 2 x3)^2
    residual[0] = (x2[0] - 2.0 * x3[0]) * (x2[0] - 2.0 * x3[0]);
    return true;
  }
};

struct F4 {
  template <typename T> bool operator()(const T* const x1,
                                        const T* const x4,
                                        T* residual) const {
    // f4 = sqrt(10) (x1 - x4)^2
    residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
    return true;
  }
};

3.2 构造最小二乘问题的框架

直接上代码,理解在注释里:

 Problem problem;
  // 还是用autoDiff的方式来求解导数,前面是costfunction的规模,后面是相关的参量,使用&来表明
  problem.AddResidualBlock(new AutoDiffCostFunction<F1, 1, 1, 1>(new F1),NULL,&x1, &x2);
  problem.AddResidualBlock(new AutoDiffCostFunction<F2, 1, 1, 1>(new F2),NULL,&x3, &x4);
  problem.AddResidualBlock(new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), NULL,&x2, &x3);
  problem.AddResidualBlock(new AutoDiffCostFunction<F4, 1, 1, 1>(new F4),NULL,&x1, &x4);

3.3 构建Solver并输出结果

//Solver的配置类
Solver::Options options;
  LOG_IF(FATAL, !ceres::StringToMinimizerType(FLAGS_minimizer,
                                              &options.minimizer_type))
      << "Invalid minimizer: " << FLAGS_minimizer
      << ", valid options are: trust_region and line_search.";

  options.max_num_iterations = 100;//设置最大迭代次数
  options.linear_solver_type = ceres::DENSE_QR;//使用稠密QR方法解决
  options.minimizer_progress_to_stdout = true;//可以输出过程变量

  std::cout << "Initial x1 = " << x1
            << ", x2 = " << x2
            << ", x3 = " << x3
            << ", x4 = " << x4
            << "\n";

  // 运行solver!
  Solver::Summary summary;//用来放中间报告
  Solve(options, &problem, &summary);

  std::cout << summary.FullReport() << "\n";
  std::cout << "Final x1 = " << x1
            << ", x2 = " << x2
            << ", x3 = " << x3
            << ", x4 = " << x4
            << "\n";

运行结果为:

Final x1 = 0.000292189, x2 = -2.92189e-05, x3 = 4.79511e-05, x4 = 4.79511e-05

4.ceres在曲线拟合(Curve Fitting)中的实现

本部分为本笔记的重点,是我们第一次真正意义上的使用ceres解决非线性最小二乘问题,当然,结构是还是一致的,只需要稍加改动就可以实现。
数据来源:
我们使用一个在 y = e 0.3 x + 0.1 y = e^{0.3x+0.1} y=e0.3x+0.1的增加 σ = 0.2 \sigma=0.2 σ=0.2的高斯噪声。
我们来拟合曲线:
y = e m x + c y=e^{mx+c} y=emx+c

4.1 建立损失函数与残差

和前文一致,先建立损失函数,此处和前面不同的是第一次引入了数据,所以residual和前面有所区别,但这个位真正的非线性最小二乘的结构。

struct ExponentialResidual {
//定义数据的x和y的析构函数
  ExponentialResidual(double x, double y)
      : x_(x), y_(y) {}

  template <typename T>
  //两个待估参量m,c类型为指向T* 的指针
  bool operator()(const T* const m, const T* const c, T* residual) const {
   	//残差为一组数据中 r = y - f(x)
    residual[0] = T(y_) - exp(m[0] * T(x_) + c[0]);
    return true;
  }

 private:
  // 一组观测样本
  const double x_;
  const double y_;
};

4.2 构建最小二乘问题

//设置初值
double m = 0.0;
double c = 0.0;
//构建最小二乘问题
Problem problem;
for (int i = 0; i < kNumObservations; ++i) {
  CostFunction* cost_function =
  		//残差维度为1,m维度为1,c维度为1:这就是三个1的意思
       new AutoDiffCostFunction<ExponentialResidual, 1, 1, 1>(
       //data中偶数位存放x的样本值,奇数位存放y的样本值
           new ExponentialResidual(data[2 * i], data[2 * i + 1]));
           //残差的参量是m和c
  problem.AddResidualBlock(cost_function, NULL, &m, &c);
}

4.3 构建Solver并输出结果

  Solver::Options options;
  options.max_num_iterations = 25;
  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 << "Initial m: " << 0.0 << " c: " << 0.0 << "\n";
  std::cout << "Final   m: " << m << " c: " << c << "\n";

4.4 结果及分析

完整代码为:

#include "ceres/ceres.h"
#include "glog/logging.h"

using ceres::AutoDiffCostFunction;
using ceres::CostFunction;
using ceres::Problem;
using ceres::Solver;
using ceres::Solve;

// Data generated using the following octave code.
//   randn('seed', 23497);
//   m = 0.3;
//   c = 0.1;
//   x=[0:0.075:5];
//   y = exp(m * x + c);
//   noise = randn(size(x)) * 0.2;
//   y_observed = y + noise;
//   data = [x', y_observed'];

const int kNumObservations = 67;
const double data[] = {
  0.000000e+00, 1.133898e+00,
  7.500000e-02, 1.334902e+00,
  1.500000e-01, 1.213546e+00,
  2.250000e-01, 1.252016e+00,
  3.000000e-01, 1.392265e+00,
  3.750000e-01, 1.314458e+00,
  4.500000e-01, 1.472541e+00,
  5.250000e-01, 1.536218e+00,
  6.000000e-01, 1.355679e+00,
  6.750000e-01, 1.463566e+00,
  7.500000e-01, 1.490201e+00,
  8.250000e-01, 1.658699e+00,
  9.000000e-01, 1.067574e+00,
  9.750000e-01, 1.464629e+00,
  1.050000e+00, 1.402653e+00,
  1.125000e+00, 1.713141e+00,
  1.200000e+00, 1.527021e+00,
  1.275000e+00, 1.702632e+00,
  1.350000e+00, 1.423899e+00,
  1.425000e+00, 1.543078e+00,
  1.500000e+00, 1.664015e+00,
  1.575000e+00, 1.732484e+00,
  1.650000e+00, 1.543296e+00,
  1.725000e+00, 1.959523e+00,
  1.800000e+00, 1.685132e+00,
  1.875000e+00, 1.951791e+00,
  1.950000e+00, 2.095346e+00,
  2.025000e+00, 2.361460e+00,
  2.100000e+00, 2.169119e+00,
  2.175000e+00, 2.061745e+00,
  2.250000e+00, 2.178641e+00,
  2.325000e+00, 2.104346e+00,
  2.400000e+00, 2.584470e+00,
  2.475000e+00, 1.914158e+00,
  2.550000e+00, 2.368375e+00,
  2.625000e+00, 2.686125e+00,
  2.700000e+00, 2.712395e+00,
  2.775000e+00, 2.499511e+00,
  2.850000e+00, 2.558897e+00,
  2.925000e+00, 2.309154e+00,
  3.000000e+00, 2.869503e+00,
  3.075000e+00, 3.116645e+00,
  3.150000e+00, 3.094907e+00,
  3.225000e+00, 2.471759e+00,
  3.300000e+00, 3.017131e+00,
  3.375000e+00, 3.232381e+00,
  3.450000e+00, 2.944596e+00,
  3.525000e+00, 3.385343e+00,
  3.600000e+00, 3.199826e+00,
  3.675000e+00, 3.423039e+00,
  3.750000e+00, 3.621552e+00,
  3.825000e+00, 3.559255e+00,
  3.900000e+00, 3.530713e+00,
  3.975000e+00, 3.561766e+00,
  4.050000e+00, 3.544574e+00,
  4.125000e+00, 3.867945e+00,
  4.200000e+00, 4.049776e+00,
  4.275000e+00, 3.885601e+00,
  4.350000e+00, 4.110505e+00,
  4.425000e+00, 4.345320e+00,
  4.500000e+00, 4.161241e+00,
  4.575000e+00, 4.363407e+00,
  4.650000e+00, 4.161576e+00,
  4.725000e+00, 4.619728e+00,
  4.800000e+00, 4.737410e+00,
  4.875000e+00, 4.727863e+00,
  4.950000e+00, 4.669206e+00,
};

struct ExponentialResidual {
  ExponentialResidual(double x, double y)
      : x_(x), y_(y) {}

  template <typename T> bool operator()(const T* const m,
                                        const T* const c,
                                        T* residual) const {
    residual[0] = y_ - exp(m[0] * x_ + c[0]);
    return true;
  }

 private:
  const double x_;
  const double y_;
};

int main(int argc, char** argv) {
  google::InitGoogleLogging(argv[0]);

  double m = 0.0;
  double c = 0.0;

  Problem problem;
  for (int i = 0; i < kNumObservations; ++i) {
    problem.AddResidualBlock(
        new AutoDiffCostFunction<ExponentialResidual, 1, 1, 1>(
            new ExponentialResidual(data[2 * i], data[2 * i + 1])),
        NULL,
        &m, &c);
  }

  Solver::Options options;
  options.max_num_iterations = 25;
  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 << "Initial m: " << 0.0 << " c: " << 0.0 << "\n";
  std::cout << "Final   m: " << m << " c: " << c << "\n";
  return 0;

输出结果为:

Final   m: 0.291861 c: 0.131439

可以看出,结果与真实的曲线 m = 0.3 m=0.3 m=0.3 c = 0.1 c= 0.1 c=0.1有一定的差别,这正好说明了拟合的正确性,因为我们添加了高斯噪音。
可视化的结果为:在这里插入图片描述

5.ceres在简单版的bundle adjustment中的实战

接下里到了我们真正的主角,Bundle Adjustment,光束法平差(Bundle Adjustment,BA)。或者叫做捆集调整。

在摄影测量中,光束法平差的思想是每一条光束都可以列共线方程作为观测方程,把 [公式] 个待求地面(加密点,连接点,tie point)坐标和 [公式] 张像片的外参(位姿)一起作为未知数,一般把内参当作固定已知值,又有加密点对应像点坐标已知,就可以进行平差解算。

我们可以发现,平差的目标最小二乘在BA中的意义即为最小化所有光束重投影误差(reprojection error)的平方和。重投影误差在此指的就是通过共线方程将物点投影至像片上的位置与物点对应像点间的距离。

光束平差法的最终目的归结为:减少观测图像的点和参考图像(预测图像)的点之间位置投影变换(再投影)误差。这最小化误差算法使用的是最小二乘算法,目前使用最为成功是Levenberg-Marquardt, 它具有易于实现,对大范围的初始估计能够快速收敛的优点。

此部分需要比较深入的了解和背景知识,先留作一个,近期补充完毕。

待续


  1. 此小结的过于简单,但是需要注意的是待解决的问题为1/2(10-x)^2的最小值,在costFunctor却只用了f(x) = 10-x来构建,但是了解最小二乘问题的话就会理解,残差是2范数的平方。 ↩︎

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值