sgmm2-comp-prexform.cc
Go to the documentation of this file.
1 // sgmm2bin/sgmm2-comp-prexform.cc
2 
3 // Copyright 2009-2012 Saarland University (author: Arnab Ghoshal)
4 // Johns Hopkins University (author: Daniel Povey)
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 "util/common-utils.h"
22 #include "sgmm2/am-sgmm2.h"
23 #include "sgmm2/fmllr-sgmm2.h"
24 #include "hmm/transition-model.h"
25 
26 int main(int argc, char *argv[]) {
27  try {
28  typedef kaldi::int32 int32;
29 
30  const char *usage =
31  "Compute \"pre-transform\" parameters required for estimating fMLLR with\n"
32  "SGMMs, and write to a model file, after the SGMM.\n"
33  "Usage: sgmm2-comp-prexform [options] <sgmm2-in> <occs-in> <sgmm-out>\n";
34 
35  bool binary = true;
36  kaldi::ParseOptions po(usage);
37  po.Register("binary", &binary, "Write output in binary mode");
38  po.Read(argc, argv);
39 
40  if (po.NumArgs() < 3) {
41  po.PrintUsage();
42  exit(1);
43  }
44 
45  std::string sgmm_in_filename = po.GetArg(1),
46  occs_filename = po.GetArg(2),
47  sgmm_out_filename = po.GetArg(3);
48 
49  kaldi::AmSgmm2 sgmm_in;
50  kaldi::TransitionModel trans_model;
51  {
52  bool binary_read;
53  kaldi::Input ki(sgmm_in_filename, &binary_read);
54  trans_model.Read(ki.Stream(), binary_read);
55  sgmm_in.Read(ki.Stream(), binary_read);
56  }
57 
59  {
60  bool binary_read;
61  kaldi::Input ki(occs_filename, &binary_read);
62  occs.Read(ki.Stream(), binary_read);
63  }
64 
65  kaldi::Sgmm2FmllrGlobalParams fmllr_globals;
66  sgmm_in.ComputeFmllrPreXform(occs, &fmllr_globals.pre_xform_,
67  &fmllr_globals.inv_xform_,
68  &fmllr_globals.mean_scatter_);
69 
70  {
71  kaldi::Output ko(sgmm_out_filename, binary);
72  trans_model.Write(ko.Stream(), binary);
73  sgmm_in.Write(ko.Stream(), binary, kaldi::kSgmmWriteAll);
74  fmllr_globals.Write(ko.Stream(), binary);
75  }
76 
77  KALDI_LOG << "Written model to " << sgmm_out_filename;
78  } catch(const std::exception &e) {
79  std::cerr << e.what() << '\n';
80  return -1;
81  }
82 }
83 
84 
void Write(std::ostream &os, bool binary, SgmmWriteFlagsType write_params) const
Definition: am-sgmm2.cc:203
Class for definition of the subspace Gmm acoustic model.
Definition: am-sgmm2.h:231
void Write(std::ostream &out_stream, bool binary) const
Definition: fmllr-sgmm2.cc:103
Matrix< BaseFloat > pre_xform_
Pre-transform matrix. Dim is [D][D+1].
Definition: fmllr-sgmm2.h:103
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Read(std::istream &is, bool binary)
Definition: am-sgmm2.cc:89
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::istream & Stream()
Definition: kaldi-io.cc:826
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
Matrix< BaseFloat > inv_xform_
Inverse of pre-transform. Dim is [D][D+1].
Definition: fmllr-sgmm2.h:105
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void Write(std::ostream &os, bool binary) const
A class representing a vector.
Definition: kaldi-vector.h:406
int main(int argc, char *argv[])
Global adaptation parameters.
Definition: fmllr-sgmm2.h:91
Vector< BaseFloat > mean_scatter_
Diagonal of mean-scatter matrix. Dim is [D].
Definition: fmllr-sgmm2.h:107
#define KALDI_LOG
Definition: kaldi-error.h:153
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.
void ComputeFmllrPreXform(const Vector< BaseFloat > &pdf_occs, Matrix< BaseFloat > *xform, Matrix< BaseFloat > *inv_xform, Vector< BaseFloat > *diag_mean_scatter) const
Computes the LDA-like pre-transform and its inverse as well as the eigenvalues of the scatter of the ...
Definition: am-sgmm2.cc:965