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

References kaldi::AlignUtteranceWrapper(), TrainingGraphCompiler::CompileGraphFromText(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), fst::ReadFstKaldi(), kaldi::ReadIntegerVectorSimple(), kaldi::ReadKaldiObject(), AlignConfig::Register(), TrainingGraphCompilerOptions::Register(), ParseOptions::Register(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), and SequentialTableReader< Holder >::Value().

32  {
33  try {
34  using namespace kaldi;
35  typedef kaldi::int32 int32;
36  using fst::SymbolTable;
37  using fst::VectorFst;
38  using fst::StdArc;
39 
40  const char *usage =
41  "Align features given [GMM-based] models.\n"
42  "Usage: gmm-align [options] tree-in model-in lexicon-fst-in feature-rspecifier transcriptions-rspecifier alignments-wspecifier\n"
43  "e.g.: \n"
44  " gmm-align tree 1.mdl lex.fst scp:train.scp ark:train.tra ark:1.ali\n";
45  ParseOptions po(usage);
46  AlignConfig align_config;
47  BaseFloat acoustic_scale = 1.0;
48  std::string disambig_rxfilename;
50 
51  align_config.Register(&po);
52  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
53  po.Register("read-disambig-syms", &disambig_rxfilename, "File containing "
54  "list of disambiguation symbols in phone symbol table");
55 
56  gopts.Register(&po);
57  po.Read(argc, argv);
58 
59  if (po.NumArgs() != 6) {
60  po.PrintUsage();
61  exit(1);
62  }
63 
64  std::string tree_in_filename = po.GetArg(1);
65  std::string model_in_filename = po.GetArg(2);
66  std::string lex_in_filename = po.GetArg(3);
67  std::string feature_rspecifier = po.GetArg(4);
68  std::string transcript_rspecifier = po.GetArg(5);
69  std::string alignment_wspecifier = po.GetArg(6);
70 
71  ContextDependency ctx_dep;
72  ReadKaldiObject(tree_in_filename, &ctx_dep);
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  // ownership will be taken by gc.
84  VectorFst<StdArc> *lex_fst = fst::ReadFstKaldi(lex_in_filename);
85 
86  std::vector<int32> disambig_syms;
87  if (disambig_rxfilename != "")
88  if (!ReadIntegerVectorSimple(disambig_rxfilename, &disambig_syms))
89  KALDI_ERR << "fstcomposecontext: Could not read disambiguation symbols from "
90  << disambig_rxfilename;
91 
92  TrainingGraphCompiler gc(trans_model, ctx_dep, lex_fst, disambig_syms,
93  gopts);
94 
95  lex_fst = NULL; // we gave ownership to gc.
96 
97  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
98  RandomAccessInt32VectorReader transcript_reader(transcript_rspecifier);
99  Int32VectorWriter alignment_writer(alignment_wspecifier);
100 
101  int32 num_done = 0, num_err = 0, num_retry = 0;
102  double tot_like = 0.0;
103  kaldi::int64 frame_count = 0;
104  for (; !feature_reader.Done(); feature_reader.Next()) {
105  std::string utt = feature_reader.Key();
106  if (!transcript_reader.HasKey(utt)) {
107  KALDI_WARN << "No transcript found for utterance " << utt;
108  num_err++;
109  continue;
110  }
111 
112  const Matrix<BaseFloat> &features = feature_reader.Value();
113  const std::vector<int32> &transcript = transcript_reader.Value(utt);
114 
115  VectorFst<StdArc> decode_fst;
116  if (!gc.CompileGraphFromText(transcript, &decode_fst)) {
117  KALDI_WARN << "Problem creating decoding graph for utterance "
118  << utt <<" [serious error]";
119  num_err++;
120  continue;
121  }
122  if (features.NumRows() == 0) {
123  KALDI_WARN << "Zero-length features for utterance: " << utt;
124  num_err++;
125  continue;
126  }
127 
128  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
129  acoustic_scale);
130 
131 
132  AlignUtteranceWrapper(align_config, utt,
133  acoustic_scale, &decode_fst, &gmm_decodable,
134  &alignment_writer, NULL,
135  &num_done, &num_err, &num_retry,
136  &tot_like, &frame_count);
137  }
138  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count)
139  << " over " << frame_count<< " frames.";
140  KALDI_LOG << "Retried " << num_retry << " out of "
141  << (num_done + num_err) << " utterances.";
142  KALDI_LOG << "Done " << num_done << ", errors on " << num_err;
143  return (num_done != 0 ? 0 : 1);
144  } catch(const std::exception &e) {
145  std::cerr << e.what();
146  return -1;
147  }
148 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Register(OptionsItf *opts)
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 AlignUtteranceWrapper(const AlignConfig &config, const std::string &utt, BaseFloat acoustic_scale, fst::VectorFst< fst::StdArc > *fst, DecodableInterface *decodable, Int32VectorWriter *alignment_writer, BaseFloatWriter *scores_writer, int32 *num_done, int32 *num_error, int32 *num_retried, double *tot_like, int64 *frame_count)
AlignUtteranceWapper is a wrapper for alignment code used in training, that is called from many diffe...
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
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
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
void ReadFstKaldi(std::istream &is, bool binary, VectorFst< Arc > *fst)
#define KALDI_LOG
Definition: kaldi-error.h:133
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
bool ReadIntegerVectorSimple(std::string rxfilename, std::vector< int32 > *list)
ReadFromList attempts to read this list of integers, one per line, from the given file...