第3章 Ceres库|求解最小二乘
3.1 Ceres Solver库
官网:
安装:
sudo apt-get install liblapack-dev libsuitesparse-dev libcxsparse3.1.4 libgflags-dev libgoogle-glog-dev libgtest-dev
git clone https://github.com/ceres-solver/ceres-solver.git
cd ceres-solver/
mkdir build
cd build
cmake ..
make
make install
Ceres求解最小二乘问题:
Ceres库面向通用的最小二乘问题的求解,用户需要做的就是定义优化问题,然后设置一些选项,输入进Ceres求解即可。步骤一般包含:定义代价函数计算模型;构建最小二乘问题;配置求解器。
-
核函数:$ \rho $
-
代价函数:$ f_i $
-
最简情况:取$ l_j=-\infty,u_j=\infty$,$ \rho $为恒等函数,得到无约束的最小二乘问题。
$$
\begin{aligned}
&\min_x \quad \frac{1}{2} \sum_i \rho_i \left[ {|| f_i(x_{i_1},\cdots,x_{i_n}) ||} \right] \
&s.t. \quad l_j \leq x_j \leq u_j
\end{aligned}
$$
3.2 拟合曲线
完整代码:https://gitee.com/deng-ke/slam_notes/tree/master/ceres_curve_fitting
- 目标:使用Ceres拟合如下曲线
$$ y = exp(ax^2+bx+c)+\omega $$
-
输入:$ N $对$ x、y $;高斯噪声$ \omega $。
-
输出:曲线参数$ a、b、c $。
-
方法:求如下最小二乘问题以估计曲线参数。(相当于机器学习中最小化代价函数)
$$ \min_{a,b,c} \frac{1}{2} \sum_{i=1}^N {||y_i-exp(a{x_i}^2+bx_i+c)||}^2 $$
3.2.1 定义代价函数计算模型
以仿函数形式,重载(),定义代价函数。
/***************************************************************************
*定义代价函数的计算模型
***************************************************************************/
struct CURVE_FITTING_COST
{
CURVE_FITTING_COST ( double x, double y ) : _x ( x ), _y ( y ) {}
/*残差的计算(重载(),仿函数)*/
template <typename T>
bool operator() (
const T* const abc, // 模型参数,有3维
T* residual ) const // 残差
{
residual[0] = T ( _y ) - ceres::exp ( abc[0]*T ( _x ) *T ( _x ) + abc[1]*T ( _x ) + abc[2] );
return true;
}
//x、y数据
const double _x, _y;
};
3.2.2 生成数据
根据真实的模型生成$ x、y $真值,再加入随机的高斯噪声。
/***************************************************************************
*生成数据
***************************************************************************/
double a=1.0, b=2.0, c=1.0; // 真实参数值
int N=100; // 数据点
double w_sigma=1.0; // 噪声Sigma值
cv::RNG rng; // OpenCV随机数产生器
double abc[3] = {0,0,0}; // abc参数的估计值
vector<double> x_data, y_data; // 数据
/*根据真实的模型生成x、y真值,再加入随机的高斯噪声*/
cout<<"generating data: "<<endl;
for ( int i=0; i<N; i++ )
{
double x = i/100.0;
x_data.push_back ( x );
y_data.push_back (
exp ( a*x*x + b*x + c ) + rng.gaussian ( w_sigma )
);
cout<<x_data[i]<<" "<<y_data[i]<<endl;
}
3.2.3 构建最小二乘问题
导入前面定义的代价函数模型。
/***************************************************************************
*构建最小二乘问题
***************************************************************************/
ceres::Problem problem;
for ( int i=0; i<N; i++ )
{
problem.AddResidualBlock ( // 向问题中添加误差项
// 使用自动求导,模板参数:误差类型,输出维度,输入维度。(维数要与前面struct中一致)
new ceres::AutoDiffCostFunction<CURVE_FITTING_COST, 1, 3> (
new CURVE_FITTING_COST ( x_data[i], y_data[i] )
),
nullptr, // 核函数,这里不使用,为空
abc // 待估计参数
);
}
3.2.4 配置求解器
/***************************************************************************
*配置求解器
***************************************************************************/
ceres::Solver::Options options; // 这里有很多配置项可以填
options.linear_solver_type = ceres::DENSE_QR; // 增量方程如何求解
options.minimizer_progress_to_stdout = true; // 输出到cout
ceres::Solver::Summary summary; // 优化信息
chrono::steady_clock::time_point t1 = chrono::steady_clock::now();
ceres::Solve ( options, &problem, &summary ); // 开始优化
chrono::steady_clock::time_point t2 = chrono::steady_clock::now();
chrono::duration<double> time_used = chrono::duration_cast<chrono::duration<double>>( t2-t1 );
cout<<"solve time cost = "<<time_used.count()<<" seconds. "<<endl;
// 输出结果
cout<<summary.BriefReport() <<endl;
cout<<"estimated a,b,c = ";
for ( auto a:abc ) cout<<a<<" ";
cout<<endl;
3.2.5 结果

© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END


暂无评论内容