nnet-am-reinitialize.cc File Reference
Include dependency graph for nnet-am-reinitialize.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 26 of file nnet-am-reinitialize.cc.

References ParseOptions::GetArg(), KALDI_LOG, ParseOptions::NumArgs(), AmNnet::NumPdfs(), TransitionModel::NumPdfs(), ParseOptions::PrintUsage(), AmNnet::Read(), ParseOptions::Read(), TransitionModel::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), AmNnet::ResizeOutputLayer(), Output::Stream(), Input::Stream(), AmNnet::Write(), and TransitionModel::Write().

26  {
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 Read(std::istream &is, bool binary)
Definition: am-nnet.cc:39
kaldi::int32 int32
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
void Read(std::istream &is, bool binary)
void Write(std::ostream &os, bool binary) const
Definition: am-nnet.cc:31
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