nnet-latgen-faster.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-latgen-faster.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // Johns Hopkins University (author: Daniel Povey)
5 // 2014 Guoguo Chen
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 
23 #include "base/kaldi-common.h"
24 #include "util/common-utils.h"
25 #include "tree/context-dep.h"
26 #include "hmm/transition-model.h"
27 #include "fstext/kaldi-fst-io.h"
30 #include "base/timer.h"
31 
32 
33 int main(int argc, char *argv[]) {
34  try {
35  using namespace kaldi;
36  using namespace kaldi::nnet2;
37  typedef kaldi::int32 int32;
38  using fst::SymbolTable;
39  using fst::Fst;
40  using fst::StdArc;
41 
42  const char *usage =
43  "Generate lattices using neural net model.\n"
44  "Usage: nnet-latgen-faster [options] <nnet-in> <fst-in|fsts-rspecifier> <features-rspecifier>"
45  " <lattice-wspecifier> [ <words-wspecifier> [<alignments-wspecifier>] ]\n";
46  ParseOptions po(usage);
47  Timer timer;
48  bool allow_partial = false;
49  BaseFloat acoustic_scale = 0.1;
51 
52  std::string word_syms_filename;
53  config.Register(&po);
54  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
55  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
56  po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
57 
58  po.Read(argc, argv);
59 
60  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
61  po.PrintUsage();
62  exit(1);
63  }
64 
65  std::string model_in_filename = po.GetArg(1),
66  fst_in_str = po.GetArg(2),
67  feature_rspecifier = po.GetArg(3),
68  lattice_wspecifier = po.GetArg(4),
69  words_wspecifier = po.GetOptArg(5),
70  alignment_wspecifier = po.GetOptArg(6);
71 
72  TransitionModel trans_model;
73  AmNnet am_nnet;
74  {
75  bool binary;
76  Input ki(model_in_filename, &binary);
77  trans_model.Read(ki.Stream(), binary);
78  am_nnet.Read(ki.Stream(), binary);
79  }
80 
81  bool determinize = config.determinize_lattice;
82  CompactLatticeWriter compact_lattice_writer;
83  LatticeWriter lattice_writer;
84  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
85  : lattice_writer.Open(lattice_wspecifier)))
86  KALDI_ERR << "Could not open table for writing lattices: "
87  << lattice_wspecifier;
88 
89  Int32VectorWriter words_writer(words_wspecifier);
90 
91  Int32VectorWriter alignment_writer(alignment_wspecifier);
92 
93  fst::SymbolTable *word_syms = NULL;
94  if (word_syms_filename != "")
95  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
96  KALDI_ERR << "Could not read symbol table from file "
97  << word_syms_filename;
98 
99 
100  double tot_like = 0.0;
101  kaldi::int64 frame_count = 0;
102  int num_success = 0, num_fail = 0;
103 
104  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
105  SequentialBaseFloatCuMatrixReader feature_reader(feature_rspecifier);
106 
107  // Input FST is just one FST, not a table of FSTs.
108  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
109  timer.Reset();
110 
111  {
112  LatticeFasterDecoder decoder(*decode_fst, config);
113 
114  for (; !feature_reader.Done(); feature_reader.Next()) {
115  std::string utt = feature_reader.Key();
116  const CuMatrix<BaseFloat> &features (feature_reader.Value());
117  if (features.NumRows() == 0) {
118  KALDI_WARN << "Zero-length utterance: " << utt;
119  num_fail++;
120  continue;
121  }
122  bool pad_input = true;
123  DecodableAmNnet nnet_decodable(trans_model,
124  am_nnet,
125  features,
126  pad_input,
127  acoustic_scale);
128  double like;
130  decoder, nnet_decodable, trans_model, word_syms, utt,
131  acoustic_scale, determinize, allow_partial, &alignment_writer,
132  &words_writer, &compact_lattice_writer, &lattice_writer,
133  &like)) {
134  tot_like += like;
135  frame_count += features.NumRows();
136  num_success++;
137  } else num_fail++;
138  }
139  }
140  delete decode_fst; // delete this only after decoder goes out of scope.
141  } else { // We have different FSTs for different utterances.
142  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
143  RandomAccessBaseFloatCuMatrixReader feature_reader(feature_rspecifier);
144  for (; !fst_reader.Done(); fst_reader.Next()) {
145  std::string utt = fst_reader.Key();
146  if (!feature_reader.HasKey(utt)) {
147  KALDI_WARN << "Not decoding utterance " << utt
148  << " because no features available.";
149  num_fail++;
150  continue;
151  }
152  const CuMatrix<BaseFloat> &features = feature_reader.Value(utt);
153  if (features.NumRows() == 0) {
154  KALDI_WARN << "Zero-length utterance: " << utt;
155  num_fail++;
156  continue;
157  }
158 
159  LatticeFasterDecoder decoder(fst_reader.Value(), config);
160 
161  bool pad_input = true;
162  DecodableAmNnet nnet_decodable(trans_model,
163  am_nnet,
164  features,
165  pad_input,
166  acoustic_scale);
167  double like;
169  decoder, nnet_decodable, trans_model, word_syms, utt,
170  acoustic_scale, determinize, allow_partial, &alignment_writer,
171  &words_writer, &compact_lattice_writer, &lattice_writer,
172  &like)) {
173  tot_like += like;
174  frame_count += features.NumRows();
175  num_success++;
176  } else num_fail++;
177  }
178  }
179 
180  double elapsed = timer.Elapsed();
181  KALDI_LOG << "Time taken "<< elapsed
182  << "s: real-time factor assuming 100 frames/sec is "
183  << (elapsed*100.0/frame_count);
184  KALDI_LOG << "Done " << num_success << " utterances, failed for "
185  << num_fail;
186  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
187  << frame_count<<" frames.";
188 
189  delete word_syms;
190  if (num_success != 0) return 0;
191  else return 1;
192  } catch(const std::exception &e) {
193  std::cerr << e.what();
194  return -1;
195  }
196 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
bool Open(const std::string &wspecifier)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
void Reset()
Definition: timer.h:71
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
void Read(std::istream &is, bool binary)
Definition: am-nnet.cc:39
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...
This class represents a matrix that&#39;s stored on the GPU if we have one, and in memory if not...
Definition: matrix-common.h:71
void Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
std::istream & Stream()
Definition: kaldi-io.cc:826
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const T & Value(const std::string &key)
void Read(std::istream &is, bool binary)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
This is the "normal" lattice-generating decoder.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
int main(int argc, char *argv[])
DecodableAmNnet is a decodable object that decodes with a neural net acoustic model of type AmNnet...
MatrixIndexT NumRows() const
Dimensions.
Definition: cu-matrix.h:215
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
std::string GetOptArg(int param) const