forked from falvaro/seshat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmm.cc
137 lines (106 loc) · 3.03 KB
/
gmm.cc
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
/*Copyright 2014 Francisco Alvaro
This file is part of SESHAT.
SESHAT 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.
SESHAT 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 SESHAT. If not, see <http://www.gnu.org/licenses/>.
*/
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <cfloat>
#include "gmm.h"
#define PI 3.14159265359
using namespace std;
GMM::GMM( char *model ) {
loadModel( model );
}
void GMM::loadModel( char *str ) {
FILE *fd = fopen(str, "r");
if( !fd ) {
fprintf(stderr, "Error loading GMM model file '%s'\n", str);
exit(-1);
}
//Read parameters
fscanf(fd, "%d %d %d", &C, &D, &G);
//Read prior probabilities
prior = new float[C];
for(int i=0; i<C; i++)
fscanf(fd, "%f", &prior[i]);
invcov = new float*[C*G];
mean = new float*[C*G];
weight = new float*[C];
det = new float[C*G];
//Read a GMM for each class
for(int c=0; c<C; c++) {
//Read diagonal covariances
for(int i=0; i<G; i++) {
invcov[c*G+i] = new float[D];
det[c*G+i] = 1.0;
for(int j=0; j<D; j++) {
fscanf(fd, "%f", &invcov[c*G+i][j]);
//Compute determinant of convariance matrix (diagonal)
det[c*G+i] *= invcov[c*G+i][j];
//Save the inverse of the convariance to save future computations
if( invcov[c*G+i][j] == 0.0 ) {
fprintf(stderr, "Warning: covariance value equal to zero in GMM\n");
invcov[c*G+i][j] = 1.0/1.0e-10;
}
else
invcov[c*G+i][j] = 1.0/invcov[c*G+i][j];
}
}
//Read means
for(int i=0; i<G; i++) {
mean[c*G+i] = new float[D];
for(int j=0; j<D; j++)
fscanf(fd, "%f", &mean[c*G+i][j]);
}
//Read mixture weights
weight[c] = new float[G];
for(int i=0; i<G; i++)
fscanf(fd, "%f", &weight[c][i]);
}
fclose(fd);
}
//Probability density function
float GMM::pdf(int c, float *v) {
float pr = 0.0;
for(int i=0; i<G; i++) {
float exponent = 0.0;
for(int j=0; j<D; j++)
exponent += (v[j] - mean[c*G+i][j]) * invcov[c*G+i][j] * (v[j] - mean[c*G+i][j]);
exponent *= -0.5;
pr += weight[c][i] * pow(2 * PI, -D/2.0) * pow(det[c*G+i], -0.5) * exp( exponent );
}
return prior[c] * pr;
}
GMM::~GMM() {
for(int c=0; c<C; c++) {
for(int i=0; i<G; i++) {
delete[] invcov[c*G+i];
delete[] mean[c*G+i];
}
delete[] weight[c];
}
delete[] det;
delete[] prior;
delete[] invcov;
delete[] mean;
delete[] weight;
}
void GMM::posterior(float *x, float *pr) {
float total=0.0;
for(int c=0; c<C; c++) {
pr[c] = pdf(c, x);
total += pr[c];
}
for(int c=0; c<C; c++)
pr[c] /= total;
}