nnet-am-init.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-am-init.cc
2 
3 // Copyright 2012 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  // 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 }
109 
110 
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
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 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
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.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void Write(std::ostream &os, bool binary) const
int32 NumPdfs() const
Definition: am-nnet.h:55
#define KALDI_LOG
Definition: kaldi-error.h:153
int main(int argc, char *argv[])
Definition: nnet-am-init.cc:26