Ceres Solver를 활용한 최적화
Jinyong Jeong
https://github.com/JinyongJeong/helloceres
Jinyong Jeong
Motion2AI CTO
Personal Blog
http://jinyongjeong.github.io/about/
This is a sample text. Insert your desired text here. This is a sample text. Insert your desired text here. This is a sample text. This is a sample text. Insert your desired text here.
Company Vision
This is a sample text. Insert your desired text here. This is a sample text. Insert your desired text here. This is a sample text. This is a sample text. Insert your desired text here.
Motion2Ai
Real-time Localization
Data Analysis & Report
Safety Warning & Report
This is a sample text. Insert your desired text here. This is a sample text. Insert your desired text here. This is a sample text. This is a sample text. Insert your desired text here.
Company Vision
This is a sample text. Insert your desired text here. This is a sample text. Insert your desired text here. This is a sample text. This is a sample text. Insert your desired text here.
Linear Algebra
Non-homogeneous System
This is a sample text. Insert your desired text here. This is a sample text. Insert your desired text here. This is a sample text. This is a sample text. Insert your desired text here.
Homogeneous System
Linear Algebra
Plane Fitting Problem
void prepareData(vector<Eigen::Vector3d>& input){
input.push_back(Eigen::Vector3d(1.0, 2.0, -4.777));
input.push_back(Eigen::Vector3d(-3.0, 1.1, 7.477));
input.push_back(Eigen::Vector3d(-3.9, 8.1, 4.6333));
input.push_back(Eigen::Vector3d(-1.9, -8.1, 11.4556));
input.push_back(Eigen::Vector3d(-10.9, -18.1, 45.2333));
input.push_back(Eigen::Vector3d(-20.9, 31.1, 35.855));
input.push_back(Eigen::Vector3d(-21.912, 21.155, 46.514));
input.push_back(Eigen::Vector3d(-27.912, 22.155, 63.0697));
input.push_back(Eigen::Vector3d(-1.912, 30.155, -18.263));
input.push_back(Eigen::Vector3d(-7.912, 20.155, 6.84744));
input.push_back(Eigen::Vector3d(-10.912, 10.155, 23.291));
input.push_back(Eigen::Vector3d(14.912, -51.155, -3.62522));
input.push_back(Eigen::Vector3d(14.912, 91.155, -114.311));
// input.push_back(Eigen::Vector3d(-4.312, 33.135, -16.261)); // large noise
// input.push_back(Eigen::Vector3d(-14.112, 15.152, 28.291)); // large noise
}
Ax+By+Cz+D = 0
Linear Algebra
Plane Fitting Problem
void prepareData(vector<Eigen::Vector3d>& input){
input.push_back(Eigen::Vector3d(1.0, 2.0, -4.777));
input.push_back(Eigen::Vector3d(-3.0, 1.1, 7.477));
input.push_back(Eigen::Vector3d(-3.9, 8.1, 4.6333));
input.push_back(Eigen::Vector3d(-1.9, -8.1, 11.4556));
input.push_back(Eigen::Vector3d(-10.9, -18.1, 45.2333));
input.push_back(Eigen::Vector3d(-20.9, 31.1, 35.855));
input.push_back(Eigen::Vector3d(-21.912, 21.155, 46.514));
input.push_back(Eigen::Vector3d(-27.912, 22.155, 63.0697));
input.push_back(Eigen::Vector3d(-1.912, 30.155, -18.263));
input.push_back(Eigen::Vector3d(-7.912, 20.155, 6.84744));
input.push_back(Eigen::Vector3d(-10.912, 10.155, 23.291));
input.push_back(Eigen::Vector3d(14.912, -51.155, -3.62522));
input.push_back(Eigen::Vector3d(14.912, 91.155, -114.311));
// input.push_back(Eigen::Vector3d(-4.312, 33.135, -16.261)); // large noise
// input.push_back(Eigen::Vector3d(-14.112, 15.152, 28.291)); // large noise
}
Ax+By+Cz+D = 0
Linear Algebra
Plane Fitting Problem
non-trivial solution
U & V 는 orthogonal matrix
Linear Algebra
Plane Fitting Problem
Nx1
Mx1
k 는 smallest singular value (N번째 행)
Mx1
= k
Linear Algebra
Plane Fitting Problem
non-trivial solution
즉 Homogeneous system 에서의 해는 SVD (Singular Value Decomposition)에서 V matrix의 마지막 column이 Trivial solution!
Linear Algebra
Non-homogeneous System
Linear Algebra
Non-homogeneous System
데이터의 양 = 미지수의 양
(유일 해 존재)
데이터의 양 > 미지수의 양
(일반적인 경우)
Exact solution은 존재 하지 않음
근사해 (Approximate solution)
데이터의 양 < 미지수의 양
(무수히 많은 해 존재)
Linear Algebra
Non-homogeneous System (Over-determined)
How to solve?
Least square Problem!
Linear Algebra
Non-homogeneous System (Over-determined)
Least square Problem!
제곱오차함수
(squared error function)
Information matrix
Linear Algebra
Non-homogeneous System (Over-determined)
Least square Problem!
제곱오차함수
(squared error function)
Information matrix
x1 = 왼쪽바퀴 weight
x2 = 오른쪽바퀴 weight
왼쪽바퀴 count
오른쪽 바퀴 count
실제 이동거리
SLAM 의 종류
Graph-based SLAM
Solver의 종류
https://www.cv-learn.com/20210607-solvers/
Ceres Solver
Ceres Solver Tutorial
Ceres Solver Tutorial
Visual Studio Code를 활용한 개발 환경 설정
Ceres Solver Tutorial
1_helloworld
Ceres solver는 Non-Linear Least Square 문제를 푸는 Tool
Residual block
Cost Function
CostFunction *cost_function =
new AutoDiffCostFunction<CostFunctor, 1, 1>(new CostFunctor);
Residual의 수, Parameter의 수
Ceres Solver Tutorial
2_add_residual_1 , 3_add_residual_2
첫번째 function: 0.5 ( 10 - x ) ^ 2
두번째 function: 0.5 ( x ) ^2
두 function의 합을 최소로 만드는 x를 계산! => 예상되는 최종 결과는?
첫번째 cost function 의 중요한 경우 첫번째 function 쪽으로 최적화가 되게 하는 방법은?
Ceres Solver Tutorial
3_multivariate
다변수의 parameter를 입력으로 받기
두 변수의 차가 최소화 되도록 최적화
CostFunction *cost_function =
new AutoDiffCostFunction<CostFunctor, 3, 3, 3>(new CostFunctor);
Residual의 수, Parameter의 수
Ceres Solver Tutorial
Analytic & Numeric derivative
Ceres Solver Tutorial
Analytic_diff
Let’s draw the function (https://www.wolframalpha.com/)
Ceres Solver Tutorial
Analytic_diff vs Auto Diff (Numeric)
=========== Analytic solver ===========
iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
0 4.089800e+06 0.00e+00 4.72e+06 0.00e+00 0.00e+00 1.00e+04 0 3.10e-05 1.35e-04
1 3.290684e+05 3.76e+06 6.12e+05 1.73e+00 9.20e-01 2.44e+04 1 5.01e-05 2.25e-04
2 2.692007e+04 3.02e+05 8.00e+04 1.08e+00 9.18e-01 5.89e+04 1 1.19e-05 2.51e-04
…
7 1.109799e-06 2.48e-02 6.71e-02 4.87e-03 1.00e+00 1.16e+07 1 8.11e-06 3.48e-04
8 2.342133e-15 1.11e-06 3.08e-06 3.31e-05 1.00e+00 3.48e+07 1 7.87e-06 3.65e-04
Ceres Solver Report: Iterations: 9, Initial cost: 4.089800e+06, Final cost: 2.342133e-15, Termination: CONVERGENCE
x : 5 -> 0.853867
=========== Numaric solver ===========
iter cost cost_change |gradient| |step| tr_ratio tr_radius ls_iter iter_time total_time
0 4.089800e+06 0.00e+00 4.72e+06 0.00e+00 0.00e+00 1.00e+04 0 1.91e-05 4.20e-05
1 3.290684e+05 3.76e+06 6.12e+05 1.73e+00 9.20e-01 2.44e+04 1 3.10e-05 9.11e-05
2 2.692007e+04 3.02e+05 8.00e+04 1.08e+00 9.18e-01 5.89e+04 1 2.00e-05 1.23e-04
…
7 1.109799e-06 2.48e-02 6.71e-02 4.87e-03 1.00e+00 1.16e+07 1 2.00e-05 2.77e-04
8 2.342133e-15 1.11e-06 3.08e-06 3.31e-05 1.00e+00 3.48e+07 1 1.88e-05 3.06e-04
Ceres Solver Report: Iterations: 9, Initial cost: 4.089800e+06, Final cost: 2.342133e-15, Termination: CONVERGENCE
x : 5 -> 0.853867
Ceres Solver Tutorial
9_curve_fitting
Let’s draw the raw data with scatter plot
(https://www.rapidtables.com/tools/scatter-plot.html)
Ceres Solver Tutorial
10_curve_fitting2
f(x) = 2x^2 + 3 x + 1
Let’s draw the raw data with scatter plot
(https://www.rapidtables.com/tools/scatter-plot.html)
Ceres Solver Tutorial
10_curve_fitting2
Ceres Solver Tutorial
Pose_2d_fixed
Graph Optimization 어떤 경우에 Node를 Fix 해야 하는가?
Ceres Solver Tutorial
Pose_2d_fixed
첫번째 Node의 위치를 어떻게 Fix 시킬수 있을까?
(x: 3.0, y: 1.0, yaw: 0.523)
(x: 5.1, y: 2.9, yaw:0.79)
(x: 2.73, y:0.732, yaw:0.26)
Ceres Solver Tutorial
Pose Graph
g2o data format
VERTEX_SE2 0 0.000000 0.000000 0.000000
VERTEX_SE2 1 1.030390 0.011350 -0.012958
VERTEX_SE2 2 2.043445 -0.060422 -0.026183
VERTEX_SE2 3 3.070548 -0.094779 -0.021350
VERTEX_SE2 4 3.079976 0.909609 1.545440
VERTEX_SE2 5 3.091176 1.925681 1.529136
EDGE_SE2 73 74 0.985691 0.011144 -0.001664 44.721360 0.000000 0.000000 44.721360 0.000000 44.721360
EDGE_SE2 74 75 0.981205 -0.005965 0.022669 44.721360 0.000000 0.000000 44.721360 0.000000 44.721360
EDGE_SE2 75 76 -0.008260 0.981841 1.564555 44.721360 0.000000 0.000000 44.721360 0.000000 44.721360
x y yaw
x y yaw [ Information Matix ]
Ceres Solver Tutorial
Pose Graph
Manifold은 무엇인가?
Ceres Solver Tutorial
Pose Graph
Loss Function (Robust Kernel)
Ceres Solver Tutorial
Pose Graph 2D
Ceres Solver Tutorial
Pose Graph 3D
Ceres Solver Summary
Reference
https://github.com/JinyongJeong/helloceres
https://notes.andrewtorgesen.com/doku.php?id=public:ceres-slides
https://www.cvl.isy.liu.se/education/graduate/opencv/ceres-presentation.pdf
http://ceres-solver.org/features.html
https://www.cv-learn.com/20210607-solvers/
https://velog.io/@nabi4622/Ceres-Solver-%ED%8A%9C%ED%86%A0%EB%A6%AC%EC%96%BC