All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
modify-cmvn-stats.cc File Reference
Include dependency graph for modify-cmvn-stats.cc:

Go to the source code of this file.

Functions

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

Function Documentation

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

Definition at line 26 of file modify-cmvn-stats.cc.

References count, SequentialTableReader< Holder >::Done(), kaldi::FakeStatsForSomeDims(), ParseOptions::GetArg(), rnnlm::i, KALDI_ERR, KALDI_LOG, KALDI_WARN, SequentialTableReader< Holder >::Key(), SequentialTableReader< Holder >::Next(), ParseOptions::NumArgs(), ParseOptions::PrintUsage(), ParseOptions::Read(), ParseOptions::Register(), kaldi::SplitStringToIntegers(), SequentialTableReader< Holder >::Value(), and TableWriter< Holder >::Write().

26  {
27  try {
28  using namespace kaldi;
29 
30  const char *usage =
31  "Copy cepstral mean/variance stats so that some dimensions have 'fake' stats\n"
32  "that will skip normalization\n"
33  "Usage: modify-cmvn-stats [options] [<fake-dims>] <in-rspecifier> <out-wspecifier>\n"
34  "e.g.: modify-cmvn-stats 13:14:15 ark:- ark:-\n"
35  "or: modify-cmvn-stats --convert-to-mean-and-var=true ark:- ark:-\n"
36  "See also: compute-cmvn-stats\n";
37 
38  bool convert_to_mean_and_var = false;
39 
40  ParseOptions po(usage);
41 
42  po.Register("convert-to-mean-and-var", &convert_to_mean_and_var,
43  "If true, convert the stats to a matrix containing the mean "
44  "and the centered variance in each dimension");
45 
46  po.Read(argc, argv);
47 
48  if (po.NumArgs() != 2 && po.NumArgs() != 3) {
49  po.PrintUsage();
50  exit(1);
51  }
52 
53  int32 num_done = 0;
54 
55 
56  std::string skip_dims_str, rspecifier, wspecifier;
57  if (po.NumArgs() == 3) {
58  skip_dims_str = po.GetArg(1);
59  rspecifier = po.GetArg(2);
60  wspecifier = po.GetArg(3);
61  } else {
62  rspecifier = po.GetArg(1);
63  wspecifier = po.GetArg(2);
64  }
65 
66  std::vector<int32> skip_dims;
67  if (!SplitStringToIntegers(skip_dims_str, ":", false, &skip_dims)) {
68  KALDI_ERR << "Bad first argument (should be colon-separated list of "
69  << "integers)";
70  }
71 
72  SequentialDoubleMatrixReader reader(rspecifier);
73  DoubleMatrixWriter writer(wspecifier);
74 
75  for (; !reader.Done(); reader.Next()) {
76  Matrix<double> mat(reader.Value());
77 
78  if (mat.NumRows() != 2)
79  KALDI_ERR << "Expected input to be CMVN stats (should have two rows)";
80 
81  FakeStatsForSomeDims(skip_dims, &mat);
82  if (!convert_to_mean_and_var) {
83  writer.Write(reader.Key(), mat);
84  num_done++;
85  } else {
86  int32 dim = mat.NumCols() - 1;
87  double count = mat(0, dim);
88  Matrix<double> modified_mat(2, dim);
89  if (count <= 0.0) {
90  KALDI_WARN << "Zero or negative count for speaker " << reader.Key()
91  << ", not outputting mean and variance stats.";
92  continue;
93  }
94  for (int32 i = 0; i < dim; i++) {
95  double mean = mat(0, i) / count,
96  variance = mat(1, i) / count - mean * mean;
97  modified_mat(0, i) = mean;
98  modified_mat(1, i) = variance;
99  }
100  writer.Write(reader.Key(), modified_mat);
101  num_done++;
102  }
103  }
104  KALDI_LOG << "Modified " << num_done << " sets of stats.";
105  return (num_done != 0 ? 0 : 1);
106  } catch(const std::exception &e) {
107  std::cerr << e.what();
108  return -1;
109  }
110 }
Relabels neural network egs with the read pdf-id alignments.
Definition: chain.dox:20
bool SplitStringToIntegers(const std::string &full, const char *delim, bool omit_empty_strings, std::vector< I > *out)
Split a string (e.g.
Definition: text-utils.h:68
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:366
const size_t count
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
#define KALDI_ERR
Definition: kaldi-error.h:127
#define KALDI_WARN
Definition: kaldi-error.h:130
#define KALDI_LOG
Definition: kaldi-error.h:133
void FakeStatsForSomeDims(const std::vector< int32 > &dims, MatrixBase< double > *stats)
Modify the stats so that for some dimensions (specified in "dims"), we replace them with "fake" stats...
Definition: cmvn.cc:166