compile-train-graphs.cc
Go to the documentation of this file.
1 // bin/compile-train-graphs.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  "\n"
40  "Usage: compile-train-graphs [options] <tree-in> <model-in> "
41  "<lexicon-fst-in> <transcriptions-rspecifier> <graphs-wspecifier>\n"
42  "e.g.: \n"
43  " compile-train-graphs tree 1.mdl lex.fst "
44  "'ark:sym2int.pl -f 2- words.txt text|' ark:graphs.fsts\n";
45  ParseOptions po(usage);
46 
48  int32 batch_size = 250;
49  gopts.transition_scale = 0.0; // Change the default to 0.0 since we will generally add the
50  // transition probs in the alignment phase (since they change eacm time)
51  gopts.self_loop_scale = 0.0; // Ditto for self-loop probs.
52  std::string disambig_rxfilename;
53  gopts.Register(&po);
54 
55  po.Register("batch-size", &batch_size,
56  "Number of FSTs to compile at a time (more -> faster but uses "
57  "more memory. E.g. 500");
58  po.Register("read-disambig-syms", &disambig_rxfilename, "File containing "
59  "list of disambiguation symbols in phone symbol table");
60 
61  po.Read(argc, argv);
62 
63  if (po.NumArgs() != 5) {
64  po.PrintUsage();
65  exit(1);
66  }
67 
68  std::string tree_rxfilename = po.GetArg(1);
69  std::string model_rxfilename = po.GetArg(2);
70  std::string lex_rxfilename = po.GetArg(3);
71  std::string transcript_rspecifier = po.GetArg(4);
72  std::string fsts_wspecifier = po.GetArg(5);
73 
74  ContextDependency ctx_dep; // the tree.
75  ReadKaldiObject(tree_rxfilename, &ctx_dep);
76 
77  TransitionModel trans_model;
78  ReadKaldiObject(model_rxfilename, &trans_model);
79 
80  // need VectorFst because we will change it by adding subseq symbol.
81  VectorFst<StdArc> *lex_fst = fst::ReadFstKaldi(lex_rxfilename);
82 
83  std::vector<int32> disambig_syms;
84  if (disambig_rxfilename != "")
85  if (!ReadIntegerVectorSimple(disambig_rxfilename, &disambig_syms))
86  KALDI_ERR << "fstcomposecontext: Could not read disambiguation symbols from "
87  << disambig_rxfilename;
88 
89  TrainingGraphCompiler gc(trans_model, ctx_dep, lex_fst, disambig_syms, gopts);
90 
91  lex_fst = NULL; // we gave ownership to gc.
92 
93  SequentialInt32VectorReader transcript_reader(transcript_rspecifier);
94  TableWriter<fst::VectorFstHolder> fst_writer(fsts_wspecifier);
95 
96  int num_succeed = 0, num_fail = 0;
97 
98  if (batch_size == 1) { // We treat batch_size of 1 as a special case in order
99  // to test more parts of the code.
100  for (; !transcript_reader.Done(); transcript_reader.Next()) {
101  std::string key = transcript_reader.Key();
102  const std::vector<int32> &transcript = transcript_reader.Value();
103  VectorFst<StdArc> decode_fst;
104 
105  if (!gc.CompileGraphFromText(transcript, &decode_fst)) {
106  decode_fst.DeleteStates(); // Just make it empty.
107  }
108  if (decode_fst.Start() != fst::kNoStateId) {
109  num_succeed++;
110  fst_writer.Write(key, decode_fst);
111  } else {
112  KALDI_WARN << "Empty decoding graph for utterance "
113  << key;
114  num_fail++;
115  }
116  }
117  } else {
118  std::vector<std::string> keys;
119  std::vector<std::vector<int32> > transcripts;
120  while (!transcript_reader.Done()) {
121  keys.clear();
122  transcripts.clear();
123  for (; !transcript_reader.Done() &&
124  static_cast<int32>(transcripts.size()) < batch_size;
125  transcript_reader.Next()) {
126  keys.push_back(transcript_reader.Key());
127  transcripts.push_back(transcript_reader.Value());
128  }
129  std::vector<fst::VectorFst<fst::StdArc>* > fsts;
130  if (!gc.CompileGraphsFromText(transcripts, &fsts)) {
131  KALDI_ERR << "Not expecting CompileGraphs to fail.";
132  }
133  KALDI_ASSERT(fsts.size() == keys.size());
134  for (size_t i = 0; i < fsts.size(); i++) {
135  if (fsts[i]->Start() != fst::kNoStateId) {
136  num_succeed++;
137  fst_writer.Write(keys[i], *(fsts[i]));
138  } else {
139  KALDI_WARN << "Empty decoding graph for utterance "
140  << keys[i];
141  num_fail++;
142  }
143  }
144  DeletePointers(&fsts);
145  }
146  }
147  KALDI_LOG << "compile-train-graphs: succeeded for " << num_succeed
148  << " graphs, failed for " << num_fail;
149  return (num_succeed != 0 ? 0 : 1);
150  } catch(const std::exception &e) {
151  std::cerr << e.what();
152  return -1;
153  }
154 }
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
int main(int argc, char *argv[])
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
bool CompileGraphFromText(const std::vector< int32 > &transcript, fst::VectorFst< fst::StdArc > *out_fst)
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 CompileGraphsFromText(const std::vector< std::vector< int32 > > &word_grammar, std::vector< fst::VectorFst< fst::StdArc > *> *out_fsts)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
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