All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
append-post-to-feats.cc File Reference
Include dependency graph for append-post-to-feats.cc:

Go to the source code of this file.

Namespaces

 kaldi
 Relabels neural network egs with the read pdf-id alignments.
 

Functions

void AppendPostToFeats (const Matrix< BaseFloat > &in, const Posterior &post, const int32 post_dim, Matrix< BaseFloat > *out)
 
int main (int argc, char *argv[])
 

Function Documentation

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

Definition at line 50 of file append-post-to-feats.cc.

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

50  {
51  try {
52  using namespace kaldi;
53  using namespace std;
54 
55  const char *usage =
56  "Append posteriors to features\n"
57  "\n"
58  "Usage: append-post-to-feats [options] <in-rspecifier1> <in-rspecifier2> <out-wspecifier>\n"
59  " or: append-post-to-feats [options] <in-rxfilename1> <in-rxfilename2> <out-wxfilename>\n"
60  "e.g.: append-post-to-feats --post-dim=50 ark:input.ark scp:post.scp ark:output.ark\n"
61  "See also: paste-feats, concat-feats, append-vector-to-feats\n";
62 
63  ParseOptions po(usage);
64 
65  bool binary = true;
66  po.Register("binary", &binary, "If true, output files in binary "
67  "(only relevant for single-file operation, i.e. no tables)");
68 
69  int32 post_dim = 0;
70  po.Register("post-dim", &post_dim, "Dimensionality of the posteriors.");
71 
72  po.Read(argc, argv);
73 
74  if (po.NumArgs() != 3) {
75  po.PrintUsage();
76  exit(1);
77  }
78 
79  if (post_dim == 0) {
80  KALDI_ERR << "You have to set the dimensionality of posteriors "
81  "with '--post-dim=D'";
82  }
83 
84  if (ClassifyRspecifier(po.GetArg(1), NULL, NULL)
85  != kNoRspecifier) {
86  // We're operating on tables, e.g. archives.
87 
88  string feat_rspecifier = po.GetArg(1);
89  SequentialBaseFloatMatrixReader feat_reader(feat_rspecifier);
90 
91  string post_rspecifier = po.GetArg(2);
92  RandomAccessPosteriorReader post_reader(post_rspecifier);
93 
94  string wspecifier = po.GetArg(3);
95  BaseFloatMatrixWriter feat_writer(wspecifier);
96 
97  int32 num_done = 0, num_err = 0;
98  // Main loop
99  for (; !feat_reader.Done(); feat_reader.Next()) {
100  string utt = feat_reader.Key();
101  KALDI_VLOG(2) << "Processing utterance " << utt;
102 
103  const Matrix<BaseFloat> &feats(feat_reader.Value());
104 
105  if (!post_reader.HasKey(utt)) {
106  KALDI_WARN << "Could not read posteriors for utterance " << utt;
107  num_err++;
108  continue;
109  }
110  const Posterior &post(post_reader.Value(utt));
111 
112  Matrix<BaseFloat> output;
113  AppendPostToFeats(feats, post, post_dim, &output);
114  feat_writer.Write(utt, output);
115  num_done++;
116  }
117  KALDI_LOG << "Done " << num_done << " utts, errors on "
118  << num_err;
119 
120  return (num_done == 0 ? -1 : 0);
121  } else {
122  // We're operating on rxfilenames|wxfilenames, most likely files.
123  Matrix<BaseFloat> mat;
124  ReadKaldiObject(po.GetArg(1), &mat);
125 
126  Posterior post;
127  bool binary_in;
128  Input ki(po.GetArg(2), &binary_in);
129  ReadPosterior(ki.Stream(), binary_in, &post);
130 
131  Matrix<BaseFloat> output;
132  AppendPostToFeats(mat, post, post_dim, &output);
133 
134  std::string output_wxfilename = po.GetArg(3);
135  WriteKaldiObject(output, output_wxfilename, binary);
136  KALDI_LOG << "Wrote appended features to " << output_wxfilename;
137  return 0;
138  }
139  } catch(const std::exception &e) {
140  std::cerr << e.what();
141  return -1;
142  }
143 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void AppendPostToFeats(const Matrix< BaseFloat > &in, const Posterior &post, const int32 post_dim, Matrix< BaseFloat > *out)
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
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
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
#define KALDI_WARN
Definition: kaldi-error.h:130
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