All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
copy-matrix.cc File Reference
Include dependency graph for copy-matrix.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 26 of file copy-matrix.cc.

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

26  {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Copy matrices, or archives of matrices (e.g. features or transforms)\n"
32  "Also see copy-feats which has other format options\n"
33  "\n"
34  "Usage: copy-matrix [options] <matrix-in-rspecifier> <matrix-out-wspecifier>\n"
35  " or: copy-matrix [options] <matrix-in-rxfilename> <matrix-out-wxfilename>\n"
36  " e.g.: copy-matrix --binary=false 1.mat -\n"
37  " copy-matrix ark:2.trans ark,t:-\n"
38  "See also: copy-feats\n";
39 
40  bool binary = true;
41  BaseFloat scale = 1.0;
42  ParseOptions po(usage);
43 
44  po.Register("binary", &binary,
45  "Write in binary mode (only relevant if output is a wxfilename)");
46  po.Register("scale", &scale,
47  "This option can be used to scale the matrices being copied.");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 2) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56 
57  std::string matrix_in_fn = po.GetArg(1),
58  matrix_out_fn = po.GetArg(2);
59 
60  // all these "fn"'s are either rspecifiers or filenames.
61 
62  bool in_is_rspecifier =
63  (ClassifyRspecifier(matrix_in_fn, NULL, NULL)
64  != kNoRspecifier),
65  out_is_wspecifier =
66  (ClassifyWspecifier(matrix_out_fn, NULL, NULL, NULL)
67  != kNoWspecifier);
68 
69  if (in_is_rspecifier != out_is_wspecifier)
70  KALDI_ERR << "Cannot mix archives with regular files (copying matrices)";
71 
72  if (!in_is_rspecifier) {
74  ReadKaldiObject(matrix_in_fn, &mat);
75  if (scale != 1.0) mat.Scale(scale);
76  Output ko(matrix_out_fn, binary);
77  mat.Write(ko.Stream(), binary);
78  KALDI_LOG << "Copied matrix to " << matrix_out_fn;
79  return 0;
80  } else {
81  int num_done = 0;
82  BaseFloatMatrixWriter writer(matrix_out_fn);
83  SequentialBaseFloatMatrixReader reader(matrix_in_fn);
84  for (; !reader.Done(); reader.Next(), num_done++) {
85  if (scale != 1.0) {
86  Matrix<BaseFloat> mat(reader.Value());
87  mat.Scale(scale);
88  writer.Write(reader.Key(), mat);
89  } else {
90  writer.Write(reader.Key(), reader.Value());
91  }
92  }
93  KALDI_LOG << "Copied " << num_done << " matrices.";
94  return (num_done != 0 ? 0 : 1);
95  }
96  } catch(const std::exception &e) {
97  std::cerr << e.what();
98  return -1;
99  }
100 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
float BaseFloat
Definition: kaldi-types.h:29
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void Scale(Real alpha)
Multiply each element with a scalar value.
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:127
void Write(std::ostream &out, bool binary) const
write to stream.
RspecifierType ClassifyRspecifier(const std::string &rspecifier, std::string *wxfilename, RspecifierOptions *opts)
Definition: kaldi-table.cc:225
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
#define KALDI_LOG
Definition: kaldi-error.h:133