init-ubm.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "util/kaldi-io.h"
#include "gmm/diag-gmm.h"
#include "gmm/full-gmm.h"
#include "gmm/am-diag-gmm.h"
#include "hmm/transition-model.h"
Include dependency graph for init-ubm.cc:

Go to the source code of this file.

Functions

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

Function Documentation

◆ main()

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

Definition at line 30 of file init-ubm.cc.

References UbmClusteringOptions::Check(), kaldi::ClusterGaussiansToUbm(), FullGmm::CopyFromDiagGmm(), ParseOptions::GetArg(), KALDI_LOG, ParseOptions::NumArgs(), AmDiagGmm::NumPdfs(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), Vector< Real >::Read(), ParseOptions::Register(), UbmClusteringOptions::Register(), Vector< Real >::Resize(), Output::Stream(), Input::Stream(), FullGmm::Write(), and DiagGmm::Write().

30  {
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 }
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
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).
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
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)
int32 NumPdfs() const
Definition: am-diag-gmm.h:82
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
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.