transf-to-nnet.cc
Go to the documentation of this file.
1 // nnetbin/transf-to-nnet.cc
2 
3 // Copyright 2012 Brno University of Technology
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "nnet/nnet-nnet.h"
24 
25 int main(int argc, char *argv[]) {
26  try {
27  using namespace kaldi;
28  using namespace kaldi::nnet1;
29  typedef kaldi::int32 int32;
30 
31  const char *usage =
32  "Convert transformation matrix to <affine-transform>\n"
33  "Usage: transf-to-nnet [options] <transf-in> <nnet-out>\n"
34  "e.g.:\n"
35  " transf-to-nnet --binary=false transf.mat nnet.mdl\n";
36 
37  bool binary_write = false;
38 
39  ParseOptions po(usage);
40  po.Register("binary", &binary_write, "Write output in binary mode");
41 
42  po.Read(argc, argv);
43 
44  if (po.NumArgs() != 2) {
45  po.PrintUsage();
46  exit(1);
47  }
48 
49  std::string transform_rxfilename = po.GetArg(1),
50  model_out_filename = po.GetArg(2);
51 
52  // read the matrix,
53  Matrix<BaseFloat> transform;
54  {
55  bool binary_read;
56  Input ki(transform_rxfilename, &binary_read);
57  transform.Read(ki.Stream(), binary_read);
58  }
59 
60  // wrapping as Nnet with <LinearTransform>,
61  Nnet nnet;
62  LinearTransform lin_tran(transform.NumCols(), transform.NumRows());
63  lin_tran.SetLinearity(transform);
64  nnet.AppendComponent(lin_tran);
65 
66  // write the nnet,
67  {
68  Output ko(model_out_filename, binary_write);
69  nnet.Write(ko.Stream(), binary_write);
70  KALDI_LOG << "Written transform in 'nnet1' model: " << model_out_filename;
71  }
72  return 0;
73  } catch(const std::exception &e) {
74  std::cerr << e.what();
75  return -1;
76  }
77 }
78 
79 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
int main(int argc, char *argv[])
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Write(const std::string &wxfilename, bool binary) const
Write Nnet to &#39;wxfilename&#39;,.
Definition: nnet-nnet.cc:367
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
std::istream & Stream()
Definition: kaldi-io.cc:826
void Read(std::istream &in, bool binary, bool add=false)
read from stream.
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
void SetLinearity(const MatrixBase< BaseFloat > &l)
int Read(int argc, const char *const *argv)
Parses the command line options and fills the ParseOptions-registered variables.
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
MatrixIndexT NumRows() const
Returns number of rows (or zero for empty matrix).
Definition: kaldi-matrix.h:64
#define KALDI_LOG
Definition: kaldi-error.h:153
void AppendComponent(const Component &comp)
Append Component to &#39;this&#39; instance of Nnet (deep copy),.
Definition: nnet-nnet.cc:182