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

Go to the source code of this file.

Namespaces

 kaldi
 Relabels neural network egs with the read pdf-id alignments.
 

Functions

void ReadSharedPhonesList (std::string rxfilename, std::vector< std::vector< int32 > > *list_out)
 
int main (int argc, char *argv[])
 

Function Documentation

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

Definition at line 52 of file gmm-init-mono.cc.

References AmDiagGmm::AddPdf(), VectorBase< Real >::AddVec(), VectorBase< Real >::AddVec2(), DiagGmm::ComputeGconsts(), VectorBase< Real >::CopyFromVec(), count, SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), AmDiagGmm::GetPdf(), HmmTopology::GetPhones(), rnnlm::i, VectorBase< Real >::InvertElements(), KALDI_ASSERT, KALDI_ERR, VectorBase< Real >::Min(), kaldi::MonophoneContextDependency(), kaldi::MonophoneContextDependencyShared(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), HmmTopology::NumPdfClasses(), ContextDependency::NumPdfs(), MatrixBase< Real >::NumRows(), DiagGmm::Perturb(), ParseOptions::PrintUsage(), ParseOptions::Read(), HmmTopology::Read(), kaldi::ReadSharedPhonesList(), ParseOptions::Register(), DiagGmm::Resize(), MatrixBase< Real >::Row(), VectorBase< Real >::Scale(), VectorBase< Real >::Set(), DiagGmm::SetInvVarsAndMeans(), DiagGmm::SetWeights(), Output::Stream(), Input::Stream(), SequentialTableReader< Holder >::Value(), AmDiagGmm::Write(), ContextDependency::Write(), and TransitionModel::Write().

