ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
robust_kernel.cpp
Go to the documentation of this file.
1 // ----------------------------------------------------------------------------
2 // - CloudViewer: www.cloudViewer.org -
3 // ----------------------------------------------------------------------------
4 // Copyright (c) 2018-2024 www.cloudViewer.org
5 // SPDX-License-Identifier: MIT
6 // ----------------------------------------------------------------------------
7 
8 #include <Logging.h>
9 
10 #include "pybind/docstring.h"
13 
14 namespace cloudViewer {
15 namespace t {
16 namespace pipelines {
17 namespace registration {
18 
19 void pybind_robust_kernels(py::module& m) {
20  py::module m_robust_kernel = m.def_submodule(
21  "robust_kernel",
22  "Tensor-based robust kernel for outlier rejection.");
23  py::native_enum<RobustKernelMethod>(
24  m_robust_kernel, "RobustKernelMethod", "enum.Enum",
25  "Robust kernel method for outlier rejection.")
26  .value("L2Loss", RobustKernelMethod::L2Loss)
27  .value("L1Loss", RobustKernelMethod::L1Loss)
28  .value("HuberLoss", RobustKernelMethod::HuberLoss)
29  .value("CauchyLoss", RobustKernelMethod::CauchyLoss)
30  .value("GMLoss", RobustKernelMethod::GMLoss)
31  .value("TukeyLoss", RobustKernelMethod::TukeyLoss)
32  .value("GeneralizedLoss", RobustKernelMethod::GeneralizedLoss)
33  .export_values()
34  .finalize();
35  py::class_<RobustKernel> robust_kernel(m_robust_kernel, "RobustKernel",
36  R"(
37 Base class that models a robust kernel for outlier rejection. The virtual
38 function ``weight()`` must be implemented in derived classes.
39 
40 The main idea of a robust loss is to downweight large residuals that are
41 assumed to be caused from outliers such that their influence on the solution
42 is reduced. This is achieved by optimizing:
43 
44 .. math::
45 \def\argmin{\mathop{\rm argmin}}
46 \begin{equation}
47 x^{*} = \argmin_{x} \sum_{i=1}^{N} \rho({r_i(x)})
48 \end{equation}
49 :label: robust_loss
50 
51 where :math:`\rho(r)` is also called the robust loss or kernel and
52 :math:`r_i(x)` is the residual.
53 
54 Several robust kernels have been proposed to deal with different kinds of
55 outliers such as Huber, Cauchy, and others.
56 
57 The optimization problem in :eq:`robust_loss` can be solved using the
58 iteratively reweighted least squares (IRLS) approach, which solves a sequence
59 of weighted least squares problems. We can see the relation between the least
60 squares optimization in stanad non-linear least squares and robust loss
61 optimization by comparing the respective gradients which go to zero at the
62 optimum (illustrated only for the :math:`i^\mathrm{th}` residual):
63 
64 .. math::
65 \begin{eqnarray}
66 \frac{1}{2}\frac{\partial (w_i r^2_i(x))}{\partial{x}}
67 &=&
68 w_i r_i(x) \frac{\partial r_i(x)}{\partial{x}} \\
69 \label{eq:gradient_ls}
70 \frac{\partial(\rho(r_i(x)))}{\partial{x}}
71 &=&
72 \rho'(r_i(x)) \frac{\partial r_i(x)}{\partial{x}}.
73 \end{eqnarray}
74 
75 By setting the weight :math:`w_i= \frac{1}{r_i(x)}\rho'(r_i(x))`, we
76 can solve the robust loss optimization problem by using the existing techniques
77 for weighted least-squares. This scheme allows standard solvers using
78 Gauss-Newton and Levenberg-Marquardt algorithms to optimize for robust losses
79 and is the one implemented in CloudViewer.
80 
81 Then we minimize the objective function using Gauss-Newton and determine
82 increments by iteratively solving:
83 
84 .. math::
85 \newcommand{\mat}[1]{\mathbf{#1}}
86 \newcommand{\veca}[1]{\vec{#1}}
87 \renewcommand{\vec}[1]{\mathbf{#1}}
88 \begin{align}
89 \left(\mat{J}^\top \mat{W} \mat{J}\right)^{-1}\mat{J}^\top\mat{W}\vec{r},
90 \end{align}
91 
92 where :math:`\mat{W} \in \mathbb{R}^{n\times n}` is a diagonal matrix containing
93 weights :math:`w_i` for each residual :math:`r_i`
94 
95 The different loss functions will only impact in the weight for each residual
96 during the optimization step.
97 Therefore, the only impact of the choice on the kernel is through its first
98 order derivate.
99 
100 The kernels implemented so far, and the notation has been inspired by the
101 publication: **"Analysis of Robust Functions for Registration Algorithms"**, by
102 Philippe Babin et al.
103 
104 For more information please also see: **"Adaptive Robust Kernels for
105 Non-Linear Least Squares Problems"**, by Nived Chebrolu et al.
106 )");
107 
108  py::detail::bind_copy_functions<RobustKernel>(robust_kernel);
109  robust_kernel
110  .def(py::init([](const RobustKernelMethod type,
111  const double scaling_parameter,
112  const double shape_parameter) {
113  return new RobustKernel(type, scaling_parameter,
114  shape_parameter);
115  }),
116  py::arg_v("type", RobustKernelMethod::L2Loss,
117  "cloudViewer.t.pipelines.registration."
118  "RobustKernelMethod."
119  "L2Loss"),
120  "scaling_parameter"_a = 1.0, "shape_parameter"_a = 1.0)
121  .def_readwrite("type", &RobustKernel::type_, "Loss type.")
122  .def_readwrite("scaling_parameter",
124  "Scaling parameter.")
125  .def_readwrite("shape_parameter", &RobustKernel::shape_parameter_,
126  "Shape parameter.")
127  .def("__repr__", [](const RobustKernel& rk) {
128  return fmt::format(
129  "RobustKernel[scaling_parameter_={:e}, "
130  "shape_parameter_={:e}].",
132  });
133 }
134 
135 } // namespace registration
136 } // namespace pipelines
137 } // namespace t
138 } // namespace cloudViewer
filament::Texture::InternalFormat format
char type
Generic file read and write utility for python interface.