gmm-acc-mllt.cc
Go to the documentation of this file.
1 // gmmbin/gmm-acc-mllt.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 2014 Guoguo Chen
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 
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 "transform/mllt.h"
27 #include "hmm/posterior.h"
28 
29 
30 int main(int argc, char *argv[]) {
31  using namespace kaldi;
32  try {
33  const char *usage =
34  "Accumulate MLLT (global STC) statistics\n"
35  "Usage: gmm-acc-mllt [options] <model-in> <feature-rspecifier> <posteriors-rspecifier> <stats-out>\n"
36  "e.g.: \n"
37  " gmm-acc-mllt 1.mdl scp:train.scp ark:1.post 1.macc\n";
38 
39  ParseOptions po(usage);
40  bool binary = true;
41  BaseFloat rand_prune = 0.25;
42  po.Register("binary", &binary, "Write output in binary mode");
43  po.Register("rand-prune", &rand_prune, "Randomized pruning parameter to speed up "
44  "accumulation (larger -> more pruning. May exceed one).");
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 4) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string model_filename = po.GetArg(1),
53  feature_rspecifier = po.GetArg(2),
54  posteriors_rspecifier = po.GetArg(3),
55  accs_wxfilename = po.GetArg(4);
56 
57  using namespace kaldi;
58  typedef kaldi::int32 int32;
59 
60  AmDiagGmm am_gmm;
61  TransitionModel trans_model;
62  {
63  bool binary;
64  Input ki(model_filename, &binary);
65  trans_model.Read(ki.Stream(), binary);
66  am_gmm.Read(ki.Stream(), binary);
67  }
68 
69  MlltAccs mllt_accs(am_gmm.Dim(), rand_prune);
70 
71  double tot_like = 0.0;
72  double tot_t = 0.0;
73 
74  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
75  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
76 
77  int32 num_done = 0, num_no_posterior = 0, num_other_error = 0;
78  for (; !feature_reader.Done(); feature_reader.Next()) {
79  std::string key = feature_reader.Key();
80  if (!posteriors_reader.HasKey(key)) {
81  num_no_posterior++;
82  } else {
83  const Matrix<BaseFloat> &mat = feature_reader.Value();
84  const Posterior &posterior = posteriors_reader.Value(key);
85 
86  if (static_cast<int32>(posterior.size()) != mat.NumRows()) {
87  KALDI_WARN << "Posterior vector has wrong size "<< (posterior.size()) << " vs. "<< (mat.NumRows());
88  num_other_error++;
89  continue;
90  }
91 
92  num_done++;
93  BaseFloat tot_like_this_file = 0.0, tot_weight = 0.0;
94 
95  Posterior pdf_posterior;
96  ConvertPosteriorToPdfs(trans_model, posterior, &pdf_posterior);
97  for (size_t i = 0; i < posterior.size(); i++) {
98  for (size_t j = 0; j < pdf_posterior[i].size(); j++) {
99  int32 pdf_id = pdf_posterior[i][j].first;
100  BaseFloat weight = pdf_posterior[i][j].second;
101 
102  tot_like_this_file += mllt_accs.AccumulateFromGmm(am_gmm.GetPdf(pdf_id),
103  mat.Row(i),
104  weight) * weight;
105  tot_weight += weight;
106  }
107  }
108  KALDI_LOG << "Average like for this file is "
109  << (tot_like_this_file/tot_weight) << " over "
110  << tot_weight << " frames.";
111  tot_like += tot_like_this_file;
112  tot_t += tot_weight;
113  if (num_done % 10 == 0)
114  KALDI_LOG << "Avg like per frame so far is "
115  << (tot_like/tot_t);
116  }
117  }
118 
119  KALDI_LOG << "Done " << num_done << " files, " << num_no_posterior
120  << " with no posteriors, " << num_other_error
121  << " with other errors.";
122 
123  KALDI_LOG << "Overall avg like per frame (Gaussian only) = "
124  << (tot_like/tot_t) << " over " << tot_t << " frames.";
125 
126  WriteKaldiObject(mllt_accs, accs_wxfilename, binary);
127  KALDI_LOG << "Written accs.";
128  return (num_done != 0 ? 0 : 1);
129  } catch(const std::exception &e) {
130  std::cerr << e.what();
131  return -1;
132  }
133 }
134 
135 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
Definition: gmm-acc-mllt.cc:30
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
float BaseFloat
Definition: kaldi-types.h:29
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:42
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
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)
int32 Dim() const
Definition: am-diag-gmm.h:79
int NumArgs() const
Number of positional parameters (c.f. argc-1).
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
void ConvertPosteriorToPdfs(const TransitionModel &tmodel, const Posterior &post_in, Posterior *post_out)
Converts a posterior over transition-ids to be a posterior over pdf-ids.
Definition: posterior.cc:322
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
A class for estimating Maximum Likelihood Linear Transform, also known as global Semi-tied Covariance...
Definition: mllt.h:42