Blame view

src/gmmbin/gmm-est-rescale.cc 3.62 KB
8dcb6dfcb   Yannick Estève   first commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
  // gmmbin/gmm-est-rescale.cc
  
  // Copyright 2012  Johns Hopkins University (Author: Daniel Povey)
  
  // See ../../COPYING for clarification regarding multiple authors
  //
  // Licensed under the Apache License, Version 2.0 (the "License");
  // you may not use this file except in compliance with the License.
  // You may obtain a copy of the License at
  //
  //  http://www.apache.org/licenses/LICENSE-2.0
  //
  // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
  // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
  // MERCHANTABLITY OR NON-INFRINGEMENT.
  // See the Apache 2 License for the specific language governing permissions and
  // limitations under the License.
  
  #include "base/kaldi-common.h"
  #include "util/common-utils.h"
  #include "gmm/indirect-diff-diag-gmm.h"
  #include "tree/context-dep.h"
  #include "hmm/transition-model.h"
  
  int main(int argc, char *argv[]) {
    using namespace kaldi;
    typedef kaldi::int32 int32;
    try {
      const char *usage =
          "Do \"re-scaling\" re-estimation of GMM-based model
  "
          " (this update changes the model as features change, but preserves
  "
          "  the difference between the model and the features, to keep
  "
          "  the effect of any prior discriminative training).  Used in fMPE.
  "
          "  Does not update the transitions or weights.
  "
          "Usage: gmm-est-rescale [options] <model-in> <old-stats-in> <new-stats-in> <model-out>
  "
          "e.g.: gmm-est-rescale 1.mdl old.acc new.acc 2.mdl
  ";
      
      bool binary_write = true;
      MleDiagGmmOptions opts; // Not passed to command-line-- just a mechanism to
      // ensure our options have the same default values as those ones.
      BaseFloat min_variance = opts.min_variance;
      BaseFloat min_gaussian_occupancy = opts.min_gaussian_occupancy;
      
      ParseOptions po(usage);
      po.Register("binary", &binary_write, "Write output in binary mode");
      po.Register("min-variance", &min_variance,
                  "Variance floor (absolute variance).");
      po.Register("min-gaussian-occupancy", &min_gaussian_occupancy,
                  "Minimum occupancy to update a Gaussian.");
      
      po.Read(argc, argv);
  
      if (po.NumArgs() != 4) {
        po.PrintUsage();
        exit(1);
      }
  
      std::string model_rxfilename = po.GetArg(1),
          old_stats_rxfilename = po.GetArg(2),
          new_stats_rxfilename = po.GetArg(3),
          model_wxfilename = po.GetArg(4);
  
      AmDiagGmm am_gmm;
      TransitionModel trans_model;
      {
        bool binary_read;
        Input ki(model_rxfilename, &binary_read);
        trans_model.Read(ki.Stream(), binary_read);
        am_gmm.Read(ki.Stream(), binary_read);
      }
  
      AccumAmDiagGmm old_gmm_accs, new_gmm_accs;
      {
        Vector<double> transition_accs;
        bool binary;
        Input ki(old_stats_rxfilename, &binary);
        transition_accs.Read(ki.Stream(), binary);
        old_gmm_accs.Read(ki.Stream(), binary, true);
      }
      {
        Vector<double> transition_accs;
        bool binary;
        Input ki(new_stats_rxfilename, &binary);
        transition_accs.Read(ki.Stream(), binary);
        new_gmm_accs.Read(ki.Stream(), binary, true);
      }
  
      DoRescalingUpdate(old_gmm_accs, new_gmm_accs,
                        min_variance, min_gaussian_occupancy,
                        &am_gmm);
      
      {
        Output ko(model_wxfilename, binary_write);
        trans_model.Write(ko.Stream(), binary_write);
        am_gmm.Write(ko.Stream(), binary_write);
      }
  
      KALDI_LOG << "Rescaled model and wrote to " << model_wxfilename;
      return 0;
    } catch(const std::exception &e) {
      std::cerr << e.what() << '
  ';
      return -1;
    }
  }