nnet3-compute-prob.cc
Go to the documentation of this file.
1 // nnet3bin/nnet3-compute-prob.cc
2 
3 // Copyright 2015 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 "nnet3/nnet-diagnostics.h"
23 #include "nnet3/nnet-utils.h"
24 
25 
26 int main(int argc, char *argv[]) {
27  try {
28  using namespace kaldi;
29  using namespace kaldi::nnet3;
30  typedef kaldi::int32 int32;
31  typedef kaldi::int64 int64;
32 
33  const char *usage =
34  "Computes and prints in logging messages the average log-prob per frame of\n"
35  "the given data with an nnet3 neural net. The input of this is the output of\n"
36  "e.g. nnet3-get-egs | nnet3-merge-egs.\n"
37  "\n"
38  "Usage: nnet3-compute-prob [options] <raw-model-in> <training-examples-in>\n"
39  "e.g.: nnet3-compute-prob 0.raw ark:valid.egs\n";
40 
41 
42  bool batchnorm_test_mode = true, dropout_test_mode = true,
43  collapse_model = true;
44 
45  // This program doesn't support using a GPU, because these probabilities are
46  // used for diagnostics, and you can just compute them with a small enough
47  // amount of data that a CPU can do it within reasonable time.
48 
50 
51  ParseOptions po(usage);
52 
53  po.Register("batchnorm-test-mode", &batchnorm_test_mode,
54  "If true, set test-mode to true on any BatchNormComponents.");
55  po.Register("dropout-test-mode", &dropout_test_mode,
56  "If true, set test-mode to true on any DropoutComponents and "
57  "DropoutMaskComponents.");
58  po.Register("collapse-model", &collapse_model,
59  "If true, collapse model to the extent possible before "
60  "using it (for efficiency).");
61 
62  opts.Register(&po);
63 
64  po.Read(argc, argv);
65 
66  if (po.NumArgs() != 2) {
67  po.PrintUsage();
68  exit(1);
69  }
70 
71  std::string raw_nnet_rxfilename = po.GetArg(1),
72  examples_rspecifier = po.GetArg(2);
73 
74  Nnet nnet;
75  ReadKaldiObject(raw_nnet_rxfilename, &nnet);
76 
77  if (batchnorm_test_mode)
78  SetBatchnormTestMode(true, &nnet);
79 
80  if (dropout_test_mode)
81  SetDropoutTestMode(true, &nnet);
82 
83  if (collapse_model)
85 
86  NnetComputeProb prob_computer(opts, nnet);
87 
88  SequentialNnetExampleReader example_reader(examples_rspecifier);
89 
90  for (; !example_reader.Done(); example_reader.Next())
91  prob_computer.Compute(example_reader.Value());
92 
93  bool ok = prob_computer.PrintTotalStats();
94 
95  return (ok ? 0 : 1);
96  } catch(const std::exception &e) {
97  std::cerr << e.what() << '\n';
98  return -1;
99  }
100 }
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void CollapseModel(const CollapseModelConfig &config, Nnet *nnet)
This function modifies the neural net for efficiency, in a way that suitable to be done in test time...
Definition: nnet-utils.cc:2100
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
void Compute(const NnetExample &eg)
void SetBatchnormTestMode(bool test_mode, Nnet *nnet)
This function affects only components of type BatchNormComponent.
Definition: nnet-utils.cc:564
kaldi::int32 int32
This class is for computing cross-entropy and accuracy values in a neural network, for diagnostics.
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
This file contains some miscellaneous functions dealing with class Nnet.
void SetDropoutTestMode(bool test_mode, Nnet *nnet)
This function affects components of child-classes of RandomComponent.
Definition: nnet-utils.cc:573
The class ParseOptions is for parsing command-line options; see Parsing command-line options for more...
Definition: parse-options.h:36
A templated class for reading objects sequentially from an archive or script file; see The Table conc...
Definition: kaldi-table.h:287
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 main(int argc, char *argv[])
int NumArgs() const
Number of positional parameters (c.f. argc-1).
Config class for the CollapseModel function.
Definition: nnet-utils.h:240