nnet-replace-last-layers.cc File Reference
Include dependency graph for nnet-replace-last-layers.cc:

Go to the source code of this file.

Functions

int main (int argc, char *argv[])
 

Function Documentation

◆ main()

int main ( int  argc,
char *  argv[] 
)

Definition at line 27 of file nnet-replace-last-layers.cc.

References ParseOptions::GetArg(), AmNnet::GetNnet(), KALDI_LOG, ParseOptions::NumArgs(), Nnet::NumComponents(), ParseOptions::PrintUsage(), AmNnet::Read(), ParseOptions::Read(), TransitionModel::Read(), kaldi::ReadKaldiObject(), ParseOptions::Register(), kaldi::nnet2::ReplaceLastComponents(), Output::Stream(), Input::Stream(), AmNnet::Write(), and TransitionModel::Write().

27  {
28  try {
29  using namespace kaldi;
30  using namespace kaldi::nnet2;
31  typedef kaldi::int32 int32;
32 
33  const char *usage =
34  "This program is for adding new layers to a neural-network acoustic model.\n"
35  "It removes the last --remove-layers layers, and adds the layers from the\n"
36  "supplied raw-nnet. The typical use is to remove the last two layers\n"
37  "(the softmax, and the affine component before it), and add in replacements\n"
38  "for them newly initialized by nnet-init. This program is a more flexible\n"
39  "way of adding layers than nnet-insert, but the inserted network needs to\n"
40  "contain replacements for the removed layers.\n"
41  "\n"
42  "Usage: nnet-replace-last-layers [options] <nnet-in> <raw-nnet-to-insert-in> <nnet-out>\n"
43  "e.g.:\n"
44  " nnet-replace-last-layers 1.nnet \"nnet-init hidden_layer.config -|\" 2.nnet\n";
45 
46  bool binary_write = true;
47  int32 remove_layers = 2;
48 
49  ParseOptions po(usage);
50 
51  po.Register("binary", &binary_write, "Write output in binary mode");
52  po.Register("remove-layers", &remove_layers, "Number of final layers "
53  "to remove before adding input raw network.");
54 
55  po.Read(argc, argv);
56 
57  if (po.NumArgs() != 3) {
58  po.PrintUsage();
59  exit(1);
60  }
61 
62  std::string nnet_rxfilename = po.GetArg(1),
63  raw_nnet_rxfilename = po.GetArg(2),
64  nnet_wxfilename = po.GetArg(3);
65 
66  TransitionModel trans_model;
67  AmNnet am_nnet;
68  {
69  bool binary;
70  Input ki(nnet_rxfilename, &binary);
71  trans_model.Read(ki.Stream(), binary);
72  am_nnet.Read(ki.Stream(), binary);
73  }
74 
75  Nnet src_nnet; // the one we'll insert.
76  ReadKaldiObject(raw_nnet_rxfilename, &src_nnet);
77 
78 
79  // This function is declared in nnet-functions.h
80  ReplaceLastComponents(src_nnet,
81  remove_layers,
82  &(am_nnet.GetNnet()));
83  KALDI_LOG << "Removed " << remove_layers << " components and added "
84  << src_nnet.NumComponents();
85 
86  {
87  Output ko(nnet_wxfilename, binary_write);
88  trans_model.Write(ko.Stream(), binary_write);
89  am_nnet.Write(ko.Stream(), binary_write);
90  }
91  KALDI_LOG << "Write neural-net acoustic model to " << nnet_wxfilename;
92  return 0;
93  } catch(const std::exception &e) {
94  std::cerr << e.what() << '\n';
95  return -1;
96  }
97 }
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: am-nnet.cc:39
kaldi::int32 int32
void ReadKaldiObject(const std::string &filename, Matrix< float > *m)
Definition: kaldi-io.cc:832
int32 NumComponents() const
Returns number of components– think of this as similar to # of layers, but e.g.
Definition: nnet-nnet.h:69
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
void Read(std::istream &is, bool binary)
void Write(std::ostream &os, bool binary) const
Definition: am-nnet.cc:31
void ReplaceLastComponents(const Nnet &src_nnet, int32 num_to_remove, Nnet *dest_nnet)
Removes the last "num_to_remove" components and adds the components from "src_nnet".
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