nnet3-am-init.cc
Go to the documentation of this file.
1 // nnet3bin/nnet3-am-init.cc
2 
3 // Copyright 2012-2015 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 "hmm/transition-model.h"
23 #include "tree/context-dep.h"
24 #include "nnet3/am-nnet-simple.h"
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  using namespace kaldi::nnet3;
30  typedef kaldi::int32 int32;
31 
32  const char *usage =
33  "Initialize nnet3 am-nnet (i.e. neural network-based acoustic model, with\n"
34  "associated transition model) from an existing transition model and nnet..\n"
35  "Search for examples in scripts in /egs/wsj/s5/steps/nnet3/\n"
36  "Set priors using nnet3-am-train-transitions or nnet3-am-adjust-priors\n"
37  "\n"
38  "Usage: nnet3-am-init [options] <tree-in> <topology-in> <input-raw-nnet> <output-am-nnet>\n"
39  " or: nnet3-am-init [options] <trans-model-in> <input-raw-nnet> <output-am-nnet>\n"
40  "e.g.:\n"
41  " nnet3-am-init tree topo 0.raw 0.mdl\n"
42  "See also: nnet3-init, nnet3-am-copy, nnet3-am-info, nnet3-am-train-transitions,\n"
43  " nnet3-am-adjust-priors\n";
44 
45  bool binary_write = true;
46 
47  ParseOptions po(usage);
48  po.Register("binary", &binary_write, "Write output in binary mode");
49 
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() < 3 || po.NumArgs() > 4) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string raw_nnet_rxfilename,
58  am_nnet_wxfilename;
59  TransitionModel *trans_model = NULL;
60 
61  if (po.NumArgs() == 4) {
62  std::string tree_rxfilename = po.GetArg(1),
63  topo_rxfilename = po.GetArg(2);
64  raw_nnet_rxfilename = po.GetArg(3);
65  am_nnet_wxfilename = po.GetArg(4);
66 
67  ContextDependency ctx_dep;
68  ReadKaldiObject(tree_rxfilename, &ctx_dep);
69 
70  HmmTopology topo;
71  ReadKaldiObject(topo_rxfilename, &topo);
72 
73  // Construct the transition model from the tree and the topology file.
74  trans_model = new TransitionModel(ctx_dep, topo);
75  } else {
76  std::string trans_model_rxfilename = po.GetArg(1);
77  raw_nnet_rxfilename = po.GetArg(2);
78  am_nnet_wxfilename = po.GetArg(3);
79 
80  trans_model = new TransitionModel();
81  ReadKaldiObject(trans_model_rxfilename, trans_model);
82  }
83 
84  Nnet nnet;
85  ReadKaldiObject(raw_nnet_rxfilename, &nnet);
86 
87  // priors won't be set yet.
88  AmNnetSimple am_nnet(nnet);
89 
90  {
91  Output ko(am_nnet_wxfilename, binary_write);
92  trans_model->Write(ko.Stream(), binary_write);
93  am_nnet.Write(ko.Stream(), binary_write);
94  }
95  delete trans_model;
96  KALDI_LOG << "Initialized am-nnet (neural net acoustic model) and wrote to "
97  << am_nnet_wxfilename;
98  return 0;
99  } catch(const std::exception &e) {
100  std::cerr << e.what() << '\n';
101  return -1;
102  }
103 }
104 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
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
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
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
int main(int argc, char *argv[])
#define KALDI_LOG
Definition: kaldi-error.h:153
void Write(std::ostream &os, bool binary) const