gmm-align.cc
Go to the documentation of this file.
1 // gmmbin/gmm-align.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // 2012-2014 Johns Hopkins University (Author: 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 "gmm/am-diag-gmm.h"
24 #include "hmm/transition-model.h"
25 #include "fstext/fstext-utils.h"
29 #include "lat/kaldi-lattice.h" // for {Compact}LatticeArc
30 
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 [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 }
151 
152 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
Definition: gmm-align.cc:32
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
std::istream & Stream()
Definition: kaldi-io.cc:826
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
bool CompileGraphFromText(const std::vector< int32 > &transcript, fst::VectorFst< fst::StdArc > *out_fst)
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 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 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