fmpe-acc-stats.cc
Go to the documentation of this file.
1 // featbin/fmpe-acc-stats.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "transform/fmpe.h"
23 
24 int main(int argc, char *argv[]) {
25  using namespace kaldi;
26  using kaldi::int32;
27  try {
28  const char *usage =
29  "Compute statistics for fMPE training\n"
30  "Usage: fmpe-acc-stats [options...] <fmpe-object> "
31  "<feat-rspecifier> <feat-diff-rspecifier> <gselect-rspecifier> <stats-out>\n"
32  "Note: gmm-fmpe-acc-stats avoids computing the features an extra time\n";
33 
34  ParseOptions po(usage);
35  bool binary = true;
36  po.Register("binary", &binary, "If true, output stats in binary mode.");
37  po.Read(argc, argv);
38 
39  if (po.NumArgs() != 5) {
40  po.PrintUsage();
41  exit(1);
42  }
43 
44  std::string fmpe_rxfilename = po.GetArg(1),
45  feat_rspecifier = po.GetArg(2),
46  feat_diff_rspecifier = po.GetArg(3),
47  gselect_rspecifier = po.GetArg(4),
48  stats_wxfilename = po.GetArg(5);
49 
50  Fmpe fmpe;
51  ReadKaldiObject(fmpe_rxfilename, &fmpe);
52 
53  SequentialBaseFloatMatrixReader feat_reader(feat_rspecifier);
54  RandomAccessBaseFloatMatrixReader diff_reader(feat_diff_rspecifier);
55  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
56 
57  // fmpe stats...
58  FmpeStats fmpe_stats(fmpe);
59 
60  int32 num_done = 0, num_err = 0;
61 
62  for (; !feat_reader.Done(); feat_reader.Next()) {
63  std::string key = feat_reader.Key();
64  const Matrix<BaseFloat> feat_in(feat_reader.Value());
65  if (!gselect_reader.HasKey(key)) {
66  KALDI_WARN << "No gselect information for key " << key;
67  num_err++;
68  continue;
69  }
70  const std::vector<std::vector<int32> > &gselect =
71  gselect_reader.Value(key);
72  if (static_cast<int32>(gselect.size()) != feat_in.NumRows()) {
73  KALDI_WARN << "gselect information has wrong size";
74  num_err++;
75  continue;
76  }
77  if (!diff_reader.HasKey(key)) {
78  KALDI_WARN << "No gradient information for key " << key;
79  num_err++;
80  continue;
81  }
82  const Matrix<BaseFloat> &feat_deriv = diff_reader.Value(key);
83 
84  if (feat_deriv.NumCols() == feat_in.NumCols()) { // Only direct derivative.
85  fmpe.AccStats(feat_in, gselect, feat_deriv, NULL, &fmpe_stats);
86  } else if (feat_deriv.NumCols() == feat_in.NumCols() * 2) { // +indirect.
87  SubMatrix<BaseFloat> direct_deriv(feat_deriv, 0, feat_deriv.NumRows(),
88  0, feat_in.NumCols()),
89  indirect_deriv(feat_deriv, 0, feat_deriv.NumRows(),
90  feat_in.NumCols(), feat_in.NumCols());
91  fmpe.AccStats(feat_in, gselect, direct_deriv, &indirect_deriv, &fmpe_stats);
92  } else {
93  KALDI_ERR << "Mismatch in dimension of feature derivative.";
94  }
95  num_done++;
96  }
97 
98  KALDI_LOG << " Done " << num_done << " utterances, " << num_err
99  << " had errors.";
100 
101  WriteKaldiObject(fmpe_stats, stats_wxfilename, binary);
102 
103  return (num_done != 0 ? 0 : 1);
104  } catch(const std::exception &e) {
105  std::cerr << e.what();
106  return -1;
107  }
108 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
int main(int argc, char *argv[])
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
const T & Value(const std::string &key)
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
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
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:153
Sub-matrix representation.
Definition: kaldi-matrix.h:988
void AccStats(const MatrixBase< BaseFloat > &feat_in, const std::vector< std::vector< int32 > > &gselect, const MatrixBase< BaseFloat > &direct_feat_deriv, const MatrixBase< BaseFloat > *indirect_feat_deriv, FmpeStats *stats) const
Definition: fmpe.cc:395