All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
copy-vector.cc File Reference
Include dependency graph for copy-vector.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 27 of file copy-vector.cc.

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

27  {
28  try {
29  using namespace kaldi;
30 
31  const char *usage =
32  "Copy vectors, or archives of vectors (e.g. transition-accs; speaker vectors)\n"
33  "\n"
34  "Usage: copy-vector [options] (<vector-in-rspecifier>|<vector-in-rxfilename>) (<vector-out-wspecifier>|<vector-out-wxfilename>)\n"
35  " e.g.: copy-vector --binary=false 1.mat -\n"
36  " copy-vector ark:2.trans ark,t:-\n"
37  "see also: dot-weights, append-vector-to-feats\n";
38 
39  bool binary = true;
40  int32 change_dim = -1;
41  float scale = 1.0;
42  ParseOptions po(usage);
43 
44  po.Register("binary", &binary, "Write in binary mode (only "
45  "relevant if output is a wxfilename)");
46  po.Register("change_dim", &change_dim,
47  "Use this option to truncate or zero-pad the vectors.");
48  po.Register("scale", &scale,
49  "This option can be used to scale the vectors being copied.");
50 
51  po.Read(argc, argv);
52 
53  if (po.NumArgs() != 2) {
54  po.PrintUsage();
55  exit(1);
56  }
57 
58 
59  std::string vector_in_fn = po.GetArg(1),
60  vector_out_fn = po.GetArg(2);
61 
62  // all these "fn"'s are either rspecifiers or filenames.
63 
64  bool in_is_rspecifier =
65  (ClassifyRspecifier(vector_in_fn, NULL, NULL)
66  != kNoRspecifier),
67  out_is_wspecifier =
68  (ClassifyWspecifier(vector_out_fn, NULL, NULL, NULL)
69  != kNoWspecifier);
70 
71  if (in_is_rspecifier != out_is_wspecifier)
72  KALDI_ERR << "Cannot mix archives with regular files (copying vectors)";
73 
74  if (!in_is_rspecifier) {
76  ReadKaldiObject(vector_in_fn, &vec);
77  Output ko(vector_out_fn, binary);
78  if (change_dim >= 0) vec.Resize(change_dim, kCopyData);
79  vec.Write(ko.Stream(), binary);
80  KALDI_LOG << "Copied vector to " << vector_out_fn;
81  return 0;
82  } else {
83  int num_done = 0;
84  BaseFloatVectorWriter writer(vector_out_fn);
85  SequentialBaseFloatVectorReader reader(vector_in_fn);
86  if (change_dim < 0 && scale == 1.0) {
87  for (; !reader.Done(); reader.Next(), num_done++) {
88  writer.Write(reader.Key(), reader.Value());
89  }
90  KALDI_LOG << "Copied " << num_done << " vectors.";
91  } else {
92  for (; !reader.Done(); reader.Next(), num_done++) {
93  Vector<BaseFloat> vec(reader.Value());
94  if (change_dim >= 0) vec.Resize(change_dim, kCopyData);
95  if (scale != 1.0) vec.Scale(scale);
96  writer.Write(reader.Key(), reader.Value());
97  }
98  KALDI_LOG << "Copied " << num_done << " vectors, setting dim to "
99  << change_dim << " scaled by " << scale;
100  }
101  return (num_done != 0 ? 0 : 1);
102  }
103  } catch(const std::exception &e) {
104  std::cerr << e.what();
105  return -1;
106  }
107 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
void Write(std::ostream &Out, bool binary) const
Writes to C++ stream (option to write in binary).
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:818
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:127
void Scale(Real alpha)
Multiplies all elements by this constant.
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