nnet-am-init.cc File Reference
Include dependency graph for nnet-am-init.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-init.cc.

References ParseOptions::GetArg(), AmNnet::Init(), KALDI_ERR, KALDI_LOG, ParseOptions::NumArgs(), AmNnet::NumPdfs(), TransitionModel::NumPdfs(), ParseOptions::PrintUsage(), ParseOptions::Read(), Nnet::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), 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  // TODO: specify in the usage message where the example scripts are.
33  const char *usage =
34  "Initialize the neural network acoustic model and its associated\n"
35  "transition-model, from a tree, a topology file, and a neural-net\n"
36  "without an associated acoustic model.\n"
37  "See example scripts to see how this works in practice.\n"
38  "\n"
39  "Usage: nnet-am-init [options] <tree-in> <topology-in> <raw-nnet-in> <nnet-am-out>\n"
40  "or: nnet-am-init [options] <transition-model-in> <raw-nnet-in> <nnet-am-out>\n"
41  "e.g.:\n"
42  " nnet-am-init tree topo \"nnet-init nnet.config - |\" 1.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 && po.NumArgs() != 4) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56  std::string raw_nnet_rxfilename, nnet_wxfilename;
57 
58  TransitionModel *trans_model = NULL;
59 
60  if (po.NumArgs() == 4) {
61  std::string tree_rxfilename = po.GetArg(1),
62  topo_rxfilename = po.GetArg(2);
63  raw_nnet_rxfilename = po.GetArg(3);
64  nnet_wxfilename = po.GetArg(4);
65 
66  ContextDependency ctx_dep;
67  ReadKaldiObject(tree_rxfilename, &ctx_dep);
68 
69  HmmTopology topo;
70  ReadKaldiObject(topo_rxfilename, &topo);
71 
72  // Construct the transition model from the tree and the topology file.
73  trans_model = new TransitionModel(ctx_dep, topo);
74  } else {
75  std::string trans_model_rxfilename = po.GetArg(1);
76  raw_nnet_rxfilename = po.GetArg(2);
77  nnet_wxfilename = po.GetArg(3);
78  trans_model = new TransitionModel();
79  ReadKaldiObject(trans_model_rxfilename, trans_model);
80  }
81 
82  AmNnet am_nnet;
83  {
84  Nnet nnet;
85  bool binary;
86  Input ki(raw_nnet_rxfilename, &binary);
87  nnet.Read(ki.Stream(), binary);
88  am_nnet.Init(nnet);
89  }
90 
91  if (am_nnet.NumPdfs() != trans_model->NumPdfs())
92  KALDI_ERR << "Mismatch in number of pdfs, neural net has "
93  << am_nnet.NumPdfs() << ", transition model has "
94  << trans_model->NumPdfs();
95 
96  {
97  Output ko(nnet_wxfilename, binary_write);
98  trans_model->Write(ko.Stream(), binary_write);
99  am_nnet.Write(ko.Stream(), binary_write);
100  }
101  delete trans_model;
102  KALDI_LOG << "Initialized neural net and wrote it to " << nnet_wxfilename;
103  return 0;
104  } catch(const std::exception &e) {
105  std::cerr << e.what() << '\n';
106  return -1;
107  }
108 }
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: nnet-nnet.cc:175
A class for storing topology information for phones.
Definition: hmm-topology.h:93
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 Init(std::istream &config_is)
Initialize the neural network based acoustic model from a config file.
Definition: am-nnet.cc:26
void Write(std::ostream &os, bool binary) const
Definition: am-nnet.cc:31
#define KALDI_ERR
Definition: kaldi-error.h:147
void Write(std::ostream &os, bool binary) const
int32 NumPdfs() const
Definition: am-nnet.h:55
#define KALDI_LOG
Definition: kaldi-error.h:153