decoder-wrappers.cc
Go to the documentation of this file.
1 // decoder/decoder-wrappers.cc
2 
3 // Copyright 2014 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
21 #include "decoder/faster-decoder.h"
23 #include "decoder/grammar-fst.h"
24 #include "lat/lattice-functions.h"
25 
26 namespace kaldi {
27 
28 
29 
30 
31 
33  LatticeFasterDecoder *decoder,
34  DecodableInterface *decodable,
35  const TransitionModel &trans_model,
36  const fst::SymbolTable *word_syms,
37  const std::string &utt,
38  BaseFloat acoustic_scale,
39  bool determinize,
40  bool allow_partial,
41  Int32VectorWriter *alignments_writer,
42  Int32VectorWriter *words_writer,
43  CompactLatticeWriter *compact_lattice_writer,
44  LatticeWriter *lattice_writer,
45  double *like_sum, // on success, adds likelihood to this.
46  int64 *frame_sum, // on success, adds #frames to this.
47  int32 *num_done, // on success (including partial decode), increments this.
48  int32 *num_err, // on failure, increments this.
49  int32 *num_partial): // If partial decode (final-state not reached), increments this.
50  decoder_(decoder), decodable_(decodable), trans_model_(&trans_model),
51  word_syms_(word_syms), utt_(utt), acoustic_scale_(acoustic_scale),
52  determinize_(determinize), allow_partial_(allow_partial),
53  alignments_writer_(alignments_writer),
54  words_writer_(words_writer),
55  compact_lattice_writer_(compact_lattice_writer),
56  lattice_writer_(lattice_writer),
57  like_sum_(like_sum), frame_sum_(frame_sum),
58  num_done_(num_done), num_err_(num_err),
59  num_partial_(num_partial),
60  computed_(false), success_(false), partial_(false),
61  clat_(NULL), lat_(NULL) { }
62 
63 
65  // Decoding and lattice determinization happens here.
66  computed_ = true; // Just means this function was called-- a check on the
67  // calling code.
68  success_ = true;
69  using fst::VectorFst;
70  if (!decoder_->Decode(decodable_)) {
71  KALDI_WARN << "Failed to decode utterance with id " << utt_;
72  success_ = false;
73  }
74  if (!decoder_->ReachedFinal()) {
75  if (allow_partial_) {
76  KALDI_WARN << "Outputting partial output for utterance " << utt_
77  << " since no final-state reached\n";
78  partial_ = true;
79  } else {
80  KALDI_WARN << "Not producing output for utterance " << utt_
81  << " since no final-state reached and "
82  << "--allow-partial=false.\n";
83  success_ = false;
84  }
85  }
86  if (!success_) return;
87 
88  // Get lattice, and do determinization if requested.
89  lat_ = new Lattice;
91  if (lat_->NumStates() == 0)
92  KALDI_ERR << "Unexpected problem getting lattice for utterance " << utt_;
93  fst::Connect(lat_);
94  if (determinize_) {
95  clat_ = new CompactLattice;
97  *trans_model_,
98  lat_,
100  clat_,
102  KALDI_WARN << "Determinization finished earlier than the beam for "
103  << "utterance " << utt_;
104  delete lat_;
105  lat_ = NULL;
106  // We'll write the lattice without acoustic scaling.
107  if (acoustic_scale_ != 0.0)
109  } else {
110  // We'll write the lattice without acoustic scaling.
111  if (acoustic_scale_ != 0.0)
113  }
114 }
115 
117  if (!computed_)
118  KALDI_ERR << "Destructor called without operator (), error in calling code.";
119 
120  if (!success_) {
121  if (num_err_ != NULL) (*num_err_)++;
122  } else { // successful decode.
123  // Getting the one-best output is lightweight enough that we can do it in
124  // the destructor (easier than adding more variables to the class, and
125  // will rarely slow down the main thread.)
126  double likelihood;
127  LatticeWeight weight;
128  int32 num_frames;
129  { // First do some stuff with word-level traceback...
130  // This is basically for diagnostics.
131  fst::VectorFst<LatticeArc> decoded;
132  decoder_->GetBestPath(&decoded);
133  if (decoded.NumStates() == 0) {
134  // Shouldn't really reach this point as already checked success.
135  KALDI_ERR << "Failed to get traceback for utterance " << utt_;
136  }
137  std::vector<int32> alignment;
138  std::vector<int32> words;
139  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
140  num_frames = alignment.size();
141  if (words_writer_->IsOpen())
142  words_writer_->Write(utt_, words);
143  if (alignments_writer_->IsOpen())
144  alignments_writer_->Write(utt_, alignment);
145  if (word_syms_ != NULL) {
146  std::cerr << utt_ << ' ';
147  for (size_t i = 0; i < words.size(); i++) {
148  std::string s = word_syms_->Find(words[i]);
149  if (s == "")
150  KALDI_ERR << "Word-id " << words[i] << " not in symbol table.";
151  std::cerr << s << ' ';
152  }
153  std::cerr << '\n';
154  }
155  likelihood = -(weight.Value1() + weight.Value2());
156  }
157 
158  // Ouptut the lattices.
159  if (determinize_) { // CompactLattice output.
160  KALDI_ASSERT(compact_lattice_writer_ != NULL && clat_ != NULL);
161  if (clat_->NumStates() == 0) {
162  KALDI_WARN << "Empty lattice for utterance " << utt_;
163  } else {
165  }
166  delete clat_;
167  clat_ = NULL;
168  } else {
169  KALDI_ASSERT(lattice_writer_ != NULL && lat_ != NULL);
170  if (lat_->NumStates() == 0) {
171  KALDI_WARN << "Empty lattice for utterance " << utt_;
172  } else {
174  }
175  delete lat_;
176  lat_ = NULL;
177  }
178 
179  // Print out logging information.
180  KALDI_LOG << "Log-like per frame for utterance " << utt_ << " is "
181  << (likelihood / num_frames) << " over "
182  << num_frames << " frames.";
183  KALDI_VLOG(2) << "Cost for utterance " << utt_ << " is "
184  << weight.Value1() << " + " << weight.Value2();
185 
186  // Now output the various diagnostic variables.
187  if (like_sum_ != NULL) *like_sum_ += likelihood;
188  if (frame_sum_ != NULL) *frame_sum_ += num_frames;
189  if (num_done_ != NULL) (*num_done_)++;
190  if (partial_ && num_partial_ != NULL) (*num_partial_)++;
191  }
192  // We were given ownership of these two objects that were passed in in
193  // the initializer.
194  delete decoder_;
195  delete decodable_;
196 }
197 
198 template <typename FST>
200  LatticeIncrementalDecoderTpl<FST> &decoder, // not const but is really an input.
201  DecodableInterface &decodable, // not const but is really an input.
202  const TransitionModel &trans_model,
203  const fst::SymbolTable *word_syms,
204  std::string utt,
205  double acoustic_scale,
206  bool determinize,
207  bool allow_partial,
208  Int32VectorWriter *alignment_writer,
209  Int32VectorWriter *words_writer,
210  CompactLatticeWriter *compact_lattice_writer,
211  LatticeWriter *lattice_writer,
212  double *like_ptr) { // puts utterance's like in like_ptr on success.
213  using fst::VectorFst;
214  if (!decoder.Decode(&decodable)) {
215  KALDI_WARN << "Failed to decode utterance with id " << utt;
216  return false;
217  }
218  if (!decoder.ReachedFinal()) {
219  if (allow_partial) {
220  KALDI_WARN << "Outputting partial output for utterance " << utt
221  << " since no final-state reached\n";
222  } else {
223  KALDI_WARN << "Not producing output for utterance " << utt
224  << " since no final-state reached and "
225  << "--allow-partial=false.\n";
226  return false;
227  }
228  }
229 
230  // Get lattice
231  CompactLattice clat = decoder.GetLattice(decoder.NumFramesDecoded(), true);
232  if (clat.NumStates() == 0)
233  KALDI_ERR << "Unexpected problem getting lattice for utterance " << utt;
234 
235  double likelihood;
236  LatticeWeight weight;
237  int32 num_frames;
238  { // First do some stuff with word-level traceback...
239  CompactLattice decoded_clat;
240  CompactLatticeShortestPath(clat, &decoded_clat);
241  Lattice decoded;
242  fst::ConvertLattice(decoded_clat, &decoded);
243 
244  if (decoded.Start() == fst::kNoStateId)
245  // Shouldn't really reach this point as already checked success.
246  KALDI_ERR << "Failed to get traceback for utterance " << utt;
247 
248  std::vector<int32> alignment;
249  std::vector<int32> words;
250  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
251  num_frames = alignment.size();
252  KALDI_ASSERT(num_frames == decoder.NumFramesDecoded());
253  if (words_writer->IsOpen())
254  words_writer->Write(utt, words);
255  if (alignment_writer->IsOpen())
256  alignment_writer->Write(utt, alignment);
257  if (word_syms != NULL) {
258  std::cerr << utt << ' ';
259  for (size_t i = 0; i < words.size(); i++) {
260  std::string s = word_syms->Find(words[i]);
261  if (s == "")
262  KALDI_ERR << "Word-id " << words[i] << " not in symbol table.";
263  std::cerr << s << ' ';
264  }
265  std::cerr << '\n';
266  }
267  likelihood = -(weight.Value1() + weight.Value2());
268  }
269 
270  // We'll write the lattice without acoustic scaling.
271  if (acoustic_scale != 0.0)
272  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &clat);
273  Connect(&clat);
274  compact_lattice_writer->Write(utt, clat);
275  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
276  << (likelihood / num_frames) << " over "
277  << num_frames << " frames.";
278  KALDI_VLOG(2) << "Cost for utterance " << utt << " is "
279  << weight.Value1() << " + " << weight.Value2();
280  *like_ptr = likelihood;
281  return true;
282 }
283 
284 
285 // Takes care of output. Returns true on success.
286 template <typename FST>
288  LatticeFasterDecoderTpl<FST> &decoder, // not const but is really an input.
289  DecodableInterface &decodable, // not const but is really an input.
290  const TransitionModel &trans_model,
291  const fst::SymbolTable *word_syms,
292  std::string utt,
293  double acoustic_scale,
294  bool determinize,
295  bool allow_partial,
296  Int32VectorWriter *alignment_writer,
297  Int32VectorWriter *words_writer,
298  CompactLatticeWriter *compact_lattice_writer,
299  LatticeWriter *lattice_writer,
300  double *like_ptr) { // puts utterance's like in like_ptr on success.
301  using fst::VectorFst;
302 
303  if (!decoder.Decode(&decodable)) {
304  KALDI_WARN << "Failed to decode utterance with id " << utt;
305  return false;
306  }
307  if (!decoder.ReachedFinal()) {
308  if (allow_partial) {
309  KALDI_WARN << "Outputting partial output for utterance " << utt
310  << " since no final-state reached\n";
311  } else {
312  KALDI_WARN << "Not producing output for utterance " << utt
313  << " since no final-state reached and "
314  << "--allow-partial=false.\n";
315  return false;
316  }
317  }
318 
319  double likelihood;
320  LatticeWeight weight;
321  int32 num_frames;
322  { // First do some stuff with word-level traceback...
323  VectorFst<LatticeArc> decoded;
324  if (!decoder.GetBestPath(&decoded))
325  // Shouldn't really reach this point as already checked success.
326  KALDI_ERR << "Failed to get traceback for utterance " << utt;
327 
328  std::vector<int32> alignment;
329  std::vector<int32> words;
330  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
331  num_frames = alignment.size();
332  if (words_writer->IsOpen())
333  words_writer->Write(utt, words);
334  if (alignment_writer->IsOpen())
335  alignment_writer->Write(utt, alignment);
336  if (word_syms != NULL) {
337  std::cerr << utt << ' ';
338  for (size_t i = 0; i < words.size(); i++) {
339  std::string s = word_syms->Find(words[i]);
340  if (s == "")
341  KALDI_ERR << "Word-id " << words[i] << " not in symbol table.";
342  std::cerr << s << ' ';
343  }
344  std::cerr << '\n';
345  }
346  likelihood = -(weight.Value1() + weight.Value2());
347  }
348 
349  // Get lattice, and do determinization if requested.
350  Lattice lat;
351  decoder.GetRawLattice(&lat);
352  if (lat.NumStates() == 0)
353  KALDI_ERR << "Unexpected problem getting lattice for utterance " << utt;
354  fst::Connect(&lat);
355  if (determinize) {
356  CompactLattice clat;
358  trans_model,
359  &lat,
360  decoder.GetOptions().lattice_beam,
361  &clat,
362  decoder.GetOptions().det_opts))
363  KALDI_WARN << "Determinization finished earlier than the beam for "
364  << "utterance " << utt;
365  // We'll write the lattice without acoustic scaling.
366  if (acoustic_scale != 0.0)
367  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &clat);
368  compact_lattice_writer->Write(utt, clat);
369  } else {
370  // We'll write the lattice without acoustic scaling.
371  if (acoustic_scale != 0.0)
372  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &lat);
373  lattice_writer->Write(utt, lat);
374  }
375  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
376  << (likelihood / num_frames) << " over "
377  << num_frames << " frames.";
378  KALDI_VLOG(2) << "Cost for utterance " << utt << " is "
379  << weight.Value1() << " + " << weight.Value2();
380  *like_ptr = likelihood;
381  return true;
382 }
383 
384 // Instantiate the template above for the two required FST types.
386  LatticeIncrementalDecoderTpl<fst::Fst<fst::StdArc> > &decoder,
387  DecodableInterface &decodable,
388  const TransitionModel &trans_model,
389  const fst::SymbolTable *word_syms,
390  std::string utt,
391  double acoustic_scale,
392  bool determinize,
393  bool allow_partial,
394  Int32VectorWriter *alignment_writer,
395  Int32VectorWriter *words_writer,
396  CompactLatticeWriter *compact_lattice_writer,
397  LatticeWriter *lattice_writer,
398  double *like_ptr);
399 
402  DecodableInterface &decodable,
403  const TransitionModel &trans_model,
404  const fst::SymbolTable *word_syms,
405  std::string utt,
406  double acoustic_scale,
407  bool determinize,
408  bool allow_partial,
409  Int32VectorWriter *alignment_writer,
410  Int32VectorWriter *words_writer,
411  CompactLatticeWriter *compact_lattice_writer,
412  LatticeWriter *lattice_writer,
413  double *like_ptr);
414 
415 
416 template bool DecodeUtteranceLatticeFaster(
417  LatticeFasterDecoderTpl<fst::Fst<fst::StdArc> > &decoder,
418  DecodableInterface &decodable,
419  const TransitionModel &trans_model,
420  const fst::SymbolTable *word_syms,
421  std::string utt,
422  double acoustic_scale,
423  bool determinize,
424  bool allow_partial,
425  Int32VectorWriter *alignment_writer,
426  Int32VectorWriter *words_writer,
427  CompactLatticeWriter *compact_lattice_writer,
428  LatticeWriter *lattice_writer,
429  double *like_ptr);
430 
431 template bool DecodeUtteranceLatticeFaster(
433  DecodableInterface &decodable,
434  const TransitionModel &trans_model,
435  const fst::SymbolTable *word_syms,
436  std::string utt,
437  double acoustic_scale,
438  bool determinize,
439  bool allow_partial,
440  Int32VectorWriter *alignment_writer,
441  Int32VectorWriter *words_writer,
442  CompactLatticeWriter *compact_lattice_writer,
443  LatticeWriter *lattice_writer,
444  double *like_ptr);
445 
446 
447 // Takes care of output. Returns true on success.
449  LatticeSimpleDecoder &decoder, // not const but is really an input.
450  DecodableInterface &decodable, // not const but is really an input.
451  const TransitionModel &trans_model,
452  const fst::SymbolTable *word_syms,
453  std::string utt,
454  double acoustic_scale,
455  bool determinize,
456  bool allow_partial,
457  Int32VectorWriter *alignment_writer,
458  Int32VectorWriter *words_writer,
459  CompactLatticeWriter *compact_lattice_writer,
460  LatticeWriter *lattice_writer,
461  double *like_ptr) { // puts utterance's like in like_ptr on success.
462  using fst::VectorFst;
463 
464  if (!decoder.Decode(&decodable)) {
465  KALDI_WARN << "Failed to decode utterance with id " << utt;
466  return false;
467  }
468  if (!decoder.ReachedFinal()) {
469  if (allow_partial) {
470  KALDI_WARN << "Outputting partial output for utterance " << utt
471  << " since no final-state reached\n";
472  } else {
473  KALDI_WARN << "Not producing output for utterance " << utt
474  << " since no final-state reached and "
475  << "--allow-partial=false.\n";
476  return false;
477  }
478  }
479 
480  double likelihood;
482  int32 num_frames;
483  { // First do some stuff with word-level traceback...
484  VectorFst<LatticeArc> decoded;
485  if (!decoder.GetBestPath(&decoded))
486  // Shouldn't really reach this point as already checked success.
487  KALDI_ERR << "Failed to get traceback for utterance " << utt;
488 
489  std::vector<int32> alignment;
490  std::vector<int32> words;
491  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
492  num_frames = alignment.size();
493  if (words_writer->IsOpen())
494  words_writer->Write(utt, words);
495  if (alignment_writer->IsOpen())
496  alignment_writer->Write(utt, alignment);
497  if (word_syms != NULL) {
498  std::cerr << utt << ' ';
499  for (size_t i = 0; i < words.size(); i++) {
500  std::string s = word_syms->Find(words[i]);
501  if (s == "")
502  KALDI_ERR << "Word-id " << words[i] << " not in symbol table.";
503  std::cerr << s << ' ';
504  }
505  std::cerr << '\n';
506  }
507  likelihood = -(weight.Value1() + weight.Value2());
508  }
509 
510  // Get lattice, and do determinization if requested.
511  Lattice lat;
512  if (!decoder.GetRawLattice(&lat))
513  KALDI_ERR << "Unexpected problem getting lattice for utterance " << utt;
514  fst::Connect(&lat);
515  if (determinize) {
516  CompactLattice clat;
518  trans_model,
519  &lat,
520  decoder.GetOptions().lattice_beam,
521  &clat,
522  decoder.GetOptions().det_opts))
523  KALDI_WARN << "Determinization finished earlier than the beam for "
524  << "utterance " << utt;
525  // We'll write the lattice without acoustic scaling.
526  if (acoustic_scale != 0.0)
527  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &clat);
528  compact_lattice_writer->Write(utt, clat);
529  } else {
530  // We'll write the lattice without acoustic scaling.
531  if (acoustic_scale != 0.0)
532  fst::ScaleLattice(fst::AcousticLatticeScale(1.0 / acoustic_scale), &lat);
533  lattice_writer->Write(utt, lat);
534  }
535  KALDI_LOG << "Log-like per frame for utterance " << utt << " is "
536  << (likelihood / num_frames) << " over "
537  << num_frames << " frames.";
538  KALDI_VLOG(2) << "Cost for utterance " << utt << " is "
539  << weight.Value1() << " + " << weight.Value2();
540  *like_ptr = likelihood;
541  return true;
542 }
543 
544 
545 // see comment in header.
547  fst::VectorFst<fst::StdArc> *fst) {
548  typedef fst::StdArc Arc;
549  typedef Arc::StateId StateId;
550  typedef Arc::Label Label;
551  typedef Arc::Weight Weight;
552  StateId num_states = fst->NumStates();
553  if (num_states == 0) {
554  KALDI_WARN << "Empty FST input.";
555  return;
556  }
557  Weight zero = Weight::Zero();
558  // fst_rhs will be the right hand side of the Concat operation.
559  fst::VectorFst<fst::StdArc> fst_rhs(*fst);
560  // first remove the final-probs from fst_rhs.
561  for (StateId state = 0; state < num_states; state++)
562  fst_rhs.SetFinal(state, zero);
563  StateId pre_initial = fst_rhs.AddState();
564  Arc to_initial(0, 0, Weight::One(), fst_rhs.Start());
565  fst_rhs.AddArc(pre_initial, to_initial);
566  fst_rhs.SetStart(pre_initial);
567  // make the pre_initial state final with probability one;
568  // this is equivalent to keeping the final-probs of the first
569  // FST when we do concat (otherwise they would get deleted).
570  fst_rhs.SetFinal(pre_initial, Weight::One());
571  fst::VectorFst<fst::StdArc> fst_concat;
572  fst::Concat(fst, fst_rhs);
573 }
574 
575 
577  const AlignConfig &config,
578  const std::string &utt,
579  BaseFloat acoustic_scale, // affects scores written to scores_writer, if
580  // present
581  fst::VectorFst<fst::StdArc> *fst, // non-const in case config.careful ==
582  // true.
583  DecodableInterface *decodable, // not const but is really an input.
584  Int32VectorWriter *alignment_writer,
585  BaseFloatWriter *scores_writer,
586  int32 *num_done,
587  int32 *num_error,
588  int32 *num_retried,
589  double *tot_like,
590  int64 *frame_count,
591  BaseFloatVectorWriter *per_frame_acwt_writer) {
592 
593  if ((config.retry_beam != 0 && config.retry_beam <= config.beam) ||
594  config.beam <= 0.0) {
595  KALDI_ERR << "Beams do not make sense: beam " << config.beam
596  << ", retry-beam " << config.retry_beam;
597  }
598 
599  if (fst->Start() == fst::kNoStateId) {
600  KALDI_WARN << "Empty decoding graph for " << utt;
601  if (num_error != NULL) (*num_error)++;
602  return;
603  }
604 
605  if (config.careful)
607 
608  FasterDecoderOptions decode_opts;
609  decode_opts.beam = config.beam;
610 
611  FasterDecoder decoder(*fst, decode_opts);
612  decoder.Decode(decodable);
613 
614  bool ans = decoder.ReachedFinal(); // consider only final states.
615 
616  if (!ans && config.retry_beam != 0.0) {
617  if (num_retried != NULL) (*num_retried)++;
618  KALDI_WARN << "Retrying utterance " << utt << " with beam "
619  << config.retry_beam;
620  decode_opts.beam = config.retry_beam;
621  decoder.SetOptions(decode_opts);
622  decoder.Decode(decodable);
623  ans = decoder.ReachedFinal();
624  }
625 
626  if (!ans) { // Still did not reach final state.
627  KALDI_WARN << "Did not successfully decode file " << utt << ", len = "
628  << decodable->NumFramesReady();
629  if (num_error != NULL) (*num_error)++;
630  return;
631  }
632 
633  fst::VectorFst<LatticeArc> decoded; // linear FST.
634  decoder.GetBestPath(&decoded);
635  if (decoded.NumStates() == 0) {
636  KALDI_WARN << "Error getting best path from decoder (likely a bug)";
637  if (num_error != NULL) (*num_error)++;
638  return;
639  }
640 
641  std::vector<int32> alignment;
642  std::vector<int32> words;
643  LatticeWeight weight;
644 
645  GetLinearSymbolSequence(decoded, &alignment, &words, &weight);
646  BaseFloat like = -(weight.Value1()+weight.Value2()) / acoustic_scale;
647 
648  if (num_done != NULL) (*num_done)++;
649  if (tot_like != NULL) (*tot_like) += like;
650  if (frame_count != NULL) (*frame_count) += decodable->NumFramesReady();
651 
652  if (alignment_writer != NULL && alignment_writer->IsOpen())
653  alignment_writer->Write(utt, alignment);
654 
655  if (scores_writer != NULL && scores_writer->IsOpen())
656  scores_writer->Write(utt, -(weight.Value1()+weight.Value2()));
657 
658  Vector<BaseFloat> per_frame_loglikes;
659  if (per_frame_acwt_writer != NULL && per_frame_acwt_writer->IsOpen()) {
660  GetPerFrameAcousticCosts(decoded, &per_frame_loglikes);
661  per_frame_loglikes.Scale(-1 / acoustic_scale);
662  per_frame_acwt_writer->Write(utt, per_frame_loglikes);
663  }
664 }
665 
666 } // end namespace kaldi.
int32 words[kMaxOrder]
fst::StdArc::StateId StateId
fst::StdArc::Label Label
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool DecodeUtteranceLatticeIncremental(LatticeIncrementalDecoderTpl< FST > &decoder, DecodableInterface &decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, std::string utt, double acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignment_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
TODO.
DecodeUtteranceLatticeFasterClass(LatticeFasterDecoder *decoder, DecodableInterface *decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, const std::string &utt, BaseFloat acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignments_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_sum, int64 *frame_sum, int32 *num_done, int32 *num_err, int32 *num_partial)
virtual int32 NumFramesReady() const
The call NumFramesReady() will return the number of frames currently available for this decodable obj...
const LatticeFasterDecoderConfig & GetOptions() const
DecodableInterface provides a link between the (acoustic-modeling and feature-processing) code and th...
Definition: decodable-itf.h:82
bool GetRawLattice(Lattice *ofst, bool use_final_probs=true) const
Outputs an FST corresponding to the raw, state-level tracebacks.
void SetOptions(const FasterDecoderOptions &config)
CompactLatticeWriter * compact_lattice_writer_
This is an extention to the "normal" lattice-generating decoder.
Lattice::StateId StateId
For an extended explanation of the framework of which grammar-fsts are a part, please see Support for...
Definition: graph.dox:21
bool ReachedFinal() const
says whether a final-state was active on the last frame.
fst::StdArc StdArc
bool Decode(DecodableInterface *decodable)
void Decode(DecodableInterface *decodable)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
bool DecodeUtteranceLatticeFaster(LatticeFasterDecoderTpl< FST > &decoder, DecodableInterface &decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, std::string utt, double acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignment_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
This function DecodeUtteranceLatticeFaster is used in several decoders, and we have moved it here...
void GetPerFrameAcousticCosts(const Lattice &nbest, Vector< BaseFloat > *per_frame_loglikes)
This function extracts the per-frame log likelihoods from a linear lattice (which we refer to as an &#39;...
const LatticeSimpleDecoderConfig & GetOptions() const
bool GetLinearSymbolSequence(const Fst< Arc > &fst, std::vector< I > *isymbols_out, std::vector< I > *osymbols_out, typename Arc::Weight *tot_weight_out)
GetLinearSymbolSequence gets the symbol sequence from a linear FST.
void Write(const std::string &key, const T &value) const
bool GetBestPath(fst::MutableFst< LatticeArc > *fst_out, bool use_final_probs=true)
GetBestPath gets the decoding traceback.
void CompactLatticeShortestPath(const CompactLattice &clat, CompactLattice *shortest_path)
A form of the shortest-path/best-path algorithm that&#39;s specially coded for CompactLattice.
std::vector< std::vector< double > > AcousticLatticeScale(double acwt)
bool GetRawLattice(Lattice *lat, bool use_final_probs=true) const
float BaseFloat
Definition: kaldi-types.h:29
void ScaleLattice(const std::vector< std::vector< ScaleFloat > > &scale, MutableFst< ArcTpl< Weight > > *fst)
Scales the pairs of weights in LatticeWeight or CompactLatticeWeight by viewing the pair (a...
void ConvertLattice(const ExpandedFst< ArcTpl< Weight > > &ifst, MutableFst< ArcTpl< CompactLatticeWeightTpl< Weight, Int > > > *ofst, bool invert)
Convert lattice from a normal FST to a CompactLattice FST.
static const LatticeWeightTpl Zero()
fst::VectorFst< LatticeArc > Lattice
Definition: kaldi-lattice.h:44
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
void Scale(Real alpha)
Multiplies all elements by this constant.
fst::StdArc::Label Label
Simplest possible decoder, included largely for didactic purposes and as a means to debug more highly...
bool DecodeUtteranceLatticeSimple(LatticeSimpleDecoder &decoder, DecodableInterface &decodable, const TransitionModel &trans_model, const fst::SymbolTable *word_syms, std::string utt, double acoustic_scale, bool determinize, bool allow_partial, Int32VectorWriter *alignment_writer, Int32VectorWriter *words_writer, CompactLatticeWriter *compact_lattice_writer, LatticeWriter *lattice_writer, double *like_ptr)
bool GetBestPath(Lattice *ofst, bool use_final_probs=true) const
Outputs an FST corresponding to the single best path through the lattice.
fst::VectorFst< CompactLatticeArc > CompactLattice
Definition: kaldi-lattice.h:46
fst::StdArc::Weight Weight
void ModifyGraphForCarefulAlignment(fst::VectorFst< fst::StdArc > *fst)
This function modifies the decoding graph for what we call "careful alignment".
fst::DeterminizeLatticePhonePrunedOptions det_opts
This is the "normal" lattice-generating decoder.
bool Decode(DecodableInterface *decodable)
Decodes until there are no more frames left in the "decodable" object.
A class representing a vector.
Definition: kaldi-vector.h:406
int32 NumFramesDecoded() const
Returns the number of frames decoded so far.
Arc::Weight Weight
Definition: kws-search.cc:31
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool Decode(DecodableInterface *decodable)
CAUTION: it&#39;s unlikely that you will ever want to call this function.
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
bool ReachedFinal() const
says whether a final-state was active on the last frame.
bool GetBestPath(Lattice *lat, bool use_final_probs=true) const
fst::DeterminizeLatticePhonePrunedOptions det_opts
const CompactLattice & GetLattice(int32 num_frames_to_include, bool use_final_probs=false)
This decoder has no GetBestPath() function.
void AlignUtteranceWrapper(const AlignConfig &config, const std::string &utt, BaseFloat acoustic_scale, fst::VectorFst< fst::StdArc > *fst, DecodableInterface *decodable, Int32VectorWriter *alignment_writer, BaseFloatWriter *scores_writer, int32 *num_done, int32 *num_error, int32 *num_retried, double *tot_like, int64 *frame_count, BaseFloatVectorWriter *per_frame_acwt_writer)
AlignUtteranceWapper is a wrapper for alignment code used in training, that is called from many diffe...
#define KALDI_LOG
Definition: kaldi-error.h:153
bool ReachedFinal() const
Returns true if a final state was active on the last frame.
bool DeterminizeLatticePhonePrunedWrapper(const kaldi::TransitionModel &trans_model, MutableFst< kaldi::LatticeArc > *ifst, double beam, MutableFst< kaldi::CompactLatticeArc > *ofst, DeterminizeLatticePhonePrunedOptions opts)
This function is a wrapper of DeterminizeLatticePhonePruned() that works for Lattice type FSTs...
bool ReachedFinal() const
says whether a final-state was active on the last frame.