nnet-copy-egs-discriminative.cc
Go to the documentation of this file.
1 // nnet2bin/nnet-copy-egs-discriminative.cc
2 
3 // Copyright 2012-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 "hmm/transition-model.h"
24 
25 namespace kaldi {
26 namespace nnet2 {
27 // returns an integer randomly drawn with expected value "expected_count"
28 // (will be either floor(expected_count) or ceil(expected_count)).
29 // this will go into an infinite loop if expected_count is very huge, but
30 // it should never be that huge.
31 int32 GetCount(double expected_count) {
32  KALDI_ASSERT(expected_count >= 0.0);
33  int32 ans = 0;
34  while (expected_count > 1.0) {
35  ans++;
36  expected_count--;
37  }
38  if (WithProb(expected_count))
39  ans++;
40  return ans;
41 }
42 void AverageConstPart(int32 const_feat_dim,
44  if (eg->spk_info.Dim() != 0) { // already has const part.
45  KALDI_ASSERT(eg->spk_info.Dim() == const_feat_dim);
46  // and nothing to do.
47  } else {
48  int32 dim = eg->input_frames.NumCols(),
49  basic_dim = dim - const_feat_dim;
50  KALDI_ASSERT(const_feat_dim < eg->input_frames.NumCols());
51  Matrix<BaseFloat> mat(eg->input_frames); // copy to non-compressed matrix.
52  eg->input_frames = mat.Range(0, mat.NumRows(), 0, basic_dim);
53  eg->spk_info.Resize(const_feat_dim);
54  eg->spk_info.AddRowSumMat(1.0 / mat.NumRows(),
55  mat.Range(0, mat.NumRows(),
56  basic_dim, const_feat_dim),
57  0.0);
58  }
59 }
60 
61 
62 } // namespace nnet2
63 } // namespace kaldi
64 
65 int main(int argc, char *argv[]) {
66  try {
67  using namespace kaldi;
68  using namespace kaldi::nnet2;
69  typedef kaldi::int32 int32;
70  typedef kaldi::int64 int64;
71 
72  const char *usage =
73  "Copy examples for discriminative neural\n"
74  "network training. Supports multiple wspecifiers, in\n"
75  "which case it will write the examples round-robin to the outputs.\n"
76  "\n"
77  "Usage: nnet-copy-egs-discriminative [options] <egs-rspecifier> <egs-wspecifier1> [<egs-wspecifier2> ...]\n"
78  "\n"
79  "e.g.\n"
80  "nnet-copy-egs-discriminative ark:train.degs ark,t:text.degs\n"
81  "or:\n"
82  "nnet-copy-egs-discriminative ark:train.degs ark:1.degs ark:2.degs\n";
83 
84  bool random = false;
85  int32 srand_seed = 0;
86  BaseFloat keep_proportion = 1.0;
87  int32 const_feat_dim = 0;
88 
89  ParseOptions po(usage);
90  po.Register("random", &random, "If true, will write frames to output "
91  "archives randomly, not round-robin.");
92  po.Register("keep-proportion", &keep_proportion, "If <1.0, this program will "
93  "randomly keep this proportion of the input samples. If >1.0, it will "
94  "in expectation copy a sample this many times. It will copy it a number "
95  "of times equal to floor(keep-proportion) or ceil(keep-proportion).");
96  po.Register("srand", &srand_seed, "Seed for random number generator "
97  "(only relevant if --random=true or --keep-proportion != 1.0)");
98  po.Register("const-feat-dim", &const_feat_dim,
99  "Dimension of part of features (last dims) which varies little "
100  "or not at all with time, and which should be stored as a single "
101  "vector for each example rather than in the feature matrix."
102  "Useful in systems that use iVectors. Helpful to save space.");
103 
104  po.Read(argc, argv);
105 
106  srand(srand_seed);
107 
108  if (po.NumArgs() < 2) {
109  po.PrintUsage();
110  exit(1);
111  }
112 
113  std::string examples_rspecifier = po.GetArg(1);
114 
116  examples_rspecifier);
117 
118  int32 num_outputs = po.NumArgs() - 1;
119  std::vector<DiscriminativeNnetExampleWriter*> example_writers(num_outputs);
120  for (int32 i = 0; i < num_outputs; i++)
121  example_writers[i] = new DiscriminativeNnetExampleWriter(
122  po.GetArg(i+2));
123 
124 
125  int64 num_read = 0, num_written = 0, num_frames_written = 0;
126  for (; !example_reader.Done(); example_reader.Next(), num_read++) {
127  int32 count = GetCount(keep_proportion);
128  for (int32 c = 0; c < count; c++) {
129  int32 index = (random ? Rand() : num_written) % num_outputs;
130  std::ostringstream ostr;
131  ostr << num_written;
132  if (const_feat_dim == 0) {
133  example_writers[index]->Write(ostr.str(),
134  example_reader.Value());
135  } else {
136  DiscriminativeNnetExample eg = example_reader.Value();
137  AverageConstPart(const_feat_dim, &eg);
138  example_writers[index]->Write(ostr.str(), eg);
139  }
140  num_written++;
141  num_frames_written +=
142  static_cast<int64>(example_reader.Value().num_ali.size());
143  }
144  }
145 
146  for (int32 i = 0; i < num_outputs; i++)
147  delete example_writers[i];
148  KALDI_LOG << "Read " << num_read << " discriminative neural-network training"
149  << " examples, wrote " << num_written << ", consisting of "
150  << num_frames_written << " frames.";
151  return (num_written == 0 ? 1 : 0);
152  } catch(const std::exception &e) {
153  std::cerr << e.what() << '\n';
154  return -1;
155  }
156 }
157 
158 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
Definition: chain.dox:20
void AverageConstPart(int32 const_feat_dim, DiscriminativeNnetExample *eg)
MatrixIndexT NumCols() const
Returns number of columns (or zero for empty matrix).
Definition: kaldi-matrix.h:67
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
bool WithProb(BaseFloat prob, struct RandomState *state)
Definition: kaldi-math.cc:72
kaldi::int32 int32
int main(int argc, char *argv[])
void Register(const std::string &name, bool *ptr, const std::string &doc)
TableWriter< KaldiObjectHolder< DiscriminativeNnetExample > > DiscriminativeNnetExampleWriter
Definition: nnet-example.h:181
const size_t count
float BaseFloat
Definition: kaldi-types.h:29
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
Vector< BaseFloat > spk_info
spk_info contains any component of the features that varies slowly or not at all with time (and hence...
Definition: nnet-example.h:171
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.
Matrix< BaseFloat > input_frames
The input data– typically with a number of frames [NumRows()] larger than labels.size(), because it includes features to the left and right as needed for the temporal context of the network.
Definition: nnet-example.h:159
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
int NumArgs() const
Number of positional parameters (c.f. argc-1).
This struct is used to store the information we need for discriminative training (MMI or MPE)...
Definition: nnet-example.h:136
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
SubMatrix< Real > Range(const MatrixIndexT row_offset, const MatrixIndexT num_rows, const MatrixIndexT col_offset, const MatrixIndexT num_cols) const
Return a sub-part of matrix.
Definition: kaldi-matrix.h:202
#define KALDI_LOG
Definition: kaldi-error.h:153
int32 GetCount(double expected_count)
Note on how to parse this filename: it contains functions relatied to neural-net training examples...