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

References kaldi::ClassifyRspecifier(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), KALDI_ERR, KALDI_LOG, KALDI_VLOG, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), kaldi::PosteriorToMatrix(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadPosterior(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), TableWriter< Holder >::Write(), and kaldi::WriteKaldiObject().

26  {
27  try {
28  using namespace kaldi;
29  using namespace std;
30 
31  const char *usage =
32  "Convert posteriors to features\n"
33  "\n"
34  "Usage: post-to-feats [options] <in-rspecifier> <out-wspecifier>\n"
35  " or: post-to-feats [options] <in-rxfilename> <out-wxfilename>\n"
36  "e.g.: post-to-feats --post-dim=50 ark:post.ark ark:feat.ark\n"
37  "See also: post-to-weights feat-to-post, append-vector-to-feats, append-post-to-feats\n";
38 
39  ParseOptions po(usage);
40 
41  bool binary = true;
42  po.Register("binary", &binary, "If true, output files in binary "
43  "(only relevant for single-file operation, i.e. no tables)");
44 
45  int32 post_dim = 0;
46  po.Register("post-dim", &post_dim, "Dimensionality of the posteriors.");
47 
48  po.Read(argc, argv);
49 
50  if (po.NumArgs() != 2) {
51  po.PrintUsage();
52  exit(1);
53  }
54 
55  if (post_dim == 0) {
56  KALDI_ERR << "You have to set the dimensionality of posteriors "
57  "with '--post-dim=D'";
58  }
59 
60  if (ClassifyRspecifier(po.GetArg(1), NULL, NULL)
61  != kNoRspecifier) {
62  // We're operating on tables, e.g. archives.
63 
64  string post_rspecifier = po.GetArg(1);
65  SequentialPosteriorReader post_reader(post_rspecifier);
66 
67  string wspecifier = po.GetArg(2);
68  BaseFloatMatrixWriter feat_writer(wspecifier);
69 
70  int32 num_done = 0, num_err = 0;
71  // Main loop
72  for (; !post_reader.Done(); post_reader.Next()) {
73  string utt = post_reader.Key();
74  KALDI_VLOG(2) << "Processing utterance " << utt;
75 
76  const Posterior &post(post_reader.Value());
77 
78  Matrix<BaseFloat> output;
79  PosteriorToMatrix(post, post_dim, &output);
80 
81  feat_writer.Write(utt, output);
82  num_done++;
83  }
84  KALDI_LOG << "Done " << num_done << " utts, errors on "
85  << num_err;
86 
87  return (num_done == 0 ? -1 : 0);
88  } else {
89  // We're operating on rxfilenames|wxfilenames, most likely files.
90  Posterior post;
91  bool binary_in;
92  Input ki(po.GetArg(1), &binary_in);
93  ReadPosterior(ki.Stream(), binary_in, &post);
94 
95  Matrix<BaseFloat> output;
96  PosteriorToMatrix(post, post_dim, &output);
97 
98  std::string output_wxfilename = po.GetArg(3);
99  WriteKaldiObject(output, output_wxfilename, binary);
100  KALDI_LOG << "Wrote posteriors as feature-matrix to " << output_wxfilename;
101  return 0;
102  }
103  } catch(const std::exception &e) {
104  std::cerr << e.what();
105  return -1;
106  }
107 }
void PosteriorToMatrix(const Posterior &post, const int32 post_dim, Matrix< Real > *mat)
This converts a Posterior to a Matrix.
Definition: posterior.cc:474
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
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
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
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
#define KALDI_VLOG(v)
Definition: kaldi-error.h:136
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:133
void ReadPosterior(std::istream &is, bool binary, Posterior *post)
stand-alone function for reading a Posterior.
Definition: posterior.cc:64