Blame view

src/gmmbin/gmm-acc-stats-ali.cc 4.27 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
  // gmmbin/gmm-acc-stats-ali.cc
  
  // Copyright 2009-2012  Microsoft Corporation  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/am-diag-gmm.h"
  #include "hmm/transition-model.h"
  #include "gmm/mle-am-diag-gmm.h"
  
  
  
  
  int main(int argc, char *argv[]) {
    using namespace kaldi;
    typedef kaldi::int32 int32;
    try {
      const char *usage =
          "Accumulate stats for GMM training.
  "
          "Usage:  gmm-acc-stats-ali [options] <model-in> <feature-rspecifier> "
          "<alignments-rspecifier> <stats-out>
  "
          "e.g.:
   gmm-acc-stats-ali 1.mdl scp:train.scp ark:1.ali 1.acc
  ";
  
      ParseOptions po(usage);
      bool binary = true;
      po.Register("binary", &binary, "Write output in binary mode");
      po.Read(argc, argv);
  
      if (po.NumArgs() != 4) {
        po.PrintUsage();
        exit(1);
      }
  
      std::string model_filename = po.GetArg(1),
          feature_rspecifier = po.GetArg(2),
          alignments_rspecifier = po.GetArg(3),
          accs_wxfilename = po.GetArg(4);
  
      AmDiagGmm am_gmm;
      TransitionModel trans_model;
      {
        bool binary;
        Input ki(model_filename, &binary);
        trans_model.Read(ki.Stream(), binary);
        am_gmm.Read(ki.Stream(), binary);
      }
  
      Vector<double> transition_accs;
      trans_model.InitStats(&transition_accs);
      AccumAmDiagGmm gmm_accs;
      gmm_accs.Init(am_gmm, kGmmAll);
  
      double tot_like = 0.0;
      kaldi::int64 tot_t = 0;
  
      SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
      RandomAccessInt32VectorReader alignments_reader(alignments_rspecifier);
  
      int32 num_done = 0, num_err = 0;
      for (; !feature_reader.Done(); feature_reader.Next()) {
        std::string key = feature_reader.Key();
        if (!alignments_reader.HasKey(key)) {
          KALDI_WARN << "No alignment for utterance " << key;
          num_err++;
        } else {
          const Matrix<BaseFloat> &mat = feature_reader.Value();
          const std::vector<int32> &alignment = alignments_reader.Value(key);
  
          if (alignment.size() != mat.NumRows()) {
            KALDI_WARN << "Alignments has wrong size " << (alignment.size())
                       << " vs. " << (mat.NumRows());
            num_err++;
            continue;
          }
  
          num_done++;
          BaseFloat tot_like_this_file = 0.0;
  
          for (size_t i = 0; i < alignment.size(); i++) {
            int32 tid = alignment[i],  // transition identifier.
                pdf_id = trans_model.TransitionIdToPdf(tid);
            trans_model.Accumulate(1.0, tid, &transition_accs);
            tot_like_this_file += gmm_accs.AccumulateForGmm(am_gmm, mat.Row(i),
                                                            pdf_id, 1.0);
          }
          tot_like += tot_like_this_file;
          tot_t += alignment.size();
          if (num_done % 50 == 0) {
            KALDI_LOG << "Processed " << num_done << " utterances; for utterance "
                      << key << " avg. like is "
                      << (tot_like_this_file/alignment.size())
                      << " over " << alignment.size() <<" frames.";
          }
        }
      }
      KALDI_LOG << "Done " << num_done << " files, " << num_err
                << " with errors.";
  
      KALDI_LOG << "Overall avg like per frame (Gaussian only) = "
                << (tot_like/tot_t) << " over " << tot_t << " frames.";
  
      {
        Output ko(accs_wxfilename, binary);
        transition_accs.Write(ko.Stream(), binary);
        gmm_accs.Write(ko.Stream(), binary);
      }
      KALDI_LOG << "Written accs.";
      if (num_done != 0)
        return 0;
      else
        return 1;
    } catch(const std::exception &e) {
      std::cerr << e.what();
      return -1;
    }
  }