compile-train-graphs-fsts.cc
Go to the documentation of this file.
1 // bin/compile-train-graphs-fsts.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // 2012-2015 Johns Hopkins University (Author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "tree/context-dep.h"
24 #include "hmm/transition-model.h"
25 #include "fstext/fstext-lib.h"
27 
28 
29 int main(int argc, char *argv[]) {
30  try {
31  using namespace kaldi;
32  typedef kaldi::int32 int32;
33  using fst::SymbolTable;
34  using fst::VectorFst;
35  using fst::StdArc;
36 
37  const char *usage =
38  "Creates training graphs (without transition-probabilities, by default)\n"
39  "This version takes FSTs as inputs (e.g., representing a separate weighted\n"
40  "grammar for each utterance)\n"
41  "Note: the lexicon should contain disambiguation symbols and you should\n"
42  "supply the --read-disambig-syms option which is the filename of a list\n"
43  "of disambiguation symbols.\n"
44  "Warning: you probably want to set the --transition-scale and --self-loop-scale\n"
45  "options; the defaults (zero) are probably not appropriate.\n"
46  "Usage: compile-train-graphs-fsts [options] <tree-in> <model-in> <lexicon-fst-in> "
47  " <graphs-rspecifier> <graphs-wspecifier>\n"
48  "e.g.: \n"
49  " compile-train-graphs-fsts --read-disambig-syms=disambig.list\\\n"
50  " tree 1.mdl lex.fst ark:train.fsts ark:graphs.fsts\n";
51  ParseOptions po(usage);
52 
54  int32 batch_size = 250;
55  gopts.transition_scale = 0.0; // Change the default to 0.0 since we will generally add the
56  // transition probs in the alignment phase (since they change each time)
57  gopts.self_loop_scale = 0.0; // Ditto for self-loop probs.
58  std::string disambig_rxfilename;
59  gopts.Register(&po);
60 
61  po.Register("batch-size", &batch_size,
62  "Number of FSTs to compile at a time (more -> faster but uses "
63  "more memory. E.g. 500");
64  po.Register("read-disambig-syms", &disambig_rxfilename, "File containing "
65  "list of disambiguation symbols in phone symbol table");
66 
67  po.Read(argc, argv);
68 
69  if (po.NumArgs() != 5) {
70  po.PrintUsage();
71  exit(1);
72  }
73 
74  std::string tree_rxfilename = po.GetArg(1);
75  std::string model_rxfilename = po.GetArg(2);
76  std::string lex_rxfilename = po.GetArg(3);
77  std::string fsts_rspecifier = po.GetArg(4);
78  std::string fsts_wspecifier = po.GetArg(5);
79 
80  ContextDependency ctx_dep; // the tree.
81  ReadKaldiObject(tree_rxfilename, &ctx_dep);
82 
83  TransitionModel trans_model;
84  ReadKaldiObject(model_rxfilename, &trans_model);
85 
86  // need VectorFst because we will change it by adding subseq symbol.
87  // ownership will be taken by gc.
88  VectorFst<StdArc> *lex_fst = fst::ReadFstKaldi(lex_rxfilename);
89 
90  std::vector<int32> disambig_syms;
91  if (disambig_rxfilename != "")
92  if (!ReadIntegerVectorSimple(disambig_rxfilename, &disambig_syms))
93  KALDI_ERR << "fstcomposecontext: Could not read disambiguation symbols from "
94  << disambig_rxfilename;
95  if (disambig_syms.empty())
96  KALDI_WARN << "You supplied no disambiguation symbols; note, these are "
97  << "typically necessary when compiling graphs from FSTs (i.e. "
98  << "supply L_disambig.fst and the list of disambig syms with\n"
99  << "--read-disambig-syms)";
100  TrainingGraphCompiler gc(trans_model, ctx_dep, lex_fst, disambig_syms, gopts);
101 
102  lex_fst = NULL; // we gave ownership to gc.
103 
104  SequentialTableReader<fst::VectorFstHolder> fst_reader(fsts_rspecifier);
105  TableWriter<fst::VectorFstHolder> fst_writer(fsts_wspecifier);
106 
107  int num_succeed = 0, num_fail = 0;
108 
109  if (batch_size == 1) { // We treat batch_size of 1 as a special case in order
110  // to test more parts of the code.
111  for (; !fst_reader.Done(); fst_reader.Next()) {
112  std::string key = fst_reader.Key();
113  const VectorFst<StdArc> &grammar = fst_reader.Value(); // weighted
114  // grammar for this utterance.
115  VectorFst<StdArc> decode_fst;
116 
117  if (!gc.CompileGraph(grammar, &decode_fst)) {
118  decode_fst.DeleteStates(); // Just make it empty.
119  }
120  if (decode_fst.Start() != fst::kNoStateId) {
121  num_succeed++;
122  fst_writer.Write(key, decode_fst);
123  } else {
124  KALDI_WARN << "Empty decoding graph for utterance "
125  << key;
126  num_fail++;
127  }
128  }
129  } else {
130  std::vector<std::string> keys;
131  std::vector<const VectorFst<StdArc>*> grammars; // word grammars.
132  while (!fst_reader.Done()) {
133  keys.clear();
134  grammars.clear();
135  for (; !fst_reader.Done() &&
136  static_cast<int32>(grammars.size()) < batch_size;
137  fst_reader.Next()) {
138  keys.push_back(fst_reader.Key());
139  grammars.push_back(new VectorFst<StdArc>(fst_reader.Value()));
140  }
141  std::vector<fst::VectorFst<fst::StdArc>* > fsts;
142  if (!gc.CompileGraphs(grammars, &fsts))
143  KALDI_ERR << "Not expecting CompileGraphs to fail.";
144  KALDI_ASSERT(fsts.size() == keys.size());
145 
146  for (size_t i = 0; i < fsts.size(); i++) {
147  delete grammars[i];
148  if (fsts[i]->Start() != fst::kNoStateId) {
149  num_succeed++;
150  fst_writer.Write(keys[i], *(fsts[i]));
151  } else {
152  KALDI_WARN << "Empty decoding graph for utterance "
153  << keys[i];
154  num_fail++;
155  }
156  }
157  DeletePointers(&fsts);
158  }
159  }
160  KALDI_LOG << "compile-train-graphs: succeeded for " << num_succeed
161  << " graphs, failed for " << num_fail;
162  return 0;
163  } catch(const std::exception &e) {
164  std::cerr << e.what();
165  return -1;
166  }
167 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void DeletePointers(std::vector< A *> *v)
Deletes any non-NULL pointers in the vector v, and sets the corresponding entries of v to NULL...
Definition: stl-utils.h:184
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 Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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 CompileGraph(const fst::VectorFst< fst::StdArc > &word_grammar, fst::VectorFst< fst::StdArc > *out_fst)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
bool CompileGraphs(const std::vector< const fst::VectorFst< fst::StdArc > *> &word_fsts, std::vector< fst::VectorFst< fst::StdArc > *> *out_fsts)
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
int main(int argc, char *argv[])
bool ReadIntegerVectorSimple(const std::string &rxfilename, std::vector< int32 > *list)
ReadFromList attempts to read this list of integers, one per line, from the given file...
#define KALDI_LOG
Definition: kaldi-error.h:153