All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-latgen-map.cc File Reference
#include <string>
#include <vector>
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "gmm/am-diag-gmm.h"
#include "gmm/mle-am-diag-gmm.h"
#include "hmm/transition-model.h"
#include "transform/fmllr-diag-gmm.h"
#include "fstext/fstext-lib.h"
#include "decoder/decoder-wrappers.h"
#include "gmm/decodable-am-diag-gmm.h"
#include "base/timer.h"
#include "lat/kaldi-lattice.h"
Include dependency graph for gmm-latgen-map.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 38 of file gmm-latgen-map.cc.

References kaldi::ClassifyRspecifier(), AmDiagGmm::CopyFromAmDiagGmm(), kaldi::DecodeUtteranceLatticeFaster(), LatticeFasterDecoderConfig::determinize_lattice, SequentialTableReader< Holder >::Done(), Timer::Elapsed(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), RandomAccessTableReader< Holder >::HasKey(), RandomAccessTableReaderMapped< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), TableWriter< Holder >::Open(), ParseOptions::PrintUsage(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldiGeneric(), LatticeFasterDecoderConfig::Register(), ParseOptions::Register(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and RandomAccessTableReaderMapped< Holder >::Value().

38  {
39  try {
40  using namespace kaldi;
41  typedef kaldi::int32 int32;
42  using fst::SymbolTable;
43  using fst::Fst;
44  using fst::StdArc;
45 
46  const char *usage = "Decode features using GMM-based model. Note: the input\n"
47  "<gmms-rspecifier> will typically be piped in from gmm-est-map.\n"
48  "Note: <model-in> is only needed for the transition-model, which isn't\n"
49  "included in <gmms-rspecifier>.\n"
50  "\n"
51  "Usage: gmm-latgen-map [options] <model-in> "
52  "<gmms-rspecifier> <fsts-rxfilename|fsts-rspecifier> <features-rspecifier> "
53  "<lattice-wspecifier> [ <words-wspecifier> [ <alignments-wspecifier> ] ]\n";
54 
55  ParseOptions po(usage);
56  bool binary = true;
57  bool allow_partial = true;
58  BaseFloat acoustic_scale = 0.1;
59 
60  std::string word_syms_filename, utt2spk_rspecifier;
61  LatticeFasterDecoderConfig decoder_opts;
62  decoder_opts.Register(&po);
63  po.Register("utt2spk", &utt2spk_rspecifier, "rspecifier for utterance to "
64  "speaker map");
65  po.Register("binary", &binary, "Write output in binary mode");
66  po.Register("acoustic-scale", &acoustic_scale,
67  "Scaling factor for acoustic likelihoods");
68  po.Register("word-symbol-table", &word_syms_filename,
69  "Symbol table for words [for debug output]");
70  po.Register("allow-partial", &allow_partial,
71  "Produce output even when final state was not reached");
72  po.Read(argc, argv);
73 
74  if (po.NumArgs() < 5 || po.NumArgs() > 7) {
75  po.PrintUsage();
76  exit(1);
77  }
78 
79  std::string model_in_filename = po.GetArg(1),
80  gmms_rspecifier = po.GetArg(2),
81  fst_in_filename = po.GetArg(3),
82  feature_rspecifier = po.GetArg(4),
83  lattice_wspecifier = po.GetArg(5),
84  words_wspecifier = po.GetOptArg(6),
85  alignment_wspecifier = po.GetOptArg(7);
86 
87  TransitionModel trans_model;
88  {
89  bool binary_read;
90  Input is(model_in_filename, &binary_read);
91  trans_model.Read(is.Stream(), binary_read);
92  }
93  RandomAccessMapAmDiagGmmReaderMapped gmms_reader(gmms_rspecifier,
94  utt2spk_rspecifier);
95 
96  Int32VectorWriter words_writer(words_wspecifier);
97  Int32VectorWriter alignment_writer(alignment_wspecifier);
98 
99 
100  bool determinize = decoder_opts.determinize_lattice;
101  if (!determinize)
102  KALDI_WARN << "determinize is set to FASLE ...";
103  CompactLatticeWriter compact_lattice_writer;
104  LatticeWriter lattice_writer;
105 
106  if (lattice_wspecifier != "") {
107  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
108  : lattice_writer.Open(lattice_wspecifier)))
109  KALDI_ERR << "Could not open table for writing lattices: "
110  << lattice_wspecifier;
111  }
112 
113  fst::SymbolTable *word_syms = NULL;
114  if (word_syms_filename != "") {
115  word_syms = fst::SymbolTable::ReadText(word_syms_filename);
116  if (!word_syms) {
117  KALDI_ERR << "Could not read symbol table from file "
118  << word_syms_filename;
119  }
120  }
121 
122  BaseFloat tot_like = 0.0;
123  kaldi::int64 frame_count = 0;
124  int num_success = 0, num_fail = 0;
125  Timer timer;
126 
127  if (ClassifyRspecifier(fst_in_filename, NULL, NULL) == kNoRspecifier) {
128  // Input FST is just one FST, not a table of FSTs.
129  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_filename);
130 
131  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
132  for (; !feature_reader.Done(); feature_reader.Next()) {
133  string utt = feature_reader.Key();
134 
135  if (!gmms_reader.HasKey(utt)) {
136  KALDI_WARN << "Utterance " << utt
137  << " has no corresponding MAP model skipping this utterance.";
138  num_fail++;
139  continue;
140  }
141  AmDiagGmm am_gmm;
142  am_gmm.CopyFromAmDiagGmm(gmms_reader.Value(utt));
143 
144  Matrix<BaseFloat> features(feature_reader.Value());
145  feature_reader.FreeCurrent();
146  if (features.NumRows() == 0) {
147  KALDI_WARN << "Zero-length utterance: " << utt;
148  num_fail++;
149  continue;
150  }
151 
152  LatticeFasterDecoder decoder(*decode_fst, decoder_opts);
153  kaldi::DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model,
154  features,
155  acoustic_scale);
156  double like;
158  decoder, gmm_decodable, trans_model, word_syms, utt,
159  acoustic_scale, determinize, allow_partial, &alignment_writer,
160  &words_writer, &compact_lattice_writer, &lattice_writer,
161  &like)) {
162  tot_like += like;
163  frame_count += features.NumRows();
164  num_success++;
165  } else num_fail++;
166  } // end looping over all utterances
167  } else {
168  RandomAccessTableReader<fst::VectorFstHolder> fst_reader(fst_in_filename);
169  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
170  for (; !feature_reader.Done(); feature_reader.Next()) {
171  string utt = feature_reader.Key();
172 
173  if (!fst_reader.HasKey(utt)) {
174  KALDI_WARN << "Utterance " << utt << " has no corresponding FST"
175  << "skipping this utterance.";
176  num_fail++;
177  continue;
178  }
179 
180  if (!gmms_reader.HasKey(utt)) {
181  KALDI_WARN << "Utterance " << utt
182  << " has no corresponding MAP model skipping this utterance.";
183  num_fail++;
184  continue;
185  }
186  AmDiagGmm am_gmm;
187  am_gmm.CopyFromAmDiagGmm(gmms_reader.Value(utt));
188 
189  Matrix<BaseFloat> features(feature_reader.Value());
190  feature_reader.FreeCurrent();
191  if (features.NumRows() == 0) {
192  KALDI_WARN << "Zero-length utterance: " << utt;
193  num_fail++;
194  continue;
195  }
196 
197  LatticeFasterDecoder decoder(fst_reader.Value(utt), decoder_opts);
198  kaldi::DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model,
199  features,
200  acoustic_scale);
201  double like;
203  decoder, gmm_decodable, trans_model, word_syms, utt,
204  acoustic_scale, determinize, allow_partial, &alignment_writer,
205  &words_writer, &compact_lattice_writer, &lattice_writer,
206  &like)) {
207  tot_like += like;
208  frame_count += features.NumRows();
209  num_success++;
210  } else num_fail++;
211  } // end looping over all utterances
212  }
213  KALDI_LOG << "Average log-likelihood per frame is "
214  << (tot_like / frame_count) << " over " << frame_count << " frames.";
215 
216  double elapsed = timer.Elapsed();
217  KALDI_LOG << "Time taken [excluding initialization] " << elapsed
218  << "s: real-time factor assuming 100 frames/sec is "
219  << (elapsed * 100.0 / frame_count);
220  KALDI_LOG << "Done " << num_success << " utterances, failed for "
221  << num_fail;
222 
223  delete word_syms;
224  return (num_success != 0 ? 0 : 1);
225  }
226  catch(const std::exception& e) {
227  std::cerr << e.what();
228  return -1;
229  }
230 }
A bit more optimized version of the lattice decoder.
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void CopyFromAmDiagGmm(const AmDiagGmm &other)
Copies the parameters from another model. Allocates necessary memory.
Definition: am-diag-gmm.cc:79
bool Open(const std::string &wspecifier)
Fst< StdArc > * ReadFstKaldiGeneric(std::string rxfilename, bool throw_on_err)
Definition: kaldi-fst-io.cc:45
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:431
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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
void Read(std::istream &is, bool binary)
bool DecodeUtteranceLatticeFaster(LatticeFasterDecoder &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...
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:127
#define KALDI_WARN
Definition: kaldi-error.h:130
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
#define KALDI_LOG
Definition: kaldi-error.h:133