Blame view

src/online2bin/ivector-randomize.cc 3.04 KB
8dcb6dfcb   Yannick Estève   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
  // online2bin/ivector-randomize.cc
  
  // Copyright 2014  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 "matrix/kaldi-matrix.h"
  #include "transform/transform-common.h"
  
  
  int main(int argc, char *argv[]) {
    try {
      using namespace kaldi;
  
      const char *usage =
          "Copy matrices of online-estimated iVectors, but randomize them;
  "
          "this is intended primarily for training the online nnet2 setup
  "
          "with iVectors.  For each input matrix, each row with index t is,
  "
          "with probability given by the option --randomize-prob, replaced
  "
          "with the contents an input row chosen randomly from the interval [t, T]
  "
          "where T is the index of the last row of the matrix.
  "
          "
  "
          "Usage: ivector-randomize [options] <ivector-rspecifier> <ivector-wspecifier>
  "
          " e.g.: ivector-randomize ark:- ark:-
  "
          "See also: ivector-extract-online, ivector-extract-online2, subsample-feats
  ";
  
      int32 srand_seed = 0;
      BaseFloat randomize_prob = 0.5;
      
      ParseOptions po(usage);
  
      po.Register("srand", &srand_seed, "Seed for random number generator");
      po.Register("randomize-prob", &randomize_prob, "For each row, replace it with a "
                  "random row with this probability.");
      
      po.Read(argc, argv);
      
      if (po.NumArgs() != 2) {
        po.PrintUsage();
        exit(1);
      }
  
  
      std::string ivector_rspecifier = po.GetArg(1),
          ivector_wspecifier = po.GetArg(2);
  
      int num_done = 0;
      SequentialBaseFloatMatrixReader reader(ivector_rspecifier);
      BaseFloatMatrixWriter writer(ivector_wspecifier);
      
      for (; !reader.Done(); reader.Next(), num_done++) {
        std::string utt = reader.Key();
        const Matrix<BaseFloat> &ivectors_in = reader.Value();
        int32 T = ivectors_in.NumRows(), dim = ivectors_in.NumCols();
        Matrix<BaseFloat> ivectors_out(T, dim, kUndefined);
        for (int32 t = 0; t < T; t++) {
          int32 t_src;
          if (WithProb(randomize_prob)) t_src = RandInt(t, T-1);
          else t_src = t;
          ivectors_out.Row(t).CopyFromVec(ivectors_in.Row(t_src));
        }
        writer.Write(utt, ivectors_out);
        num_done++;
      }
      KALDI_LOG << "Randomized " << num_done << " iVectors.";
      return (num_done != 0 ? 0 : 1);
    } catch(const std::exception &e) {
      std::cerr << e.what();
      return -1;
    }
  }