fmpe-apply-transform.cc
Go to the documentation of this file.
1 // featbin/fmpe-apply-transform.cc
2 
3 // Copyright 2012 Johns Hopkins University (Author: Daniel Povey) Yanmin Qian
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 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "transform/fmpe.h"
23 
24 int main(int argc, char *argv[]) {
25  using namespace kaldi;
26  using kaldi::int32;
27  try {
28  const char *usage =
29  "Apply fMPE transform to features\n"
30  "Usage: fmpe-apply-transform [options...] <fmpe-object> "
31  "<feat-rspecifier> <gselect-rspecifier> <feat-wspecifier>\n";
32 
33  ParseOptions po(usage);
34  bool add_to_features = true;
35  po.Register("add-to-features", &add_to_features, "If true, add original "
36  "features to fMPE offsets (false useful for diagnostics)");
37  // no non-default options.
38  po.Read(argc, argv);
39 
40  if (po.NumArgs() != 4) {
41  po.PrintUsage();
42  exit(1);
43  }
44 
45  std::string fmpe_rxfilename = po.GetArg(1),
46  feat_rspecifier = po.GetArg(2),
47  gselect_rspecifier = po.GetArg(3),
48  feat_wspecifier = po.GetArg(4);
49 
50  Fmpe fmpe;
51  ReadKaldiObject(fmpe_rxfilename, &fmpe);
52 
53  SequentialBaseFloatMatrixReader feat_reader(feat_rspecifier);
54  RandomAccessInt32VectorVectorReader gselect_reader(gselect_rspecifier);
55  BaseFloatMatrixWriter feat_writer(feat_wspecifier);
56 
57  int32 num_done = 0, num_err = 0;
58 
59  for (; !feat_reader.Done(); feat_reader.Next()) {
60  std::string key = feat_reader.Key();
61  const Matrix<BaseFloat> feat_in(feat_reader.Value());
62  if (!gselect_reader.HasKey(key)) {
63  KALDI_WARN << "No gselect information for key " << key;
64  num_err++;
65  continue;
66  }
67  const std::vector<std::vector<int32> > &gselect =
68  gselect_reader.Value(key);
69  if (static_cast<int32>(gselect.size()) != feat_in.NumRows()) {
70  KALDI_WARN << "gselect information has wrong size";
71  num_err++;
72  continue;
73  }
74  Matrix<BaseFloat> feat_out(feat_in.NumRows(), feat_in.NumCols());
75  fmpe.ComputeFeatures(feat_in, gselect, &feat_out);
76  if (add_to_features) // feat_out += feat_in.
77  feat_out.AddMat(1.0, feat_in, kNoTrans);
78 
79  feat_writer.Write(key, feat_out);
80  num_done++;
81  }
82  KALDI_LOG << " Done " << num_done << " utterances, " << num_err
83  << " had errors.";
84  return (num_done != 0 ? 0 : 1);
85  } catch(const std::exception &e) {
86  std::cerr << e.what();
87  return -1;
88  }
89 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void ComputeFeatures(const MatrixBase< BaseFloat > &feat_in, const std::vector< std::vector< int32 > > &gselect, Matrix< BaseFloat > *feat_out) const
Definition: fmpe.cc:370
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
int main(int argc, char *argv[])
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
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_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
bool HasKey(const std::string &key)
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_LOG
Definition: kaldi-error.h:153