nnet-am-fix.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-am-fix.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/nnet-fix.h"
23 #include "nnet2/am-nnet.h"
24 #include "hmm/transition-model.h"
25 #include "tree/context-dep.h"
26 
27 int main(int argc, char *argv[]) {
28  try {
29  using namespace kaldi;
30  using namespace kaldi::nnet2;
31  typedef kaldi::int32 int32;
32 
33  const char *usage =
34  "Copy a (cpu-based) neural net and its associated transition model,\n"
35  "but modify it to remove certain pathologies. We use the average\n"
36  "derivative statistics stored with the layers derived from\n"
37  "NonlinearComponent. Note: some processes, such as nnet-combine-fast,\n"
38  "may not process these statistics correctly, and you may have to recover\n"
39  "them using the --stats-from option of nnet-am-copy before you use.\n"
40  "this program.\n"
41  "\n"
42  "Usage: nnet-am-fix [options] <nnet-in> <nnet-out>\n"
43  "e.g.:\n"
44  " nnet-am-fix 1.mdl 1_fixed.mdl\n"
45  "or:\n"
46  " nnet-am-fix --get-counts-from=1.gradient 1.mdl 1_shrunk.mdl\n";
47 
48  bool binary_write = true;
49  NnetFixConfig config;
50 
51  ParseOptions po(usage);
52  po.Register("binary", &binary_write, "Write output in binary mode");
53  config.Register(&po);
54 
55  po.Read(argc, argv);
56 
57  if (po.NumArgs() != 2) {
58  po.PrintUsage();
59  exit(1);
60  }
61 
62  std::string nnet_rxfilename = po.GetArg(1),
63  nnet_wxfilename = po.GetArg(2);
64 
65  TransitionModel trans_model;
66  AmNnet am_nnet;
67  {
68  bool binary;
69  Input ki(nnet_rxfilename, &binary);
70  trans_model.Read(ki.Stream(), binary);
71  am_nnet.Read(ki.Stream(), binary);
72  }
73 
74  FixNnet(config, &am_nnet.GetNnet());
75 
76  {
77  Output ko(nnet_wxfilename, binary_write);
78  trans_model.Write(ko.Stream(), binary_write);
79  am_nnet.Write(ko.Stream(), binary_write);
80  }
81  KALDI_LOG << "Copied neural net from " << nnet_rxfilename
82  << " to " << nnet_wxfilename;
83  return 0;
84  } catch(const std::exception &e) {
85  std::cerr << e.what() << '\n';
86  return -1;
87  }
88 }
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 FixNnet(const NnetFixConfig &config, Nnet *nnet)
Definition: nnet-fix.cc:31
void Register(OptionsItf *opts)
Definition: nnet-fix.h:54
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
int main(int argc, char *argv[])
Definition: nnet-am-fix.cc:27
#define KALDI_LOG
Definition: kaldi-error.h:153
const Nnet & GetNnet() const
Definition: am-nnet.h:61