nnet-am-widen.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-am-widen.cc
2 
3 // Copyright 2012-2013 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 "nnet2/widen-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  "possibly changing the binary mode\n"
36  "Also supports multiplying all the learning rates by a factor\n"
37  "(the --learning-rate-factor option) and setting them all to a given\n"
38  "value (the --learning-rate options)\n"
39  "\n"
40  "Usage: nnet-am-widen [options] <nnet-in> <nnet-out>\n"
41  "e.g.:\n"
42  " nnet-am-widen --hidden-layer-dim=1024 1.mdl 2.mdl\n";
43 
44  NnetWidenConfig config;
45  bool binary_write = true;
46 
47  ParseOptions po(usage);
48  config.Register(&po);
49 
50  po.Read(argc, argv);
51 
52  if (po.NumArgs() != 2) {
53  po.PrintUsage();
54  exit(1);
55  }
56 
57  std::string nnet_rxfilename = po.GetArg(1),
58  nnet_wxfilename = po.GetArg(2);
59 
60  TransitionModel trans_model;
61  AmNnet am_nnet;
62  {
63  bool binary;
64  Input ki(nnet_rxfilename, &binary);
65  trans_model.Read(ki.Stream(), binary);
66  am_nnet.Read(ki.Stream(), binary);
67  }
68 
69  WidenNnet(config, &(am_nnet.GetNnet()));
70 
71  {
72  Output ko(nnet_wxfilename, binary_write);
73  trans_model.Write(ko.Stream(), binary_write);
74  am_nnet.Write(ko.Stream(), binary_write);
75  }
76  KALDI_LOG << "Mixed up neural net from " << nnet_rxfilename
77  << " and wrote it to " << nnet_wxfilename;
78  return 0;
79  } catch(const std::exception &e) {
80  std::cerr << e.what() << '\n';
81  return -1;
82  }
83 }
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
int main(int argc, char *argv[])
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.
void Register(OptionsItf *opts)
Definition: widen-nnet.h:42
Configuration class that controls neural net "widening", which means increasing the dimension of the ...
Definition: widen-nnet.h:33
int NumArgs() const
Number of positional parameters (c.f. argc-1).
void Write(std::ostream &os, bool binary) const
#define KALDI_LOG
Definition: kaldi-error.h:153
const Nnet & GetNnet() const
Definition: am-nnet.h:61
void WidenNnet(const NnetWidenConfig &widen_config, Nnet *nnet)
This function widens a neural network by increasing the hidden-layer dimensions to the target...
Definition: widen-nnet.cc:62