ACloudViewer  3.9.4
A Modern Library for 3D Data Processing
robust_kernels.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 <memory>
11 
12 #include "pipelines/registration/RobustKernel.h"
13 #include "pybind/docstring.h"
15 
16 namespace cloudViewer {
17 namespace pipelines {
18 namespace registration {
19 
20 template <class RobustKernelBase = RobustKernel>
21 class PyRobustKernelT : public RobustKernelBase {
22 public:
23  using RobustKernelBase::RobustKernelBase;
24  double Weight(double residual) const override {
25  PYBIND11_OVERLOAD_PURE(double, RobustKernelBase, residual);
26  }
27 };
28 
29 // Type aliases to improve readability
37 
38 void pybind_robust_kernels(py::module &m) {
39  // CloudViewer.registration.RobustKernel
40  py::class_<RobustKernel, std::shared_ptr<RobustKernel>, PyRobustKernel> rk(
41  m, "RobustKernel",
42  R"(
43 Base class that models a robust kernel for outlier rejection. The virtual
44 function ``weight()`` must be implemented in derived classes.
45 
46 The main idea of a robust loss is to downweight large residuals that are
47 assumed to be caused from outliers such that their influence on the solution
48 is reduced. This is achieved by optimizing:
49 
50 .. math::
51  \def\argmin{\mathop{\rm argmin}}
52  \begin{equation}
53  x^{*} = \argmin_{x} \sum_{i=1}^{N} \rho({r_i(x)})
54  \end{equation}
55  :label: robust_loss
56 
57 where :math:`\rho(r)` is also called the robust loss or kernel and
58 :math:`r_i(x)` is the residual.
59 
60 Several robust kernels have been proposed to deal with different kinds of
61 outliers such as Huber, Cauchy, and others.
62 
63 The optimization problem in :eq:`robust_loss` can be solved using the
64 iteratively reweighted least squares (IRLS) approach, which solves a sequence
65 of weighted least squares problems. We can see the relation between the least
66 squares optimization in stanad non-linear least squares and robust loss
67 optimization by comparing the respective gradients which go to zero at the
68 optimum (illustrated only for the :math:`i^\mathrm{th}` residual):
69 
70 .. math::
71  \begin{eqnarray}
72  \frac{1}{2}\frac{\partial (w_i r^2_i(x))}{\partial{x}}
73  &=&
74  w_i r_i(x) \frac{\partial r_i(x)}{\partial{x}} \\
75  \label{eq:gradient_ls}
76  \frac{\partial(\rho(r_i(x)))}{\partial{x}}
77  &=&
78  \rho'(r_i(x)) \frac{\partial r_i(x)}{\partial{x}}.
79  \end{eqnarray}
80 
81 By setting the weight :math:`w_i= \frac{1}{r_i(x)}\rho'(r_i(x))`, we
82 can solve the robust loss optimization problem by using the existing techniques
83 for weighted least-squares. This scheme allows standard solvers using
84 Gauss-Newton and Levenberg-Marquardt algorithms to optimize for robust losses
85 and is the one implemented in CloudViewer.
86 
87 Then we minimize the objective function using Gauss-Newton and determine
88 increments by iteratively solving:
89 
90 .. math::
91  \newcommand{\mat}[1]{\mathbf{#1}}
92  \newcommand{\veca}[1]{\vec{#1}}
93  \renewcommand{\vec}[1]{\mathbf{#1}}
94  \begin{align}
95  \left(\mat{J}^\top \mat{W} \mat{J}\right)^{-1}\mat{J}^\top\mat{W}\vec{r},
96  \end{align}
97 
98 where :math:`\mat{W} \in \mathbb{R}^{n\times n}` is a diagonal matrix containing
99 weights :math:`w_i` for each residual :math:`r_i`
100 
101 The different loss functions will only impact in the weight for each residual
102 during the optimization step.
103 Therefore, the only impact of the choice on the kernel is thorugh its first
104 order derivate.
105 
106 The kernels implemented so far, and the notation has been inspired by the
107 publication: **"Analysis of Robust Functions for Registration Algorithms"**, by
108 Philippe Babin et al.
109 
110 For more information please also see: **"Adaptive Robust Kernels for
111 Non-Linear Least Squares Problems"**, by Nived Chebrolu et al.
112 )");
113  rk.def("weight", &RobustKernel::Weight, "residual"_a,
114  "Obtain the weight for the given residual according to the "
115  "robust kernel model.");
117  m, "RobustKernel", "weight",
118  {{"residual", "value obtained during the optimization problem"}});
119 
120  // CloudViewer.registration.L2Loss
121  py::class_<L2Loss, std::shared_ptr<L2Loss>, PyL2Loss, RobustKernel> l2_loss(
122  m, "L2Loss",
123  R"(
124 The loss :math:`\rho(r)` for a given residual ``r`` is given by:
125 
126 .. math:: \rho(r) = \frac{r^2}{2}
127 
128 The weight :math:`w(r)` for a given residual ``r`` is given by:
129 
130 .. math:: w(r) = 1
131 )");
132  py::detail::bind_default_constructor<L2Loss>(l2_loss);
133  py::detail::bind_copy_functions<L2Loss>(l2_loss);
134  l2_loss.def("__repr__", [](const L2Loss &rk) {
135  (void)rk;
136  return "RobustKernel::L2Loss";
137  });
138 
139  // CloudViewer.registration.L1Loss:RobustKernel
140  py::class_<L1Loss, std::shared_ptr<L1Loss>, PyL1Loss, RobustKernel> l1_loss(
141  m, "L1Loss",
142  R"(
143 The loss :math:`\rho(r)` for a given residual ``r`` is given by:
144 
145 .. math:: \rho(r) = |r|
146 
147 The weight :math:`w(r)` for a given residual ``r`` is given by:
148 
149 .. math:: w(r) = \frac{1}{|r|}
150 )");
151  py::detail::bind_default_constructor<L1Loss>(l1_loss);
152  py::detail::bind_copy_functions<L1Loss>(l1_loss);
153  l1_loss.def("__repr__", [](const L1Loss &rk) {
154  (void)rk;
155  return "RobustKernel::L1Loss";
156  });
157 
158  // CloudViewer.registration.HuberLoss
159  py::class_<HuberLoss, std::shared_ptr<HuberLoss>, PyHuberLoss, RobustKernel>
160  h_loss(m, "HuberLoss",
161  R"(
162 The loss :math:`\rho(r)` for a given residual ``r`` is:
163 
164 .. math::
165  \begin{equation}
166  \rho(r)=
167  \begin{cases}
168  \frac{r^{2}}{2}, & |r| \leq k.\\
169  k(|r|-k / 2), & \text{otherwise}.
170  \end{cases}
171  \end{equation}
172 
173 The weight :math:`w(r)` for a given residual ``r`` is given by:
174 
175 .. math::
176  \begin{equation}
177  w(r)=
178  \begin{cases}
179  1, & |r| \leq k. \\
180  \frac{k}{|r|} , & \text{otherwise}.
181  \end{cases}
182  \end{equation}
183 )");
184  py::detail::bind_copy_functions<HuberLoss>(h_loss);
185  h_loss.def(py::init(
186  [](double k) { return std::make_shared<HuberLoss>(k); }),
187  "k"_a)
188  .def("__repr__",
189  [](const HuberLoss &rk) {
190  return std::string("RobustKernel::HuberLoss with k=") +
191  std::to_string(rk.k_);
192  })
193  .def_readwrite("k", &HuberLoss::k_, "Parameter of the loss");
194 
195  // CloudViewer.registration.CauchyLoss
196  py::class_<CauchyLoss, std::shared_ptr<CauchyLoss>, PyCauchyLoss,
197  RobustKernel>
198  c_loss(m, "CauchyLoss",
199  R"(
200 The loss :math:`\rho(r)` for a given residual ``r`` is:
201 
202 .. math::
203  \begin{equation}
204  \rho(r)=
205  \frac{k^2}{2} \log\left(1 + \left(\frac{r}{k}\right)^2\right)
206  \end{equation}
207 
208 The weight :math:`w(r)` for a given residual ``r`` is given by:
209 
210 .. math::
211  \begin{equation}
212  w(r)=
213  \frac{1}{1 + \left(\frac{r}{k}\right)^2}
214  \end{equation}
215 )");
216  py::detail::bind_copy_functions<CauchyLoss>(c_loss);
217  c_loss.def(py::init([](double k) {
218  return std::make_shared<CauchyLoss>(k);
219  }),
220  "k"_a)
221  .def("__repr__",
222  [](const CauchyLoss &rk) {
223  return std::string("RobustKernel::CauchyLoss with k=") +
224  std::to_string(rk.k_);
225  })
226  .def_readwrite("k", &CauchyLoss::k_, "Parameter of the loss.");
227 
228  // CloudViewer.registration.GMLoss
229  py::class_<GMLoss, std::shared_ptr<GMLoss>, PyGMLoss, RobustKernel> gm_loss(
230  m, "GMLoss",
231  R"(
232 The loss :math:`\rho(r)` for a given residual ``r`` is:
233 
234 .. math::
235  \begin{equation}
236  \rho(r)=
237  \frac{r^2/ 2}{k + r^2}
238  \end{equation}
239 
240 The weight :math:`w(r)` for a given residual ``r`` is given by:
241 
242 .. math::
243  \begin{equation}
244  w(r)=
245  \frac{k}{\left(k + r^2\right)^2}
246  \end{equation}
247 )");
248  py::detail::bind_copy_functions<GMLoss>(gm_loss);
249  gm_loss.def(py::init([](double k) { return std::make_shared<GMLoss>(k); }),
250  "k"_a)
251  .def("__repr__",
252  [](const GMLoss &rk) {
253  return std::string("RobustKernel::GMLoss with k=") +
254  std::to_string(rk.k_);
255  })
256  .def_readwrite("k", &GMLoss::k_, "Parameter of the loss.");
257 
258  // CloudViewer.registration.TukeyLoss:RobustKernel
259  py::class_<TukeyLoss, std::shared_ptr<TukeyLoss>, PyTukeyLoss, RobustKernel>
260  t_loss(m, "TukeyLoss",
261  R"(
262 The loss :math:`\rho(r)` for a given residual ``r`` is:
263 
264 .. math::
265  \begin{equation}
266  \rho(r)=
267  \begin{cases}
268  \frac{k^2\left[1-\left(1-\left(\frac{e}{k}\right)^2\right)^3\right]}{2}, & |r| \leq k. \\
269  \frac{k^2}{2}, & \text{otherwise}.
270  \end{cases}
271  \end{equation}
272 
273 The weight :math:`w(r)` for a given residual ``r`` is given by:
274 
275 .. math::
276  \begin{equation}
277  w(r)=
278  \begin{cases}
279  \left(1 - \left(\frac{r}{k}\right)^2\right)^2, & |r| \leq k. \\
280  0 , & \text{otherwise}.
281  \end{cases}
282  \end{equation}
283 )");
284  py::detail::bind_copy_functions<TukeyLoss>(t_loss);
285  t_loss.def(py::init(
286  [](double k) { return std::make_shared<TukeyLoss>(k); }),
287  "k"_a)
288  .def("__repr__",
289  [](const TukeyLoss &tk) {
290  return std::string("RobustKernel::TukeyLoss with k=") +
291  std::to_string(tk.k_);
292  })
293  .def_readwrite("k", &TukeyLoss::k_,
294  "``k`` Is a tunning constant for the loss.");
295 } // namespace pipelines
296 
297 } // namespace registration
298 } // namespace pipelines
299 } // namespace cloudViewer
double Weight(double residual) const override
virtual double Weight(double residual) const =0
void ClassMethodDocInject(py::module &pybind_module, const std::string &class_name, const std::string &function_name, const std::unordered_map< std::string, std::string > &map_parameter_body_docs)
Definition: docstring.cpp:27
PyRobustKernelT< L2Loss > PyL2Loss
PyRobustKernelT< HuberLoss > PyHuberLoss
PyRobustKernelT< GMLoss > PyGMLoss
PyRobustKernelT< CauchyLoss > PyCauchyLoss
PyRobustKernelT< L1Loss > PyL1Loss
PyRobustKernelT< TukeyLoss > PyTukeyLoss
Generic file read and write utility for python interface.
std::string to_string(const T &n)
Definition: Common.h:20