gmm-est-rescale.cc
Go to the documentation of this file.
1 // gmmbin/gmm-est-rescale.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"
23 #include "tree/context-dep.h"
24 #include "hmm/transition-model.h"
25 
26 int main(int argc, char *argv[]) {
27  using namespace kaldi;
28  typedef kaldi::int32 int32;
29  try {
30  const char *usage =
31  "Do \"re-scaling\" re-estimation of GMM-based model\n"
32  " (this update changes the model as features change, but preserves\n"
33  " the difference between the model and the features, to keep\n"
34  " the effect of any prior discriminative training). Used in fMPE.\n"
35  " Does not update the transitions or weights.\n"
36  "Usage: gmm-est-rescale [options] <model-in> <old-stats-in> <new-stats-in> <model-out>\n"
37  "e.g.: gmm-est-rescale 1.mdl old.acc new.acc 2.mdl\n";
38 
39  bool binary_write = true;
40  MleDiagGmmOptions opts; // Not passed to command-line-- just a mechanism to
41  // ensure our options have the same default values as those ones.
42  BaseFloat min_variance = opts.min_variance;
43  BaseFloat min_gaussian_occupancy = opts.min_gaussian_occupancy;
44 
45  ParseOptions po(usage);
46  po.Register("binary", &binary_write, "Write output in binary mode");
47  po.Register("min-variance", &min_variance,
48  "Variance floor (absolute variance).");
49  po.Register("min-gaussian-occupancy", &min_gaussian_occupancy,
50  "Minimum occupancy to update a Gaussian.");
51 
52  po.Read(argc, argv);
53 
54  if (po.NumArgs() != 4) {
55  po.PrintUsage();
56  exit(1);
57  }
58 
59  std::string model_rxfilename = po.GetArg(1),
60  old_stats_rxfilename = po.GetArg(2),
61  new_stats_rxfilename = po.GetArg(3),
62  model_wxfilename = po.GetArg(4);
63 
64  AmDiagGmm am_gmm;
65  TransitionModel trans_model;
66  {
67  bool binary_read;
68  Input ki(model_rxfilename, &binary_read);
69  trans_model.Read(ki.Stream(), binary_read);
70  am_gmm.Read(ki.Stream(), binary_read);
71  }
72 
73  AccumAmDiagGmm old_gmm_accs, new_gmm_accs;
74  {
75  Vector<double> transition_accs;
76  bool binary;
77  Input ki(old_stats_rxfilename, &binary);
78  transition_accs.Read(ki.Stream(), binary);
79  old_gmm_accs.Read(ki.Stream(), binary, true);
80  }
81  {
82  Vector<double> transition_accs;
83  bool binary;
84  Input ki(new_stats_rxfilename, &binary);
85  transition_accs.Read(ki.Stream(), binary);
86  new_gmm_accs.Read(ki.Stream(), binary, true);
87  }
88 
89  DoRescalingUpdate(old_gmm_accs, new_gmm_accs,
90  min_variance, min_gaussian_occupancy,
91  &am_gmm);
92 
93  {
94  Output ko(model_wxfilename, binary_write);
95  trans_model.Write(ko.Stream(), binary_write);
96  am_gmm.Write(ko.Stream(), binary_write);
97  }
98 
99  KALDI_LOG << "Rescaled model and wrote to " << model_wxfilename;
100  return 0;
101  } catch(const std::exception &e) {
102  std::cerr << e.what() << '\n';
103  return -1;
104  }
105 }
106 
107 
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].
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
std::ostream & Stream()
Definition: kaldi-io.cc:701
void DoRescalingUpdate(const AccumDiagGmm &old_ml_acc, const AccumDiagGmm &new_ml_acc, BaseFloat min_variance, BaseFloat min_gaussian_occupancy, DiagGmm *gmm, double *tot_count, double *tot_divergence)
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 Write(std::ostream &os, bool binary) const
void Write(std::ostream &out_stream, bool binary) const
Definition: am-diag-gmm.cc:163
#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.
int main(int argc, char *argv[])