vector-scale.cc
Go to the documentation of this file.
1 // bin/vector-scale.cc
2 
3 // Copyright 2009-2011 Microsoft Corporation
4 // 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-matrix.h"
24 
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Scale vectors, or archives of vectors (useful for speaker vectors and "
32  "per-frame weights)\n"
33  "Usage: vector-scale [options] <vector-in-rspecifier> <vector-out-wspecifier>\n"
34  " or: vector-scale [options] <vector-in-rxfilename> <vector-out-wxfilename>\n"
35  " e.g.: vector-scale --scale=-1.0 1.vec -\n"
36  " vector-scale --scale=-2.0 ark:vec.ark ark,t:-\n"
37  "See also: copy-vector, vector-sum\n";
38 
39  ParseOptions po(usage);
40  BaseFloat scale = 1.0;
41  bool binary = false;
42 
43  po.Register("binary", &binary, "If true, write output as binary "
44  "not relevant for archives");
45  po.Register("scale", &scale, "Scaling factor for vectors");
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() != 2) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  std::string vector_in_fn = po.GetArg(1);
54  std::string vector_out_fn = po.GetArg(2);
55 
56  if (ClassifyWspecifier(vector_in_fn, NULL, NULL, NULL) != kNoWspecifier) {
57  if (ClassifyRspecifier(vector_in_fn, NULL, NULL) == kNoRspecifier) {
58  KALDI_ERR << "Cannot mix archives and regular files";
59  }
60  BaseFloatVectorWriter vec_writer(vector_out_fn);
61  SequentialBaseFloatVectorReader vec_reader(vector_in_fn);
62  for (; !vec_reader.Done(); vec_reader.Next()) {
63  Vector<BaseFloat> vec(vec_reader.Value());
64  vec.Scale(scale);
65  vec_writer.Write(vec_reader.Key(), vec);
66  }
67  } else {
68  if (ClassifyRspecifier(vector_in_fn, NULL, NULL) != kNoRspecifier) {
69  KALDI_ERR << "Cannot mix archives and regular files";
70  }
71  bool binary_in;
72  Input ki(vector_in_fn, &binary_in);
74  vec.Read(ki.Stream(), binary_in);
75  vec.Scale(scale);
76  Output ko(vector_out_fn, binary);
77  vec.Write(ko.Stream(), binary);
78  }
79 
80  return 0;
81  } catch(const std::exception &e) {
82  std::cerr << e.what();
83  return -1;
84  }
85 }
86 
87 
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
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
std::istream & Stream()
Definition: kaldi-io.cc:826
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
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.
void Scale(Real alpha)
Multiplies all elements by this constant.
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
int main(int argc, char *argv[])
Definition: vector-scale.cc:26
void Read(std::istream &in, bool binary, bool add=false)
Read function using C++ streams.