-
Notifications
You must be signed in to change notification settings - Fork 0
/
ModelReader.js
307 lines (237 loc) · 9.04 KB
/
ModelReader.js
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
//@Compute GMM
// Reads phoneme model jnas-mono-16mix-gid.hmmdefs
// Calculates GMM
// Defines macros
// https://github.com/julius-speech/julius/blob/6d135a686a74376495a7a6f55d3d67df54186f83/libsent/include/sent/stddefs.h
var LOG_ZERO = -1000000;
var LOG_ADDMIN = -13.815510558;
var INV_LOG_TEN = 0.434294482;
var max = LOG_ZERO;
var min = 0;
function Model(URL, mfccDim){
this.model = {};
this.prob = {};
this.phonemeP = {};
// Several frames
this.probOverTime = {};
this.nFrame = 20; // 20 default if GMM_VAD - gmm_margin
// Compute several states?
this.nStates = 1;
// MFCC comparison dimension (default 25)
this.mfccDim = mfccDim || 12;
this.ready = false;
// Julius log table for GMM computation
this.makeLogTbl();
// Read GMM model
this.read(URL);
}
// Calculate mixtures
Model.prototype.compute = function(mfcc){
this.maxProb = LOG_ZERO;
this.minProb = 0;
this.result = "";
this.resultLow = "";
var phonemes = Object.keys(this.model);
var prob = this.prob;
var phonemeP = this.phonemeP;
// gmm_proceed
// outprob_state_nocache
// gmm_calc_mix
// gmm_gprune_safe
// gmm_compute_g_safe
// https://github.com/julius-speech/julius/blob/6d135a686a74376495a7a6f55d3d67df54186f83/libjulius/src/gmm.c
for (var p = 0; p < phonemes.length; p++){
var ph = phonemes[p]; // key name
var phModel = this.model[ph];
prob[ph] = [];
phonemeP[ph] = {};
var gmm_score = this.gmm_score = [];
// 3 States? Only one in Julius? d->s[1]
// outprob_state_nocache(gc, mfcc->f, d->s[1], mfcc->param);
for (var s = 0; s < this.nStates; s++){
gmm_score[s] = 0;
for (var i = 0; i < phModel[s].length; i++){ // phModel[s].length = 16
var g = phModel[s][i];
var tmp = g.gconst;
//for (var j = 0; j < mfcc.length; j++){ // mfcc.length = g.mean.length = 25
for (var j = 0; j < this.mfccDim; j++){ // No deltas! // *************** MODIFICATION!! Deltas are computed every 16ms, not as Julius
var x = mfcc[j]-g.mean[j];
tmp += x * x * g.variance[j];
//-------------------------------------------------------
}
tmp *= -0.5;
// In Julius, the stored values for each gaussian mixture (1 of the 16) are LOGPROB
// They use a table to compute the logarithm
// static LOGPROB tbl[TBLSIZE]; ///< Table of @f$\log (1+e^x)@f$
// Check:
// https://github.com/julius-speech/julius/blob/6d135a686a74376495a7a6f55d3d67df54186f83/libsent/src/phmm/addlog.c
prob[ph][i] = tmp;
}
// - Adds to each score a mixture weight from HMM: "state->pdf[s]->bweight[gc->OP_calced_id[i]];"
for (var i = 0; i < phModel.length; i++){
prob[ph][i]+= phModel[i][s].bweight;
}
// Sort probabilities (higher at the end)
// Should be done before adding bweight, but change might be trivial (bweight = (-2,-3))
prob[ph].sort(function(a, b){return a-b});
// addlog_array
var x = 0;
var y = LOG_ZERO;
var tmp = 0;
// Add probabilities of the 16 gaussians
// In Julius, just the best first 10
for (var i = 6; i < phModel[s].length; i++){
// addlog_array -> a unique value for each of the 16 gaussians
x = prob[ph][i];
if (x > y){
tmp = x;
x = y;
y = tmp;
}
if ((tmp = x-y) < LOG_ADDMIN) continue;
else{
var idx = Math.floor(Math.abs(-tmp * this.tMag + 0.5));
y += this.tbl[idx];
}
}
// gmm_calc_mix
var logsum = y;//phonemeP[ph] = y; // Unique value
// - If states, add all 3 unique state values to check if valid stream or lowest
// If a state prob sum is bigger than LOG_ZERO use it.
//if (phonemeP[ph] == 0.0 || phonemeP[ph] <= LOG_ZERO) phonemeP[ph] = LOG_ZERO;
//else phonemeP[ph] *= INV_LOG_TEN;
if (logsum == 0.0 || logsum <= LOG_ZERO) logsum = LOG_ZERO;
else logsum *= INV_LOG_TEN;
// Return prob[ph] * INV_LOG_TEN in gmm_calc_mix:
// https://github.com/julius-speech/julius/blob/6d135a686a74376495a7a6f55d3d67df54186f83/libjulius/src/gmm.c
// outprob_state_nocache
// gmm_proceed
gmm_score[s] = logsum;
//phonemeP[ph]+=logsum;
} // End FOR state
// Find best of each state and compute confidence measure (CM)
// gmm_end compute maxProb
var tmpMax = LOG_ZERO;
for (s = 0; s <this.nStates; s++){
if (tmpMax < gmm_score[s]){
phonemeP[ph].prob = gmm_score[s];
tmpMax = gmm_score[s];
}
}
// Several frames
// Shift
this.probOverTime[ph].shift();
// Store new prob (last item)
this.probOverTime[ph][this.nFrame-1] = tmpMax;
// Compute addition
phonemeP[ph].prob = 0;
for (var i = 0; i< this.nFrame; i++)
phonemeP[ph].prob+=this.probOverTime[ph][i];
if (phonemeP[ph].prob > this.maxProb){
this.maxProb = phonemeP[ph].prob;
this.result = ph;
this.confidence = phonemeP[ph].conf;
}if (phonemeP[ph].prob < this.minProb){
this.minProb = phonemeP[ph].prob;
this.resultLow = ph;
}
} // End for phonemes
// Compute confidence measure (CM)
// gmm_proceed -> for several frames add scores
// gmm_end -> compute confidence measure
var sum = 0;
for (var p = 0; p < phonemes.length; p++){
var ph = phonemes[p]; // key name
sum += Math.pow(10, 0.05 * (phonemeP[ph].prob-this.maxProb));
}
this.confidence = 1/sum;
//phonemeP[ph].conf = sum;
//console.log("PHONEME:", this.result, ", CM:", sum);
}
Model.prototype.makeLogTbl = function(){
this.tbl = []; // length = 500000 = tblSize
this.tblSize = 500000; // Table size (precision depends on this)
this.vRange = 15; // Must be larger than -LOG_ADDMIN
this.tMag = 33333.3333 // TBLSIZE / VRANGE
var f = 0;
for (var i = 0; i<this.tblSize; i++){
f = - this.vRange * i / this.tblSize;
this.tbl[i] = Math.log(1+Math.exp(f));
}
}
Model.prototype.read = function(url){
var that = this;
var req = new XMLHttpRequest();
req.open('GET', url, true);
req.onload = function()
{
var response = this.response;
if(this.status < 200 || this.status >= 300)
return console.error("File not found: ", this.status);
console.log("Finised loading.");
that.ready = that.parse(this.response);
return;
}
req.addEventListener("progress", function(e){
var progress = e.loaded/e.total;
console.log("Download progress: ", progress);
});
req.send();
}
Model.prototype.parse = function(data){
// Each new phoneme starts with ~h
var phonemeData = data.split("~h");
phonemeData.shift();
phonemeData.shift();
// There are 42 phonemes (approx) sil stands for silence
for (var i = 0; i<phonemeData.length; i++){
// The name of the phoneme is inside quotation marks
var tmp = phonemeData[i].split('\"');
var phoneme = tmp[1];
// Store 16 GMM for each phoneme (there are 16x3 states)
this.model[phoneme] = [];
// Each letter has 3 states. Each state has 16 GMM (mixture, mean, variance). 48 GMM in total.
var phonemeContent = tmp[2];
// 3 states
var states = phonemeContent.split("<STATE>");
states.shift();
for (var s = 0; s < 3; s++){
var stateContent = states[s];
this.model[phoneme][s] = [];
// 16 mixtures
var mixContent = stateContent.split("<MIXTURE>");
mixContent.shift(); // First part is not a mixture
for (var j = 0; j<mixContent.length; j++){
var mix = {};
var mixData = mixContent[j].split("\n");
//https://github.com/julius-speech/julius/blob/6d135a686a74376495a7a6f55d3d67df54186f83/libsent/src/hmminfo/rdhmmdef_mpdf.c
// mixture is bweight in Julius
mix.bweight = Math.log(parseFloat(mixData[0].split(" ")[2]));
mix.mean = mixData[2].split(" ");
mix.mean.shift();
// Variance is var->vec[]
mix.variance = mixData[4].split(" ");
mix.variance.shift();
for (var k = 0; k < mix.mean.length; k++){
mix.mean[k] = parseFloat(mix.mean[k]);
// Variance is inversed in Julius
mix.variance[k] = 1/parseFloat(mix.variance[k]);
}
mix.gconst = parseFloat(mixData[5].split(" ")[1]);
this.model[phoneme][s].push(mix);
}
}
}
// Prepare previous probabilities array
var phonemes = Object.keys(this.model);
// Use several frames to compute probs (init) TODOOOOOO
for (var p = 0; p < phonemes.length; p++){
var ph = phonemes[p]; // key name
this.probOverTime[ph] = [];
for (var i = 0; i<this.nFrame; i++)
this.probOverTime[ph][i] = LOG_ZERO;
}
console.log("Finised parsing.");//, this.model);
return true;
//console.log(this.model);
}