copy-vector.cc
Go to the documentation of this file.
1 // bin/copy-vector.cc
2 
3 // Copyright 2009-2012 Microsoft Corporation
4 // 2012-2014 Johns Hopkins University (Author: Daniel Povey)
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "matrix/kaldi-vector.h"
25 
26 
27 int main(int argc, char *argv[]) {
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 }
108 
109 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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:368
kaldi::int32 int32
void Resize(MatrixIndexT length, MatrixResizeType resize_type=kSetZero)
Set vector to a specified size (can be zero).
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
std::ostream & Stream()
Definition: kaldi-io.cc:701
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_ERR
Definition: kaldi-error.h:147
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
WspecifierType ClassifyWspecifier(const std::string &wspecifier, std::string *archive_wxfilename, std::string *script_wxfilename, WspecifierOptions *opts)
Definition: kaldi-table.cc:135
int NumArgs() const
Number of positional parameters (c.f. argc-1).
A class representing a vector.
Definition: kaldi-vector.h:406
#define KALDI_LOG
Definition: kaldi-error.h:153
int main(int argc, char *argv[])
Definition: copy-vector.cc:27