sgmm2-gselect.cc
Go to the documentation of this file.
1 // sgmm2bin/sgmm2-gselect.cc
2 
3 // Copyright 2009-2012 Saarland University Microsoft Corporation
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 
22 #include "base/kaldi-common.h"
23 #include "util/common-utils.h"
24 #include "sgmm2/am-sgmm2.h"
25 #include "hmm/transition-model.h"
26 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  const char *usage =
31  "Precompute Gaussian indices for SGMM training "
32  "Usage: sgmm2-gselect [options] <model-in> <feature-rspecifier> <gselect-wspecifier>\n"
33  "e.g.: sgmm2-gselect 1.sgmm \"ark:feature-command |\" ark:1.gs\n"
34  "Note: you can do the same thing by combining the programs sgmm2-write-ubm, fgmm-global-to-gmm,\n"
35  "gmm-gselect and fgmm-gselect\n";
36 
37  ParseOptions po(usage);
38  kaldi::Sgmm2GselectConfig sgmm_opts;
39  std::string preselect_rspecifier;
40  std::string likelihood_wspecifier;
41  po.Register("write-likes", &likelihood_wspecifier, "Wspecifier for likelihoods per "
42  "utterance");
43  sgmm_opts.Register(&po);
44  po.Read(argc, argv);
45 
46  if (po.NumArgs() != 3) {
47  po.PrintUsage();
48  exit(1);
49  }
50 
51  std::string model_filename = po.GetArg(1),
52  feature_rspecifier = po.GetArg(2),
53  gselect_wspecifier = po.GetArg(3);
54 
55  using namespace kaldi;
56  typedef kaldi::int32 int32;
57 
58  AmSgmm2 am_sgmm;
59  {
60  bool binary;
61  Input ki(model_filename, &binary);
62  TransitionModel trans_model;
63  trans_model.Read(ki.Stream(), binary);
64  am_sgmm.Read(ki.Stream(), binary);
65  }
66 
67  double tot_like = 0.0;
68  kaldi::int64 tot_t = 0;
69 
70  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
71  Int32VectorVectorWriter gselect_writer(gselect_wspecifier);
72  BaseFloatWriter likelihood_writer(likelihood_wspecifier);
73 
74  int32 num_done = 0, num_err = 0;
75  for (; !feature_reader.Done(); feature_reader.Next()) {
76  int32 tot_t_this_file = 0; double tot_like_this_file = 0;
77  std::string utt = feature_reader.Key();
78  const Matrix<BaseFloat> &mat = feature_reader.Value();
79  std::vector<std::vector<int32> > gselect_vec(mat.NumRows());
80  tot_t_this_file += mat.NumRows();
81  for (int32 i = 0; i < mat.NumRows(); i++)
82  tot_like_this_file += am_sgmm.GaussianSelection(sgmm_opts, mat.Row(i), &(gselect_vec[i]));
83 
84  gselect_writer.Write(utt, gselect_vec);
85  if (num_done % 10 == 0)
86  KALDI_LOG << "For " << num_done << "'th file, average UBM likelihood over "
87  << tot_t_this_file << " frames is "
88  << (tot_like_this_file/tot_t_this_file);
89  tot_t += tot_t_this_file;
90  tot_like += tot_like_this_file;
91 
92  if(likelihood_wspecifier != "")
93  likelihood_writer.Write(utt, tot_like_this_file);
94  num_done++;
95  }
96 
97  KALDI_LOG << "Done " << num_done << " files, " << num_err
98  << " with errors, average UBM log-likelihood is "
99  << (tot_like/tot_t) << " over " << tot_t << " frames.";
100 
101 
102  if (num_done != 0) return 0;
103  else return 1;
104  } catch(const std::exception &e) {
105  std::cerr << e.what();
106  return -1;
107  }
108 }
109 
110 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
Class for definition of the subspace Gmm acoustic model.
Definition: am-sgmm2.h:231
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
int main(int argc, char *argv[])
void Read(std::istream &is, bool binary)
Definition: am-sgmm2.cc:89
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
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
const SubVector< Real > Row(MatrixIndexT i) const
Return specific row of matrix [const].
Definition: kaldi-matrix.h:188
void Read(std::istream &is, bool binary)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
BaseFloat GaussianSelection(const Sgmm2GselectConfig &config, const VectorBase< BaseFloat > &data, std::vector< int32 > *gselect) const
Computes the top-scoring Gaussian indices (used for pruning of later stages of computation).
Definition: am-sgmm2.cc:1406
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).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_LOG
Definition: kaldi-error.h:153
void Register(OptionsItf *opts)
Definition: am-sgmm2.h:129