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

References SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), rnnlm::i, rnnlm::j, KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), TransitionModel::NumPdfs(), TransitionModel::NumTransitionIds(), ParseOptions::PrintUsage(), ParseOptions::Read(), TransitionModel::Read(), ParseOptions::Register(), Output::Stream(), Input::Stream(), TransitionModel::TransitionIdToPdf(), SequentialTableReader< Holder >::Value(), and VectorBase< Real >::Write().

27  {
28  try {
29  using namespace kaldi;
30  typedef kaldi::int32 int32;
31 
32  const char *usage =
33  "From posteriors, compute transition-accumulators\n"
34  "The output is a vector of counts/soft-counts, indexed by transition-id)\n"
35  "Note: the model is only read in order to get the size of the vector\n"
36  "\n"
37  "Usage: post-to-tacc [options] <model> <post-rspecifier> <accs>\n"
38  " e.g.: post-to-tacc --binary=false 1.mdl \"ark:ali-to-post 1.ali|\" 1.tacc\n";
39 
40  bool binary = true;
41  bool per_pdf = false;
42  ParseOptions po(usage);
43  po.Register("binary", &binary, "Write output in binary mode.");
44  po.Register("per-pdf", &per_pdf, "if ture, accumulate counts per pdf-id"
45  " rather than transition-id. (default: false)");
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() != 3) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  std::string model_rxfilename = po.GetArg(1),
54  post_rspecifier = po.GetArg(2),
55  accs_wxfilename = po.GetArg(3);
56 
57  kaldi::SequentialPosteriorReader posterior_reader(post_rspecifier);
58 
59  int32 num_transition_ids;
60 
61  bool binary_in;
62  Input ki(model_rxfilename, &binary_in);
63  TransitionModel trans_model;
64  trans_model.Read(ki.Stream(), binary_in);
65  num_transition_ids = trans_model.NumTransitionIds();
66 
67  Vector<double> transition_accs(num_transition_ids+1); // +1 because they're
68  // 1-based; position zero is empty. We'll write as float.
69  int32 num_done = 0;
70 
71  for (; !posterior_reader.Done(); posterior_reader.Next()) {
72  const kaldi::Posterior &posterior = posterior_reader.Value();
73  int32 num_frames = static_cast<int32>(posterior.size());
74  for (int32 i = 0; i < num_frames; i++) {
75  for (int32 j = 0; j < static_cast<int32>(posterior[i].size()); j++) {
76  int32 tid = posterior[i][j].first;
77  if (tid <= 0 || tid > num_transition_ids)
78  KALDI_ERR << "Invalid transition-id " << tid
79  << " encountered for utterance "
80  << posterior_reader.Key();
81  transition_accs(tid) += posterior[i][j].second;
82  }
83  }
84  num_done++;
85  }
86 
87  if (per_pdf) {
88  KALDI_LOG << "accumulate counts per pdf-id";
89  int32 num_pdf_ids = trans_model.NumPdfs();
90  Vector<double> pdf_accs(num_pdf_ids);
91  for (int32 i = 1; i < num_transition_ids; i++) {
92  int32 pid = trans_model.TransitionIdToPdf(i);
93  pdf_accs(pid) += transition_accs(i);
94  }
95  Vector<BaseFloat> pdf_accs_float(pdf_accs);
96  Output ko(accs_wxfilename, binary);
97  pdf_accs_float.Write(ko.Stream(), binary);
98  } else {
99  Vector<BaseFloat> transition_accs_float(transition_accs);
100  Output ko(accs_wxfilename, binary);
101  transition_accs_float.Write(ko.Stream(), binary);
102  }
103  KALDI_LOG << "Done computing transition stats over "
104  << num_done << " utterances; wrote stats to "
105  << accs_wxfilename;
106  return (num_done != 0 ? 0 : 1);
107  } catch(const std::exception &e) {
108  std::cerr << e.what();
109  return -1;
110  }
111 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
int32 TransitionIdToPdf(int32 trans_id) const
int32 NumTransitionIds() const
Returns the total number of transition-ids (note, these are one-based).
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_ERR
Definition: kaldi-error.h:127
#define KALDI_LOG
Definition: kaldi-error.h:133