gmm-latgen-map.cc
Go to the documentation of this file.
1 // gmmbin/gmm-latgen-map.cc
2 
3 // Copyright 2012 Neha Agrawal, Cisco Systems;
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 #include <string>
23 #include <vector>
24 
25 #include "base/kaldi-common.h"
26 #include "util/common-utils.h"
27 #include "gmm/am-diag-gmm.h"
28 #include "gmm/mle-am-diag-gmm.h"
29 #include "hmm/transition-model.h"
31 #include "fstext/fstext-lib.h"
34 #include "base/timer.h"
35 #include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
36 
37 
38 int main(int argc, char *argv[]) {
39  try {
40  using namespace kaldi;
41  using std::string;
42  typedef kaldi::int32 int32;
43  using fst::SymbolTable;
44  using fst::Fst;
45  using fst::StdArc;
46 
47  const char *usage = "Decode features using GMM-based model. Note: the input\n"
48  "<gmms-rspecifier> will typically be piped in from gmm-est-map.\n"
49  "Note: <model-in> is only needed for the transition-model, which isn't\n"
50  "included in <gmms-rspecifier>.\n"
51  "\n"
52  "Usage: gmm-latgen-map [options] <model-in> "
53  "<gmms-rspecifier> <fsts-rxfilename|fsts-rspecifier> <features-rspecifier> "
54  "<lattice-wspecifier> [ <words-wspecifier> [ <alignments-wspecifier> ] ]\n";
55 
56  ParseOptions po(usage);
57  bool binary = true;
58  bool allow_partial = true;
59  BaseFloat acoustic_scale = 0.1;
60 
61  std::string word_syms_filename, utt2spk_rspecifier;
62  LatticeFasterDecoderConfig decoder_opts;
63  decoder_opts.Register(&po);
64  po.Register("utt2spk", &utt2spk_rspecifier, "rspecifier for utterance to "
65  "speaker map");
66  po.Register("binary", &binary, "Write output in binary mode");
67  po.Register("acoustic-scale", &acoustic_scale,
68  "Scaling factor for acoustic likelihoods");
69  po.Register("word-symbol-table", &word_syms_filename,
70  "Symbol table for words [for debug output]");
71  po.Register("allow-partial", &allow_partial,
72  "Produce output even when final state was not reached");
73  po.Read(argc, argv);
74 
75  if (po.NumArgs() < 5 || po.NumArgs() > 7) {
76  po.PrintUsage();
77  exit(1);
78  }
79 
80  std::string model_in_filename = po.GetArg(1),
81  gmms_rspecifier = po.GetArg(2),
82  fst_in_filename = po.GetArg(3),
83  feature_rspecifier = po.GetArg(4),
84  lattice_wspecifier = po.GetArg(5),
85  words_wspecifier = po.GetOptArg(6),
86  alignment_wspecifier = po.GetOptArg(7);
87 
88  TransitionModel trans_model;
89  {
90  bool binary_read;
91  Input is(model_in_filename, &binary_read);
92  trans_model.Read(is.Stream(), binary_read);
93  }
94  RandomAccessMapAmDiagGmmReaderMapped gmms_reader(gmms_rspecifier,
95  utt2spk_rspecifier);
96 
97  Int32VectorWriter words_writer(words_wspecifier);
98  Int32VectorWriter alignment_writer(alignment_wspecifier);
99 
100 
101  bool determinize = decoder_opts.determinize_lattice;
102  if (!determinize)
103  KALDI_WARN << "determinize is set to FASLE ...";
104  CompactLatticeWriter compact_lattice_writer;
105  LatticeWriter lattice_writer;
106 
107  if (lattice_wspecifier != "") {
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 
114  fst::SymbolTable *word_syms = NULL;
115  if (word_syms_filename != "") {
116  word_syms = fst::SymbolTable::ReadText(word_syms_filename);
117  if (!word_syms) {
118  KALDI_ERR << "Could not read symbol table from file "
119  << word_syms_filename;
120  }
121  }
122 
123  BaseFloat tot_like = 0.0;
124  kaldi::int64 frame_count = 0;
125  int num_success = 0, num_fail = 0;
126  Timer timer;
127 
128  if (ClassifyRspecifier(fst_in_filename, NULL, NULL) == kNoRspecifier) {
129  // Input FST is just one FST, not a table of FSTs.
130  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_filename);
131 
132  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
133  for (; !feature_reader.Done(); feature_reader.Next()) {
134  string utt = feature_reader.Key();
135 
136  if (!gmms_reader.HasKey(utt)) {
137  KALDI_WARN << "Utterance " << utt
138  << " has no corresponding MAP model skipping this utterance.";
139  num_fail++;
140  continue;
141  }
142  AmDiagGmm am_gmm;
143  am_gmm.CopyFromAmDiagGmm(gmms_reader.Value(utt));
144 
145  Matrix<BaseFloat> features(feature_reader.Value());
146  feature_reader.FreeCurrent();
147  if (features.NumRows() == 0) {
148  KALDI_WARN << "Zero-length utterance: " << utt;
149  num_fail++;
150  continue;
151  }
152 
153  LatticeFasterDecoder decoder(*decode_fst, decoder_opts);
154  kaldi::DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model,
155  features,
156  acoustic_scale);
157  double like;
159  decoder, gmm_decodable, trans_model, word_syms, utt,
160  acoustic_scale, determinize, allow_partial, &alignment_writer,
161  &words_writer, &compact_lattice_writer, &lattice_writer,
162  &like)) {
163  tot_like += like;
164  frame_count += features.NumRows();
165  num_success++;
166  } else num_fail++;
167  } // end looping over all utterances
168  } else {
169  RandomAccessTableReader<fst::VectorFstHolder> fst_reader(fst_in_filename);
170  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
171  for (; !feature_reader.Done(); feature_reader.Next()) {
172  string utt = feature_reader.Key();
173 
174  if (!fst_reader.HasKey(utt)) {
175  KALDI_WARN << "Utterance " << utt << " has no corresponding FST"
176  << "skipping this utterance.";
177  num_fail++;
178  continue;
179  }
180 
181  if (!gmms_reader.HasKey(utt)) {
182  KALDI_WARN << "Utterance " << utt
183  << " has no corresponding MAP model skipping this utterance.";
184  num_fail++;
185  continue;
186  }
187  AmDiagGmm am_gmm;
188  am_gmm.CopyFromAmDiagGmm(gmms_reader.Value(utt));
189 
190  Matrix<BaseFloat> features(feature_reader.Value());
191  feature_reader.FreeCurrent();
192  if (features.NumRows() == 0) {
193  KALDI_WARN << "Zero-length utterance: " << utt;
194  num_fail++;
195  continue;
196  }
197 
198  LatticeFasterDecoder decoder(fst_reader.Value(utt), decoder_opts);
199  kaldi::DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model,
200  features,
201  acoustic_scale);
202  double like;
204  decoder, gmm_decodable, trans_model, word_syms, utt,
205  acoustic_scale, determinize, allow_partial, &alignment_writer,
206  &words_writer, &compact_lattice_writer, &lattice_writer,
207  &like)) {
208  tot_like += like;
209  frame_count += features.NumRows();
210  num_success++;
211  } else num_fail++;
212  } // end looping over all utterances
213  }
214  KALDI_LOG << "Average log-likelihood per frame is "
215  << (tot_like / frame_count) << " over " << frame_count << " frames.";
216 
217  double elapsed = timer.Elapsed();
218  KALDI_LOG << "Time taken [excluding initialization] " << elapsed
219  << "s: real-time factor assuming 100 frames/sec is "
220  << (elapsed * 100.0 / frame_count);
221  KALDI_LOG << "Done " << num_success << " utterances, failed for "
222  << num_fail;
223 
224  delete word_syms;
225  return (num_success != 0 ? 0 : 1);
226  }
227  catch(const std::exception& e) {
228  std::cerr << e.what();
229  return -1;
230  }
231 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
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
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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
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 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).
const T & Value(const std::string &key)
#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