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

References kaldi::AddTransitionProbs(), kaldi::AlignUtteranceWrapper(), SequentialTableReader< Holder >::Done(), SequentialTableReader< Holder >::FreeCurrent(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), AlignConfig::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-compiled [options] <model-in> <graphs-rspecifier> "
43  "<feature-rspecifier> <alignments-wspecifier> [scores-wspecifier]\n"
44  "e.g.: \n"
45  " gmm-align-compiled 1.mdl ark:graphs.fsts scp:train.scp ark:1.ali\n"
46  "or:\n"
47  " compile-train-graphs tree 1.mdl lex.fst 'ark:sym2int.pl -f 2- words.txt text|' \\\n"
48  " ark:- | gmm-align-compiled 1.mdl ark:- scp:train.scp t, ark:1.ali\n";
49 
50  ParseOptions po(usage);
51  AlignConfig align_config;
52  BaseFloat acoustic_scale = 1.0;
53  BaseFloat transition_scale = 1.0;
54  BaseFloat self_loop_scale = 1.0;
55  std::string per_frame_acwt_wspecifier;
56 
57  align_config.Register(&po);
58  po.Register("transition-scale", &transition_scale,
59  "Transition-probability scale [relative to acoustics]");
60  po.Register("acoustic-scale", &acoustic_scale,
61  "Scaling factor for acoustic likelihoods");
62  po.Register("self-loop-scale", &self_loop_scale,
63  "Scale of self-loop versus non-self-loop log probs [relative to acoustics]");
64  po.Register("write-per-frame-acoustic-loglikes", &per_frame_acwt_wspecifier,
65  "Wspecifier for table of vectors containing the acoustic log-likelihoods "
66  "per frame for each utterance. E.g. ark:foo/per_frame_logprobs.1.ark");
67  po.Read(argc, argv);
68 
69  if (po.NumArgs() < 4 || po.NumArgs() > 5) {
70  po.PrintUsage();
71  exit(1);
72  }
73 
74  std::string model_in_filename = po.GetArg(1),
75  fst_rspecifier = po.GetArg(2),
76  feature_rspecifier = po.GetArg(3),
77  alignment_wspecifier = po.GetArg(4),
78  scores_wspecifier = po.GetOptArg(5);
79 
80  TransitionModel trans_model;
81  AmDiagGmm am_gmm;
82  {
83  bool binary;
84  Input ki(model_in_filename, &binary);
85  trans_model.Read(ki.Stream(), binary);
86  am_gmm.Read(ki.Stream(), binary);
87  }
88 
89  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_rspecifier);
90  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
91  Int32VectorWriter alignment_writer(alignment_wspecifier);
92  BaseFloatWriter scores_writer(scores_wspecifier);
93  BaseFloatVectorWriter per_frame_acwt_writer(per_frame_acwt_wspecifier);
94 
95  int num_done = 0, num_err = 0, num_retry = 0;
96  double tot_like = 0.0;
97  kaldi::int64 frame_count = 0;
98 
99  for (; !fst_reader.Done(); fst_reader.Next()) {
100  std::string utt = fst_reader.Key();
101  if (!feature_reader.HasKey(utt)) {
102  num_err++;
103  KALDI_WARN << "No features for utterance " << utt;
104  } else {
105  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
106  VectorFst<StdArc> decode_fst(fst_reader.Value());
107  fst_reader.FreeCurrent(); // this stops copy-on-write of the fst
108  // by deleting the fst inside the reader, since we're about to mutate
109  // the fst by adding transition probs.
110 
111  if (features.NumRows() == 0) {
112  KALDI_WARN << "Zero-length utterance: " << utt;
113  num_err++;
114  continue;
115  }
116 
117  { // Add transition-probs to the FST.
118  std::vector<int32> disambig_syms; // empty.
119  AddTransitionProbs(trans_model, disambig_syms,
120  transition_scale, self_loop_scale,
121  &decode_fst);
122  }
123 
124  DecodableAmDiagGmmScaled gmm_decodable(am_gmm, trans_model, features,
125  acoustic_scale);
126 
127  KALDI_LOG << utt;
128  AlignUtteranceWrapper(align_config, utt,
129  acoustic_scale, &decode_fst, &gmm_decodable,
130  &alignment_writer, &scores_writer,
131  &num_done, &num_err, &num_retry,
132  &tot_like, &frame_count, &per_frame_acwt_writer);
133  }
134  }
135  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count)
136  << " over " << frame_count<< " frames.";
137  KALDI_LOG << "Retried " << num_retry << " out of "
138  << (num_done + num_err) << " utterances.";
139  KALDI_LOG << "Done " << num_done << ", errors on " << num_err;
140  return (num_done != 0 ? 0 : 1);
141  } catch(const std::exception &e) {
142  std::cerr << e.what();
143  return -1;
144  }
145 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Register(OptionsItf *opts)
fst::StdArc StdArc
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...
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
void AddTransitionProbs(const TransitionModel &trans_model, const std::vector< int32 > &disambig_syms, BaseFloat transition_scale, BaseFloat self_loop_scale, fst::VectorFst< fst::StdArc > *fst)
Adds transition-probs, with the supplied scales (see Scaling of transition and acoustic probabilities...
Definition: hmm-utils.cc:1042
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_WARN
Definition: kaldi-error.h:130
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_LOG
Definition: kaldi-error.h:133
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147