post-to-smat.cc
Go to the documentation of this file.
1 // bin/post-to-smat.cc
2 
3 // Copyright 2017 Johns Hopkins University (Author: Daniel Povey)
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 
25 int main(int argc, char *argv[]) {
26  using namespace kaldi;
27  typedef kaldi::int32 int32;
28  try {
29  const char *usage =
30  "This program turns an archive of per-frame posteriors, e.g. from\n"
31  "ali-to-post | post-to-pdf-post,\n"
32  "into an archive of SparseMatrix. This is just a format transformation.\n"
33  "This may not make sense if the indexes in question are one-based (at least,\n"
34  "you'd have to increase the dimension by one.\n"
35  "\n"
36  "See also: post-to-phone-post, ali-to-post, post-to-pdf-post\n"
37  "\n"
38  "Usage: post-to-smat [options] <posteriors-rspecifier> <sparse-matrix-wspecifier>\n"
39  "e.g.: post-to-smat --dim=1038 ark:- ark:-\n";
40 
41  ParseOptions po(usage);
42 
43  int32 dim = -1;
44 
45  po.Register("dim", &dim, "The num-cols in each output SparseMatrix. All "
46  "the integers in the input posteriors are expected to be \n"
47  ">= 0 and < dim. This must be specified.");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 2) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56  if (dim <= 0) {
57  KALDI_ERR << "The --dim option must be specified.";
58  }
59 
60 
61  std::string posteriors_rspecifier = po.GetArg(1),
62  sparse_matrix_wspecifier = po.GetArg(2);
63 
64 
65  SequentialPosteriorReader posterior_reader(posteriors_rspecifier);
66 
68  sparse_matrix_wspecifier);
69 
70  int32 num_done = 0;
71  for (; !posterior_reader.Done(); posterior_reader.Next()) {
72  const kaldi::Posterior &posterior = posterior_reader.Value();
73  // The following constructor will throw an error if there is some kind of
74  // dimension mismatch.
75  SparseMatrix<BaseFloat> smat(dim, posterior);
76  sparse_matrix_writer.Write(posterior_reader.Key(), smat);
77  num_done++;
78  }
79  KALDI_LOG << "Done converting " << num_done
80  << " posteriors into sparse matrices.";
81  } catch(const std::exception &e) {
82  std::cerr << e.what();
83  return -1;
84  }
85 }
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)
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
int main(int argc, char *argv[])
Definition: post-to-smat.cc:25
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_LOG
Definition: kaldi-error.h:153