gmm-get-stats-deriv.cc
Go to the documentation of this file.
1 // gmmbin/gmm-get-stats-deriv.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "gmm/am-diag-gmm.h"
23 #include "tree/context-dep.h"
24 #include "hmm/transition-model.h"
26 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31  MleDiagGmmOptions gmm_opts;
32 
33  const char *usage =
34  "Get statistics derivative for GMM models\n"
35  "(used in fMPE/fMMI feature-space discriminative training)\n"
36  "Usage: gmm-get-stats-deriv [options] <model-in> <num-stats-in>"
37  " <den-stats-in> <ml-stats-in> <deriv-out>\n"
38  "e.g. (for fMMI/fBMMI): gmm-get-stats-deriv 1.mdl 1.acc 2.mdl\n";
39 
40  bool binary_write = true;
41  MleDiagGmmOptions opts; // Not passed to command-line-- just a mechanism to
42  // ensure our options have the same default values as those ones.
43  BaseFloat min_variance = opts.min_variance;
44  BaseFloat min_gaussian_occupancy = opts.min_gaussian_occupancy;
45 
46  ParseOptions po(usage);
47  po.Register("binary", &binary_write, "Write output in binary mode");
48  po.Register("min-variance", &min_variance,
49  "Variance floor (absolute variance).");
50  po.Register("min-gaussian-occupancy", &min_gaussian_occupancy,
51  "Minimum occupancy to update a Gaussian.");
52 
53  po.Read(argc, argv);
54 
55  if (po.NumArgs() != 5) {
56  po.PrintUsage();
57  exit(1);
58  }
59 
60  std::string model_rxfilename = po.GetArg(1),
61  num_stats_rxfilename = po.GetArg(2),
62  den_stats_rxfilename = po.GetArg(3),
63  ml_stats_rxfilename = po.GetArg(4),
64  deriv_wxfilename = po.GetArg(5);
65 
66  AmDiagGmm am_gmm;
67  TransitionModel trans_model;
68  {
69  bool binary_read;
70  Input ki(model_rxfilename, &binary_read);
71  trans_model.Read(ki.Stream(), binary_read);
72  am_gmm.Read(ki.Stream(), binary_read);
73  }
74 
75  Vector<double> transition_accs; // Reuse this for all transition accs we
76  // read, as it's not needed.
77  AccumAmDiagGmm num_stats, den_stats, ml_stats;
78  {
79  bool binary_read;
80  Input ki(num_stats_rxfilename, &binary_read);
81  transition_accs.Read(ki.Stream(), binary_read);
82  num_stats.Read(ki.Stream(), binary_read, false);
83  }
84  {
85  bool binary_read;
86  Input ki(den_stats_rxfilename, &binary_read);
87  transition_accs.Read(ki.Stream(), binary_read);
88  den_stats.Read(ki.Stream(), binary_read, false);
89  }
90  {
91  bool binary_read;
92  Input ki(ml_stats_rxfilename, &binary_read);
93  transition_accs.Read(ki.Stream(), binary_read);
94  ml_stats.Read(ki.Stream(), binary_read, false);
95  }
96 
97  AccumAmDiagGmm model_deriv; // Use GMM accumulators to represent
98  // derivative of discriminative objective function w.r.t.
99  // accumulated stats.
100 
101  GetStatsDerivative(am_gmm, num_stats, den_stats, ml_stats,
102  min_variance, min_gaussian_occupancy,
103  &model_deriv);
104 
105  WriteKaldiObject(model_deriv, deriv_wxfilename, binary_write);
106 
107  KALDI_LOG << "Computed model derivative and wrote it to "
108  << deriv_wxfilename;
109 
110  return 0;
111  } catch(const std::exception &e) {
112  std::cerr << e.what() << '\n';
113  return -1;
114  }
115 }
116 
117 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
BaseFloat min_gaussian_occupancy
Minimum count below which a Gaussian is not updated (and is removed, if remove_low_count_gaussians ==...
Definition: mle-diag-gmm.h:47
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
kaldi::int32 int32
double min_variance
Minimum allowed variance in any dimension (if no variance floor) It is in double since the variance i...
Definition: mle-diag-gmm.h:50
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
void GetStatsDerivative(const DiagGmm &gmm, const AccumDiagGmm &num_acc, const AccumDiagGmm &den_acc, const AccumDiagGmm &ml_acc, BaseFloat min_variance, BaseFloat min_gaussian_occupancy, AccumDiagGmm *out_accs)
void Read(std::istream &is, bool binary)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
Configuration variables like variance floor, minimum occupancy, etc.
Definition: mle-diag-gmm.h:38
void Read(std::istream &in_stream, bool binary, bool add=false)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.