post-to-feats.cc
Go to the documentation of this file.
1 // featbin/post-to-feats.cc
2 
3 // Copyright 2016 Brno University of Technology (Author: Karel Vesely)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "matrix/kaldi-matrix.h"
24 #include "hmm/posterior.h"
25 
26 int main(int argc, char *argv[]) {
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:512
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *rxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
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:42
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
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
#define KALDI_ERR
Definition: kaldi-error.h:147
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_VLOG(v)
Definition: kaldi-error.h:156
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153
void ReadPosterior(std::istream &is, bool binary, Posterior *post)
stand-alone function for reading a Posterior.
Definition: posterior.cc:64