nnet-to-raw-nnet.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-to-raw-nnet.cc
2 
3 // Copyright 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 "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 
31  typedef kaldi::int32 int32;
32 
33  const char *usage =
34  "Copy a (cpu-based) neural net: reads the AmNnet with its transition model, but\n"
35  "writes just the Nnet with no transition model (i.e. the raw neural net.)\n"
36  "\n"
37  "Usage: nnet-to-raw-nnet [options] <nnet-in> <raw-nnet-out>\n"
38  "e.g.:\n"
39  " nnet-to-raw-nnet --binary=false 1.mdl 1.raw\n";
40 
41  int32 truncate = -1;
42  bool binary_write = true;
43 
44  ParseOptions po(usage);
45  po.Register("binary", &binary_write, "Write output in binary mode");
46  po.Register("truncate", &truncate, "If set, will truncate the neural net "
47  "to this many components by removing the last components.");
48 
49  po.Read(argc, argv);
50 
51  if (po.NumArgs() != 2) {
52  po.PrintUsage();
53  exit(1);
54  }
55 
56  std::string nnet_rxfilename = po.GetArg(1),
57  raw_nnet_wxfilename = po.GetArg(2);
58 
59  TransitionModel trans_model;
60  AmNnet am_nnet;
61  {
62  bool binary;
63  Input ki(nnet_rxfilename, &binary);
64  trans_model.Read(ki.Stream(), binary);
65  am_nnet.Read(ki.Stream(), binary);
66  }
67 
68  if (truncate >= 0) {
69  KALDI_LOG << "Truncating neural net to " << truncate << " layers.";
70  am_nnet.GetNnet().Resize(truncate);
71  }
72 
73  const Nnet &nnet = am_nnet.GetNnet();
74  WriteKaldiObject(nnet, raw_nnet_wxfilename, binary_write);
75 
76  KALDI_LOG << "Read neural net from " << nnet_rxfilename
77  << " and wrote raw neural net to " << raw_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
void Resize(int32 num_components)
Removes final components from the neural network (used for debugging).
Definition: nnet-nnet.cc:490
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
int main(int argc, char *argv[])
void Read(std::istream &is, bool binary)
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 WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:153
const Nnet & GetNnet() const
Definition: am-nnet.h:61