笔者在学习视觉SLAM十四讲的第6讲非线性优化时,运行使用Ceres拟合曲线的例程,发现当生成数据为如下代码时
vector<double> x_data, y_data; // 数据
for (int i = 0; i < N; i++) {
double x = i / 100.0;
x_data.push_back(x);
double real = exp(ar * x * x + br * x + cr);
double gauss_noise = rng.gaussian(w_sigma * w_sigma);
y_data.push_back(real + gauss_noise);
// rng.gaussian(w_sigma * w_sigma);
}
输出为
solve time cost = 0.000777462 seconds.
Ceres Solver Report: Iterations: 8, Initial cost: 1.597873e+06, Final cost: 5.096851e+01, Termination: CONVERGENCE
estimated a,b,c = 0.890908 2.1719 0.943628
当把注释去掉,即为如下代码时
vector<double> x_data, y_data; // 数据
for (int i = 0; i < N; i++)
{
double x = i / 100.0;
x_data.push_back(x);
double real = exp(ar * x * x + br * x + cr);
double gauss_noise = rng.gaussian(w_sigma * w_sigma);
y_data.push_back(real + gauss_noise);
rng.gaussian(w_sigma * w_sigma); //按理来说此行不应该影响结果。 此处替换为其他无关语句时,结果不受影响
}
输出结果为
solve time cost = 0.000914116 seconds.
Ceres Solver Report: Iterations: 7, Initial cost: 1.599017e+06, Final cost: 5.808070e+01, Termination: CONVERGENCE
estimated a,b,c = 1.05448 1.92195 1.02832
即优化得到了一个更好的结果,重复运行了多次都是这样。
画出曲线,发现这两套参数拟合的结果与真实值都很接近。
将数据点增加到一万个时,有注释结果如下
solve time cost = 0.0271037 seconds.
Ceres Solver Report: Iterations: 8, Initial cost: 1.624262e+08, Final cost: 5.071043e+03, Termination: CONVERGENCE
estimated a,b,c = 1.02485 1.96084 1.01517
无注释即多运行一次rng.gaussian结果如下
solve time cost = 0.10454 seconds.
Ceres Solver Report: Iterations: 8, Initial cost: 1.623919e+08, Final cost: 4.873203e+03, Termination: CONVERGENCE
estimated a,b,c = 1.00244 1.99048 1.00779
可见数据量增大后,能够较好的拟合曲线。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)