nnet-am-reinitialize.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-am-reinitialize.cc
2 
3 // Copyright 2014 Johns Hopkins University (author: Daniel Povey)
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 "nnet2/am-nnet.h"
23 #include "hmm/transition-model.h"
24 #include "tree/context-dep.h"
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  using namespace kaldi::nnet2;
30  typedef kaldi::int32 int32;
31 
32  const char *usage =
33  "This program can used when transferring a neural net from one language\n"
34  "to another (or one tree to another). It takes a neural net and a\n"
35  "transition model from a different neural net, resizes the last layer\n"
36  "to match the new transition model, zeroes it, and writes out the new,\n"
37  "resized .mdl file. If the original model had been 'mixed-up', the associated\n"
38  "SumGroupComponent will be removed.\n"
39  "\n"
40  "Usage: nnet-am-reinitialize [options] <nnet-in> <new-transition-model> <nnet-out>\n"
41  "e.g.:\n"
42  " nnet-am-reinitialize 1.mdl exp/tri6/final.mdl 2.mdl\n";
43 
44  bool binary_write = true;
45 
46  ParseOptions po(usage);
47  po.Register("binary", &binary_write, "Write output in binary mode");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 3) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56  std::string nnet_rxfilename = po.GetArg(1),
57  transition_model_rxfilename = po.GetArg(2),
58  nnet_wxfilename = po.GetArg(3);
59 
60  TransitionModel orig_trans_model;
61  AmNnet am_nnet;
62  {
63  bool binary;
64  Input ki(nnet_rxfilename, &binary);
65  orig_trans_model.Read(ki.Stream(), binary);
66  am_nnet.Read(ki.Stream(), binary);
67  }
68 
69  TransitionModel new_trans_model;
70  ReadKaldiObject(transition_model_rxfilename, &new_trans_model);
71 
72  am_nnet.ResizeOutputLayer(new_trans_model.NumPdfs());
73 
74  {
75  Output ko(nnet_wxfilename, binary_write);
76  new_trans_model.Write(ko.Stream(), binary_write);
77  am_nnet.Write(ko.Stream(), binary_write);
78  }
79  KALDI_LOG << "Resized neural net from " << nnet_rxfilename
80  << " to " << am_nnet.NumPdfs()
81  << " pdfs, and wrote to " << nnet_wxfilename;
82  return 0;
83  } catch(const std::exception &e) {
84  std::cerr << e.what() << '\n';
85  return -1;
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 Read(std::istream &is, bool binary)
Definition: am-nnet.cc:39
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
std::istream & Stream()
Definition: kaldi-io.cc:826
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 Read(std::istream &is, bool binary)
void Write(std::ostream &os, bool binary) const
Definition: am-nnet.cc:31
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).
void Write(std::ostream &os, bool binary) const
void ResizeOutputLayer(int32 new_num_pdfs)
This function is used when doing transfer learning to a new system.
Definition: am-nnet.cc:76
int32 NumPdfs() const
Definition: am-nnet.h:55
#define KALDI_LOG
Definition: kaldi-error.h:153
int main(int argc, char *argv[])