All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
gmm-get-feat-deriv.cc File Reference
Include dependency graph for gmm-get-feat-deriv.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 28 of file gmm-get-feat-deriv.cc.

References kaldi::ComputeAmGmmFeatureDeriv(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), AmDiagGmm::Read(), ParseOptions::Read(), TransitionModel::Read(), Input::Stream(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

28  {
29  using namespace kaldi;
30  using kaldi::int32;
31  try {
32  const char *usage =
33  "From GMM model and posteriors (which don't have to be positive),\n"
34  "output for each utterance a matrix of likelihood derivatives w.r.t.\n"
35  "the features.\n"
36  "E.g. used in feature-space discriminative training.\n"
37  "\n"
38  "Usage: gmm-get-feat-deriv [options] <model-in> <feature-rspecifier> "
39  "<posteriors-rspecifier> <feature-deriv-wspecifier>\n"
40  "e.g.: \n"
41  " gmm-get-feat-deriv 1.mdl \"$feats\" ark:1.post ark:1.deriv\n";
42 
43  ParseOptions po(usage);
44 
45  po.Read(argc, argv);
46 
47  if (po.NumArgs() != 4) {
48  po.PrintUsage();
49  exit(1);
50  }
51 
52  std::string model_filename = po.GetArg(1),
53  feature_rspecifier = po.GetArg(2),
54  posteriors_rspecifier = po.GetArg(3),
55  deriv_wspecifier = po.GetArg(4);
56 
57  AmDiagGmm am_gmm;
58  TransitionModel trans_model;
59  {
60  bool binary;
61  Input ki(model_filename, &binary);
62  trans_model.Read(ki.Stream(), binary);
63  am_gmm.Read(ki.Stream(), binary);
64  }
65 
66  SequentialBaseFloatMatrixReader feature_reader(feature_rspecifier);
67  RandomAccessPosteriorReader posteriors_reader(posteriors_rspecifier);
68  BaseFloatMatrixWriter deriv_writer(deriv_wspecifier);
69 
70  int32 num_done = 0, num_err = 0;
71 
72  for (; !feature_reader.Done(); feature_reader.Next()) {
73  std::string key = feature_reader.Key();
74  if (!posteriors_reader.HasKey(key)) {
75  KALDI_WARN << "No posteriors for utterance " << key;
76  num_err++;
77  } else {
78  const Matrix<BaseFloat> &mat = feature_reader.Value();
79  const Posterior &posterior = posteriors_reader.Value(key);
80 
81  if (static_cast<int32>(posterior.size()) != mat.NumRows()) {
82  KALDI_WARN << "Posterior vector has wrong size " <<
83  (posterior.size()) << " vs. "<< (mat.NumRows());
84  num_err++;
85  continue;
86  }
87 
88  num_done++;
89 
90  // Derivative of likelihood (or whatever objective func.)
91  // w.r.t. features.
92  Matrix<BaseFloat> deriv;
93  ComputeAmGmmFeatureDeriv(am_gmm, trans_model, posterior,
94  mat, &deriv);
95 
96  deriv_writer.Write(key, deriv);
97  if (num_done % 100 == 0)
98  KALDI_LOG << "Processed " << num_done << " utterances.";
99  }
100  }
101 
102  KALDI_LOG << "Done " << num_done << " files, " << num_err
103  << " with errors.";
104  if (num_done != 0) return 0;
105  else return 1;
106  } catch(const std::exception &e) {
107  std::cerr << e.what();
108  return -1;
109  }
110 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
BaseFloat ComputeAmGmmFeatureDeriv(const AmDiagGmm &am_gmm, const TransitionModel &trans_model, const Posterior &posterior, const MatrixBase< BaseFloat > &features, Matrix< BaseFloat > *direct_deriv, const AccumAmDiagGmm *model_diff, Matrix< BaseFloat > *indirect_deriv)
Computes derivatives of the likelihood of these states (weighted), w.r.t.
Definition: fmpe.cc:522
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
std::vector< std::vector< std::pair< int32, BaseFloat > > > Posterior
Posterior is a typedef for storing acoustic-state (actually, transition-id) posteriors over an uttera...
Definition: posterior.h:43
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
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
#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
#define KALDI_LOG
Definition: kaldi-error.h:133
void Read(std::istream &in_stream, bool binary)
Definition: am-diag-gmm.cc:147