gmm-sum-accs.cc
Go to the documentation of this file.
1 // gmmbin/gmm-sum-accs.cc
2 
3 // Copyright 2009-2011 Saarland University; Microsoft Corporation
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 "util/common-utils.h"
21 #include "gmm/mle-am-diag-gmm.h"
22 #include "hmm/transition-model.h"
23 
24 
25 int main(int argc, char *argv[]) {
26  try {
27  typedef kaldi::int32 int32;
28 
29  const char *usage =
30  "Sum multiple accumulated stats files for GMM training.\n"
31  "Usage: gmm-sum-accs [options] <stats-out> <stats-in1> <stats-in2> ...\n"
32  "E.g.: gmm-sum-accs 1.acc 1.1.acc 1.2.acc\n";
33 
34  bool binary = true;
35  kaldi::ParseOptions po(usage);
36  po.Register("binary", &binary, "Write output in binary mode");
37  po.Read(argc, argv);
38 
39  if (po.NumArgs() < 2) {
40  po.PrintUsage();
41  exit(1);
42  }
43 
44  std::string stats_out_filename = po.GetArg(1);
45  kaldi::Vector<double> transition_accs;
46  kaldi::AccumAmDiagGmm gmm_accs;
47 
48  int num_accs = po.NumArgs() - 1;
49  for (int i = 2, max = po.NumArgs(); i <= max; i++) {
50  std::string stats_in_filename = po.GetArg(i);
51  bool binary_read;
52  kaldi::Input ki(stats_in_filename, &binary_read);
53  transition_accs.Read(ki.Stream(), binary_read, true /*add read values*/);
54  gmm_accs.Read(ki.Stream(), binary_read, true /*add read values*/);
55  }
56 
57  // Write out the accs
58  {
59  kaldi::Output ko(stats_out_filename, binary);
60  transition_accs.Write(ko.Stream(), binary);
61  gmm_accs.Write(ko.Stream(), binary);
62  }
63  KALDI_LOG << "Summed " << num_accs << " stats, total count "
64  << gmm_accs.TotCount() << ", avg like/frame "
65  << (gmm_accs.TotLogLike() / gmm_accs.TotCount());
66  KALDI_LOG << "Total count of stats is " << gmm_accs.TotStatsCount();
67  KALDI_LOG << "Written stats to " << stats_out_filename;
68  } catch(const std::exception &e) {
69  std::cerr << e.what() << '\n';
70  return -1;
71  }
72 }
73 
74 
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
kaldi::int32 int32
BaseFloat TotCount() const
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::istream & Stream()
Definition: kaldi-io.cc:826
BaseFloat TotLogLike() const
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
int main(int argc, char *argv[])
Definition: gmm-sum-accs.cc:25
BaseFloat TotStatsCount() const
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.
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 &out_stream, bool binary) const
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.