gmm-make-regtree.cc
Go to the documentation of this file.
1 // gmmbin/gmm-make-regtree.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 "base/kaldi-common.h"
21 #include "util/kaldi-io.h"
22 #include "util/text-utils.h"
23 #include "gmm/mle-am-diag-gmm.h"
24 #include "tree/context-dep.h"
25 #include "hmm/transition-model.h"
27 
28 
29 int main(int argc, char *argv[]) {
30  try {
31  typedef kaldi::int32 int32;
33 
34  const char *usage =
35  "Build regression class tree.\n"
36  "Usage: gmm-make-regtree [options] <model-file> <regtree-out>\n"
37  "E.g.: gmm-make-regtree --silphones=1:2:3 --state-occs=1.occs 1.mdl 1.regtree\n"
38  " [Note: state-occs come from --write-occs option of gmm-est]\n";
39 
40  std::string occs_in_filename;
41  std::string sil_phones_str;
42  bool binary_write = true;
43  int32 max_leaves = 1;
44  kaldi::ParseOptions po(usage);
45  po.Register("state-occs", &occs_in_filename, "File containing state occupancies (use --write-occs in gmm-est)");
46  po.Register("sil-phones", &sil_phones_str, "Colon-separated list of integer ids of silence phones, e.g. 1:2:3; if used, create top-level speech/sil split (only one reg-class for silence).");
47  po.Register("binary", &binary_write, "Write output in binary mode");
48  po.Register("max-leaves", &max_leaves, "Maximum number of leaves in regression tree.");
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 2) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56  std::string model_in_filename = po.GetArg(1),
57  tree_out_filename = po.GetArg(2);
58 
59  kaldi::AmDiagGmm am_gmm;
60  kaldi::TransitionModel trans_model;
61  {
62  bool binary_read;
63  kaldi::Input ki(model_in_filename, &binary_read);
64  trans_model.Read(ki.Stream(), binary_read);
65  am_gmm.Read(ki.Stream(), binary_read);
66  }
67 
68  kaldi::Vector<BaseFloat> state_occs;
69  if (occs_in_filename != "") {
70  bool binary_read;
71  kaldi::Input ki(occs_in_filename, &binary_read);
72  state_occs.Read(ki.Stream(), binary_read);
73  } else {
74  KALDI_LOG << "--state-occs option not provided so using constant occupancies.";
75  state_occs.Resize(am_gmm.NumPdfs());
76  state_occs.Set(1.0);
77  }
78 
79  std::vector<int32> sil_pdfs;
80  if (sil_phones_str != "") {
81  std::vector<int32> sil_phones;
82  if (!kaldi::SplitStringToIntegers(sil_phones_str, ":", false, &sil_phones))
83  KALDI_ERR << "invalid sil-phones option " << sil_phones_str;
84  std::sort(sil_phones.begin(), sil_phones.end());
85  bool ans = GetPdfsForPhones(trans_model, sil_phones, &sil_pdfs);
86  if (!ans)
87  KALDI_WARN << "Pdfs associated with silence phones are not only "
88  "associated with silence phones: your speech-silence split "
89  "may not be meaningful.";
90  }
91 
92  kaldi::RegressionTree regtree;
93  regtree.BuildTree(state_occs, sil_pdfs, am_gmm, max_leaves);
94  // Write out the regression tree
95  {
96  kaldi::Output ko(tree_out_filename, binary_write);
97  regtree.Write(ko.Stream(), binary_write);
98  }
99 
100  KALDI_LOG << "Written regression tree to " << tree_out_filename;
101  } catch(const std::exception &e) {
102  std::cerr << e.what() << '\n';
103  return -1;
104  }
105 }
106 
107 
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
int main(int argc, char *argv[])
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void Register(const std::string &name, bool *ptr, const std::string &doc)
void Write(std::ostream &out, bool binary) const
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 BuildTree(const Vector< BaseFloat > &state_occs, const std::vector< int32 > &sil_indices, const AmDiagGmm &am, int32 max_clusters)
Top-down clustering of the Gaussians in a model based on their means.
A regression tree is a clustering of Gaussian densities in an acoustic model, such that the group of ...
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.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
void Set(Real f)
Set all members of a vector to a specified value.
bool GetPdfsForPhones(const TransitionModel &trans_model, const std::vector< int32 > &phones, std::vector< int32 > *pdfs)
Works out which pdfs might correspond to the given phones.
#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.