forked from tbright17/kaldi-dnn-ali-gop
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgmm-gop2.cc
executable file
·300 lines (257 loc) · 10.3 KB
/
gmm-gop2.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
// 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 <map>
#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 "lat/lattice-functions.h"
#include "hmm/hmm-utils.h"
#include "gop/gmm-gop2.h"
namespace kaldi {
typedef typename fst::StdArc Arc;
typedef typename Arc::StateId StateId;
typedef typename Arc::Weight Weight;
void GmmGop2::Init(std::string &tree_in_filename,
std::string &model_in_filename,
std::string &lex_in_filename,
std::string &l1_model_in_filename) {
bool binary, l1_binary;
Input ki(model_in_filename, &binary);
Input l1_ki(l1_model_in_filename, &l1_binary);
tm_.Read(ki.Stream(), binary);
am_.Read(ki.Stream(), binary);
l1_tm_.Read(l1_ki.Stream(), l1_binary);
l1_am_.Read(l1_ki.Stream(), l1_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 GmmGop2::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 GmmGop2::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 GmmGop2::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 GmmGop2::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);
}
BaseFloat GmmGop2::ComputeL1Gop(LatticeFasterDecoder &decoder, DecodableAmDiagGmmScaled &decodable,
int32 index)
{
decoder.Decode(&decodable);
if (! decoder.ReachedFinal()) {
KALDI_WARN << "Did not successfully decode.";
}
fst::VectorFst<LatticeArc> decoded;
decoder.GetBestPath(&decoded);
std::vector<int32> alignment;
std::vector<int32> words;
LatticeWeight weight;
GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
std::vector<int32> phonemes(alignment.size());
int32 num_tids = l1_tm_.NumTransitionIds();
std::vector<BaseFloat> post;
for (int32 i = 0; i < alignment.size(); i++) {
phonemes[i] = l1_tm_.TransitionIdToPhone(alignment[i]);
}
int32 max = 0;
int32 most_common = -1;
std::map<int32,int32> m;
std::vector<int32>::iterator vi = phonemes.begin();
for (; vi != phonemes.end(); vi++) {
m[*vi]++;
if (m[*vi] > max) {
max = m[*vi];
most_common = *vi;
}
}
l1_phnseq_[index] = most_common;
for (int32 i = 0; i < alignment.size(); i++) {
if (phonemes[i] == most_common) {
Vector<BaseFloat> log_like_all(num_tids);
std::vector<BaseFloat> log_like_phn;
for (int32 j = 0; j<num_tids;j++) {
log_like_all(j) = decodable.LogLikelihood(i, j+1);
if (l1_tm_.TransitionIdToPhone(j+1) == phonemes[i]) {
log_like_phn.push_back(decodable.LogLikelihood(i, j+1));
}
}
Vector<BaseFloat> log_like_this(log_like_phn.size());
for (int32 j = 0;j<log_like_phn.size();j++) {
log_like_this(j) = log_like_phn[j];
}
post.push_back(log_like_this.LogSumExp(5) - log_like_all.LogSumExp(5));
}
}
return std::accumulate( post.begin(), post.end(), 0.0)/post.size();
}
void GmmGop2::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 GmmGop2::Compute(const Matrix<BaseFloat> &feats,
const std::vector<int32> &transcript,
const std::string l1_fst_in_str) {
// 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> alignment;
Decode(ali_fst, ali_decodable, &alignment);
KALDI_ASSERT(feats.NumRows() == alignment.size());
// L1 graph
fst::Fst<fst::StdArc> *l1_decode_fst = fst::ReadFstKaldiGeneric(l1_fst_in_str);
LatticeFasterDecoderConfig config;
config.beam = 500;
LatticeFasterDecoder l1_decoder(*l1_decode_fst, config);
// GOP
const std::vector<int32> &phone_syms = tm_.GetPhones();
const std::vector<int32> &l1_phone_syms = l1_tm_.GetPhones();
std::vector<std::vector<int32> > split;
SplitToPhones(tm_, alignment, &split);
gop_result_.Resize(split.size());
l1_gop_result_.Resize(split.size());
l1_phnseq_.resize(split.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
DecodableAmDiagGmmScaled l1_split_decodable(l1_am_, l1_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);
l1_gop_result_(i) = ComputeL1Gop(l1_decoder, l1_split_decodable, i);
gop_result_(i) = (gop_numerator - gop_denominator) / split[i].size();
frame_start_idx += split[i].size();
}
}
Vector<BaseFloat>& GmmGop2::Result() {
return gop_result_;
}
Vector<BaseFloat>& GmmGop2::l1_Result() {
return l1_gop_result_;
}
std::vector<int32>& GmmGop2::phn_seq() {
return l1_phnseq_;
}
} // End namespace kaldi