-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Move ComputeP from gmmreg_utils to utils/em_utils
- Loading branch information
Showing
4 changed files
with
65 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
#include "em_utils.h" | ||
|
||
namespace gmmreg { | ||
|
||
template <typename T> | ||
void ComputeP(const vnl_matrix<T>& x, const vnl_matrix<T>& y, vnl_matrix<T>& P, | ||
T& E, T sigma, int outliers) { | ||
T k; | ||
k = -2 * sigma * sigma; | ||
|
||
vnl_vector<T> column_sum; | ||
int m = x.rows(); | ||
int s = y.rows(); | ||
int d = x.cols(); | ||
column_sum.set_size(s); | ||
column_sum.fill(0); | ||
T outlier_term = outliers * pow((2 * sigma * sigma * 3.1415926), 0.5 * d); | ||
#pragma omp for | ||
for (int i = 0; i < m; ++i) { | ||
for (int j = 0; j < s; ++j) { | ||
T r = 0; | ||
for (int t = 0; t < d; ++t) { | ||
r += (x(i, t) - y(j, t)) * (x(i, t) - y(j, t)); | ||
} | ||
P(i, j) = exp(r / k); | ||
column_sum[j] += P(i, j); | ||
} | ||
} | ||
|
||
if (outliers != 0) { | ||
#pragma omp for | ||
for (int i = 0; i < s; ++i) column_sum[i] += outlier_term; | ||
} | ||
if (column_sum.min_value() > (1e-12)) { | ||
E = 0; | ||
#pragma omp for | ||
for (int i = 0; i < s; ++i) { | ||
for (int j = 0; j < m; ++j) { | ||
P(j, i) = P(j, i) / column_sum[i]; | ||
} | ||
E -= log(column_sum[i]); | ||
} | ||
} else { | ||
P.empty(); | ||
} | ||
} | ||
|
||
} // namespace gmmreg |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#ifndef GMMREG_UTILS_EM_UTILS_H_ | ||
#define GMMREG_UTILS_EM_UTILS_H_ | ||
|
||
#include <vnl/vnl_matrix.h> | ||
|
||
namespace gmmreg { | ||
|
||
template <typename T> | ||
void ComputeP(const vnl_matrix<T>& x, const vnl_matrix<T>& y, vnl_matrix<T>& P, | ||
T& E, T sigma, int outliers); | ||
|
||
} // namespace gmmreg | ||
|
||
#include "em_utils.cc" | ||
|
||
#endif // GMMREG_UTILS_EM_UTILS_H_ |