align-compiled-mapped.cc
Go to the documentation of this file.
1 // bin/align-compiled-mapped.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation, Karel Vesely
4 // 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 "hmm/transition-model.h"
24 #include "hmm/hmm-utils.h"
25 #include "fstext/fstext-lib.h"
29 #include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
30 
31 int main(int argc, char *argv[]) {
32  try {
33  using namespace kaldi;
34  typedef kaldi::int32 int32;
35  using fst::SymbolTable;
36  using fst::VectorFst;
37  using fst::StdArc;
38 
39  const char *usage =
40  "Generate alignments, reading log-likelihoods as matrices.\n"
41  " (model is needed only for the integer mappings in its transition-model)\n"
42  "Usage: align-compiled-mapped [options] trans-model-in graphs-rspecifier feature-rspecifier alignments-wspecifier\n"
43  "e.g.: \n"
44  " nnet-align-compiled trans.mdl ark:graphs.fsts scp:train.scp ark:nnet.ali\n"
45  "or:\n"
46  " compile-train-graphs tree trans.mdl lex.fst ark:train.tra b, ark:- | \\\n"
47  " nnet-align-compiled trans.mdl ark:- scp:loglikes.scp t, ark:nnet.ali\n";
48 
49  ParseOptions po(usage);
50  AlignConfig align_config;
51  bool binary = true;
52  BaseFloat acoustic_scale = 1.0;
53  BaseFloat transition_scale = 1.0;
54  BaseFloat self_loop_scale = 1.0;
55 
56  align_config.Register(&po);
57  po.Register("binary", &binary, "Write output in binary mode");
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.Read(argc, argv);
65 
66  if (po.NumArgs() < 4 || po.NumArgs() > 5) {
67  po.PrintUsage();
68  exit(1);
69  }
70 
71  std::string model_in_filename = po.GetArg(1);
72  std::string fst_rspecifier = po.GetArg(2);
73  std::string feature_rspecifier = po.GetArg(3);
74  std::string alignment_wspecifier = po.GetArg(4);
75  std::string scores_wspecifier = po.GetOptArg(5);
76 
77  TransitionModel trans_model;
78  ReadKaldiObject(model_in_filename, &trans_model);
79 
80  SequentialBaseFloatMatrixReader loglikes_reader(feature_rspecifier);
81  RandomAccessTableReader<fst::VectorFstHolder> fst_reader(fst_rspecifier);
82  Int32VectorWriter alignment_writer(alignment_wspecifier);
83  BaseFloatWriter scores_writer(scores_wspecifier);
84 
85  int num_done = 0, num_err = 0, num_retry = 0;
86  double tot_like = 0.0;
87  kaldi::int64 frame_count = 0;
88 
89  for (; !loglikes_reader.Done(); loglikes_reader.Next()) {
90  std::string utt = loglikes_reader.Key();
91  if (!fst_reader.HasKey(utt)) {
92  KALDI_WARN << "No fst for utterance " << utt;
93  num_err++;
94  continue;
95  }
96  const Matrix<BaseFloat> &loglikes = loglikes_reader.Value();
97  VectorFst<StdArc> decode_fst(fst_reader.Value(utt));
98  // fst_reader.FreeCurrent(); // this stops copy-on-write of the fst
99  // by deleting the fst inside the reader, since we're about to mutate
100  // the fst by adding transition probs.
101 
102  if (loglikes.NumRows() == 0) {
103  KALDI_WARN << "Empty loglikes matrix utterance: " << utt;
104  num_err++;
105  continue;
106  }
107  if (decode_fst.Start() == fst::kNoStateId) {
108  KALDI_WARN << "Empty decoding graph for " << utt;
109  num_err++;
110  continue;
111  }
112 
113  { // Add transition-probs to the FST.
114  std::vector<int32> disambig_syms; // empty.
115  AddTransitionProbs(trans_model, disambig_syms,
116  transition_scale, self_loop_scale,
117  &decode_fst);
118  }
119 
120  DecodableMatrixScaledMapped decodable(trans_model, loglikes, acoustic_scale);
121 
122  AlignUtteranceWrapper(align_config, utt,
123  acoustic_scale, &decode_fst, &decodable,
124  &alignment_writer, &scores_writer,
125  &num_done, &num_err, &num_retry,
126  &tot_like, &frame_count);
127  }
128  KALDI_LOG << "Overall log-likelihood per frame is " << (tot_like/frame_count)
129  << " over " << frame_count<< " frames.";
130  KALDI_LOG << "Retried " << num_retry << " out of "
131  << (num_done + num_err) << " utterances.";
132  KALDI_LOG << "Done " << num_done << ", errors on " << num_err;
133  return (num_done != 0 ? 0 : 1);
134  } catch(const std::exception &e) {
135  std::cerr << e.what();
136  return -1;
137  }
138 }
139 
140 
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)
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
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)
int main(int argc, char *argv[])
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
std::string GetOptArg(int param) const