学习ceres记录

2024-03-05 21:59
文章标签 学习 记录 ceres

本文主要是介绍学习ceres记录,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

@学习ceres记录

http://ceres-solver.org/nnls_tutorial.html

一.非线性最小二乘

在这里插入图片描述
ρ i ( ∥ f i ( x i 1 , . . . , x i k ) ∥ 2 ) \rho_i\left(\left\|f_i\left(x_{i_1},...,x_{i_k}\right)\right\|^2\right) ρi(fi(xi1,...,xik)2)是ResidualBlock, f i ( ⋅ ) f_i(\cdot) fi()是CostFunction,
大多数情况一群小标量一起出现,例如相机的平移位姿和四元数旋转分量,例如 [ x i 1 , . . . , x i k ] \left[x_{i_1},... , x_{i_k}\right] [xi1,...,xik]是ParameterBlock,也可以是一元的。
ρ i \rho_i ρi是鲁棒核函数

1.1 例1 non-linear least squares problem

1 2 ∑ i ∥ f i ( x i 1 , . . . , x i k ) ∥ 2 \frac{1}{2}\sum_{i} \left\|f_i\left(x_{i_1}, ... ,x_{i_k}\right)\right\|^2 21ifi(xi1,...,xik)2
例如最小化:
1 2 ( 10 − x ) 2 . \frac{1}{2}(10 -x)^2. 21(10x)2.

1.1.1 step1

write a functor that will evaluate this the function: f ( x ) = 10 − x f(x) = 10 - x f(x)=10x

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

operator() is a emplated method,允许ceres调用CostFunctor::operator()
T根据需要的类型:
T=double 直接需要数据
T=Jet 需要Jacobians

1.1.2 构建non-linear least squares problem

int main(int argc, char** argv) {google::InitGoogleLogging(argv[0]);// 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, 1, 1>(new CostFunctor);problem.AddResidualBlock(cost_function, nullptr, &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 0;
}
/*AutoDiffCostFunction将CostFunctor作为输入,
自动对其进行区分,并为其提供一个CostFunction接口。
*/

输出

iter      cost      cost_change  |gradient|   |step|    tr_ratio  tr_radius  ls_iter  iter_time  total_time0  4.512500e+01    0.00e+00    9.50e+00   0.00e+00   0.00e+00  1.00e+04       0    5.33e-04    3.46e-031  4.511598e-07    4.51e+01    9.50e-04   9.50e+00   1.00e+00  3.00e+04       1    5.00e-04    4.05e-032  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

Ceres只在迭代结束时打印出显示,并在检测到收敛时立即终止,这就是为什么这里只看到两次迭代,而不是三次。

2.导数

2.1 Numeric Derivatives(数值导数)

残差调库函数时
用户构建残差,并创建NumericDiffCostFunction例如对于 f ( x ) = 10 − x f(x) = 10 - x f(x)=10x
相应的factor是

struct NumericDiffCostFunctor {bool operator()(const double* const x, double* residual) const {residual[0] = 10.0 - x[0];return true;}
};

加入到问题中变为

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

建议使用自动微分

2.2 Analytic Derivatives(解析微分)

在某些时候自动微分并不可靠,计算微分解析时比利用链式法则自动求导更有效。
在这种情况下自己提供your own residual and jacobian computation code
如果知道参数和残差的size(),定义CostFunction和SizedCostFunction的子类,以下是这种子类function的实现。

