nnet-replace-last-layers.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-replace-last-layers.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/am-nnet.h"
23 #include "nnet2/nnet-functions.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  "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 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[])
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
int32 NumComponents() const
Returns number of components– think of this as similar to # of layers, but e.g.
Definition: nnet-nnet.h:69
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.
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".
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
#define KALDI_LOG
Definition: kaldi-error.h:153
const Nnet & GetNnet() const
Definition: am-nnet.h:61