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

◆ main()

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 "
43  "transcriptions-rspecifier alignments-wspecifier\n"
44  "e.g.: \n"
45  " gmm-align tree 1.mdl lex.fst scp:train.scp "
46  "'ark:sym2int.pl -f 2- words.txt text|' ark:1.ali\n";
47  ParseOptions po(usage);
48  AlignConfig align_config;
49  BaseFloat acoustic_scale = 1.0;
50  std::string disambig_rxfilename;
52 
53  align_config.Register(&po);
54  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic likelihoods");
55  po.Register("read-disambig-syms", &disambig_rxfilename, "File containing "
56  "list of disambiguation symbols in phone symbol table");
57 
58  gopts.Register(&po);
59  po.Read(argc, argv);
60 
61  if (po.NumArgs() != 6) {
62  po.PrintUsage();
63  exit(1);
64  }
65 
66  std::string tree_in_filename = po.GetArg(1);
67  std::string model_in_filename = po.GetArg(2);
68  std::string lex_in_filename = po.GetArg(3);
69  std::string feature_rspecifier = po.GetArg(4);
70  std::string transcript_rspecifier = po.GetArg(5);
71  std::string alignment_wspecifier = po.GetArg(6);
72 
73  ContextDependency ctx_dep;
74  ReadKaldiObject(tree_in_filename, &ctx_dep);
75 
76  TransitionModel trans_model;
77  AmDiagGmm am_gmm;
78  {
79  bool binary;
80  Input ki(model_in_filename, &binary);
81  trans_model.Read(ki.Stream(), binary);
82  am_gmm.Read(ki.Stream(), binary);
83  }
84 
85  // ownership will be taken by gc.
86  VectorFst<StdArc> *lex_fst = fst::ReadFstKaldi(lex_in_filename);
87 
88  std::vector<int32> disambig_syms;
89  if (disambig_rxfilename != "")
90  if (!ReadIntegerVectorSimple(disambig_rxfilename, &disambig_syms))
91  KALDI_ERR << "fstcomposecontext: Could not read disambiguation symbols from "
92  << disambig_rxfilename;
93 
94  TrainingGraphCompiler gc(trans_model, ctx_dep, lex_fst, disambig_syms,
95  gopts);
96 
97  lex_fst = NULL; // we gave ownership to gc.
98 
99  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
100  RandomAccessInt32VectorReader transcript_reader(transcript_rspecifier);
101  Int32VectorWriter alignment_writer(alignment_wspecifier);
102 
103  int32 num_done = 0, num_err = 0, num_retry = 0;
104  double tot_like = 0.0;
105  kaldi::int64 frame_count = 0;
106  for (; !feature_reader.Done(); feature_reader.Next()) {
107  std::string utt = feature_reader.Key();
108  if (!transcript_reader.HasKey(utt)) {
109  KALDI_WARN << "No transcript found for utterance " << utt;
110  num_err++;
111  continue;
112  }
113 
114  const Matrix<BaseFloat> &features = feature_reader.Value();
115  const std::vector<int32> &transcript = transcript_reader.Value(utt);
116 
117  VectorFst<StdArc> decode_fst;
118  if (!gc.CompileGraphFromText(transcript, &decode_fst)) {
119  KALDI_WARN << "Problem creating decoding graph for utterance "
120  << utt <<" [serious error]";
121  num_err++;
122  continue;
123  }
124  if (features.NumRows() == 0) {
125  KALDI_WARN << "Zero-length features for utterance: " << utt;
126  num_err++;
127  continue;
128  }
129 
130  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
131  acoustic_scale);
132 
133 
134  AlignUtteranceWrapper(align_config, utt,
135  acoustic_scale, &decode_fst, &gmm_decodable,
136  &alignment_writer, NULL,
137  &num_done, &num_err, &num_retry,
138  &tot_like, &frame_count);
139  }
140  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count)
141  << " over " << frame_count<< " frames.";
142  KALDI_LOG << "Retried " << num_retry << " out of "
143  << (num_done + num_err) << " utterances.";
144  KALDI_LOG << "Done " << num_done << ", errors on " << num_err;
145  return (num_done != 0 ? 0 : 1);
146  } catch(const std::exception &e) {
147  std::cerr << e.what();
148  return -1;
149  }
150 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
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:368
kaldi::int32 int32
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
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:147
#define KALDI_WARN
Definition: kaldi-error.h:150
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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...
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, BaseFloatVectorWriter *per_frame_acwt_writer)
AlignUtteranceWapper is a wrapper for alignment code used in training, that is called from many diffe...
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147