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.

Namespaces

 kaldi
 Relabels neural network egs with the read pdf-id alignments.
 

Functions

void ApplySoftMaxPerRow (MatrixBase< BaseFloat > *mat)
 
int main (int argc, char *argv[])
 

Function Documentation

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

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

References MatrixBase< Real >::ApplyExp(), MatrixBase< Real >::ApplyFloor(), MatrixBase< Real >::ApplyLog(), MatrixBase< Real >::ApplyPow(), kaldi::ApplySoftMaxPerRow(), 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().

35  {
36  try {
37  using namespace kaldi;
38 
39  const char *usage =
40  "Copy matrices, or archives of matrices (e.g. features or transforms)\n"
41  "Also see copy-feats which has other format options\n"
42  "\n"
43  "Usage: copy-matrix [options] <matrix-in-rspecifier> <matrix-out-wspecifier>\n"
44  " or: copy-matrix [options] <matrix-in-rxfilename> <matrix-out-wxfilename>\n"
45  " e.g.: copy-matrix --binary=false 1.mat -\n"
46  " copy-matrix ark:2.trans ark,t:-\n"
47  "See also: copy-feats\n";
48 
49  bool binary = true;
50  bool apply_log = false;
51  bool apply_exp = false;
52  bool apply_softmax_per_row = false;
53  BaseFloat apply_power = 1.0;
54  BaseFloat scale = 1.0;
55 
56  ParseOptions po(usage);
57 
58  po.Register("binary", &binary,
59  "Write in binary mode (only relevant if output is a wxfilename)");
60  po.Register("scale", &scale,
61  "This option can be used to scale the matrices being copied.");
62  po.Register("apply-log", &apply_log,
63  "This option can be used to apply log on the matrices. "
64  "Must be avoided if matrix has negative quantities.");
65  po.Register("apply-exp", &apply_exp,
66  "This option can be used to apply exp on the matrices");
67  po.Register("apply-power", &apply_power,
68  "This option can be used to apply a power on the matrices");
69  po.Register("apply-softmax-per-row", &apply_softmax_per_row,
70  "This option can be used to apply softmax per row of the matrices");
71 
72  po.Read(argc, argv);
73 
74  if (po.NumArgs() != 2) {
75  po.PrintUsage();
76  exit(1);
77  }
78 
79  if ( (apply_log && apply_exp) || (apply_softmax_per_row && apply_exp) ||
80  (apply_softmax_per_row && apply_log) )
81  KALDI_ERR << "Only one of apply-log, apply-exp and "
82  << "apply-softmax-per-row can be given";
83 
84  std::string matrix_in_fn = po.GetArg(1),
85  matrix_out_fn = po.GetArg(2);
86 
87  // all these "fn"'s are either rspecifiers or filenames.
88 
89  bool in_is_rspecifier =
90  (ClassifyRspecifier(matrix_in_fn, NULL, NULL)
91  != kNoRspecifier),
92  out_is_wspecifier =
93  (ClassifyWspecifier(matrix_out_fn, NULL, NULL, NULL)
94  != kNoWspecifier);
95 
96  if (in_is_rspecifier != out_is_wspecifier)
97  KALDI_ERR << "Cannot mix archives with regular files (copying matrices)";
98 
99  if (!in_is_rspecifier) {
100  Matrix<BaseFloat> mat;
101  ReadKaldiObject(matrix_in_fn, &mat);
102  if (scale != 1.0) mat.Scale(scale);
103  if (apply_log) {
104  mat.ApplyFloor(1.0e-20);
105  mat.ApplyLog();
106  }
107  if (apply_exp) mat.ApplyExp();
108  if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat);
109  if (apply_power != 1.0) mat.ApplyPow(apply_power);
110  Output ko(matrix_out_fn, binary);
111  mat.Write(ko.Stream(), binary);
112  KALDI_LOG << "Copied matrix to " << matrix_out_fn;
113  return 0;
114  } else {
115  int num_done = 0;
116  BaseFloatMatrixWriter writer(matrix_out_fn);
117  SequentialBaseFloatMatrixReader reader(matrix_in_fn);
118  for (; !reader.Done(); reader.Next(), num_done++) {
119  if (scale != 1.0 || apply_log || apply_exp ||
120  apply_power != 1.0 || apply_softmax_per_row) {
121  Matrix<BaseFloat> mat(reader.Value());
122  if (scale != 1.0) mat.Scale(scale);
123  if (apply_log) {
124  mat.ApplyFloor(1.0e-20);
125  mat.ApplyLog();
126  }
127  if (apply_exp) mat.ApplyExp();
128  if (apply_softmax_per_row) ApplySoftMaxPerRow(&mat);
129  if (apply_power != 1.0) mat.ApplyPow(apply_power);
130  writer.Write(reader.Key(), mat);
131  } else {
132  writer.Write(reader.Key(), reader.Value());
133  }
134  }
135  KALDI_LOG << "Copied " << num_done << " matrices.";
136  return (num_done != 0 ? 0 : 1);
137  }
138  } catch(const std::exception &e) {
139  std::cerr << e.what();
140  return -1;
141  }
142 }
void ApplyLog()
Calculates log of all the matrix elemnts.
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
void ApplySoftMaxPerRow(MatrixBase< BaseFloat > *mat)
Definition: copy-matrix.cc:27
#define KALDI_ERR
Definition: kaldi-error.h:127
void Write(std::ostream &out, bool binary) const
write to stream.
void ApplyExp()
Exponentiate each of the elements.
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
void ApplyFloor(Real floor_val)
Applies floor to all matrix elements.
void ApplyPow(Real power)
Applies power to all matrix elements.
#define KALDI_LOG
Definition: kaldi-error.h:133