All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
matrix-logprob.cc File Reference
Include dependency graph for matrix-logprob.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 26 of file matrix-logprob.cc.

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), ParseOptions::GetOptArg(), RandomAccessTableReader< Holder >::HasKey(), KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), logprob, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumCols(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), RandomAccessTableReader< Holder >::Value(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Compute the log-prob of a particular (e.g.) pdf sequence, derived from a pdf alignment,\n"
32  "given log-probs in a matrix. The log-probs are computed over the whole data and printed\n"
33  "as a logging message. Optionally also write out the original matrix. This is for use in\n"
34  "neural net discriminative training (for computing objective functions).\n"
35  "\n"
36  "Usage: matrix-logprob [options] <matrix-rspecifier> <pdf-ali-rspecifier> [<matrix-wspecifier1>]\n";
37 
38  ParseOptions po(usage);
39 
40  po.Read(argc, argv);
41 
42  if (po.NumArgs() < 2 || po.NumArgs() > 3) {
43  po.PrintUsage();
44  exit(1);
45  }
46 
47  std::string matrix_rspecifier = po.GetArg(1),
48  pdf_ali_rspecifier = po.GetArg(2),
49  matrix_wspecifier = po.GetOptArg(3);
50 
51  SequentialBaseFloatMatrixReader matrix_reader(matrix_rspecifier);
52  RandomAccessInt32VectorReader pdf_ali_reader(pdf_ali_rspecifier);
53  BaseFloatMatrixWriter matrix_writer(matrix_wspecifier);
54 
55  int64 tot_num_frames = 0;
56  double tot_log_prob = 0;
57 
58  int32 num_done = 0, num_err = 0;
59  for (; !matrix_reader.Done(); matrix_reader.Next()) {
60  std::string key = matrix_reader.Key();
61  const Matrix<BaseFloat> &logprob = matrix_reader.Value();
62  if (pdf_ali_reader.HasKey(key)) {
63  const std::vector<int32> &ali = pdf_ali_reader.Value(key);
64  int32 num_frames = ali.size();
65  if (num_frames != logprob.NumRows()) {
66  KALDI_WARN << "Alignment has wrong size " << num_frames
67  << " vs. " << logprob.NumRows() << " for utterance " << key;
68  num_err++;
69  continue;
70  }
71  for (int32 t = 0; t < num_frames; t++) {
72  int32 pdf_id = ali[t];
73  if (pdf_id < 0 || pdf_id > logprob.NumCols()) // I'm letting this be an error.
74  KALDI_ERR << "PDF id " << pdf_id << " out of range: "
75  << " max is " << logprob.NumCols();
76  tot_log_prob += logprob(t, pdf_id);
77  }
78  tot_num_frames += num_frames;
79  num_done++;
80  } else {
81  KALDI_WARN << "No alignment for key " << key;
82  num_err++;
83  }
84  if (!matrix_wspecifier.empty()) {
85  matrix_writer.Write(key, logprob);
86  }
87  }
88 
89  KALDI_LOG << "Average log-prob per frame is " << (tot_log_prob / tot_num_frames)
90  << " over " << tot_num_frames << " frames.";
91  KALDI_LOG << "Successfully processed " << num_done << " utterances, "
92  << num_err << " with errors.";
93  return (num_done != 0 ? 0 : 1);
94  } catch(const std::exception &e) {
95  std::cerr << e.what();
96  return -1;
97  }
98 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
float logprob
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
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
#define KALDI_LOG
Definition: kaldi-error.h:133