52  {
53  try {
54  using namespace kaldi;
55  using kaldi::int32;
56 
57  const char *usage =
58  "Initialize monophone GMM.\n"
59  "Usage: gmm-init-mono <topology-in> <dim> <model-out> <tree-out> \n"
60  "e.g.: \n"
61  " gmm-init-mono topo 39 mono.mdl mono.tree\n";
62 
63  bool binary = true;
64  std::string train_feats;
65  std::string shared_phones_rxfilename;
66  BaseFloat perturb_factor = 0.0;
67  ParseOptions po(usage);
68  po.Register("binary", &binary, "Write output in binary mode");
69  po.Register("train-feats", &train_feats,
70  "rspecifier for training features [used to set mean and variance]");
71  po.Register("shared-phones", &shared_phones_rxfilename,
72  "rxfilename containing, on each line, a list of phones whose pdfs should be shared.");
73  po.Register("perturb-factor", &perturb_factor,
74  "Perturb the means using this fraction of standard deviation.");
75  po.Read(argc, argv);
76 
77  if (po.NumArgs() != 4) {
78  po.PrintUsage();
79  exit(1);
80  }
81 
82 
83  std::string topo_filename = po.GetArg(1);
84  int dim = atoi(po.GetArg(2).c_str());
85  KALDI_ASSERT(dim> 0 && dim < 10000);
86  std::string model_filename = po.GetArg(3);
87  std::string tree_filename = po.GetArg(4);
88 
89  Vector<BaseFloat> glob_inv_var(dim);
90  glob_inv_var.Set(1.0);
91  Vector<BaseFloat> glob_mean(dim);
92  glob_mean.Set(1.0);
93 
94  if (train_feats != "") {
95  double count = 0.0;
96  Vector<double> var_stats(dim);
97  Vector<double> mean_stats(dim);
98  SequentialDoubleMatrixReader feat_reader(train_feats);
99  for (; !feat_reader.Done(); feat_reader.Next()) {
100  const Matrix<double> &mat = feat_reader.Value();
101  for (int32 i = 0; i < mat.NumRows(); i++) {
102  count += 1.0;
103  var_stats.AddVec2(1.0, mat.Row(i));
104  mean_stats.AddVec(1.0, mat.Row(i));
105  }
106  }
107  if (count == 0) { KALDI_ERR << "no features were seen."; }
108  var_stats.Scale(1.0/count);
109  mean_stats.Scale(1.0/count);
110  var_stats.AddVec2(-1.0, mean_stats);
111  if (var_stats.Min() <= 0.0)
112  KALDI_ERR << "bad variance";
113  var_stats.InvertElements();
114  glob_inv_var.CopyFromVec(var_stats);
115  glob_mean.CopyFromVec(mean_stats);
116  }
117 
118  HmmTopology topo;
119  bool binary_in;
120  Input ki(topo_filename, &binary_in);
121  topo.Read(ki.Stream(), binary_in);
122 
123  const std::vector<int32> &phones = topo.GetPhones();
124 
125  std::vector<int32> phone2num_pdf_classes (1+phones.back());
126  for (size_t i = 0; i < phones.size(); i++)
127  phone2num_pdf_classes[phones[i]] = topo.NumPdfClasses(phones[i]);
128 
129  // Now the tree [not really a tree at this point]:
130  ContextDependency *ctx_dep = NULL;
131  if (shared_phones_rxfilename == "") { // No sharing of phones: standard approach.
132  ctx_dep = MonophoneContextDependency(phones, phone2num_pdf_classes);
133  } else {
134  std::vector<std::vector<int32> > shared_phones;
135  ReadSharedPhonesList(shared_phones_rxfilename, &shared_phones);
136  // ReadSharedPhonesList crashes on error.
137  ctx_dep = MonophoneContextDependencyShared(shared_phones, phone2num_pdf_classes);
138  }
139 
140  int32 num_pdfs = ctx_dep->NumPdfs();
141 
142  AmDiagGmm am_gmm;
143  DiagGmm gmm;
144  gmm.Resize(1, dim);
145  { // Initialize the gmm.
146  Matrix<BaseFloat> inv_var(1, dim);
147  inv_var.Row(0).CopyFromVec(glob_inv_var);
148  Matrix<BaseFloat> mu(1, dim);
149  mu.Row(0).CopyFromVec(glob_mean);
150  Vector<BaseFloat> weights(1);
151  weights.Set(1.0);
152  gmm.SetInvVarsAndMeans(inv_var, mu);
153  gmm.SetWeights(weights);
154  gmm.ComputeGconsts();
155  }
156 
157  for (int i = 0; i < num_pdfs; i++)
158  am_gmm.AddPdf(gmm);
159 
160  if (perturb_factor != 0.0) {
161  for (int i = 0; i < num_pdfs; i++)
162  am_gmm.GetPdf(i).Perturb(perturb_factor);
163  }
164 
165  // Now the transition model:
166  TransitionModel trans_model(*ctx_dep, topo);
167 
168  {
169  Output ko(model_filename, binary);
170  trans_model.Write(ko.Stream(), binary);
171  am_gmm.Write(ko.Stream(), binary);
172  }
173 
174  // Now write the tree.
175  ctx_dep->Write(Output(tree_filename, binary).Stream(),
176  binary);
177 
178  delete ctx_dep;
179  return 0;
180  } catch(const std::exception &e) {
181  std::cerr << e.what();
182  return -1;
183  }
184 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void AddPdf(const DiagGmm &gmm)
Adds a GMM to the model, and increments the total number of PDFs.
Definition: am-diag-gmm.cc:57
void Perturb(float perturb_factor)
Perturbs the component means with a random vector multiplied by the pertrub factor.
Definition: diag-gmm.cc:215
void SetInvVarsAndMeans(const MatrixBase< Real > &invvars, const MatrixBase< Real > &means)
Use SetInvVarsAndMeans if updating both means and (inverse) variances.
Definition: diag-gmm-inl.h:63
A class for storing topology information for phones.
Definition: hmm-topology.h:94
void ReadSharedPhonesList(std::string rxfilename, std::vector< std::vector< int32 > > *list_out)
ContextDependency * MonophoneContextDependencyShared(const std::vector< std::vector< int32 > > phone_sets, const std::vector< int32 > phone2num_pdf_classes)
Definition: context-dep.cc:334
virtual int32 NumPdfs() const
NumPdfs() returns the number of acoustic pdfs (they are numbered 0.. NumPdfs()-1).
Definition: context-dep.h:71
void Resize(int32 nMix, int32 dim)
Resizes arrays to this dim. Does not initialize data.
Definition: diag-gmm.cc:66
int32 ComputeGconsts()
Sets the gconsts.
Definition: diag-gmm.cc:114
void Read(std::istream &is, bool binary)
Definition: hmm-topology.cc:39
void Write(std::ostream &out_stream, bool binary) const
Definition: am-diag-gmm.cc:163
int32 NumPdfClasses(int32 phone) const
Returns the number of pdf-classes for this phone; throws exception if phone not covered by this topol...
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:182
const size_t count
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 Write(std::ostream &os, bool binary) const
Definition: context-dep.cc:145
const std::vector< int32 > & GetPhones() const
Returns a reference to a sorted, unique list of phones covered by the topology (these phones will be ...
Definition: hmm-topology.h:164
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
#define KALDI_ERR
Definition: kaldi-error.h:127
ContextDependency * MonophoneContextDependency(const std::vector< int32 > phones, const std::vector< int32 > phone2num_pdf_classes)
Definition: context-dep.cc:322
DiagGmm & GetPdf(int32 pdf_index)
Accessors.
Definition: am-diag-gmm.h:119
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:169
Definition for Gaussian Mixture Model with diagonal covariances.
Definition: diag-gmm.h:42
void SetWeights(const VectorBase< Real > &w)
Mutators for both float or double.
Definition: diag-gmm-inl.h:28