forked from tbright17/kaldi-dnn-ali-gop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmm-gop.cc
executable file
·365 lines (307 loc) · 12.7 KB
/
gmm-gop.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
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
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
// gop/gop-gmm.cc
// Copyright 2016-2017 Junbo Zhang
// Ming Tu
// This program based on Kaldi (https://github.com/kaldi-asr/kaldi).
// However, this program is NOT UNDER THE SAME LICENSE of Kaldi's.
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// version 2 as published by the Free Software Foundation;
//
// This program 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.
#include <algorithm>
#include <limits>
#include <string>
#include <vector>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "fstext/fstext-utils.h"
#include "decoder/decoder-wrappers.h"
#include "gmm/decodable-am-diag-gmm.h"
#include "lat/kaldi-lattice.h"
#include "hmm/hmm-utils.h"
#include "gop/gmm-gop.h"
namespace kaldi {
typedef typename fst::StdArc Arc;
typedef typename Arc::StateId StateId;
typedef typename Arc::Weight Weight;
void GmmGop::Init(std::string &tree_in_filename,
std::string &model_in_filename,
std::string &lex_in_filename) {
bool binary;
Input ki(model_in_filename, &binary);
tm_.Read(ki.Stream(), binary);
am_.Read(ki.Stream(), binary);
ReadKaldiObject(tree_in_filename, &ctx_dep_);
fst::VectorFst<fst::StdArc> *lex_fst = fst::ReadFstKaldi(lex_in_filename);
std::vector<int32> disambig_syms;
TrainingGraphCompilerOptions gopts;
gc_ = new TrainingGraphCompiler(tm_, ctx_dep_, lex_fst, disambig_syms, gopts);
for (size_t i = 0; i < tm_.NumTransitionIds(); i++) {
pdfid_to_tid[tm_.TransitionIdToPdf(i)] = i;
}
}
BaseFloat GmmGop::Decode(fst::VectorFst<fst::StdArc> &fst,
DecodableAmDiagGmmScaled &decodable,
std::vector<int32> *align) {
FasterDecoderOptions decode_opts;
decode_opts.beam = 2000; // number of beams for decoding. Larger, slower and more successful alignments. Smaller, more unsuccessful alignments.
FasterDecoder decoder(fst, decode_opts);
decoder.Decode(&decodable);
if (! decoder.ReachedFinal()) {
KALDI_WARN << "Did not successfully decode.";
}
fst::VectorFst<LatticeArc> decoded;
decoder.GetBestPath(&decoded);
std::vector<int32> osymbols;
LatticeWeight weight;
GetLinearSymbolSequence(decoded, align, &osymbols, &weight);
BaseFloat likelihood = -(weight.Value1()+weight.Value2());
return likelihood;
}
BaseFloat GmmGop::ComputeGopNumera(DecodableAmDiagGmmScaled &decodable,
std::vector<int32> &align,
MatrixIndexT start_frame,
int32 size) {
KALDI_ASSERT(start_frame + size <= align.size());
BaseFloat likelihood = 0;
for (MatrixIndexT frame = start_frame; frame < start_frame + size; frame++) {
likelihood += decodable.LogLikelihood(frame, align[frame]);
}
return likelihood;
}
BaseFloat GmmGop::ComputeGopNumeraViterbi(DecodableAmDiagGmmScaled &decodable,
int32 phone_l, int32 phone, int32 phone_r) {
KALDI_ASSERT(ctx_dep_.ContextWidth() == 3);
KALDI_ASSERT(ctx_dep_.CentralPosition() == 1);
std::vector<int32> phoneseq(3);
phoneseq[0] = phone_l;
phoneseq[1] = phone;
phoneseq[2] = phone_r;
fst::VectorFst<fst::StdArc> fst;
StateId cur_state = fst.AddState();
fst.SetStart(cur_state);
for (size_t c = 0; c < tm_.GetTopo().NumPdfClasses(phone); c++) {
int32 pdf_id;
//KALDI_ASSERT(ctx_dep_.Compute(phoneseq, c, &pdf_id));
if (!ctx_dep_.Compute(phoneseq, c, &pdf_id)) {
KALDI_ERR << "Failed to obtain pdf_id";
}
int32 tid = pdfid_to_tid[pdf_id];
StateId next_state = fst.AddState();
Arc arc(tid, 0, Weight::One(), next_state);
fst.AddArc(cur_state, arc);
cur_state = next_state;
Arc arc_selfloop(tid, 0, Weight::One(), cur_state);
fst.AddArc(cur_state, arc_selfloop);
}
fst.SetFinal(cur_state, Weight::One());
return Decode(fst, decodable);
}
BaseFloat GmmGop::ComputeGopDenominViterbi(DecodableAmDiagGmmScaled &decodable,
int32 phone_l, int32 phone_r) {
KALDI_ASSERT(ctx_dep_.ContextWidth() == 3);
KALDI_ASSERT(ctx_dep_.CentralPosition() == 1);
std::vector<int32> phoneseq(3);
phoneseq[0] = phone_l;
phoneseq[2] = phone_r;
fst::VectorFst<fst::StdArc> fst;
StateId start_state = fst.AddState();
fst.SetStart(start_state);
const std::vector<int32> &phone_syms = tm_.GetPhones();
for (size_t i = 0; i < phone_syms.size(); i++) {
int32 phone = phone_syms[i];
phoneseq[1] = phone;
const int pdfclass_num = tm_.GetTopo().NumPdfClasses(phone);
StateId cur_state = start_state;
for (size_t c = 0; c < pdfclass_num; c++) {
int32 pdf_id;
//KALDI_ASSERT(ctx_dep_.Compute(phoneseq, c, &pdf_id));
if (!ctx_dep_.Compute(phoneseq, c, &pdf_id)) {
KALDI_ERR << "Failed to obtain pdf_id";
}
int32 tid = pdfid_to_tid[pdf_id];
StateId next_state = fst.AddState();
Arc arc(tid, 0, Weight::One(), next_state);
fst.AddArc(cur_state, arc);
cur_state = next_state;
Arc arc_selfloop(tid, 0, Weight::One(), cur_state);
fst.AddArc(cur_state, arc_selfloop);
}
Arc arc(0, 0, Weight::One(), start_state);
fst.AddArc(cur_state, arc);
}
fst.SetFinal(start_state, Weight::One());
return Decode(fst, decodable);
}
Vector<BaseFloat> GmmGop::ComputePhonemesConf(DecodableAmDiagGmmScaled &decodable,
int32 phone_l, int32 phone_r,
MatrixIndexT start_frame,
int32 size) {
// KALDI_ASSERT(ctx_dep_.ContextWidth() == 3);
// KALDI_ASSERT(ctx_dep_.CentralPosition() == 1);
// std::vector<int32> phoneseq(3);
// phoneseq[0] = phone_l;
// phoneseq[2] = phone_r;
// const std::vector<int32> &phone_syms = tm_.GetPhones();
// Vector<BaseFloat> likelihood(phone_syms.size());
// for (size_t i = 0; i < phone_syms.size(); i++) {
// int32 phone = phone_syms[i];
// phoneseq[1] = phone;
// const int pdfclass_num = tm_.GetTopo().NumPdfClasses(phone);
// BaseFloat phn_likelihood = 0.0;
// for (MatrixIndexT frame = start_frame; frame < start_frame + size; frame++) {
// Vector<BaseFloat> temp_likelihood(pdfclass_num);
// for (size_t c = 0; c < pdfclass_num; c++) {
// int32 pdf_id;
// KALDI_ASSERT(ctx_dep_.Compute(phoneseq, c, &pdf_id));
// int32 tid = pdfid_to_tid[pdf_id];
// temp_likelihood(c) = decodable.LogLikelihood(frame, tid+1);
// }
// phn_likelihood += temp_likelihood.LogSumExp(5);
// }
// likelihood(i) = phn_likelihood / size;
// }
// return likelihood;
KALDI_ASSERT(ctx_dep_.ContextWidth() == 3);
KALDI_ASSERT(ctx_dep_.CentralPosition() == 1);
std::vector<int32> phoneseq(3);
phoneseq[0] = phone_l;
phoneseq[2] = phone_r;
const std::vector<int32> &phone_syms = tm_.GetPhones();
Vector<BaseFloat> likelihood(phone_syms.size());
for (size_t i = 0; i < phone_syms.size(); i++) {
fst::VectorFst<fst::StdArc> fst;
StateId start_state = fst.AddState();
fst.SetStart(start_state);
int32 phone = phone_syms[i];
phoneseq[1] = phone;
const int pdfclass_num = tm_.GetTopo().NumPdfClasses(phone);
StateId cur_state = start_state;
for (size_t c = 0; c < pdfclass_num; c++) {
int32 pdf_id;
//KALDI_ASSERT(ctx_dep_.Compute(phoneseq, c, &pdf_id));
if (!ctx_dep_.Compute(phoneseq, c, &pdf_id)) {
KALDI_ERR << "Failed to obtain pdf_id";
}
int32 tid = pdfid_to_tid[pdf_id];
StateId next_state = fst.AddState();
Arc arc(tid, 0, Weight::One(), next_state);
fst.AddArc(cur_state, arc);
cur_state = next_state;
Arc arc_selfloop(tid, 0, Weight::One(), cur_state);
fst.AddArc(cur_state, arc_selfloop);
}
fst.SetFinal(cur_state, Weight::One());
likelihood(i) = Decode(fst, decodable)/size;
}
return likelihood;
}
void GmmGop::ComputeFramePhonemesConf(DecodableAmDiagGmmScaled &decodable,
int32 phone_l, int32 phone_r,
MatrixIndexT start_frame,
int32 size) {
KALDI_ASSERT(ctx_dep_.ContextWidth() == 3);
KALDI_ASSERT(ctx_dep_.CentralPosition() == 1);
std::vector<int32> phoneseq(3);
phoneseq[0] = phone_l;
phoneseq[2] = phone_r;
const std::vector<int32> &phone_syms = tm_.GetPhones();
for (MatrixIndexT frame = start_frame; frame < start_frame + size; frame++) {
Vector<BaseFloat> likelihood(phone_syms.size());
for (size_t i = 0; i < phone_syms.size(); i++) {
int32 phone = phone_syms[i];
phoneseq[1] = phone;
const int pdfclass_num = tm_.GetTopo().NumPdfClasses(phone);
Vector<BaseFloat> temp_likelihood(pdfclass_num);
for (size_t c = 0; c < pdfclass_num; c++) {
int32 pdf_id;
//KALDI_ASSERT(ctx_dep_.Compute(phoneseq, c, &pdf_id));
if (!ctx_dep_.Compute(phoneseq, c, &pdf_id)) {
KALDI_ERR << "Failed to obtain pdf_id";
}
int32 tid = pdfid_to_tid[pdf_id];
temp_likelihood(c) = decodable.LogLikelihood(frame, tid+1);
}
likelihood(i) = temp_likelihood.LogSumExp(5);
}
phonemes_frame_conf_.CopyRowFromVec(likelihood, frame);
}
}
void GmmGop::GetContextFromSplit(std::vector<std::vector<int32> > split,
int32 index, int32 &phone_l, int32 &phone, int32 &phone_r) {
KALDI_ASSERT(index < split.size());
phone_l = (index > 0) ? tm_.TransitionIdToPhone(split[index-1][0]) : 1;
phone = tm_.TransitionIdToPhone(split[index][0]);
phone_r = (index < split.size() - 1) ? tm_.TransitionIdToPhone(split[index+1][0]): 1;
}
void GmmGop::Compute(const Matrix<BaseFloat> &feats,
const std::vector<int32> &transcript,
BaseFloatMatrixWriter *phn_conf_writer,
BaseFloatMatrixWriter *phn_frame_conf_writer) {
// Align
fst::VectorFst<fst::StdArc> ali_fst;
gc_->CompileGraphFromText(transcript, &ali_fst);
DecodableAmDiagGmmScaled ali_decodable(am_, tm_, feats, 1.0); // 1.0 is the acoustic scale
//std::vector<int32> align;
Decode(ali_fst, ali_decodable, &alignment_);
KALDI_ASSERT(feats.NumRows() == alignment_.size());
// GOP
const std::vector<int32> &phone_syms = tm_.GetPhones();
std::vector<std::vector<int32> > split;
SplitToPhones(tm_, alignment_, &split);
gop_result_.Resize(split.size());
phones_.resize(split.size());
phones_loglikelihood_.Resize(split.size());
phonemes_conf_.Resize(split.size(),phone_syms.size());
phonemes_frame_conf_.Resize(feats.NumRows(), phone_syms.size());
int32 frame_start_idx = 0;
for (MatrixIndexT i = 0; i < split.size(); i++) {
SubMatrix<BaseFloat> feats_in_phone = feats.Range(frame_start_idx, split[i].size(),
0, feats.NumCols());
const Matrix<BaseFloat> features(feats_in_phone);
DecodableAmDiagGmmScaled split_decodable(am_, tm_, features, 1.0); // 1.0 is the acoustic scale
int32 phone, phone_l, phone_r;
GetContextFromSplit(split, i, phone_l, phone, phone_r);
bool use_viterbi_numera = true;
BaseFloat gop_numerator = use_viterbi_numera ?
ComputeGopNumeraViterbi(split_decodable, phone_l, phone, phone_r):
ComputeGopNumera(ali_decodable, alignment_,
frame_start_idx, split[i].size());
BaseFloat gop_denominator = ComputeGopDenominViterbi(split_decodable, phone_l, phone_r);
gop_result_(i) = (gop_numerator - gop_denominator) / split[i].size();
phones_[i] = phone;
phones_loglikelihood_(i) = gop_numerator;
if (phn_conf_writer != NULL && phn_conf_writer->IsOpen()) {
phonemes_conf_.CopyRowFromVec(ComputePhonemesConf(split_decodable,phone_l, phone_r,frame_start_idx, split[i].size()), i);
}
if (phn_frame_conf_writer != NULL && phn_frame_conf_writer->IsOpen()) {
ComputeFramePhonemesConf(ali_decodable, phone_l, phone_r,frame_start_idx, split[i].size());
}
frame_start_idx += split[i].size();
}
}
Vector<BaseFloat>& GmmGop::Result() {
return gop_result_;
}
Vector<BaseFloat>& GmmGop::get_phn_ll() {
return phones_loglikelihood_;
}
std::vector<int32>& GmmGop::get_alignment() {
return alignment_;
}
std::vector<int32>& GmmGop::Phonemes() {
return phones_;
}
Matrix<BaseFloat>& GmmGop::PhonemesConf() {
return phonemes_conf_;
}
Matrix<BaseFloat>& GmmGop::PhonemesFrameConf() {
return phonemes_frame_conf_;
}
} // End namespace kaldi