nnet3-discriminative-copy-egs.cc File Reference
Include dependency graph for nnet3-discriminative-copy-egs.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

int32 GetCount (double expected_count)
 
int main (int argc, char *argv[])
 

Function Documentation

◆ main()

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

Definition at line 39 of file nnet3-discriminative-copy-egs.cc.

References count, SequentialTableReader< Holder >::Done(), ParseOptions::GetArg(), kaldi::nnet3::GetCount(), rnnlm::i, KALDI_LOG, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), kaldi::Rand(), ParseOptions::Read(), ParseOptions::Register(), kaldi::nnet3::ShiftDiscriminativeExampleTimes(), and SequentialTableReader< Holder >::Value().

39  {
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
kaldi::int32 int32
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
int32 GetCount(double expected_count)
TableWriter< KaldiObjectHolder< NnetDiscriminativeExample > > NnetDiscriminativeExampleWriter
int Rand(struct RandomState *state)
Definition: kaldi-math.cc:45
#define KALDI_LOG
Definition: kaldi-error.h:153
void Write(std::ostream &os, bool binary) const
NnetDiscriminativeExample is like NnetExample, but specialized for sequence training.