extend-transform-dim.cc File Reference
Include dependency graph for extend-transform-dim.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

void IncreaseTransformDimension (int32 new_dimension, Matrix< BaseFloat > *mat)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 54 of file extend-transform-dim.cc.

References kaldi::ClassifyRspecifier(), kaldi::ClassifyWspecifier(), SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), kaldi::IncreaseTransformDimension(), KALDI_ERR, KALDI_LOG, SequentialTableReader< Holder >::Key(), kaldi::kNoRspecifier, kaldi::kNoWspecifier, SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), MatrixBase< Real >::NumRows(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), SequentialTableReader< Holder >::Value(), TableWriter< Holder >::Write(), and kaldi::WriteKaldiObject().

54  {
55  try {
56  using namespace kaldi;
57 
58  const char *usage =
59  "Read in transform from dimension d -> d (affine or linear), and output a transform\n"
60  "from dimension e -> e (with e >= d, and e controlled by option --new-dimension).\n"
61  "This new transform will leave the extra dimension unaffected, and transform the old\n"
62  "dimensions in the same way.\n"
63  "Usage: extend-transform-dim [options] (transform-A-rspecifier|transform-A-rxfilename) (transform-out-wspecifier|transform-out-wxfilename)\n"
64  "E.g.: extend-transform-dim --new-dimension=117 in.mat big.mat\n";
65 
66  bool binary = true;
67  int32 new_dimension = -1;
68  ParseOptions po(usage);
69 
70  po.Register("binary", &binary, "Write in binary mode (only relevant if output is a wxfilename)");
71  po.Register("new-dimension", &new_dimension,
72  "Larger dimension we are changing matrix to");
73 
74  po.Read(argc, argv);
75 
76  if (po.NumArgs() != 2) {
77  po.PrintUsage();
78  exit(1);
79  }
80 
81  std::string transform_in_fn = po.GetArg(1);
82  std::string transform_out_fn = po.GetArg(2);
83  // all these "fn"'s are either rspecifiers or filenames.
84 
85  bool in_is_rspecifier =
86  (ClassifyRspecifier(transform_in_fn, NULL, NULL)
87  != kNoRspecifier),
88  out_is_wspecifier =
89  (ClassifyWspecifier(transform_out_fn, NULL, NULL, NULL)
90  != kNoWspecifier);
91 
92  if (in_is_rspecifier != out_is_wspecifier)
93  KALDI_ERR << "Either none or both of the (input, output) must be a Table.";
94 
95  if (in_is_rspecifier) {
96  SequentialBaseFloatMatrixReader reader(transform_in_fn);
97  BaseFloatMatrixWriter writer(transform_out_fn);
98  int32 num_done = 0;
99  for (; !reader.Done(); reader.Next()) {
100  std::string key = reader.Key();
101  Matrix<BaseFloat> mat(reader.Value());
102  IncreaseTransformDimension(new_dimension, &mat);
103  writer.Write(key, mat);
104  num_done++;
105  }
106  KALDI_LOG << "Increased transform dim to " << new_dimension
107  << " for " << num_done << " matrices.";
108  return (num_done != 0 ? 0 : 1);
109  } else {
110  Matrix<BaseFloat> mat;
111  ReadKaldiObject(transform_in_fn, &mat);
112  int32 old_dim = mat.NumRows();
113  IncreaseTransformDimension(new_dimension, &mat);
114  WriteKaldiObject(mat, transform_out_fn, binary);
115  KALDI_LOG << "Increased transform dim from " << old_dim << " to "
116  << mat.NumRows() << " and wrote to " << transform_out_fn;
117  return 0;
118  }
119  } catch(const std::exception &e) {
120  std::cerr << e.what();
121  return -1;
122  }
123 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
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
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
#define KALDI_ERR
Definition: kaldi-error.h:147
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
void IncreaseTransformDimension(int32 new_dimension, Matrix< BaseFloat > *mat)
#define KALDI_LOG
Definition: kaldi-error.h:153