append-post-to-feats.cc
Go to the documentation of this file.
1 // featbin/append-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 namespace kaldi {
27 
29  const Posterior &post,
30  const int32 post_dim,
31  Matrix<BaseFloat> *out) {
32  // Check inputs,
33  if (in.NumRows() != post.size()) {
34  KALDI_WARN << "Mismatch of length!"
35  << " features: " << in.NumRows()
36  << " posteriors: " << post.size();
37  }
38  KALDI_ASSERT(in.NumRows() == post.size());
39  // Convert post to Matrix,
40  Matrix<BaseFloat> post_mat;
41  PosteriorToMatrix(post, post_dim, &post_mat);
42  // Build the output matrix,
43  out->Resize(in.NumRows(), in.NumCols() + post_dim);
44  out->ColRange(0, in.NumCols()).CopyFromMat(in);
45  out->ColRange(in.NumCols(), post_mat.NumCols()).CopyFromMat(post_mat);
46 }
47 
48 } // namespace kaldi,
49 
50 int main(int argc, char *argv[]) {
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 }
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 AppendPostToFeats(const Matrix< BaseFloat > &in, const Posterior &post, const int32 post_dim, Matrix< BaseFloat > *out)
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
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
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
Allows random access to a collection of objects in an archive or script file; see The Table concept...
Definition: kaldi-table.h:233
SubMatrix< Real > ColRange(const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Definition: kaldi-matrix.h:213
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
const T & Value(const std::string &key)
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.
#define KALDI_WARN
Definition: kaldi-error.h:150
int main(int argc, char *argv[])
bool HasKey(const std::string &key)
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_VLOG(v)
Definition: kaldi-error.h:156
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
void Resize(const MatrixIndexT r, const MatrixIndexT c, MatrixResizeType resize_type=kSetZero, MatrixStrideType stride_type=kDefaultStride)
Sets matrix to a specified size (zero is OK as long as both r and c are zero).
#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