nnet3-discriminative-copy-egs.cc
Go to the documentation of this file.
1 // nnet3bin/nnet3-discriminative-copy-egs.cc
2 
3 // Copyright 2012-2015 Johns Hopkins University (author: Daniel Povey)
4 // 2014-2015 Vimal Manohar
5 
6 // See ../../COPYING for clarification regarding multiple authors
7 //
8 // Licensed under the Apache License, Version 2.0 (the "License");
9 // you may not use this file except in compliance with the License.
10 // You may obtain a copy of the License at
11 //
12 // http://www.apache.org/licenses/LICENSE-2.0
13 //
14 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
16 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
17 // MERCHANTABLITY OR NON-INFRINGEMENT.
18 // See the Apache 2 License for the specific language governing permissions and
19 // limitations under the License.
20 
21 #include "base/kaldi-common.h"
22 #include "util/common-utils.h"
23 #include "hmm/transition-model.h"
25 
26 namespace kaldi {
27 // returns an integer randomly drawn with expected value "expected_count"
28 // (will be either floor(expected_count) or ceil(expected_count)).
29 int32 GetCount(double expected_count) {
30  KALDI_ASSERT(expected_count >= 0.0);
31  int32 ans = floor(expected_count);
32  expected_count -= ans;
33  if (WithProb(expected_count))
34  ans++;
35  return ans;
36 }
37 }
38 
39 int main(int argc, char *argv[]) {
40  try {
41  using namespace kaldi;
42  using namespace kaldi::nnet3;
43  typedef kaldi::int32 int32;
44  typedef kaldi::int64 int64;
45 
46  const char *usage =
47  "Copy examples for nnet3 discriminative training, possibly changing the binary mode.\n"
48  "Supports multiple wspecifiers, in which case it will write the examples\n"
49  "round-robin to the outputs.\n"
50  "\n"
51  "Usage: nnet3-discriminative-copy-egs [options] <egs-rspecifier> <egs-wspecifier1> [<egs-wspecifier2> ...]\n"
52  "\n"
53  "e.g.\n"
54  "nnet3-discriminative-copy-egs ark:train.degs ark,t:text.degs\n"
55  "or:\n"
56  "nnet3-discriminative-copy-egs ark:train.degs ark:1.degs ark:2.degs\n";
57 
58  bool random = false;
59  int32 srand_seed = 0;
60  int32 frame_shift = 0;
61  BaseFloat keep_proportion = 1.0;
62 
63  ParseOptions po(usage);
64  po.Register("random", &random, "If true, will write frames to output "
65  "archives randomly, not round-robin.");
66  po.Register("keep-proportion", &keep_proportion, "If <1.0, this program will "
67  "randomly keep this proportion of the input samples. If >1.0, it will "
68  "in expectation copy a sample this many times. It will copy it a number "
69  "of times equal to floor(keep-proportion) or ceil(keep-proportion).");
70  po.Register("srand", &srand_seed, "Seed for random number generator "
71  "(only relevant if --random=true or --keep-proportion != 1.0)");
72  po.Register("frame-shift", &frame_shift, "Allows you to shift time values "
73  "in the supervision data (excluding iVector data) - useful in "
74  "augmenting data. Note, the outputs will remain at the closest "
75  "exact multiples of the frame subsampling factor");
76 
77  po.Read(argc, argv);
78 
79  srand(srand_seed);
80 
81  if (po.NumArgs() < 2) {
82  po.PrintUsage();
83  exit(1);
84  }
85 
86  std::string examples_rspecifier = po.GetArg(1);
87 
88  SequentialNnetDiscriminativeExampleReader example_reader(examples_rspecifier);
89 
90  int32 num_outputs = po.NumArgs() - 1;
91  std::vector<NnetDiscriminativeExampleWriter*> example_writers(num_outputs);
92  for (int32 i = 0; i < num_outputs; i++)
93  example_writers[i] = new NnetDiscriminativeExampleWriter(po.GetArg(i+2));
94 
95  std::vector<std::string> exclude_names; // names we never shift times of;
96  // not configurable for now.
97  exclude_names.push_back(std::string("ivector"));
98 
99 
100  int64 num_read = 0, num_written = 0;
101  for (; !example_reader.Done(); example_reader.Next(), num_read++) {
102  // count is normally 1; could be 0, or possibly >1.
103  int32 count = GetCount(keep_proportion);
104  std::string key = example_reader.Key();
105  if (frame_shift == 0) {
106  const NnetDiscriminativeExample &eg = example_reader.Value();
107  for (int32 c = 0; c < count; c++) {
108  int32 index = (random ? Rand() : num_written) % num_outputs;
109  example_writers[index]->Write(key, eg);
110  num_written++;
111  }
112  } else if (count > 0) {
113  NnetDiscriminativeExample eg = example_reader.Value();
114  if (frame_shift != 0)
115  ShiftDiscriminativeExampleTimes(frame_shift, exclude_names, &eg);
116  for (int32 c = 0; c < count; c++) {
117  int32 index = (random ? Rand() : num_written) % num_outputs;
118  example_writers[index]->Write(key, eg);
119  num_written++;
120  }
121  }
122  }
123  for (int32 i = 0; i < num_outputs; i++)
124  delete example_writers[i];
125  KALDI_LOG << "Read " << num_read
126  << " neural-network training examples, wrote " << num_written;
127  return (num_written == 0 ? 1 : 0);
128  } catch(const std::exception &e) {
129  std::cerr << e.what() << '\n';
130  return -1;
131  }
132 }
void ShiftDiscriminativeExampleTimes(int32 frame_shift, const std::vector< std::string > &exclude_names, NnetDiscriminativeExample *eg)
Shifts the time-index t of everything in the input of "eg" by adding "t_offset" to all "t" values– b...
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].
bool WithProb(BaseFloat prob, struct RandomState *state)
Definition: kaldi-math.cc:72
kaldi::int32 int32
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
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.
int32 GetCount(double expected_count)
TableWriter< KaldiObjectHolder< NnetDiscriminativeExample > > NnetDiscriminativeExampleWriter
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_ASSERT(cond)
Definition: kaldi-error.h:185
int main(int argc, char *argv[])
int32 GetCount(double expected_count)
#define KALDI_LOG
Definition: kaldi-error.h:153
NnetDiscriminativeExample is like NnetExample, but specialized for sequence training.