modify-cmvn-stats.cc
Go to the documentation of this file.
1 // featbin/modify-cmvn-stats.cc
2 
3 // Copyright 2014 Johns Hopkins University (author: Daniel Povey)
4 
5 // See ../../COPYING for clarification regarding multiple authors
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 // http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // THIS CODE IS PROVIDED *AS IS* BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 // KIND, EITHER EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION ANY IMPLIED
15 // WARRANTIES OR CONDITIONS OF TITLE, FITNESS FOR A PARTICULAR PURPOSE,
16 // MERCHANTABLITY OR NON-INFRINGEMENT.
17 // See the Apache 2 License for the specific language governing permissions and
18 // limitations under the License.
19 
20 #include "base/kaldi-common.h"
21 #include "util/common-utils.h"
22 #include "matrix/kaldi-matrix.h"
23 #include "transform/cmvn.h"
24 
25 
26 int main(int argc, char *argv[]) {
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 }
111 
112 
This code computes Goodness of Pronunciation (GOP) and extracts phone-level pronunciation feature for...
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
void PrintUsage(bool print_command_line=false)
Prints the usage documentation [provided in the constructor].
A templated class for writing objects to an archive or script file; see The Table concept...
Definition: kaldi-table.h:368
kaldi::int32 int32
void Write(const std::string &key, const T &value) const
void Register(const std::string &name, bool *ptr, const std::string &doc)
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
int main(int argc, char *argv[])
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.
#define KALDI_ERR
Definition: kaldi-error.h:147
#define KALDI_WARN
Definition: kaldi-error.h:150
std::string GetArg(int param) const
Returns one of the positional parameters; 1-based indexing for argc/argv compatibility.
int NumArgs() const
Number of positional parameters (c.f. argc-1).
#define KALDI_LOG
Definition: kaldi-error.h:153
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:168