class QuadraticCostFunction : public ceres::SizedCostFunction<1 /* number of residuals */, 1 /* size of first parameter */> {public:virtual ~QuadraticCostFunction() {}virtual bool Evaluate(double const* const* parameters,double* residuals,double** jacobians) const {const double x = parameters[0][0];residuals[0] = 10 - 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 nullptr, 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 nullptr, 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.// Compute the Jacobian if asked for.if (jacobians != nullptr && jacobians[0] != nullptr) {jacobians[0][0] = -1;}return true;}
};

SimpleCostFunction(实现的子类)::Evaluate提供了一个输入参数数组、一个用于残差的输出数组残差和一个用于 Jacobian 的输出数组 jacobians。 jacobians 数组是可选的,Evaluate 需要检查它是否为非空,如果是,则用残差函数的导数值填充它。 在这种情况下,由于残差函数是线性的,雅可比矩阵是常数。
从上面的代码片段可以看出,实现CostFunction对象有点复杂。我们建议,除非您有充分的理由自己管理雅可比计算,否则您可以使用AutoDiffCostFunction或NumericDiffCostFunction来构造剩余块。

2.3 More About Derivatives

3 Powell’s Function

error

static Eigen::Matrix<double,3,3> skew(Eigen::Matrix<double,3,1>& mat_in){                 //  反对称矩阵定义Eigen::Matrix<double,3,3> skew_mat;skew_mat.setZero();skew_mat(0,1) = -mat_in(2);skew_mat(0,2) =  mat_in(1);skew_mat(1,2) = -mat_in(0);skew_mat(1,0) =  mat_in(2);skew_mat(2,0) = -mat_in(1);skew_mat(2,1) =  mat_in(0);return skew_mat;
}class PlaneAnalyticCostFunction  :   public  ceres::SizedCostFunction<1, 4, 3>{
public:Eigen::Vector3d curr_point, last_point_j, last_point_l, last_point_m;Eigen::Vector3d ljm_norm;double s;PlaneAnalyticCostFunction(Eigen::Vector3d curr_point_, Eigen::Vector3d last_point_j_,Eigen::Vector3d last_point_l_, Eigen::Vector3d last_point_m_, double s_): curr_point(curr_point_), last_point_j(last_point_j_), last_point_l(last_point_l_),last_point_m(last_point_m_), s(s_){}virtual  bool  Evaluate(double  const  *const  *parameters, double  *residuals, double  **jacobians)const {      //   定义残差模型// 叉乘运算, j,l,m 三个但构成的平行四边面积(摸)和该面的单位法向量(方向)Eigen::Vector3d  ljm_norm = (last_point_j - last_point_l).cross(last_point_j - last_point_m);ljm_norm.normalize();    //  单位法向量Eigen::Map<const Eigen::Quaterniond>  q_last_curr(parameters[0]);Eigen::Map<const Eigen::Vector3d> t_last_curr(parameters[1]);Eigen::Vector3d  lp;      // “从当前阵的当前点” 经过转换矩阵转换到“上一阵的同线束激光点”Eigen::Vector3d  lp_r = q_last_curr *  curr_point ;                        //  for compute jacobian o rotation  L: dp_drlp = q_last_curr *  curr_point  +  t_last_curr;  // 残差函数double  phi1 =  (lp - last_point_j ).dot(ljm_norm);residuals[0]  =   std::fabs(phi1);if(jacobians != NULL){if(jacobians[0] != NULL){phi1 = phi1  /  residuals[0];//  RotationEigen::Matrix3d  skew_lp_r  = skew(lp_r);Eigen::Matrix3d  dp_dr;dp_dr.block<3,3>(0,0) =  -skew_lp_r;Eigen::Map<Eigen::Matrix<double, 1, 4, Eigen::RowMajor>>  J_so3_r(jacobians[0]);J_so3_r.setZero();J_so3_r.block<1,3>(0,0) =  phi1 *  ljm_norm.transpose() *  (dp_dr);Eigen::Map<Eigen::Matrix<double, 1, 3, Eigen::RowMajor>>  J_so3_t(jacobians[1]);J_so3_t.block<1,3>(0,0)  = phi1 * ljm_norm.transpose();                                                                                                                                                                                                                                            }}return  true;}
};

如果不加static

/usr/bin/ld: CMakeFiles/aloam_laser_odometry_node.dir/src/models/loam/aloam_registration.cpp.o: in function `skew(Eigen::Matrix<double, 3, 1, 0, 3, 1>&)':
aloam_registration.cpp:(.text+0x0): multiple definition of `skew(Eigen::Matrix<double, 3, 1, 0, 3, 1>&)'; CMakeFiles/aloam_laser_odometry_node.dir/src/aloam_laser_odometry_node.cpp.o:aloam_laser_odometry_node.cpp:(.text+0x2f0): first defined here
collect2: error: ld returned 1 exit status
make[2]: *** [lidar_localization/CMakeFiles/aloam_laser_odometry_node.dir/build.make:762: /home/quanchao/shenlanxueyuan/sensorfusion4/PA3/devel/lib/lidar_localization/aloam_laser_odometry_node] Error 1
make[1]: *** [CMakeFiles/Makefile2:590: lidar_localization/CMakeFiles/aloam_laser_odometry_node.dir/all] Error 2
make[1]: *** Waiting for unfinished jobs....

这篇关于学习ceres记录的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/777949

相关文章

HarmonyOS学习(七)——UI(五)常用布局总结

自适应布局 1.1、线性布局(LinearLayout) 通过线性容器Row和Column实现线性布局。Column容器内的子组件按照垂直方向排列,Row组件中的子组件按照水平方向排列。 属性说明space通过space参数设置主轴上子组件的间距,达到各子组件在排列上的等间距效果alignItems设置子组件在交叉轴上的对齐方式,且在各类尺寸屏幕上表现一致,其中交叉轴为垂直时,取值为Vert

Ilya-AI分享的他在OpenAI学习到的15个提示工程技巧

Ilya(不是本人,claude AI)在社交媒体上分享了他在OpenAI学习到的15个Prompt撰写技巧。 以下是详细的内容: 提示精确化:在编写提示时,力求表达清晰准确。清楚地阐述任务需求和概念定义至关重要。例:不用"分析文本",而用"判断这段话的情感倾向:积极、消极还是中性"。 快速迭代:善于快速连续调整提示。熟练的提示工程师能够灵活地进行多轮优化。例:从"总结文章"到"用

【前端学习】AntV G6-08 深入图形与图形分组、自定义节点、节点动画(下)

【课程链接】 AntV G6:深入图形与图形分组、自定义节点、节点动画(下)_哔哩哔哩_bilibili 本章十吾老师讲解了一个复杂的自定义节点中,应该怎样去计算和绘制图形,如何给一个图形制作不间断的动画,以及在鼠标事件之后产生动画。(有点难,需要好好理解) <!DOCTYPE html><html><head><meta charset="UTF-8"><title>06

学习hash总结

2014/1/29/   最近刚开始学hash,名字很陌生,但是hash的思想却很熟悉,以前早就做过此类的题,但是不知道这就是hash思想而已,说白了hash就是一个映射,往往灵活利用数组的下标来实现算法,hash的作用:1、判重;2、统计次数;

零基础学习Redis(10) -- zset类型命令使用

zset是有序集合,内部除了存储元素外,还会存储一个score,存储在zset中的元素会按照score的大小升序排列,不同元素的score可以重复,score相同的元素会按照元素的字典序排列。 1. zset常用命令 1.1 zadd  zadd key [NX | XX] [GT | LT]   [CH] [INCR] score member [score member ...]

【机器学习】高斯过程的基本概念和应用领域以及在python中的实例

引言 高斯过程(Gaussian Process,简称GP)是一种概率模型,用于描述一组随机变量的联合概率分布,其中任何一个有限维度的子集都具有高斯分布 文章目录 引言一、高斯过程1.1 基本定义1.1.1 随机过程1.1.2 高斯分布 1.2 高斯过程的特性1.2.1 联合高斯性1.2.2 均值函数1.2.3 协方差函数(或核函数) 1.3 核函数1.4 高斯过程回归(Gauss

【学习笔记】 陈强-机器学习-Python-Ch15 人工神经网络(1)sklearn

系列文章目录 监督学习:参数方法 【学习笔记】 陈强-机器学习-Python-Ch4 线性回归 【学习笔记】 陈强-机器学习-Python-Ch5 逻辑回归 【课后题练习】 陈强-机器学习-Python-Ch5 逻辑回归(SAheart.csv) 【学习笔记】 陈强-机器学习-Python-Ch6 多项逻辑回归 【学习笔记 及 课后题练习】 陈强-机器学习-Python-Ch7 判别分析 【学

系统架构师考试学习笔记第三篇——架构设计高级知识(20)通信系统架构设计理论与实践

本章知识考点:         第20课时主要学习通信系统架构设计的理论和工作中的实践。根据新版考试大纲,本课时知识点会涉及案例分析题(25分),而在历年考试中,案例题对该部分内容的考查并不多,虽在综合知识选择题目中经常考查,但分值也不高。本课时内容侧重于对知识点的记忆和理解,按照以往的出题规律,通信系统架构设计基础知识点多来源于教材内的基础网络设备、网络架构和教材外最新时事热点技术。本课时知识

线性代数|机器学习-P36在图中找聚类

文章目录 1. 常见图结构2. 谱聚类 感觉后面几节课的内容跨越太大,需要补充太多的知识点,教授讲得内容跨越较大,一般一节课的内容是书本上的一章节内容,所以看视频比较吃力,需要先预习课本内容后才能够很好的理解教授讲解的知识点。 1. 常见图结构 假设我们有如下图结构: Adjacency Matrix:行和列表示的是节点的位置,A[i,j]表示的第 i 个节点和第 j 个

Node.js学习记录(二)

目录 一、express 1、初识express 2、安装express 3、创建并启动web服务器 4、监听 GET&POST 请求、响应内容给客户端 5、获取URL中携带的查询参数 6、获取URL中动态参数 7、静态资源托管 二、工具nodemon 三、express路由 1、express中路由 2、路由的匹配 3、路由模块化 4、路由模块添加前缀 四、中间件