All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-make-regtree.cc File Reference
Include dependency graph for gmm-make-regtree.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 29 of file gmm-make-regtree.cc.

References RegressionTree::BuildTree(), ParseOptions::GetArg(), kaldi::GetPdfsForPhones(), KALDI_ERR, KALDI_LOG, KALDI_WARN, ParseOptions::NumArgs(), AmDiagGmm::NumPdfs(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), Vector< Real >::Read(), ParseOptions::Register(), Vector< Real >::Resize(), VectorBase< Real >::Set(), kaldi::SplitStringToIntegers(), Output::Stream(), Input::Stream(), and RegressionTree::Write().

29  {
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 }
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 Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void Write(std::ostream &out, bool binary) const
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
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
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)
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
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:133
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.