init-ubm.cc
Go to the documentation of this file.
1 // sgmmbin/init-ubm.cc
2 
3 // Copyright 2009-2011 Saarland University
4 // Author: Arnab Ghoshal
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "util/kaldi-io.h"
24 #include "gmm/diag-gmm.h"
25 #include "gmm/full-gmm.h"
26 #include "gmm/am-diag-gmm.h"
27 #include "hmm/transition-model.h"
28 
29 
30 int main(int argc, char *argv[]) {
31  try {
32  typedef kaldi::int32 int32;
34 
35  const char *usage =
36  "Cluster the Gaussians in a diagonal-GMM acoustic model\n"
37  "to a single full-covariance or diagonal-covariance GMM.\n"
38  "Usage: init-ubm [options] <model-file> <state-occs> <gmm-out>\n";
39 
40  bool binary_write = true, fullcov_ubm = true;
41  kaldi::ParseOptions po(usage);
42  po.Register("binary", &binary_write, "Write output in binary mode");
43  po.Register("fullcov-ubm", &fullcov_ubm, "Write out full covariance UBM.");
45  ubm_opts.Register(&po);
46 
47  po.Read(argc, argv);
48 
49  if (po.NumArgs() != 3) {
50  po.PrintUsage();
51  exit(1);
52  }
53  ubm_opts.Check();
54 
55  std::string model_in_filename = po.GetArg(1),
56  occs_in_filename = po.GetArg(2),
57  gmm_out_filename = po.GetArg(3);
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  state_occs.Resize(am_gmm.NumPdfs());
70  {
71  bool binary_read;
72  kaldi::Input ki(occs_in_filename, &binary_read);
73  state_occs.Read(ki.Stream(), binary_read);
74  }
75 
76  kaldi::DiagGmm ubm;
77  ClusterGaussiansToUbm(am_gmm, state_occs, ubm_opts, &ubm);
78  if (fullcov_ubm) {
79  kaldi::FullGmm full_ubm;
80  full_ubm.CopyFromDiagGmm(ubm);
81  kaldi::Output ko(gmm_out_filename, binary_write);
82  full_ubm.Write(ko.Stream(), binary_write);
83  } else {
84  kaldi::Output ko(gmm_out_filename, binary_write);
85  ubm.Write(ko.Stream(), binary_write);
86  }
87 
88  KALDI_LOG << "Written UBM to " << gmm_out_filename;
89  } catch(const std::exception &e) {
90  std::cerr << e.what() << '\n';
91  return -1;
92  }
93 }
94 
95 
void Write(std::ostream &os, bool binary) const
Definition: diag-gmm.cc:705
Definition for Gaussian Mixture Model with full covariances.
Definition: full-gmm.h:40
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Write(std::ostream &os, bool binary) const
Definition: full-gmm.cc:758
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)
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 ClusterGaussiansToUbm(const AmDiagGmm &am, const Vector< BaseFloat > &state_occs, UbmClusteringOptions opts, DiagGmm *ubm_out)
Clusters the Gaussians in an acoustic model to a single GMM with specified number of components...
Definition: am-diag-gmm.cc:195
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.
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).
void Register(OptionsItf *opts)
Definition: am-diag-gmm.h:176
A class representing a vector.
Definition: kaldi-vector.h:406
void CopyFromDiagGmm(const DiagGmm &diaggmm)
Copies from given DiagGmm.
Definition: full-gmm.cc:77
int main(int argc, char *argv[])
Definition: init-ubm.cc:30
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
#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.