gmm-align-compiled.cc
Go to the documentation of this file.
1 // gmmbin/gmm-align-compiled.cc
2 
3 // Copyright 2009-2013 Microsoft Corporation
4 // Johns Hopkins University (author: Daniel Povey)
5 
6 
7 // See ../../COPYING for clarification regarding multiple authors
8 //
9 // Licensed under the Apache License, Version 2.0 (the "License");
10 // you may not use this file except in compliance with the License.
11 // You may obtain a copy of the License at
12 //
13 // http://www.apache.org/licenses/LICENSE-2.0
14 //
15 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
17 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
18 // MERCHANTABLITY OR NON-INFRINGEMENT.
19 // See the Apache 2 License for the specific language governing permissions and
20 // limitations under the License.
21 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "gmm/am-diag-gmm.h"
25 #include "hmm/transition-model.h"
26 #include "hmm/hmm-utils.h"
27 #include "fstext/fstext-lib.h"
30 #include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
31 
32 int main(int argc, char *argv[]) {
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 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void Register(OptionsItf *opts)
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 Register(const std::string &name, bool *ptr, const std::string &doc)
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
std::istream & Stream()
Definition: kaldi-io.cc:826
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:1088
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
const T & Value(const std::string &key)
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#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 HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
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
int main(int argc, char *argv[])
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
std::string GetOptArg(int param) const