nnet3-average.cc File Reference
Include dependency graph for nnet3-average.cc:

Go to the source code of this file.

Namespaces

 kaldi
 This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for mispronunciations detection tasks, the reference:
 

Functions

void GetWeights (const std::string &weights_str, int32 num_inputs, std::vector< BaseFloat > *weights)
 
void ReadModels (std::vector< std::pair< std::string, BaseFloat > > models_and_weights, nnet3::Nnet *output_nnet, int32 *success)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 79 of file nnet3-average.cc.

References kaldi::nnet3::AddNnet(), ParseOptions::GetArg(), kaldi::GetWeights(), rnnlm::j, KALDI_ERR, KALDI_LOG, ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), kaldi::ReadModels(), ParseOptions::Register(), and kaldi::WriteKaldiObject().

79  {
80  try {
81  using namespace kaldi;
82  using namespace kaldi::nnet3;
83  typedef kaldi::int32 int32;
84  typedef kaldi::int64 int64;
85 
86  const char *usage =
87  "This program averages the parameters over a number of 'raw' nnet3 neural nets.\n"
88  "\n"
89  "Usage: nnet3-average [options] <model1> <model2> ... <modelN> <model-out>\n"
90  "\n"
91  "e.g.:\n"
92  " nnet3-average 1.1.nnet 1.2.nnet 1.3.nnet 2.nnet\n";
93 
94  bool binary_write = true;
95  int32 num_threads = -1;
96 
97  ParseOptions po(usage);
98  po.Register("binary", &binary_write, "Write output in binary mode");
99  std::string weights_str;
100  po.Register("weights", &weights_str, "Colon-separated list of weights, one "
101  "for each input model. These will be normalized to sum to one.");
102  po.Register("num-threads", &num_threads, "Number of threads to read the "
103  "models (will be set automatically if not set.");
104 
105  po.Read(argc, argv);
106 
107  if (po.NumArgs() < 2) {
108  po.PrintUsage();
109  exit(1);
110  }
111 
112  std::string
113  first_nnet_rxfilename = po.GetArg(1),
114  nnet_wxfilename = po.GetArg(po.NumArgs());
115 
116  int32 num_inputs = po.NumArgs() - 1;
117 
118  if (num_threads <= 0) {
119  // Default logic for selecting the number of threads.
120  if (num_inputs > 10) num_threads = 3;
121  else if (num_inputs > 5) num_threads = 2;
122  else num_threads = 1;
123  }
124 
125  if (num_threads > 1 && num_threads * 2 > num_inputs) {
126  num_threads = num_inputs / 2;
127  }
128 
129  std::vector<BaseFloat> model_weights;
130  GetWeights(weights_str, num_inputs, &model_weights);
131 
132  std::vector<Nnet> nnets(num_threads);
133  std::vector<int32> return_statuses(num_threads);
134 
135  std::vector<std::thread*> threads(num_threads);
136 
137  for (int32 thread_id = 0; thread_id < num_threads; thread_id++) {
138  std::vector<std::pair<std::string, BaseFloat> > this_models_and_weights;
139  for (int32 j = 1 + thread_id; j < po.NumArgs(); j += num_threads) {
140  this_models_and_weights.push_back(std::pair<std::string, BaseFloat>(
141  po.GetArg(j), model_weights[j - 1]));
142  }
143  threads[thread_id] = new std::thread(ReadModels, this_models_and_weights,
144  &(nnets[thread_id]),
145  &(return_statuses[thread_id]));
146  }
147 
148  bool success = true;
149  for (int32 thread_id = 0; thread_id < num_threads; thread_id++) {
150  threads[thread_id]->join();
151  delete threads[thread_id];
152  if (!return_statuses[thread_id])
153  success = false;
154  if (success && thread_id > 0)
155  AddNnet(nnets[thread_id], 1.0, &(nnets[0]));
156  }
157 
158  if (!success) {
159  KALDI_ERR << "Error detected in a model-reading thread.";
160  }
161 
162  WriteKaldiObject(nnets[0], nnet_wxfilename, binary_write);
163 
164  KALDI_LOG << "Averaged parameters of " << num_inputs
165  << " neural nets, and wrote to " << nnet_wxfilename;
166  return 0; // it will throw an exception if there are any problems.
167  } catch(const std::exception &e) {
168  std::cerr << e.what() << '\n';
169  return -1;
170  }
171 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void GetWeights(const std::string &weights_str, int32 num_inputs, std::vector< BaseFloat > *weights)
kaldi::int32 int32
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
#define KALDI_ERR
Definition: kaldi-error.h:147
void ReadModels(std::vector< std::pair< std::string, BaseFloat > > models_and_weights, nnet3::Nnet *output_nnet, int32 *success)
void WriteKaldiObject(const C &c, const std::string &filename, bool binary)
Definition: kaldi-io.h:257
#define KALDI_LOG
Definition: kaldi-error.h:153
void AddNnet(const Nnet &src, BaseFloat alpha, Nnet *dest)
Does *dest += alpha * src (affects nnet parameters and stored stats).
Definition: nnet-utils.cc:349