feat-to-post.cc
Go to the documentation of this file.
1 // nnetbin/feat-to-post.cc
2 
3 // Copyright 2014 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 "hmm/posterior.h"
24 
27 int main(int argc, char *argv[]) {
28  using namespace kaldi;
29  typedef kaldi::int32 int32;
30  try {
31  const char *usage =
32  "Convert features into posterior format, which is the generic format \n"
33  "of NN training targets in Karel's nnet1 tools.\n"
34  "(speed is not an issue for reasonably low NN-output dimensions)\n"
35  "Usage: feat-to-post [options] feat-rspecifier posteriors-wspecifier\n"
36  "e.g.:\n"
37  " feat-to-post scp:feats.scp ark:feats.post\n";
38 
39  ParseOptions po(usage);
40 
41  po.Read(argc, argv);
42 
43  if (po.NumArgs() != 2) {
44  po.PrintUsage();
45  exit(1);
46  }
47 
48  std::string feats_rspecifier = po.GetArg(1);
49  std::string posteriors_wspecifier = po.GetArg(2);
50 
51  int32 num_done = 0;
52  SequentialBaseFloatMatrixReader feats_reader(feats_rspecifier);
53  PosteriorWriter posterior_writer(posteriors_wspecifier);
54 
55  for (; !feats_reader.Done(); feats_reader.Next()) {
56  num_done++;
57  const Matrix<BaseFloat> &mat = feats_reader.Value();
58  int32 num_frames = mat.NumRows(),
59  num_dims = mat.NumCols();
60  // Posterior is vector<vector<pair<int32, BaseFloat> > >
61  Posterior post(num_frames);
62  // Fill posterior with matrix values,
63  for (int32 f = 0; f < num_frames; f++) {
64  for (int32 d = 0; d < num_dims; d++) {
65  post[f].push_back(std::make_pair(d, mat(f, d)));
66  }
67  KALDI_ASSERT(post[f].size() == num_dims);
68  }
69  // Store
70  posterior_writer.Write(feats_reader.Key(), post);
71  }
72  KALDI_LOG << "Converted " << num_done << " alignments.";
73  return (num_done != 0 ? 0 : 1);
74  } catch(const std::exception &e) {
75  std::cerr << e.what();
76  return -1;
77  }
78 }
79 
80 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
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
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.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int main(int argc, char *argv[])
Converts features into posterior format, which is the generic format of NN training targets in &#39;nnet1...
Definition: feat-to-post.cc:27
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_LOG
Definition: kaldi-error.h:153