sgmm2-align-compiled.cc
Go to the documentation of this file.
1 // sgmm2bin/sgmm2-align-compiled.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation; Saarland University
4 // 2012-2014 Johns Hopkins University (Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "sgmm2/am-sgmm2.h"
24 #include "hmm/transition-model.h"
25 #include "hmm/hmm-utils.h"
26 #include "fstext/fstext-lib.h"
30 #include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
31 
32 
33 int main(int argc, char *argv[]) {
34  try {
35  using namespace kaldi;
36  typedef kaldi::int32 int32;
37  using fst::SymbolTable;
38  using fst::VectorFst;
39  using fst::StdArc;
40 
41  const char *usage =
42  "Align features given [SGMM-based] models.\n"
43  "Usage: sgmm2-align-compiled [options] <model-in> <graphs-rspecifier> "
44  "<feature-rspecifier> <alignments-wspecifier>\n"
45  "e.g.: sgmm2-align-compiled 1.mdl ark:graphs.fsts scp:train.scp ark:1.ali\n";
46 
47  ParseOptions po(usage);
48  bool binary = true;
49  AlignConfig align_config;
50  BaseFloat acoustic_scale = 1.0;
51  BaseFloat transition_scale = 1.0;
52  BaseFloat self_loop_scale = 1.0;
53  BaseFloat log_prune = 5.0;
54  std::string gselect_rspecifier, spkvecs_rspecifier, utt2spk_rspecifier;
55  std::string per_frame_acwt_wspecifier;
56 
57  align_config.Register(&po);
58  po.Register("binary", &binary, "Write output in binary mode");
59  po.Register("log-prune", &log_prune, "Pruning beam used to reduce number "
60  "of exp() evaluations.");
61  po.Register("spk-vecs", &spkvecs_rspecifier, "Speaker vectors (rspecifier)");
62  po.Register("utt2spk", &utt2spk_rspecifier,
63  "rspecifier for utterance to speaker map");
64  po.Register("acoustic-scale", &acoustic_scale, "Scaling factor for acoustic "
65  "likelihoods");
66  po.Register("transition-scale", &transition_scale, "Scaling factor for "
67  "some transition probabilities [see also self-loop-scale].");
68  po.Register("self-loop-scale", &self_loop_scale, "Scaling factor for "
69  "self-loop versus non-self-loop probability mass [controls "
70  "most transition probabilities.]");
71  po.Register("write-per-frame-acoustic-loglikes", &per_frame_acwt_wspecifier,
72  "Wspecifier for table of vectors containing the acoustic log-likelihoods "
73  "per frame for each utterance. E.g. ark:foo/per_frame_logprobs.1.ark");
74  po.Register("gselect", &gselect_rspecifier, "Precomputed Gaussian indices "
75  "(rspecifier)");
76 
77  po.Read(argc, argv);
78 
79  if (po.NumArgs() != 4) {
80  po.PrintUsage();
81  exit(1);
82  }
83 
84  if (gselect_rspecifier == "")
85  KALDI_ERR << "--gselect option is mandatory.";
86 
87  std::string model_in_filename = po.GetArg(1),
88  fst_rspecifier = po.GetArg(2),
89  feature_rspecifier = po.GetArg(3),
90  alignment_wspecifier = po.GetArg(4);
91 
92  TransitionModel trans_model;
93  AmSgmm2 am_sgmm;
94  {
95  bool binary;
96  Input ki(model_in_filename, &binary);
97  trans_model.Read(ki.Stream(), binary);
98  am_sgmm.Read(ki.Stream(), binary);
99  }
100 
101  SequentialTableReader<fst::VectorFstHolder> fst_reader(fst_rspecifier);
102  RandomAccessBaseFloatMatrixReader feature_reader(feature_rspecifier);
103  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
104 
105  RandomAccessBaseFloatVectorReaderMapped spkvecs_reader(spkvecs_rspecifier,
106  utt2spk_rspecifier);
107 
108  Int32VectorWriter alignment_writer(alignment_wspecifier);
109  BaseFloatVectorWriter per_frame_acwt_writer(per_frame_acwt_wspecifier);
110 
111  int num_done = 0, num_err = 0, num_retry = 0;
112  double tot_like = 0.0;
113  kaldi::int64 frame_count = 0;
114 
115  for (; !fst_reader.Done(); fst_reader.Next()) {
116  std::string utt = fst_reader.Key();
117  if (!feature_reader.HasKey(utt)) {
118  KALDI_WARN << "No feature found for utterance " << utt;
119  num_err++;
120  continue;
121  }
122  VectorFst<StdArc> decode_fst(fst_reader.Value());
123  // stops copy-on-write of the fst by deleting the fst inside the reader,
124  // since we're about to mutate the fst by adding transition probs.
125  fst_reader.FreeCurrent();
126 
127  const Matrix<BaseFloat> &features = feature_reader.Value(utt);
128  if (features.NumRows() == 0) {
129  KALDI_WARN << "Zero-length utterance: " << utt;
130  num_err++;
131  continue;
132  }
133 
134  Sgmm2PerSpkDerivedVars spk_vars;
135  if (spkvecs_reader.IsOpen()) {
136  if (spkvecs_reader.HasKey(utt)) {
137  spk_vars.SetSpeakerVector(spkvecs_reader.Value(utt));
138  am_sgmm.ComputePerSpkDerivedVars(&spk_vars);
139  } else {
140  KALDI_WARN << "Cannot find speaker vector for " << utt;
141  num_err++;
142  continue;
143  }
144  } // else spk_vars is "empty"
145 
146  if (!gselect_reader.HasKey(utt)
147  && gselect_reader.Value(utt).size() != features.NumRows()) {
148  KALDI_WARN << "No Gaussian-selection info available for utterance "
149  << utt << " (or wrong size)";
150  num_err++;
151  }
152  const std::vector<std::vector<int32> > &gselect =
153  gselect_reader.Value(utt);
154 
155  { // Add transition-probs to the FST.
156  std::vector<int32> disambig_syms; // empty.
157  AddTransitionProbs(trans_model, disambig_syms,
158  transition_scale, self_loop_scale,
159  &decode_fst);
160  }
161 
162  DecodableAmSgmm2Scaled sgmm_decodable(am_sgmm, trans_model, features, gselect,
163  log_prune, acoustic_scale, &spk_vars);
164 
165  AlignUtteranceWrapper(align_config, utt,
166  acoustic_scale, &decode_fst, &sgmm_decodable,
167  &alignment_writer, NULL,
168  &num_done, &num_err, &num_retry,
169  &tot_like, &frame_count, &per_frame_acwt_writer);
170 
171  }
172 
173  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count)
174  << " over " << frame_count<< " frames.";
175  KALDI_LOG << "Retried " << num_retry << " out of "
176  << (num_done + num_err) << " utterances.";
177  KALDI_LOG << "Done " << num_done << ", errors on " << num_err;
178  return (num_done != 0 ? 0 : 1);
179  } catch(const std::exception &e) {
180  std::cerr << e.what();
181  return -1;
182  }
183 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Class for definition of the subspace Gmm acoustic model.
Definition: am-sgmm2.h:231
void Register(OptionsItf *opts)
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
fst::StdArc StdArc
This class is for when you are reading something in random access, but it may actually be stored per-...
Definition: kaldi-table.h:432
void Read(std::istream &is, bool binary)
Definition: am-sgmm2.cc:89
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 ComputePerSpkDerivedVars(Sgmm2PerSpkDerivedVars *vars) const
Computes the per-speaker derived vars; assumes vars->v_s is already set up.
Definition: am-sgmm2.cc:1369
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_ERR
Definition: kaldi-error.h:147
#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 main(int argc, char *argv[])
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
const T & Value(const std::string &key)
void SetSpeakerVector(const Vector< BaseFloat > &v_s_in)
Definition: am-sgmm2.h:180
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