All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
compile-train-graphs.cc File Reference
Include dependency graph for compile-train-graphs.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 29 of file compile-train-graphs.cc.

References TrainingGraphCompiler::CompileGraphFromText(), TrainingGraphCompiler::CompileGraphsFromText(), kaldi::DeletePointers(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, KALDI_ASSERT, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), fst::ReadFstKaldi(), kaldi::ReadIntegerVectorSimple(), kaldi::ReadKaldiObject(), TrainingGraphCompilerOptions::Register(), ParseOptions::Register(), TrainingGraphCompilerOptions::self_loop_scale, TrainingGraphCompilerOptions::transition_scale, SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

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