All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
fmpe-acc-stats.cc File Reference
#include "base/kaldi-common.h"
#include "util/common-utils.h"
#include "transform/fmpe.h"
Include dependency graph for fmpe-acc-stats.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 24 of file fmpe-acc-stats.cc.

References Fmpe::AccStats(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and kaldi::WriteKaldiObject().

24  {
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 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
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
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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
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
#define KALDI_WARN
Definition: kaldi-error.h:130
MatrixIndexT NumRows() const
Returns number of rows (or zero for emtpy matrix).
Definition: kaldi-matrix.h:58
MatrixIndexT NumCols() const
Returns number of columns (or zero for emtpy matrix).
Definition: kaldi-matrix.h:61
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:133
Sub-matrix representation.
Definition: kaldi-matrix.h:908