-
Notifications
You must be signed in to change notification settings - Fork 67
/
MaxSumMixture.h
158 lines (132 loc) · 4.83 KB
/
MaxSumMixture.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/***************************************************************************
* libRSF - A Robust Sensor Fusion Library
*
* Copyright (C) 2023 Chair of Automation Technology / TU Chemnitz
* For more information see https://www.tu-chemnitz.de/etit/proaut/libRSF
*
* libRSF is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* libRSF is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with libRSF. If not, see <http://www.gnu.org/licenses/>.
*
* Author: Tim Pfeifer ([email protected])
***************************************************************************/
/**
* @file MaxSumMixture.h
* @author Tim Pfeifer
* @date 07.01.2020
* @brief A vectorized, optimizer-friendly Gaussian mixture model.
* @copyright GNU Public License.
*
*/
#ifndef MAXSUMMIXTURE_H
#define MAXSUMMIXTURE_H
#include "ErrorModel.h"
#include "GaussianMixture.h"
#include "../NumericalRobust.h"
namespace libRSF
{
extern const double DampingFactor;
/** \brief The robust Max-Sum-Mixture error model
*
* \param Mixture Underlying mixture distribution
*
*/
template <int Dim, typename MixtureType>
class MaxSumMixture : public ErrorModel <Dim, Dim+1>
{
public:
MaxSumMixture()
{
this->clear();
}
virtual ~MaxSumMixture() = default;
explicit MaxSumMixture(const MixtureType &Mixture)
{
this->addMixture_(Mixture);
}
void clear()
{
Normalization_ = 0;
Mixture_.clear();
}
template <typename T>
bool weight(const VectorT<T, Dim> &RawError, T* Error) const
{
if(this->Enable_)
{
/** calculate linear errors and scalings */
const int NumberOfComponents = Mixture_.getNumberOfComponents();
MatrixT<T, Dynamic, 1> Scalings(NumberOfComponents);
MatrixT<T, Dynamic, Dim> LinearExponents(NumberOfComponents, Dim);
/** calculate component-wise */
for(int nComponent = 0; nComponent < NumberOfComponents; ++nComponent)
{
LinearExponents.row(nComponent) = Mixture_.template getExponentialPartOfComponent<T>(nComponent, RawError);
Scalings(nComponent) = T(Mixture_.template getLinearPartOfComponent<T>(nComponent, RawError));
}
/** apply the LSE */
const VectorT<T, Dim+1> LSE = VectorizedLogSumExp(LinearExponents, Scalings);
/** map the error pointer to a matrix */
VectorRef<T, Dim+1> ErrorMap(Error);
ErrorMap.template head<Dim>() = LSE.template head<Dim>(); /**< linear */
/** nonlinear part is not required if there is only one component */
if (NumberOfComponents > 1)
{
/** to prevent numerical issues close to zero, we set a lower bound of the following term */
ErrorMap(Dim) = sqrt(ceres::fmax(-2.0 * (LSE(Dim) - log(Normalization_ + DampingFactor)), T(1e-20))); /**< non-linear */
}
else
{
ErrorMap(Dim) = T(0.0); /**< cancel non-linear */
}
/** catch bad numerical cases (extreme covariances in a unimportant component) */
if (ceres::isfinite(ErrorMap(Dim)) == false)
{
ErrorMap(Dim) = T(0.0); /**< cancel non-linear */
}
}
else
{
/** pass raw error trough */
VectorRef<T, Dim> ErrorMap(Error);
ErrorMap = RawError;
/** set unused dimension to 0 */
Error[Dim] = T(0.0);
}
return true;
}
MixtureType getMixture()
{
return Mixture_;
}
private:
void addMixture_(const MixtureType &Mixture)
{
Mixture_ = Mixture;
const int NumberOfComponents = Mixture.getNumberOfComponents();
Normalization_ = Mixture.getMaximumOfComponent(0);
for(int nComponent = 1; nComponent < static_cast<int>(NumberOfComponents); ++nComponent)
{
Normalization_ = std::max(Normalization_, Mixture.getMaximumOfComponent(nComponent));
}
Normalization_ *= NumberOfComponents;
}
MixtureType Mixture_;
double Normalization_{0};
};
template <int Dim>
using MaxSumMix = MaxSumMixture<Dim, GaussianMixture<Dim>>;
using MaxSumMix1 = MaxSumMixture<1, GaussianMixture<1>>;
using MaxSumMix2 = MaxSumMixture<2, GaussianMixture<2>>;
using MaxSumMix3 = MaxSumMixture<3, GaussianMixture<3>>;
}
#endif // MAXSUMMIXTURE_H