gmm-latgen-simple.cc
Go to the documentation of this file.
1 // gmmbin/gmm-latgen-simple.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2013 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 "gmm/am-diag-gmm.h"
26 #include "tree/context-dep.h"
27 #include "hmm/transition-model.h"
28 #include "fstext/fstext-lib.h"
31 #include "base/timer.h"
32 
33 
34 
35 int main(int argc, char *argv[]) {
36  try {
37  using namespace kaldi;
38  typedef kaldi::int32 int32;
39  using fst::SymbolTable;
40  using fst::Fst;
41  using fst::StdArc;
42 
43  const char *usage =
44  "Generate lattices using GMM-based model.\n"
45  "Usage: gmm-latgen-simple [options] model-in fst-in features-rspecifier"
46  " lattice-wspecifier [ words-wspecifier [alignments-wspecifier] ]\n";
47  ParseOptions po(usage);
48  Timer timer;
49  bool allow_partial = false;
50  BaseFloat acoustic_scale = 0.1;
52 
53  std::string word_syms_filename;
54  config.Register(&po);
55  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
56 
57  po.Register("word-symbol-table", &word_syms_filename, "Symbol table for words [for debug output]");
58  po.Register("allow-partial", &allow_partial, "If true, produce output even if end state was not reached.");
59 
60  po.Read(argc, argv);
61 
62  if (po.NumArgs() < 4 || po.NumArgs() > 6) {
63  po.PrintUsage();
64  exit(1);
65  }
66 
67  std::string model_in_filename = po.GetArg(1),
68  fst_in_filename = po.GetArg(2),
69  feature_rspecifier = po.GetArg(3),
70  lattice_wspecifier = po.GetArg(4),
71  words_wspecifier = po.GetOptArg(5),
72  alignment_wspecifier = po.GetOptArg(6);
73 
74  TransitionModel trans_model;
75  AmDiagGmm am_gmm;
76  {
77  bool binary;
78  Input ki(model_in_filename, &binary);
79  trans_model.Read(ki.Stream(), binary);
80  am_gmm.Read(ki.Stream(), binary);
81  }
82 
83  Fst<StdArc> *decode_fst = fst::ReadFstKaldiGeneric(fst_in_filename);
84 
85  bool determinize = config.determinize_lattice;
86  CompactLatticeWriter compact_lattice_writer;
87  LatticeWriter lattice_writer;
88  if (! (determinize ? compact_lattice_writer.Open(lattice_wspecifier)
89  : lattice_writer.Open(lattice_wspecifier)))
90  KALDI_ERR << "Could not open table for writing lattices: "
91  << lattice_wspecifier;
92 
93  Int32VectorWriter words_writer(words_wspecifier);
94 
95  Int32VectorWriter alignment_writer(alignment_wspecifier);
96 
97  fst::SymbolTable *word_syms = NULL;
98  if (word_syms_filename != "")
99  if (!(word_syms = fst::SymbolTable::ReadText(word_syms_filename)))
100  KALDI_ERR << "Could not read symbol table from file "
101  << word_syms_filename;
102 
103  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
104 
105  BaseFloat tot_like = 0.0;
106  kaldi::int64 frame_count = 0;
107  int num_success = 0, num_fail = 0;
108  LatticeSimpleDecoder decoder(*decode_fst, config);
109 
110  for (; !feature_reader.Done(); feature_reader.Next()) {
111  std::string utt = feature_reader.Key();
112  Matrix<BaseFloat> features (feature_reader.Value());
113  feature_reader.FreeCurrent();
114  if (features.NumRows() == 0) {
115  KALDI_WARN << "Zero-length utterance: " << utt;
116  num_fail++;
117  continue;
118  }
119 
120  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
121  acoustic_scale);
122 
123  double like;
125  decoder, gmm_decodable, trans_model, word_syms, utt,
126  acoustic_scale, determinize, allow_partial, &alignment_writer,
127  &words_writer, &compact_lattice_writer, &lattice_writer, &like)) {
128  tot_like += like;
129  frame_count += features.NumRows();
130  num_success++;
131  } else num_fail++;
132  }
133 
134  double elapsed = timer.Elapsed();
135  KALDI_LOG << "Time taken "<< elapsed
136  << "s: real-time factor assuming 100 frames/sec is "
137  << (elapsed*100.0/frame_count);
138  KALDI_LOG << "Done " << num_success << " utterances, failed for "
139  << num_fail;
140  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count) << " over "
141  << frame_count<<" frames.";
142 
143  delete decode_fst;
144  delete word_syms;
145  if (num_success != 0) return 0;
146  else return 1;
147  } catch(const std::exception &e) {
148  std::cerr << e.what();
149  return -1;
150  }
151 }
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 PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
int main(int argc, char *argv[])
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
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.
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)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_LOG
Definition: kaldi-error.h:153
double Elapsed() const
Returns time in seconds.
Definition: timer.h:74
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
std::string GetOptArg(int param) const