nnet3-latgen-faster.cc File Reference
Include dependency graph for nnet3-latgen-faster.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 33 of file nnet3-latgen-faster.cc.

References NnetSimpleComputationOptions::acoustic_scale, kaldi::ClassifyRspecifier(), kaldi::nnet3::CollapseModel(), kaldi::DecodeUtteranceLatticeFaster(), LatticeFasterDecoderConfig::determinize_lattice, SequentialTableReader< Holder >::Done(), Timer::Elapsed(), NnetSimpleComputationOptions::frame_subsampling_factor, ParseOptions::GetArg(), AmNnetSimple::GetNnet(), ParseOptions::GetOptArg(), RandomAccessTableReader< Holder >::HasKey(), RandomAccessTableReaderMapped< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), DecodableAmNnetSimple::NumFramesReady(), MatrixBase< Real >::NumRows(), TableWriter< Holder >::Open(), NnetSimpleComputationOptions::optimize_config, ParseOptions::PrintUsage(), AmNnetSimple::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldiGeneric(), LatticeFasterDecoderConfig::Register(), NnetSimpleComputationOptions::Register(), ParseOptions::Register(), Timer::Reset(), kaldi::nnet3::SetBatchnormTestMode(), kaldi::nnet3::SetDropoutTestMode(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and RandomAccessTableReaderMapped< Holder >::Value().

33  {
34  // note: making this program work with GPUs is as simple as initializing the
35  // device, but it probably won't make a huge difference in speed for typical
36  // setups. You should use nnet3-latgen-faster-batch if you want to use a GPU.
37  try {
38  using namespace kaldi;
39  using namespace kaldi::nnet3;
40  typedef kaldi::int32 int32;
41  using fst::SymbolTable;
42  using fst::Fst;
43  using fst::StdArc;
44 
45  const char *usage =
46  "Generate lattices using nnet3 neural net model.\n"
47  "Usage: nnet3-latgen-faster [options] <nnet-in> <fst-in|fsts-rspecifier> <features-rspecifier>"
48  " <lattice-wspecifier> [ <words-wspecifier> [<alignments-wspecifier>] ]\n"
49  "See also: nnet3-latgen-faster-parallel, nnet3-latgen-faster-batch\n";
50  ParseOptions po(usage);
51  Timer timer;
52  bool allow_partial = false;
54  NnetSimpleComputationOptions decodable_opts;
55 
56  std::string word_syms_filename;
57  std::string ivector_rspecifier,
58  online_ivector_rspecifier,
59  utt2spk_rspecifier;
60  int32 online_ivector_period = 0;
61  config.Register(&po);
62  decodable_opts.Register(&po);
63  po.Register("word-symbol-table", &word_syms_filename,
64  "Symbol table for words [for debug output]");
65  po.Register("allow-partial", &allow_partial,
66  "If true, produce output even if end state was not reached.");
67  po.Register("ivectors", &ivector_rspecifier, "Rspecifier for "
68  "iVectors as vectors (i.e. not estimated online); per utterance "
69  "by default, or per speaker if you provide the --utt2spk option.");
70  po.Register("utt2spk", &utt2spk_rspecifier, "Rspecifier for "
71  "utt2spk option used to get ivectors per speaker");
72  po.Register("online-ivectors", &online_ivector_rspecifier, "Rspecifier for "
73  "iVectors estimated online, as matrices. If you supply this,"
74  " you must set the --online-ivector-period option.");
75  po.Register("online-ivector-period", &online_ivector_period, "Number of frames "
76  "between iVectors in matrices supplied to the --online-ivectors "
77  "option");
78 
79  po.Read(argc, argv);
80 
81  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
82  po.PrintUsage();
83  exit(1);
84  }
85 
86  std::string model_in_filename = po.GetArg(1),
87  fst_in_str = po.GetArg(2),
88  feature_rspecifier = po.GetArg(3),
89  lattice_wspecifier = po.GetArg(4),
90  words_wspecifier = po.GetOptArg(5),
91  alignment_wspecifier = po.GetOptArg(6);
92 
93  TransitionModel trans_model;
94  AmNnetSimple am_nnet;
95  {
96  bool binary;
97  Input ki(model_in_filename, &binary);
98  trans_model.Read(ki.Stream(), binary);
99  am_nnet.Read(ki.Stream(), binary);
100  SetBatchnormTestMode(true, &(am_nnet.GetNnet()));
101  SetDropoutTestMode(true, &(am_nnet.GetNnet()));
102  CollapseModel(CollapseModelConfig(), &(am_nnet.GetNnet()));
103  }
104 
105  bool determinize = config.determinize_lattice;
106  CompactLatticeWriter compact_lattice_writer;
107  LatticeWriter lattice_writer;
108  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
109  : lattice_writer.Open(lattice_wspecifier)))
110  KALDI_ERR << "Could not open table for writing lattices: "
111  << lattice_wspecifier;
112 
113  RandomAccessBaseFloatMatrixReader online_ivector_reader(
114  online_ivector_rspecifier);
116  ivector_rspecifier, utt2spk_rspecifier);
117 
118  Int32VectorWriter words_writer(words_wspecifier);
119  Int32VectorWriter alignment_writer(alignment_wspecifier);
120 
121  fst::SymbolTable *word_syms = NULL;
122  if (word_syms_filename != "")
123  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
124  KALDI_ERR << "Could not read symbol table from file "
125  << word_syms_filename;
126 
127  double tot_like = 0.0;
128  kaldi::int64 frame_count = 0;
129  int num_success = 0, num_fail = 0;
130  // this compiler object allows caching of computations across
131  // different utterances.
132  CachingOptimizingCompiler compiler(am_nnet.GetNnet(),
133  decodable_opts.optimize_config);
134 
135  if (ClassifyRspecifier(fst_in_str, NULL, NULL) == kNoRspecifier) {
136  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
137 
138  // Input FST is just one FST, not a table of FSTs.
139  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_str);
140  timer.Reset();
141 
142  {
143  LatticeFasterDecoder decoder(*decode_fst, config);
144 
145  for (; !feature_reader.Done(); feature_reader.Next()) {
146  std::string utt = feature_reader.Key();
147  const Matrix<BaseFloat> &features (feature_reader.Value());
148  if (features.NumRows() == 0) {
149  KALDI_WARN << "Zero-length utterance: " << utt;
150  num_fail++;
151  continue;
152  }
153  const Matrix<BaseFloat> *online_ivectors = NULL;
154  const Vector<BaseFloat> *ivector = NULL;
155  if (!ivector_rspecifier.empty()) {
156  if (!ivector_reader.HasKey(utt)) {
157  KALDI_WARN << "No iVector available for utterance " << utt;
158  num_fail++;
159  continue;
160  } else {
161  ivector = &ivector_reader.Value(utt);
162  }
163  }
164  if (!online_ivector_rspecifier.empty()) {
165  if (!online_ivector_reader.HasKey(utt)) {
166  KALDI_WARN << "No online iVector available for utterance " << utt;
167  num_fail++;
168  continue;
169  } else {
170  online_ivectors = &online_ivector_reader.Value(utt);
171  }
172  }
173 
174  DecodableAmNnetSimple nnet_decodable(
175  decodable_opts, trans_model, am_nnet,
176  features, ivector, online_ivectors,
177  online_ivector_period, &compiler);
178 
179  double like;
181  decoder, nnet_decodable, trans_model, word_syms, utt,
182  decodable_opts.acoustic_scale, determinize, allow_partial,
183  &alignment_writer, &words_writer, &compact_lattice_writer,
184  &lattice_writer,
185  &like)) {
186  tot_like += like;
187  frame_count += nnet_decodable.NumFramesReady();
188  num_success++;
189  } else num_fail++;
190  }
191  }
192  delete decode_fst; // delete this only after decoder goes out of scope.
193  } else { // We have different FSTs for different utterances.
194  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_in_str);
195  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
196  for (; !fst_reader.Done(); fst_reader.Next()) {
197  std::string utt = fst_reader.Key();
198  if (!feature_reader.HasKey(utt)) {
199  KALDI_WARN << "Not decoding utterance " << utt
200  << " because no features available.";
201  num_fail++;
202  continue;
203  }
204  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
205  if (features.NumRows() == 0) {
206  KALDI_WARN << "Zero-length utterance: " << utt;
207  num_fail++;
208  continue;
209  }
210 
211  LatticeFasterDecoder decoder(fst_reader.Value(), config);
212 
213  const Matrix<BaseFloat> *online_ivectors = NULL;
214  const Vector<BaseFloat> *ivector = NULL;
215  if (!ivector_rspecifier.empty()) {
216  if (!ivector_reader.HasKey(utt)) {
217  KALDI_WARN << "No iVector available for utterance " << utt;
218  num_fail++;
219  continue;
220  } else {
221  ivector = &ivector_reader.Value(utt);
222  }
223  }
224  if (!online_ivector_rspecifier.empty()) {
225  if (!online_ivector_reader.HasKey(utt)) {
226  KALDI_WARN << "No online iVector available for utterance " << utt;
227  num_fail++;
228  continue;
229  } else {
230  online_ivectors = &online_ivector_reader.Value(utt);
231  }
232  }
233 
234  DecodableAmNnetSimple nnet_decodable(
235  decodable_opts, trans_model, am_nnet,
236  features, ivector, online_ivectors,
237  online_ivector_period, &compiler);
238 
239  double like;
241  decoder, nnet_decodable, trans_model, word_syms, utt,
242  decodable_opts.acoustic_scale, determinize, allow_partial,
243  &alignment_writer, &words_writer, &compact_lattice_writer,
244  &lattice_writer, &like)) {
245  tot_like += like;
246  frame_count += nnet_decodable.NumFramesReady();
247  num_success++;
248  } else num_fail++;
249  }
250  }
251 
252  kaldi::int64 input_frame_count =
253  frame_count * decodable_opts.frame_subsampling_factor;
254 
255  double elapsed = timer.Elapsed();
256  KALDI_LOG << "Time taken "<< elapsed
257  << "s: real-time factor assuming 100 frames/sec is "
258  << (elapsed * 100.0 / input_frame_count);
259  KALDI_LOG << "Done " << num_success << " utterances, failed for "
260  << num_fail;
261  KALDI_LOG << "Overall log-likelihood per frame is "
262  << (tot_like / frame_count) << " over "
263  << frame_count << " frames.";
264 
265  delete word_syms;
266  if (num_success != 0) return 0;
267  else return 1;
268  } catch(const std::exception &e) {
269  std::cerr << e.what();
270  return -1;
271  }
272 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CollapseModel(const CollapseModelConfig &config, Nnet *nnet)
This function modifies the neural net for efficiency, in a way that suitable to be done in test time...
Definition: nnet-utils.cc:2100
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
fst::StdArc StdArc
This class is for when you are reading something in random access, but it may actually be stored per-...
Definition: kaldi-table.h:432
This class enables you to do the compilation and optimization in one call, and also ensures that if t...
void SetBatchnormTestMode(bool test_mode, Nnet *nnet)
This function affects only components of type BatchNormComponent.
Definition: nnet-utils.cc:564
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...
const Nnet & GetNnet() const
void Read(std::istream &is, bool binary)
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
void SetDropoutTestMode(bool test_mode, Nnet *nnet)
This function affects components of child-classes of RandomComponent.
Definition: nnet-utils.cc:573
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
This is the "normal" lattice-generating decoder.
A class representing a vector.
Definition: kaldi-vector.h:406
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
Config class for the CollapseModel function.
Definition: nnet-utils.h:240