Blame view
src/nnet2bin/nnet-copy-egs-discriminative.cc
5.87 KB
8dcb6dfcb first commit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
// nnet2bin/nnet-copy-egs-discriminative.cc // Copyright 2012-2013 Johns Hopkins University (author: Daniel Povey) // See ../../COPYING for clarification regarding multiple authors // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE, // MERCHANTABLITY OR NON-INFRINGEMENT. // See the Apache 2 License for the specific language governing permissions and // limitations under the License. #include "base/kaldi-common.h" #include "util/common-utils.h" #include "hmm/transition-model.h" #include "nnet2/nnet-example-functions.h" namespace kaldi { namespace nnet2 { // returns an integer randomly drawn with expected value "expected_count" // (will be either floor(expected_count) or ceil(expected_count)). // this will go into an infinite loop if expected_count is very huge, but // it should never be that huge. int32 GetCount(double expected_count) { KALDI_ASSERT(expected_count >= 0.0); int32 ans = 0; while (expected_count > 1.0) { ans++; expected_count--; } if (WithProb(expected_count)) ans++; return ans; } void AverageConstPart(int32 const_feat_dim, DiscriminativeNnetExample *eg) { if (eg->spk_info.Dim() != 0) { // already has const part. KALDI_ASSERT(eg->spk_info.Dim() == const_feat_dim); // and nothing to do. } else { int32 dim = eg->input_frames.NumCols(), basic_dim = dim - const_feat_dim; KALDI_ASSERT(const_feat_dim < eg->input_frames.NumCols()); Matrix<BaseFloat> mat(eg->input_frames); // copy to non-compressed matrix. eg->input_frames = mat.Range(0, mat.NumRows(), 0, basic_dim); eg->spk_info.Resize(const_feat_dim); eg->spk_info.AddRowSumMat(1.0 / mat.NumRows(), mat.Range(0, mat.NumRows(), basic_dim, const_feat_dim), 0.0); } } } // namespace nnet2 } // namespace kaldi int main(int argc, char *argv[]) { try { using namespace kaldi; using namespace kaldi::nnet2; typedef kaldi::int32 int32; typedef kaldi::int64 int64; const char *usage = "Copy examples for discriminative neural " "network training. Supports multiple wspecifiers, in " "which case it will write the examples round-robin to the outputs. " " " "Usage: nnet-copy-egs-discriminative [options] <egs-rspecifier> <egs-wspecifier1> [<egs-wspecifier2> ...] " " " "e.g. " "nnet-copy-egs-discriminative ark:train.degs ark,t:text.degs " "or: " "nnet-copy-egs-discriminative ark:train.degs ark:1.degs ark:2.degs "; bool random = false; int32 srand_seed = 0; BaseFloat keep_proportion = 1.0; int32 const_feat_dim = 0; ParseOptions po(usage); po.Register("random", &random, "If true, will write frames to output " "archives randomly, not round-robin."); po.Register("keep-proportion", &keep_proportion, "If <1.0, this program will " "randomly keep this proportion of the input samples. If >1.0, it will " "in expectation copy a sample this many times. It will copy it a number " "of times equal to floor(keep-proportion) or ceil(keep-proportion)."); po.Register("srand", &srand_seed, "Seed for random number generator " "(only relevant if --random=true or --keep-proportion != 1.0)"); po.Register("const-feat-dim", &const_feat_dim, "Dimension of part of features (last dims) which varies little " "or not at all with time, and which should be stored as a single " "vector for each example rather than in the feature matrix." "Useful in systems that use iVectors. Helpful to save space."); po.Read(argc, argv); srand(srand_seed); if (po.NumArgs() < 2) { po.PrintUsage(); exit(1); } std::string examples_rspecifier = po.GetArg(1); SequentialDiscriminativeNnetExampleReader example_reader( examples_rspecifier); int32 num_outputs = po.NumArgs() - 1; std::vector<DiscriminativeNnetExampleWriter*> example_writers(num_outputs); for (int32 i = 0; i < num_outputs; i++) example_writers[i] = new DiscriminativeNnetExampleWriter( po.GetArg(i+2)); int64 num_read = 0, num_written = 0, num_frames_written = 0; for (; !example_reader.Done(); example_reader.Next(), num_read++) { int32 count = GetCount(keep_proportion); for (int32 c = 0; c < count; c++) { int32 index = (random ? Rand() : num_written) % num_outputs; std::ostringstream ostr; ostr << num_written; if (const_feat_dim == 0) { example_writers[index]->Write(ostr.str(), example_reader.Value()); } else { DiscriminativeNnetExample eg = example_reader.Value(); AverageConstPart(const_feat_dim, &eg); example_writers[index]->Write(ostr.str(), eg); } num_written++; num_frames_written += static_cast<int64>(example_reader.Value().num_ali.size()); } } for (int32 i = 0; i < num_outputs; i++) delete example_writers[i]; KALDI_LOG << "Read " << num_read << " discriminative neural-network training" << " examples, wrote " << num_written << ", consisting of " << num_frames_written << " frames."; return (num_written == 0 ? 1 : 0); } catch(const std::exception &e) { std::cerr << e.what() << ' '; return -1; } } |